Пример #1
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node)
        {
            // while <condition>
            //      <bode>
            //
            // ----->
            //
            // goto continue

            // <body>
            // continue:
            // gotoTrue <condition> body
            // Break
            //



            var gotoContinue           = new BoundGotoStatment(node.ContinueLabel);
            var bodyLabelStatement     = new BoundLabelStatement(node.BodyLabel);
            var continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);
            var gotoTrue            = new BoundConditionalGotoStatment(node.BodyLabel, node.Condition);
            var breakLabelStatement = new BoundLabelStatement(node.BreakLabel);

            var result = new BoundBlockStatemnet(ImmutableArray.Create <BoundStatement>(
                                                     gotoContinue,
                                                     bodyLabelStatement,
                                                     node.Body,
                                                     continueLabelStatement,
                                                     gotoTrue,
                                                     breakLabelStatement
                                                     ));


            return(RewriteStatement(result));
        }
Пример #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 BoundConditionalGotoStatment(endLabel, node.Condition, false);
                var endLabelStatement = new BoundLabelStatement(endLabel);
                var result            = new BoundBlockStatemnet(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 BoundConditionalGotoStatment(elseLabel, node.Condition, false);
                var gotoEndStatement   = new BoundGotoStatment(endLabel);
                var elseLabelStatement = new BoundLabelStatement(elseLabel);
                var endLabelStatement  = new BoundLabelStatement(endLabel);
                var result             = new BoundBlockStatemnet(ImmutableArray.Create <BoundStatement>(
                                                                     gotoFalse,
                                                                     node.ThenStatement,
                                                                     gotoEndStatement,
                                                                     elseLabelStatement,
                                                                     node.ElseStatement,
                                                                     endLabelStatement
                                                                     ));
                return(RewriteStatement(result));
            }
        }
Пример #3
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node)
        {
            // while <condition>
            //      <bode>
            //
            // ----->
            //
            // goto check
            // continue:
            // <body>
            // check:
            // gotoTrue <condition> continue
            // end:
            //

            var continueLabel = GenerateLabel();
            var checkLabel    = GenerateLabel();
            var endLabel      = GenerateLabel();

            var gotoCheck = new BoundGotoStatment(checkLabel);
            var continueLabelStatement = new BoundLabelStatement(continueLabel);
            var checkLabelStatement    = new BoundLabelStatement(checkLabel);
            var gotoTrue          = new BoundConditionalGotoStatment(continueLabel, node.Condition);
            var endLabelStatement = new BoundLabelStatement(endLabel);

            var result = new BoundBlockStatemnet(ImmutableArray.Create <BoundStatement>(
                                                     gotoCheck,
                                                     continueLabelStatement,
                                                     node.Body,
                                                     checkLabelStatement,
                                                     gotoTrue,
                                                     endLabelStatement
                                                     ));

            return(RewriteStatement(result));
        }