/// <summary> /// Visit a parse tree produced by the <c>comparatorExpression</c> /// labeled alternative in <see cref="AntlrConditionParser.expression"/>. /// <para> /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/> /// on <paramref name="context"/>. /// </para> /// </summary> /// <param name="context">The parse tree.</param> /// <return>The visitor result.</return> public virtual Result VisitComparatorExpression([NotNull] AntlrConditionParser.ComparatorExpressionContext context) { return(VisitChildren(context)); }
/// <summary> /// Exit a parse tree produced by the <c>comparatorExpression</c> /// labeled alternative in <see cref="AntlrConditionParser.expression"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitComparatorExpression([NotNull] AntlrConditionParser.ComparatorExpressionContext context) { }
public override Result VisitComparatorExpression(AntlrConditionParser.ComparatorExpressionContext context) { var left = Visit(context.left); var right = Visit(context.right); if (left.Type != right.Type) { throw new Exception("Invalid type comparison"); } if (context.op.EQ() != null) { return new Result { Bool = left.Equals(right) } } ; if (context.op.NEQ() != null) { return new Result { Bool = !left.Equals(right) } } ; if (context.op.GT() != null) { if (left.Type != right.Type || left.Type == ResultType.Object || left.Type == ResultType.Bool) { throw new Exception("Invalid type comparison"); } if (left.Type == ResultType.Decimal) { return new Result { Bool = left.Decimal > right.Decimal } } ; if (left.Type == ResultType.String) { return new Result { Bool = string.Compare(left.String, right.String, StringComparison.Ordinal) > 0 } } ; } if (context.op.GE() != null) { if (left.Type != right.Type || left.Type == ResultType.Object || left.Type == ResultType.Bool) { throw new Exception("Invalid type comparison"); } if (left.Type == ResultType.Decimal) { return new Result { Bool = left.Decimal > right.Decimal } } ; if (left.Type == ResultType.String) { return new Result { Bool = string.Compare(left.String, right.String, StringComparison.Ordinal) >= 0 } } ; } if (context.op.LT() != null) { if (left.Type != right.Type || left.Type == ResultType.Object || left.Type == ResultType.Bool) { throw new Exception("Invalid type comparison"); } if (left.Type == ResultType.Decimal) { return new Result { Bool = left.Decimal > right.Decimal } } ; if (left.Type == ResultType.String) { return new Result { Bool = string.Compare(left.String, right.String, StringComparison.Ordinal) < 0 } } ; } if (context.op.LE() != null) { if (left.Type != right.Type || left.Type == ResultType.Object || left.Type == ResultType.Bool) { throw new Exception("Invalid type comparison"); } if (left.Type == ResultType.Decimal) { return new Result { Bool = left.Decimal > right.Decimal } } ; if (left.Type == ResultType.String) { return new Result { Bool = string.Compare(left.String, right.String, StringComparison.Ordinal) <= 0 } } ; } return(new Result()); }