private Stmt ForStatement() { Consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'."); // If the token following the ( is a semicolon then the initializer // has been omitted. Otherwise, we check for a var keyword to see // if it’s a variable declaration. If neither of those matched, it // must be an expression. We parse that and wrap it in an expression // statement so that the initializer is always of type Stmt Stmt initializer = Match(TokenType.SEMICOLON) ? null : Match(TokenType.VAR) ? VarDeclaration() : ExpressionStatement(); // If next token is a semicolon then the condition has been omitted. Expr condition = Check(TokenType.SEMICOLON) ? null : Expression(); Consume(TokenType.SEMICOLON, "Expect ';' after loop condition."); // If next token is closing right paren, increment ommitted. Expr increment = Check(TokenType.RIGHT_PAREN) ? null : Expression(); Consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses."); var body = Statement(); // Add the increment expression evaluation to end of body if not null if (increment != null) { body = new Block(new List <Stmt> { body, new Expression(increment) }); } // Wrap the body into a while based on condition. If null then set // condition to true (infinite loop) if (condition is null) { condition = new Literal(true); } body = new While(condition, body); // If initializer was set, wrap body in block with initializer to run // first before the loop body is executed if (initializer != null) { body = new Block(new List <Stmt> { initializer, body }); } return(body); }
public string VisitWhileStmt(While stmt) { throw new System.NotImplementedException(); }
public object VisitWhileStmt(While stmt) { Resolve(stmt.Condition); Resolve(stmt.Body); return(null); }