示例#1
0
 public Void Visit_BlockStmt(BlockStmt stmt)
 {
     BeginScope();
     Resolve(stmt.statements);
     EndScope();
     return(null);
 }
示例#2
0
        public object VisitBlockStmt(BlockStmt stmt)
        {
            BeginScope();
            Resolve(stmt.Statements);
            EndScope();

            return(null);
        }
示例#3
0
        // ForStatement -> "for" "(" ( varDecl | ExpressionStmt | ";" ) Expression? ";" Expression? ";" ")" Statement
        private AstNode ForStatement()
        {
            Consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'.");
            AstNode initializer = default(AstNode);

            if (!Match(TokenType.SEMICOLON))
            {
                initializer = Match(TokenType.VAR)
                    ? VarDeclaration()
                    : ExpressionStatement();
            }

            AstNode condition = Check(TokenType.SEMICOLON) ? new LiteralExpr(true) : Expression();

            Consume(TokenType.SEMICOLON, "Expect ';' after 'for' loop condition.");

            AstNode increment = Check(TokenType.RIGHT_PAREN) ? null : Expression();

            Consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses.");

            AstNode body = Statement();

            // Desugarize for loop
            // From: for (var i = 0; i < 10; ++i) doStuff();
            // To: var i = 0; while (i < 10) { doStuff(); i = i+1; }
            if (increment != null)
            {
                body = new BlockStmt(new List <AstNode> {
                    body, new ExpressionStmt(increment)
                });
            }

            body = new WhileStmt(condition, body);

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

            return(body);
        }
示例#4
0
        public object VisitBlockStmt(BlockStmt stmt)
        {
            ExecuteBlock(stmt.Statements, new Scope(_scope));

            return(null);
        }
示例#5
0
文件: Parser.cs 项目: sirgru/CsLox
        private Stmt ForStatement()
        {
            Expect(TokenType.LeftParen, ErrorType.ExpectedOpenParen);

            Stmt initializer;

            if (CheckCurrentToken(TokenType.Semicolon))
            {
                initializer = null;
                Advance();
            }
            else if (CheckCurrentToken(TokenType.Var))
            {
                Advance();
                initializer = VarDeclaration();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr condition = null;

            if (!CheckCurrentToken(TokenType.Semicolon))
            {
                condition = Expression();
            }
            Expect(TokenType.Semicolon, ErrorType.ExpectedSemicolon);

            Expr increment = null;

            if (!CheckCurrentToken(TokenType.RightParen))
            {
                increment = Expression();
            }
            Expect(TokenType.RightParen, ErrorType.ExpectedClosedParen);

            Stmt body = Statement();

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

            if (condition == null)
            {
                condition = new LiteralExpr(true);
            }
            body = new WhileStmt(condition, body);

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

            return(body);
        }
示例#6
0
 public Void Visit_BlockStmt(BlockStmt stmt)
 {
     ExecuteBlock(stmt.statements, new Environment(_environment));
     return(null);
 }
示例#7
0
 public string Visit_BlockStmt(BlockStmt stmt)
 {
     return(Parenthesize("Block", stmt.statements.ToArray()));
 }