Beispiel #1
0
        private Stmt ForStatement()
        {
            Consume(TokenType.LeftParen, "Expect '(' after 'for'.");

            Stmt initializer;

            if (Match(TokenType.Semicolon))
            {
                // None is defined
                initializer = null;
            }
            else if (Match(TokenType.Var))
            {
                initializer = VarDeclartion();
            }
            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.RightParen))
            {
                increment = Expression();
            }
            Consume(TokenType.RightParen, "Expect ')' after for clauses.");

            Exception exception = null;

            try
            {
                m_LoopDepth++;
                Stmt body = Statement();

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

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

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

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

                return(body);
            }
            catch (Exception e)
            {
                exception = e;
            }
            finally
            {
                m_LoopDepth--;
            }

            throw exception;
        }
Beispiel #2
0
 public object Visit(Stmt.While _while)
 {
     Resolve(_while.condition);
     Resolve(_while.body);
     return(null);
 }