Ejemplo n.º 1
0
 object Stmt.IVisitor <object> .visitBlockStmt(Stmt.Block stmt)
 {
     BeginScope();
     stmt.statements.ForEach(Resolve);
     EndScope();
     return(null);
 }
Ejemplo n.º 2
0
        public object visitBlockStmt(Stmt.Block block)
        {
            Environment blockEnvironment = new Environment(enclosing: this.environment);

            ExecuteBlock(block.statements, blockEnvironment);
            return(null);
        }
Ejemplo n.º 3
0
        // This returns a block with an initializer statement and a while statement, a
        // dedicated Stmt.For class is not needed.
        Stmt ParseForStatement()
        {
            consume(TokenType.LEFT_PAREN, "Expect '(' before for statement condition.");
            Stmt initializer = null;

            if (match(TokenType.VAR))
            {
                initializer = ParseVarDeclaration();
            }
            else if (!match(TokenType.SEMICOLON))
            {
                initializer = ParseExpressionStatement();
            }
            Expr condition = null;

            if (!match(TokenType.SEMICOLON))
            {
                condition = ParseExpression();
                consume(TokenType.SEMICOLON, "Expect ';' after for statement condition.");
            }
            Expr increment = null;

            if (!match(TokenType.RIGHT_PAREN))
            {
                increment = ParseExpression();
                consume(TokenType.RIGHT_PAREN, "Expect ')' after for statement condition.");
            }

            Stmt body = ParseStatement();

            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);
        }