Пример #1
0
 public BinaryExpression(int line, int col, Expression lhs, TokenKind op, Expression rhs)
     : base(line, col)
 {
     this.lhs = lhs;
     this.rhs = rhs;
     this.op = op;
 }
Пример #2
0
 public MethodCallExpression(int line, int column, Expression obj, string methodName, Expression[] parameters)
     : base(line, column)
 {
     this.obj = obj;
     this.methodName = methodName;
     this.parameters = parameters;
 }
Пример #3
0
 public FieldAccessExpression(int line, int column, Expression expression, string fieldName)
     : base(line, column)
 {
     this.expression = expression;
     this.fieldName = fieldName;
 }
Пример #4
0
 public ArrayAccessExpression(int line, int column, Expression expression, Expression index)
     : base(line, column)
 {
     this.expression = expression;
     this.index = index;
 }
Пример #5
0
 public TagNodeAttribute(string name, Expression value)
 {
     this.name = name;
     this.value = value;
 }
Пример #6
0
 public IfTag(int line, int column, Expression testExpression)
     : base(line, column, "if")
 {
     this.testExpression = testExpression;
 }
Пример #7
0
 public FunctionCallExpression(int line, int column, string functionName, Expression[] parameters)
     : base(line, column)
 {
     this.functionName = functionName;
     this.parameters = parameters;
 }
Пример #8
0
 private void ProcessExpression(Expression expression)
 {
     WriteValue(EvaluateExpression(expression));
 }
Пример #9
0
        private object EvaluateExpression(Expression expression)
        {
            currentExpression = expression;

            try {

                if (expression is StringLiteralExpression)
                    return ((StringLiteralExpression)expression).Value;

                if (expression is IdExpression)
                    return GetValue(((IdExpression)expression).Id);

                if (expression is FieldAccessExpression) {
                    FieldAccessExpression fa = (FieldAccessExpression)expression;
                    object obj = EvaluateExpression(fa.Expression);
                    string propertyName = fa.FieldName;
                    return EvaluateProperty(obj, propertyName);
                }

                if (expression is MethodCallExpression) {
                    MethodCallExpression ma = (MethodCallExpression)expression;
                    object obj = EvaluateExpression(ma.Object);
                    string methodName = ma.MethodName;

                    return EvaluateMethodCall(obj, methodName, EvaluateArguments(ma.Parameters));
                }

                if (expression is IntegerLiteralExpression)
                    return ((IntegerLiteralExpression)expression).Value;

                if (expression is DoubleLiteralExpression)
                    return ((DoubleLiteralExpression)expression).Value;

                if (expression is BooleanLiteralExpression)
                    return ((BooleanLiteralExpression) expression).Value;

                if (expression is FunctionCallExpression) {
                    FunctionCallExpression fcall = (FunctionCallExpression)expression;
                    ITemplateFunction function = GetFunction(fcall.FunctionName);
                    if (function == null)
                        throw new TemplateException(String.Format("Function {0} is not defined", fcall.FunctionName), expression.Line,
                                                    expression.Column);

                    object[] values = EvaluateArguments(fcall.Parameters);

                    return function.Evaluate(values);
                }

                if (expression is StringExpression) {
                    StringExpression stringExp = (StringExpression)expression;
                    StringBuilder sb = new StringBuilder();
                    foreach (Expression ex in stringExp)
                        sb.Append(EvaluateExpression(ex));

                    return sb.ToString();
                }

                if (expression is BinaryExpression)
                    return EvaluateBinaryExpression(expression as BinaryExpression);

                if (expression is ArrayAccessExpression)
                    return EvaluateArrayAccess(expression as ArrayAccessExpression);

                throw new TemplateException("Invalid expression type: " + expression.GetType().Name, expression.Line,
                                            expression.Column);
            } catch (TemplateException ex) {
                OnError(ex);
                return null;
            } catch (Exception ex) {
                OnError(new TemplateException(ex.Message, currentExpression.Line, currentExpression.Column));
                return null;
            }
        }
Пример #10
0
        private object[] EvaluateArguments(Expression[] args)
        {
            object[] values = new object[args.Length];
            for (int i = 0; i < values.Length; i++)
                values[i] = EvaluateExpression(args[i]);

            return values;
        }