/// <summary>
 /// Exit a parse tree produced by the <c>booleanExpr</c>
 /// labeled alternative in <see cref="KittenGrammarParser.expr"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitBooleanExpr([NotNull] KittenGrammarParser.BooleanExprContext context)
 {
 }
示例#2
0
        public override DynObj VisitBooleanExpr([NotNull] KittenGrammarParser.BooleanExprContext context)
        {
            var LHS = Visit(context.left);
            var RHS = Visit(context.right);
            var op  = context.op.Text;

            if (LHS.t == RHS.t)
            {
                if (LHS.t == vType.Number)
                {
                    switch (op)
                    {
                    case "==":
                        return(DBool(LHS.NumberVal == RHS.NumberVal));

                    case "!=":
                        return(DBool(LHS.NumberVal != RHS.NumberVal));

                    case ">=":
                        return(DBool(LHS.NumberVal >= RHS.NumberVal));

                    case "<=":
                        return(DBool(LHS.NumberVal <= RHS.NumberVal));

                    case ">":
                        return(DBool(LHS.NumberVal > RHS.NumberVal));

                    case "<":
                        return(DBool(LHS.NumberVal < RHS.NumberVal));
                    }
                }
                if (LHS.t == vType.Bool)
                {
                    switch (op)
                    {
                    case "==":
                        return(DBool(LHS.BoolVal == RHS.BoolVal));

                    case "!=":
                        return(DBool(LHS.BoolVal != RHS.BoolVal));

                    default:
                        ReportError("InvalidOperatorError", $"operator {op} used in LHS and RHS type {LHS.typeName}", context.start.Line);
                        break;
                    }
                }
                if (LHS.t == vType.String)
                {
                    switch (op)
                    {
                    case "==":
                        return(DBool(LHS.StringVal == RHS.StringVal));

                    case "!=":
                        return(DBool(LHS.StringVal != RHS.StringVal));

                    default:
                        ReportError("InvalidOperatorError", $"operator {op} used in LHS and RHS type {LHS.typeName}", context.start.Line);
                        break;
                    }
                }
                if (LHS.t == vType.Null)
                {
                    switch (op)
                    {
                    case "==":
                        return(DBool(true));

                    case "!=":
                        return(DBool(false));

                    default:
                        ReportError("InvalidOperatorError", $"operator {op} used in LHS and RHS type {LHS.typeName}", context.start.Line);
                        break;
                    }
                }
            }
            else
            {
                ReportError("InvalidTypeError", $"LHS type {LHS.typeName} and RHS type {RHS.typeName} used on operator \"{op}\"", context.start.Line);
            }
            return(base.VisitBooleanExpr(context));
        }
 /// <summary>
 /// Visit a parse tree produced by the <c>booleanExpr</c>
 /// labeled alternative in <see cref="KittenGrammarParser.expr"/>.
 /// <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 VisitBooleanExpr([NotNull] KittenGrammarParser.BooleanExprContext context)
 {
     return(VisitChildren(context));
 }