Exemplo n.º 1
0
        public override object VisitIfStatement([NotNull] IfStatementContext context)
        {
            var type = TypeVisitor.GetType(context.expression(), scope, errors);

            if (!type.InheritsFrom(TypeSystem.Instance["Boolean"]))
            {
                errors.Add(new Error(context, $"Expression type must be {TypeSystem.Instance["Boolean"]}"));
            }

            return(base.VisitIfStatement(context));
        }
Exemplo n.º 2
0
        public override Expression VisitIfStatement(IfStatementContext context)
        {
            var test    = Visit(context.expression());
            var ifBlock = Visit(context.if_body()[0]);

            if (context.ELSE() != null)
            {
                var elseBlock = Visit(context.if_body()[1]);
                return(Expression.IfThenElse(test, ifBlock, elseBlock));
            }
            return(Expression.IfThen(test, ifBlock));
        }
Exemplo n.º 3
0
        public IfStatement ParseIfStatement(IfStatementContext node)
        {
            IfStatement statement = new IfStatement();

            IfStatementItem ifItem = new IfStatementItem()
            {
                Type = IfStatementType.IF
            };

            ifItem.Condition = new TokenInfo(node.expression() as PredicateExpressionContext)
            {
                Type = TokenType.Condition
            };
            ifItem.Statements.AddRange(this.ParseProcedureStatement(node._procedureSqlStatement));
            statement.Items.Add(ifItem);

            foreach (ElifAlternativeContext elseif in node.elifAlternative())
            {
                IfStatementItem elseIfItem = new IfStatementItem()
                {
                    Type = IfStatementType.ELSEIF
                };
                elseIfItem.Condition = new TokenInfo(elseif.expression() as PredicateExpressionContext)
                {
                    Type = TokenType.Condition
                };
                elseIfItem.Statements.AddRange(elseif.procedureSqlStatement().SelectMany(item => this.ParseProcedureStatement(item)));

                statement.Items.Add(elseIfItem);
            }

            if (node._elseStatements.Count > 0)
            {
                IfStatementItem elseItem = new IfStatementItem()
                {
                    Type = IfStatementType.ELSE
                };
                elseItem.Statements.AddRange(node._elseStatements.SelectMany(item => this.ParseProcedureStatement(item)));

                statement.Items.Add(elseItem);
            }

            return(statement);
        }
Exemplo n.º 4
0
        public void VisitIfStatement(IfStatementContext context)
        {
            var elseLabel = builder.CreateLabel();
            var endLabel  = builder.CreateLabel();

            VisitExpression(context.expression());

            builder.OutputOperation(Operation.OpJumpNotIf, elseLabel);

            VisitStatementBlock(context.block1);

            builder.OutputOperation(Operation.OpJump, endLabel);
            builder.SetLabelPosition(elseLabel);

            if (context.block2 != null)
            {
                VisitStatementBlock(context.block2);
            }

            builder.SetLabelPosition(endLabel);
        }