Example #1
0
 public BinaryExpression(int line    , int col , Expression lhs , TokenKind op , Expression rhs)
     : base(TokenKind.BinaryExpression , line    , col)
 {
     this.lhs = lhs;
     this.rhs = rhs;
     this.op  = op;
 }
Example #2
0
 public void Add(Expression exp)
 {
     exps.Add(exp);
 }
Example #3
0
 public FCall(int line, int col, string name, Expression[] args)
     : base(TokenKind.FCall , line    , col)
 {
     this.name = name;
     this.args = args;
 }
Example #4
0
 private void ProcessExpression(Expression exp)
 {
     object value = EvalExpression(exp);
     WriteValue(value);
 }
Example #5
0
        private object[] ProcessArguments(Expression[] args)
        {
            object[] values = new object[args.Length];

            for (int i = 0; i < values.Length; i++) {
                values[i] = EvalExpression(args[i]);
            }

            return values;
        }
Example #6
0
        private object EvalExpression(Expression exp)
        {
            CurrentExpression = exp;

            try {

                if (exp is StringLiteral) {
                    return ((StringLiteral) exp).Content;
                } else if (exp is Name) {
                    return GetValue(((Name) exp).Id);
                } else if (exp is FieldAccess) {
                    FieldAccess fa = (FieldAccess) exp;
                    object obj = EvalExpression(fa.Exp);
                    string propertyName = fa.Field;
                    return ProcessProperty(obj, propertyName);

                } else if (exp is MCall) {
                    MCall ma = (MCall) exp;
                    object obj = EvalExpression(ma.CallObject);
                    string methodName = ma.Name;
                    return ProcessMCall(obj, methodName, ProcessArguments(ma.Args));

                } else if (exp is IntLiteral) {
                    return ((IntLiteral) exp).Value;
                } else if (exp is DoubleLiteral) {
                    return ((DoubleLiteral) exp).Value;
                } else if (exp is FCall) {
                    FCall fcall = (FCall) exp;

                    if (!_fnTbl.ContainsKey(fcall.Name)) {
                        string msg = string.Format("Function {0} is not defined", fcall.Name);
                        throw new TmplException(msg, exp.Line, exp.Col);
                    }

                    FunctionDefinition func = _fnTbl[fcall.Name];
                    object[] values = ProcessArguments(fcall.Args);

                    return func(values);

                } else if (exp is StringExpression) {
                    StringExpression stringExp = (StringExpression) exp;
                    StringBuilder sb = new StringBuilder();

                    foreach (Expression ex in stringExp.Expressions) {
                        sb.Append(EvalExpression(ex));
                    }

                    return sb.ToString();
                } else if (exp is BinaryExpression) {
                    return ProcessBinaryExpression(exp as BinaryExpression);
                } else if (exp is ArrayAccess) {
                    return ProcessArrayAccess(exp as ArrayAccess);
                } else {
                    throw new TmplException("Invalid expression type: " + exp.GetType().Name, exp.Line, exp.Col);
                }

            } catch (TmplException ex) {
                DisplayError(ex);
                return null;
            } catch (Exception ex) {

                string _Message = "Message=" + ex.Message + "," + "Source=" + ex.Source + ",StackTrace=" + ex.StackTrace + ",TargetSite=" + ex.TargetSite + "";
                DisplayError(new TmplException(_Message, CurrentExpression.Line, CurrentExpression.Col));
                return null;
            }
        }
Example #7
0
 public IfStatement(int line , int col , Expression when)
     : base(TokenKind.If , line , col)
 {
     _when = when;
 }
Example #8
0
 public FieldAccess(int line, int col, Expression exp, string field)
     : base(TokenKind.FieldAccess , line    , col)
 {
     this.exp = exp;
     this.field = field;
 }
Example #9
0
        private void visitExpression(Expression expression)
        {
            if (expression is Name)
                WriteLine("Name: " + ((Name) expression).Id);
            else if (expression is FCall) {
                FCall fcall = (FCall) expression;

                WriteLine("FCall: " + fcall.Name);

                WriteLine("Parameters: ");

                foreach (Expression exp in fcall.Args) {
                    visitExpression(exp);
                }

            } else if (expression is FieldAccess) {
                FieldAccess fa = (FieldAccess) expression;
                WriteLine("FieldAccess: " + fa.Exp + "." + fa.Field);

            } else if (expression is StringLiteral) {
                StringLiteral literal = (StringLiteral) expression;

                if (literal.Content.Length > 50)
                    WriteLine("String: " + literal.Content.Substring(0, 50) + "...");
                else
                    WriteLine("String: " + literal.Content);

            } else if (expression is StringExpression) {
                StringExpression sexp = (StringExpression) expression;
                WriteLine("StringExpression");

                foreach (Expression exp in sexp.Expressions) {
                    visitExpression(exp);
                }
            } else if (expression is BinaryExpression) {
                BinaryExpression sexp = (BinaryExpression) expression;
                WriteLine("BinaryExpression");

                visitExpression(sexp.Lhs);

                WriteLine("Operator " + sexp.Operator.ToString());

                visitExpression(sexp.Rhs);

            } else {
                WriteLine("Expression: " + expression.GetType().ToString());
            }
        }
Example #10
0
 public DotAttribute(string name, Expression expression)
 {
     this.name = name;
     _expression = expression;
 }
Example #11
0
 public ArrayAccess(int line , int col , Expression exp, Expression index)
     : base(TokenKind.ArrayAccess   , line    , col)
 {
     _exp = exp;
     _index = index;
 }