Esempio n. 1
0
        public object VisitWhileStmt(While stmt)
        {
            LoopType enclosingLoop = currentLoop;

            currentLoop = LoopType.WHILE;
            Resolve(stmt.Condition);
            Resolve(stmt.Body);
            currentLoop = enclosingLoop;

            return(null);
        }
Esempio n. 2
0
        public object VisitWhileStmt(While stmt)
        {
            while (IsTruthy(Evaluate(stmt.Condition)))
            {
                try
                {
                    Execute(stmt.Body);
                }
                catch (BreakException)
                {
                    break;
                }
            }

            return(null);
        }
Esempio n. 3
0
        private Stmt ForStatement()
        {
            Consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'.");

            Stmt 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.");

            Stmt body = Statement();

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

            if (condition == null)
            {
                condition = new Literal(true);
            }
            body = new While(condition, body);

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

            return(body);
        }