Exemplo n.º 1
0
        public override BoundNode VisitIfStatement(BoundIfStatement node)
        {
            Debug.Assert(node != null);

            var rewrittenCondition = (BoundExpression)Visit(node.Condition);
            var rewrittenConsequence = (BoundStatement)Visit(node.Consequence);
            var rewrittenAlternative = (BoundStatement)Visit(node.AlternativeOpt);
            return RewriteIfStatement(node.Syntax, rewrittenCondition, rewrittenConsequence, rewrittenAlternative, node.HasErrors);
        }
Exemplo n.º 2
0
        protected override BoundStatement RewriteIfStatement(BoundIfStatement node)
        {
            if (node.ElseStatement == null)
            {
                // if <condition>
                //      <then>
                //
                // ---->
                //
                // gotoFalse <condition> end
                //      <then>
                // end:

                var endLabel = GenerateLabel();

                var gotoFalse         = new BoundConditionalGotoStatement(endLabel, node.Condition, jumpIfTrue: false);
                var endLabelStatement = new BoundLabelStatement(endLabel);

                var result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                         gotoFalse,
                                                         node.ThenStatement,
                                                         endLabelStatement
                                                         ));

                return(RewriteStatement(result));
            }
            else
            {
                // if <condition>
                //      <then>
                // else
                //      <else>
                //
                // ---->
                //
                // gotoFalse <condition> else
                //      <then>
                // goto end
                // else:
                //      <else>
                // end:

                var elseLabel = GenerateLabel();
                var endLabel  = GenerateLabel();

                var gotoFalse          = new BoundConditionalGotoStatement(elseLabel, node.Condition, jumpIfTrue: false);
                var gotoEndStatement   = new BoundGotoStatement(endLabel);
                var elseLabelStatement = new BoundLabelStatement(elseLabel);
                var endLabelStatement  = new BoundLabelStatement(endLabel);

                var result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                         gotoFalse,
                                                         node.ThenStatement,
                                                         gotoEndStatement,
                                                         elseLabelStatement,
                                                         node.ElseStatement,
                                                         endLabelStatement
                                                         ));

                return(RewriteStatement(result));
            }
        }