private Expression _RelationalExpression() { Expression ret = _PrimaryExpression(); while (Current.TokenKind == TokenKind.OpLt || Current.TokenKind == TokenKind.OpLte || Current.TokenKind == TokenKind.OpGt || Current.TokenKind == TokenKind.OpGte) { Token tok = _Consume(); // consume operator Expression rhs = _PrimaryExpression(); ret = new BinaryExpression(ret.Line, ret.Col, ret, tok.TokenKind, rhs); } return ret; }
private Expression _AndExpression() { Expression ret = _EqualityExpression(); while (Current.TokenKind == TokenKind.OpAnd) { _Consume(); // Or Expression rhs = _EqualityExpression(); ret = new BinaryExpression(ret.Line, ret.Col, ret, TokenKind.OpAnd, rhs); } return ret; }
private Expression _EqualityExpression() { Expression ret = _RelationalExpression(); while (Current.TokenKind == TokenKind.OpIs || Current.TokenKind == TokenKind.OpIsNot) { Token tok = _Consume(); // consume operator Expression rhs = _RelationalExpression(); ret = new BinaryExpression(ret.Line, ret.Col, ret, tok.TokenKind, rhs); } return ret; }
/// <summary> /// 执行预定义运算符,并返回执行结果。 /// </summary> /// <param name="exp">指定一个预定义运算符。</param> /// <returns>object</returns> protected object EvalBinaryExpression(BinaryExpression exp) { switch (exp.Operator) { case TokenKind.OpOr: { object lhsValue = this.EvalExpression(exp.Lhs); if (Utility.ToBool(lhsValue)) return true; object rhsValue = this.EvalExpression(exp.Rhs); return Utility.ToBool(rhsValue); } case TokenKind.OpAnd: { object lhsValue = this.EvalExpression(exp.Lhs); if (!Utility.ToBool(lhsValue)) return false; object rhsValue = this.EvalExpression(exp.Rhs); return Utility.ToBool(rhsValue); } case TokenKind.OpIs: { object lhsValue = this.EvalExpression(exp.Lhs); object rhsValue = this.EvalExpression(exp.Rhs); return lhsValue.Equals(rhsValue); } case TokenKind.OpIsNot: { object lhsValue = this.EvalExpression(exp.Lhs); object rhsValue = this.EvalExpression(exp.Rhs); return !lhsValue.Equals(rhsValue); } case TokenKind.OpGt: { object lhsValue = this.EvalExpression(exp.Lhs); object rhsValue = this.EvalExpression(exp.Rhs); IComparable c1 = lhsValue as IComparable; IComparable c2 = rhsValue as IComparable; if (c1 == null || c2 == null) return false; else return c1.CompareTo(c2) == 1; } case TokenKind.OpLt: { object lhsValue = this.EvalExpression(exp.Lhs); object rhsValue = this.EvalExpression(exp.Rhs); IComparable c1 = lhsValue as IComparable; IComparable c2 = rhsValue as IComparable; if (c1 == null || c2 == null) return false; else return c1.CompareTo(c2) == -1; } case TokenKind.OpGte: { object lhsValue = this.EvalExpression(exp.Lhs); object rhsValue = this.EvalExpression(exp.Rhs); IComparable c1 = lhsValue as IComparable; IComparable c2 = rhsValue as IComparable; if (c1 == null || c2 == null) return false; else return c1.CompareTo(c2) >= 0; } case TokenKind.OpLte: { object lhsValue = this.EvalExpression(exp.Lhs); object rhsValue = this.EvalExpression(exp.Rhs); IComparable c1 = lhsValue as IComparable; IComparable c2 = rhsValue as IComparable; if (c1 == null || c2 == null) return false; else return c1.CompareTo(c2) <= 0; } default: throw new TemplateRuntimeException("遭遇未受支持的运算符:" + exp.Operator.ToString(), exp.Line, exp.Col); } }