예제 #1
0
 public object visitWhileStatement(Statement.whileStmt stmt)
 {
     if (stmt.doWhileLoop)
     {
         do
         {
             _loop = true;
             execute(stmt.body);
             _continue = false;
         } while (isTruthy(evaluate(stmt.condition)) && !_break);
     }
     else
     {
         while (isTruthy(evaluate(stmt.condition)) && !_break)
         {
             _loop = true;
             execute(stmt.body);
             _continue = false;
         }
     }
     _loop     = false;
     _break    = false;
     _continue = false;
     return(null);
 }
예제 #2
0
        private Statement forStatement()
        {
            consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'.");
            Statement initializer;

            if (match(TokenType.SEMICOLON))
            {
                initializer = null;
            }
            else if (match(TokenType.VAR))
            {
                initializer = varDeclaration();
            }
            else
            {
                initializer = expressionStatement();
            }

            Expr condition = null;

            if (!check(TokenType.SEMICOLON))
            {
                condition = expression();
            }
            consume(TokenType.SEMICOLON, "Expect ';' after loop condition.");

            Expr increment = null;

            if (!check(TokenType.RIGHT_PAREN))
            {
                increment = expression();
            }
            consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses.");
            parsingLoop = true;
            Statement body = statement();

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

            if (condition == null)
            {
                condition = new Expr.Literal(true, previous());
            }
            body = new Statement.whileStmt(condition, body, false);

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

            return(body);
        }
예제 #3
0
        public object visitWhileStatement(Statement.whileStmt whileStmt)
        {
            bool enclosing = isInLoop;

            isInLoop = true;
            resolve(whileStmt.condition);
            resolve(whileStmt.body);
            isInLoop = enclosing;
            return(null);
        }