Esempio n. 1
0
 public Unit VisitWhileStmt(Stmt.While stmt)
 {
     Resolve(stmt.Condition);
     Resolve(stmt.Body);
     return(new Unit());
 }
Esempio n. 2
0
        /// <summary>
        /// Parse a for loop
        /// </summary>
        /// <returns>The statement</returns>
        private Stmt ForStatement()
        {
            _loop_depth++;

            try
            {
                Consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'.");

                // Initializer

                Stmt initializer;
                if (Match(TokenType.SEMICOLON))
                {
                    // No initialiser
                    initializer = null;
                }
                else if (Match(TokenType.VAR))
                {
                    // Its a variable decalration
                    initializer = VarDeclaration();
                }
                else
                {
                    // Its an expression
                    // This must be a _statement_
                    initializer = ExpressionStatement();
                }

                // Condition
                Expr condition = null;
                if (!Check(TokenType.SEMICOLON))
                {
                    condition = Expression();
                }
                Consume(TokenType.SEMICOLON, "Expect ';' after loop condition.");

                // Increment
                Expr increment = null;
                if (!Check(TokenType.SEMICOLON))
                {
                    increment = Expression();
                }
                Consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses.");

                // Body
                Stmt body = Statement();

                // Convert to a while loop
                if (increment != null)
                {
                    body = new Stmt.Block(new[] { body, new Stmt.ExpressionStatement(increment) });
                }

                if (condition == null)
                {
                    // No condition, so set to true
                    condition = new Expr.Literal(true);
                }

                body = new Stmt.While(condition, body);

                if (initializer != null)
                {
                    body = new Stmt.Block(new[] { initializer, body });
                }

                return(body);
            }
            finally
            {
                _loop_depth--;
            }
        }
Esempio n. 3
0
 public object VisitWhileStmt(Stmt.While stmt, object options)
 {
     throw new NotImplementedException();
 }
Esempio n. 4
0
 public object visitWhileStmt(Stmt.While stmt)
 {
     resolve(stmt.condition);
     resolve(stmt.body);
     return(null);
 }
Esempio n. 5
0
 public object VisitWhileStmt(Stmt.While stmt)
 {
     Resolve(stmt.Condition);
     Resolve(stmt.Body);
     return(null);
 }
Esempio n. 6
0
        private Stmt ForStatment()
        {
            try
            {
                this.LoopCounter += 1;

                Consume(LEFT_PAREN, "Exepect '(' after 'for.");
                Stmt initialiser;
                if (Match(SEMICOLON))
                {
                    initialiser = null;
                }
                else if (Match(VAR))
                {
                    initialiser = VarDeclaration();
                }
                else
                {
                    initialiser = ExpressionStatement();
                }

                Expr Condition = null;
                if (!Check(SEMICOLON))
                {
                    Condition = Expression();
                }
                Consume(SEMICOLON, "Expect ';' after loop condition.");

                Expr increment = null;
                if (!Check(RIGHT_PAREN))
                {
                    increment = Expression();
                }
                Consume(RIGHT_PAREN, "Expect ')' after for clausess.");

                Stmt body = Statement();
                if (increment != null)
                {
                    body = new Stmt.Block(new List <Stmt> {
                        body, new Stmt.ExpressionStmt(increment)
                    });
                }

                if (Condition == null)
                {
                    Condition = new Expr.Literal(true);
                }
                body = new Stmt.While(Condition, body);

                if (initialiser != null)
                {
                    body = new Stmt.Block(new List <Stmt> {
                        initialiser, body
                    });
                }



                return(body);
            }
            finally
            {
                LoopCounter--;
            }
        }
Esempio n. 7
0
 public Stmt VisitWhileStmt(Stmt.While stmt)
 {
     throw new NotImplementedException();
 }
Esempio n. 8
0
    private Stmt ForStatement()
    {
        Consume(LeftParen, "Expect '(' after 'for'.");

        Stmt initializer;

        if (Match(Semicolon))
        {
            initializer = null;
        }
        else if (Match(Var))
        {
            initializer = VarDeclaration();
        }
        else
        {
            initializer = ExpressionStatement();
        }

        Expr condition = null;

        if (!Check(Semicolon))
        {
            condition = Expression();
        }

        Consume(Semicolon, "Expect ';' after loop condition.");

        Expr increment = null;

        if (!Check(RightParen))
        {
            increment = Expression();
        }

        Consume(RightParen, "Expect ')' after for clauses.");

        Stmt body = Statement();

        if (increment != null)
        {
            body = new Stmt.Block(
                new List <Stmt> {
                body, new Stmt.Expression(increment)
            });
        }

        if (condition == null)
        {
            condition = new Expr.Literal(true);
        }

        body = new Stmt.While(condition, body);

        if (initializer != null)
        {
            body = new Stmt.Block(new List <Stmt> {
                initializer, body
            });
        }

        return(body);
    }
Esempio n. 9
0
 public object VisitWhileStmt(Stmt.While stmt, object options = null)
 {
     return(null);
 }
Esempio n. 10
0
        private Stmt ForStatement()
        {
            // The "for" implementation is a bit special in that it doesn't use any special AST nodes of its own.
            // Instead, it just desugars the for loop into already existing elements in our toolbox.
            // More details: http://craftinginterpreters.com/control-flow.html#desugaring
            Consume(LEFT_PAREN, "Expect '(' after 'for'.");

            Stmt initializer;

            if (Match(SEMICOLON))
            {
                initializer = null;
            }
            else if (Match(VAR))
            {
                initializer = VarDeclaration();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr condition = null;

            if (!Check(SEMICOLON))
            {
                condition = Expression();
            }

            Consume(SEMICOLON, "Expect ';' after loop condition.");

            Expr increment = null;

            if (!Check(RIGHT_PAREN))
            {
                increment = Expression();
            }

            Consume(RIGHT_PAREN, "Expect ')' after for clauses.");

            Stmt body = Statement();

            if (increment != null)
            {
                body = new Stmt.Block(new List <Stmt>
                {
                    body,
                    new Stmt.ExpressionStmt(increment)
                });
            }

            condition ??= new Expr.Literal(true);

            body = new Stmt.While(condition, body);

            if (initializer != null)
            {
                body = new Stmt.Block(new List <Stmt> {
                    initializer, body
                });
            }

            return(body);
        }
Esempio n. 11
0
 public string VisitWhileStmt(Stmt.While stmt)
 {
     return(Parenthesize2("while", stmt.Condition, stmt.Body));
 }
Esempio n. 12
0
        private Stmt ForStatement()
        {
            Consume(LEFT_PAREN, "Expected \"(\" after \"for\".");

            Stmt initializer;

            if (Match(SEMICOLON))
            {
                initializer = null;
            }
            else if (Match(VAR))
            {
                initializer = VarDeclaration();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr condition = null;

            if (!Check(SEMICOLON))
            {
                condition = Expression();
            }
            Consume(SEMICOLON, "Expected \";\" after loop condition");

            Expr increment = null;

            if (!Check(RIGHT_PAREN))
            {
                increment = Expression();
            }
            Consume(RIGHT_PAREN, "Expected \")\" after clauses");

            Stmt body = Statement();

            if (increment != null)
            {
                body = new Stmt.Block(new List <Stmt>()
                {
                    body, new Stmt.Expression(increment)
                });
            }

            if (condition == null)
            {
                condition = new Expr.Literal(true);
            }

            body = new Stmt.While(condition, body);

            if (initializer != null)
            {
                body = new Stmt.Block(new List <Stmt>()
                {
                    initializer, body
                });
            }

            return(body);
        }