/// <summary> /// 初始化预定义运算符表达式。 /// </summary> /// <param name="line">指定预定义运算符表达式所在位置的行数。</param> /// <param name="col">指定预定义运算符表达式所在位置的列数。</param> /// <param name="lhs">指定预定义运算符表达式的左侧值。</param> /// <param name="op">指定预定义运算符表达式的运算符。</param> /// <param name="rhs">指定预定义运算符表达式的右侧值。</param> public BinaryExpression(int line, int col, Expression lhs, TokenKind op, Expression rhs) : base(line, col) { _Lhs = lhs; _Rhs = rhs; _op = op; }
/// <summary> /// 初始化回调方法表达式。 /// </summary> /// <param name="line">指定回调方法表达式所在位置的行数。</param> /// <param name="col">指定回调方法表达式所在位置的列数。</param> /// <param name="obj">指定回调方法表达式的回调对象。</param> /// <param name="name">指定回调方法表达式的名称。</param> /// <param name="args">指定回调方法表达式的参数列表。</param> public MethodCall(int line, int col, Expression obj, string name, Expression[] args) : base(line, col) { _Name = name; _Args = args; _Obj = obj; }
/// <summary> /// 添加字符串表达式的实例。 /// </summary> /// <param name="exp">指定一个表达式。</param> public void Add(Expression exp) { _Exps.Add(exp); }
/// <summary> /// 初始化字段访问表达式。 /// </summary> /// <param name="line">指定字段访问表达式所在位置的行数。</param> /// <param name="col">指定字段访问表达式所在位置的列数。</param> /// <param name="exp">指定字段访问表达式的值。</param> /// <param name="field">指定字段访问表达式的字段名。</param> public FieldAccess(int line, int col, Expression exp, string field) : base(line, col) { _Exp = exp; _Field = field; }
/// <summary> /// 初始化数组访问表达式。 /// </summary> /// <param name="line">指定数组访问表达式所在位置的行数。</param> /// <param name="col">指定数组访问表达式所在位置的列数。</param> /// <param name="exp">指定数组访问表达式的值。</param> /// <param name="index">指定数组访问表达式的索引。</param> public ArrayAccess(int line, int col, Expression exp, Expression index) : base(line, col) { _Exp = exp; _Index = index; }
/// <summary> /// 执行表达式,并返回执行结果。 /// </summary> /// <param name="exp">指定一个表达式实例。</param> /// <returns>object</returns> public 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 = this.EvalExpression(fa.Exp); string propertyName = fa.Field; return EvalProperty(obj, propertyName); } else if (exp is MethodCall) { MethodCall ma = (MethodCall)exp; object obj = this.EvalExpression(ma.CallObject); string methodName = ma.Name; return EvalMethodCall(obj, methodName, EvalArguments(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 (!_Functions.ContainsKey(fcall.Name)) { string msg = string.Format("函数未定义:{0}", fcall.Name); throw new TemplateRuntimeException(msg, exp.Line, exp.Col); } TemplateFunction func = _Functions[fcall.Name]; object[] values = EvalArguments(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(this.EvalExpression(ex)); return sb.ToString(); } else if (exp is BinaryExpression) return EvalBinaryExpression(exp as BinaryExpression); else if (exp is ArrayAccess) return EvalArrayAccess(exp as ArrayAccess); else throw new TemplateRuntimeException("无效的表达式类型:" + exp.GetType().Name, exp.Line, exp.Col); } catch (TemplateRuntimeException ex) { this.DisplayError(ex); return null; } catch (Exception ex) { this.DisplayError(new TemplateRuntimeException(ex.Message, _CurrentExpression.Line, _CurrentExpression.Col)); return null; } }
/// <summary> /// 处理表达式。 /// </summary> /// <param name="exp">指定一个表达式实例。</param> protected void ProcessExpression(Expression exp) { object value = this.EvalExpression(exp); this.WriteValue(value); }
/// <summary> /// 执行参数列表,并返回执行结果。 /// </summary> /// <param name="args">包含参数列表的表达式。</param> /// <returns>object[]</returns> protected object[] EvalArguments(Expression[] args) { object[] values = new object[args.Length]; for (int i = 0; i < values.Length; i++) values[i] = this.EvalExpression(args[i]); return values; }
/// <summary> /// 初始化回调函数表达式。 /// </summary> /// <param name="line">指定回调函数表达式所在位置的行数。</param> /// <param name="col">指定回调函数表达式所在位置的列数。</param> /// <param name="name">指定回调函数表达式的名称。</param> /// <param name="args">指定回调函数表达式的参数列表。</param> public FCall(int line, int col, string name, Expression[] args) : base(line, col) { _Name = name; _Args = args; }