Exemplo n.º 1
0
        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()}\"");
                }
            }
        }
Exemplo n.º 2
0
        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()}\"");
                    }
                }