示例#1
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;
            }
        }
示例#2
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());
            }
        }