예제 #1
0
파일: Parser.cs 프로젝트: mutuware/Lox
        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 Stmt.Block(new List <Stmt>
                {
                    body,
                    new Stmt.Expression(increment)
                });
            }

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

            if (initializer != null)
            {
                body = new Stmt.Block(new List <Stmt> {
                    initializer, body
                });
            }
            return(body);
        }
예제 #2
0
 public object VisitBlockStmt(Stmt.Block stmt)
 {
     BeginScope();
     Resolve(stmt.statements);
     EndScope();
     return(null);
 }
예제 #3
0
 public Void VisitBlockStmt(Stmt.Block stmt)
 {
     BeginScope();
     Resolve(stmt.Statements);
     EndScope();
     return(null);
 }
예제 #4
0
        public object Visit(Stmt.Block _block)
        {
            BeginScope();
            Resolve(_block.statements);
            EndScope();

            return(null);
        }
예제 #5
0
 public object visitBlockStmt(Stmt.Block stmt)
 {
     return(executeBlock(stmt.statements, new Environment(environment)));
 }
예제 #6
0
 public Void VisitBlockStmt(Stmt.Block stmt)
 {
     ExecuteBlock(stmt.Statements, new LoxEnvironment(_environment));
     return(null);
 }
예제 #7
0
        private Stmt ForStatement()
        {
            Consume(LEFT_PAREN, "Expect '(' after 'for'.");

            // For loop doesn't get its own statement.
            // Instead we desugar and reduce it to a while loop.

            Stmt initializer;

            if (Match(SEMICOLON))
            {
                initializer = null;
            }
            else if (Match(VAR))
            {
                initializer = VarDeclaration();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr condition = null;

            if (!IsCurrentTokenType(SEMICOLON))
            {
                condition = Expression();
            }
            Consume(SEMICOLON, "Expect ';' after loop condition.");

            Expr increment = null;

            if (!IsCurrentTokenType(RIGHT_PAREN))
            {
                increment = Expression();
            }
            Consume(RIGHT_PAREN, "Expect ')' after for clauses.");

            // Parse the statement body of the for loop.
            Stmt body = Statement();

            if (increment != null)
            {
                // Add increment to be executed after the main body.
                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)
            {
                // Run the initializer once before the while loop.
                body = new Stmt.Block(new List <Stmt> {
                    initializer, body
                });
            }

            return(body);
        }
예제 #8
0
파일: Interpreter.cs 프로젝트: mutuware/Lox
 // Statement.IVisitor implementaion
 public object VisitBlockStmt(Stmt.Block stmt)
 {
     ExecuteBlock(stmt.Statements, new Environment(environment));
     return(null);
 }
예제 #9
0
 public string VisitBlockStmt(Stmt.Block stmt)
 {
     throw new NotImplementedException();
 }
예제 #10
0
 // statements
 public string VisitBlockStmt(Stmt.Block stmt) => "block";