<commit_before>﻿namespace Veil.Compiler
{
    internal partial class VeilTemplateCompiler
    {
        private static void EmitWriteExpression<T>(VeilCompilerState<T> state, SyntaxTreeNode.WriteExpressionNode node)
        {
            state.Emitter.LoadWriterToStack();
            state.PushExpressionScopeOnStack(node.Expression);
            state.Emitter.LoadExpressionFromCurrentModelOnStack(node.Expression);

            if (node.HtmlEncode && node.Expression.ResultType == typeof(string))
            {
                var method = typeof(Helpers).GetMethod("HtmlEncode");
                state.Emitter.Call(method);
            }
            else
            {
                state.Emitter.CallWriteFor(node.Expression.ResultType);
            }
        }
    }
}<commit_msg>Tidy up error handling in WriteExpression<commit_after>﻿using System.Reflection;

namespace Veil.Compiler
{
    internal partial class VeilTemplateCompiler
    {
        private static MethodInfo htmlEncodeMethod = typeof(Helpers).GetMethod("HtmlEncode");

        private static void EmitWriteExpression<T>(VeilCompilerState<T> state, SyntaxTreeNode.WriteExpressionNode node)
        {
            state.Emitter.LoadWriterToStack();
            state.PushExpressionScopeOnStack(node.Expression);
            state.Emitter.LoadExpressionFromCurrentModelOnStack(node.Expression);

            if (node.HtmlEncode)
            {
                if (node.Expression.ResultType != typeof(string)) throw new VeilCompilerException("Tried to HtmlEncode an expression that does not evaluate to a string");
                state.Emitter.Call(htmlEncodeMethod);
            }
            else
            {
                state.Emitter.CallWriteFor(node.Expression.ResultType);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08333333333333333, 'fraction_numerical': 0.0, 'mean_word_length': 2.34392523364486, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4822144', 'n_tokens_mistral': 440, 'n_tokens_neox': 437, 'n_words': 76}