Пример #1
0
 public object VisitBlockStmt(Stmt.Block stmt)
 {
     BeginScope();
     Resolve(stmt.Statements);
     EndScope();
     return(null);
 }
Пример #2
0
 public object VisitBlockStmt(Stmt.Block stmt)
 {
     ExecuteBlock(stmt.Statements, new Environment(environment));
     return(null);
 }
Пример #3
0
        // our first desugaring
        private Stmt ForStatement()
        {
            loopDepth++;

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

                Stmt initializer;
                if (Match(Token.TokenType.Semicolon))
                {
                    initializer = null;
                }
                else if (Match(Token.TokenType.Var))
                {
                    initializer = VarDeclaration();
                }
                else
                {
                    initializer = ExpressionStatement();
                }

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

                Expr increment = null;
                if (!Check(Token.TokenType.RightParenthesis))
                {
                    increment = Expression();
                }
                Consume(Token.TokenType.RightParenthesis, "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);
            }
            finally
            {
                loopDepth--;
            }
        }