示例#1
0
        public override void ExitExpression(GolangParser.ExpressionContext context)
        {
            // expression
            //     : unaryExpr
            //     | expression BINARY_OP 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 (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()}\"");
            }
        }
示例#2
0
 public override Base VisitExpression([NotNull] GolangParser.ExpressionContext context)
 {
     if (context.unary_expr() != null)
     {
         return(context.unary_expr().Accept(this));
     }
     else
     {
         var newBe = new BinaryExpression(
             (context.binary_op().Accept(this) as BinaryOpWrapper).Op,
             context.expression(0).Accept(this) as Expression,
             context.expression(1).Accept(this) as Expression);
         return(FixPrecedence(newBe));
     }
 }