예제 #1
0
 public override void VisitForExpression(BoundForExpression node)
 {
     _writer.WriteKeyword("for ");
     _writer.WritePunctuation("(");
     _writer.WriteIdentifier(node.Variable.Name);
     _writer.WritePunctuation(" <- ");
     node.LowerBound.Accept(this);
     _writer.WriteKeyword(" to ");
     node.UpperBound.Accept(this);
     _writer.WritePunctuation(") ");
     _writer.WriteLine();
     WriteNestedExpression(node.Body);
 }
예제 #2
0
 public virtual void VisitForExpression(BoundForExpression node) =>
 this.DefaultVisit(node);
예제 #3
0
    protected override BoundExpression RewriteForExpression(BoundForExpression node)
    {
        /*
         * convert from for to while
         *
         * for (x <- l to u) expr
         *
         * var x = l
         * while(x < u) {
         *     expr
         *     continue:
         *     x = x + 1
         * }
         */

        var lowerBound = RewriteExpression(node.LowerBound);
        var upperBound = RewriteExpression(node.UpperBound);
        var body       = RewriteExpression(node.Body);

        var declareX = new BoundVariableDeclarationStatement(
            node.Syntax,
            node.Variable,
            lowerBound
            );

        var variableExpression = ValueExpression(node.Syntax, node.Variable);
        var condition          = new BoundBinaryExpression(
            node.Syntax,
            variableExpression,
            BoundBinaryOperator.BindOrThrow(SyntaxKind.LessThanToken, Type.Int, Type.Int),
            upperBound
            );
        var continueLabelStatement = new BoundLabelStatement(node.Syntax, node.ContinueLabel);
        var incrementX             = new BoundExpressionStatement(
            node.Syntax,
            new BoundAssignmentExpression(
                node.Syntax,
                variableExpression,
                new BoundBinaryExpression(
                    node.Syntax,
                    variableExpression,
                    BoundBinaryOperator.BindOrThrow(SyntaxKind.PlusToken, Type.Int, Type.Int),
                    new BoundLiteralExpression(node.Syntax, 1)
                    )
                )
            );
        var whileBody = new BoundBlockExpression(
            node.Syntax,
            ImmutableArray.Create <BoundStatement>(
                new BoundExpressionStatement(body.Syntax, body),
                continueLabelStatement,
                incrementX
                ),
            new BoundUnitExpression(node.Syntax)
            );

        var newBlock = new BoundBlockExpression(
            node.Syntax,
            ImmutableArray.Create <BoundStatement>(declareX),
            new BoundWhileExpression(
                node.Syntax,
                condition,
                whileBody,
                node.BreakLabel,
                new BoundLabel("continue")
                )
            );

        return(RewriteExpression(newBlock));
    }
예제 #4
0
 protected override BoundExpression RewriteForExpression(BoundForExpression node)
 {
     throw new InvalidProgramException("No `for` expression should exist at this stage");
 }