public override void ExitExpression(GoParser.ExpressionContext context)
        {
            // expression
            //     : primaryExpr
            //     | unaryExpr
            //     | expression('*' | '/' | '%' | '<<' | '>>' | '&' | '&^') expression
            //     | expression('+' | '-' | '|' | '^') expression
            //     | expression('==' | '!=' | '<' | '<=' | '>' | '>=') expression
            //     | expression '&&' expression
            //     | expression '||' expression

            if (context.expression()?.Length == 2)
            {
                string leftOperand  = Expressions[context.expression(0)];
                string rightOperand = Expressions[context.expression(1)];
                string binaryOP     = context.children[1].GetText();

                if (binaryOP.Equals("<<") || binaryOP.Equals(">>"))
                {
                    // TODO: Need expression evaluations - no cast needed if expressions is int type
                    if (!int.TryParse(rightOperand, out int _))
                    {
                        rightOperand = $"(int)({rightOperand})";
                    }
                }

                binaryOP = binaryOP.Equals("&^") ? " & ~" : $" {binaryOP} ";

                Expressions[context] = $"{leftOperand}{binaryOP}{rightOperand}";
            }
            else
            {
                if (context.primaryExpr() != null)
                {
                    if (PrimaryExpressions.TryGetValue(context.primaryExpr(), out string primaryExpression))
                    {
                        Expressions[context] = primaryExpression;
                    }
                    else
                    {
                        AddWarning(context, $"Failed to find primary expression \"{context.unaryExpr().GetText()}\" in the expression \"{context.GetText()}\"");
                    }
                }
                else if (context.unaryExpr() != null)
                {
                    if (UnaryExpressions.TryGetValue(context.unaryExpr(), out string unaryExpression))
                    {
                        Expressions[context] = unaryExpression;
                    }
                    else
                    {
                        AddWarning(context, $"Failed to find unary expression \"{context.unaryExpr().GetText()}\" in the expression \"{context.GetText()}\"");
                    }
                }
                else
                {
                    AddWarning(context, $"Unexpected expression \"{context.GetText()}\"");
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="GoParser.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 VisitExpression([NotNull] GoParser.ExpressionContext context)
 {
     return(VisitChildren(context));
 }
 /// <summary>
 /// Exit a parse tree produced by <see cref="GoParser.expression"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitExpression([NotNull] GoParser.ExpressionContext context)
 {
 }
        public override void ExitExpression(GoParser.ExpressionContext context)
        {
            // expression
            //     : primaryExpr
            //     | unaryExpr
            //     | expression('*' | '/' | '%' | '<<' | '>>' | '&' | '&^') expression
            //     | expression('+' | '-' | '|' | '^') expression
            //     | expression('==' | '!=' | '<' | '<=' | '>' | '>=') expression
            //     | expression '&&' expression
            //     | expression '||' expression

            if (context.expression()?.Length == 2)
            {
                ExpressionInfo leftOperand  = Expressions[context.expression(0)];
                ExpressionInfo rightOperand = Expressions[context.expression(1)];
                string         binaryOP     = context.children[1].GetText();

                if (binaryOP.Equals("<<") || binaryOP.Equals(">>"))
                {
                    if (!int.TryParse(rightOperand.Text, out int _))
                    {
                        rightOperand.Text = $"(int)({rightOperand.Text})";
                    }
                }

                binaryOP = binaryOP.Equals("&^") ? " & ~" : $" {binaryOP} ";

                string expression = $"{leftOperand}{binaryOP}{rightOperand}";

                if (s_comparisionOperands.Contains(binaryOP))
                {
                    Expressions[context] = new ExpressionInfo
                    {
                        Text = expression,
                        Type = new TypeInfo
                        {
                            Name         = "bool",
                            TypeName     = "bool",
                            FullTypeName = "System.Boolean",
                            TypeClass    = TypeClass.Simple,
                            IsConst      = true
                        }
                    };
                }
                else
                {
                    // TODO: If both operands are integer, expression should be treated as arbitrary-precision numbers until assigned to a variable
                    Expressions[context] = new ExpressionInfo
                    {
                        Text = expression,
                        Type = leftOperand.Type
                    };
                }
            }
            else
            {
                if (!(context.primaryExpr() is null))
                {
                    if (PrimaryExpressions.TryGetValue(context.primaryExpr(), out ExpressionInfo primaryExpression))
                    {
                        Expressions[context] = primaryExpression;
                    }
                    else
                    {
                        AddWarning(context, $"Failed to find primary expression \"{context.primaryExpr().GetText()}\" in the expression \"{context.GetText()}\"");
                    }
                }
Exemple #5
0
 public override void EnterExpression([NotNull] GoParser.ExpressionContext context)
 {
 }