def for_statement(self): self.consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'.") if self.match(TokenType.SEMICOLON): initializer = None elif self.match(TokenType.VAR): initializer = self.var_declaration() else: initializer = self.expression_statement() condition = None if not self.check(TokenType.SEMICOLON): condition = self.expression() self.consume(TokenType.SEMICOLON, "Expect ';' after loop condition.") increment = None if not self.check(TokenType.RIGHT_PAREN): increment = self.expression() self.consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses.") body = self.statement() if increment is not None: body = Stmt.Block(list(body), Stmt.Expression(increment)) if condition is None: condition = Expr.Literal(True) body = Stmt.While(condition, body) if initializer is not None: body = Stmt.Block(list(initializer), body) return body
def _for_statement(self) -> st.While: self._consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'.") initializer = None if self._match(TokenType.SEMICOLON): pass elif self._match(TokenType.VAR): initializer = self._variable_declaration() else: initializer = self._expression_statement() condition = ex.Literal(True) if self._check( TokenType.SEMICOLON) else self._expression() self._consume(TokenType.SEMICOLON, "Expect ';' after loop condition.") increment = None if self._check( TokenType.RIGHT_PAREN) else self._expression() self._consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses.") body = self._statement() if increment is not None: body = st.Block([body, st.Expression(increment)]) body = st.While(condition, body) if initializer is not None: body = st.Block([initializer, body]) return body
def for_statement(self): next = self.consume(TT.IDENTIFIER, "Expect variable name.") self.consume(TT.IN, "Expect 'in' after variable name.") collection = self.consume(TT.IDENTIFIER, "Expect collection to iterate over.") self.consume(TT.COLON, "Expect ':' to end 'for'.") self.consume(TT.NEWLINE, "Expect newline after ':'.") # self.consume(TT.LEFT_PAREN, "Expect '(' after 'for'.") # if self.match(TT.SEMICOLON): # initializer = None # elif self.match(TT.VAR): # initializer = self.var_declaration() # else: # initializer = self.expression_statement() # condition = None # if not self.check(TT.SEMICOLON): # condition = self.expression() # self.consume(TT.SEMICOLON, "Expect ';' after loop condition") # increment = None # if not self.check(TT.RIGHT_PAREN): # increment = self.expression() # self.consume(TT.RIGHT_PAREN, "Expect ')' after for clauses.") iterator = stmt.Mut('') body = self.statement() body = stmt.Block([body, Assign(iterator, collection)]) body = stmt.While(None, body) body = stmt.Block([stmt.Unstable(iterator, None), body]) # if increment is not None: # body = stmt.Block([body, stmt.Expression(increment)]) # if condition is None: # condition = Literal(True) # body = stmt.While(condition, body) # if initializer is not None: # body = stmt.Block([initializer, body]) return body
def forStatement(self) -> s.Stmt: self.consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'.") if self.match(TokenType.SEMICOLON): initializer = None elif self.match(TokenType.VAR): initializer = self.varDeclaration() else: initializer = self.expressionStatement() condition = None if not self.check(TokenType.SEMICOLON): condition = self.expression() self.consume(TokenType.SEMICOLON, "Expect ';' after loop condition.") increment = None if not self.check(TokenType.RIGHT_PAREN): increment = self.expression() self.consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses.") body: s.Stmt = self.statement() # not null if increment: body = s.Block([body, s.Expression(increment)]) # is null if condition is None: condition = e.Literal(True) body = s.While(condition, body) # not null if initializer: body = s.Block([initializer, body]) return body
def statement(self) -> s.Stmt: if self.match(TokenType.FOR): return self.forStatement() if self.match(TokenType.IF): return self.ifStatement() if self.match(TokenType.PRINT): return self.printStatement() if self.match(TokenType.RETURN): return self.returnStatement() if self.match(TokenType.WHILE): return self.whileStatement() if self.match(TokenType.LEFT_BRACE): return s.Block(self.block()) return self.expressionStatement()
def forStatement(self): init = None cond = None incr = None if self.match(TokenType.LEFT_BRACE): # infinite loop return Stmt.Block( [Stmt.For(init, cond, incr, self.blockStatement())]) elif self.match(TokenType.COMMA): first = None elif self.match(TokenType.VAR, TokenType.CONST): first = self.varDeclaration(self.peek(-1)) else: first = self.expressionStatement() if self.match(TokenType.LEFT_BRACE): # while loop return Stmt.Block( [Stmt.For(None, first, None, self.blockStatement())]) init = first self.consume(TokenType.COMMA, "requires comma between initilizer and condition") if not self.match(TokenType.COMMA): cond = self.expression() else: cond = Expr.Literal(True) self.consume(TokenType.COMMA, "requires comma between condition and increment") if not self.check(TokenType.RIGHT_BRACE): incr = self.expression() self.consume(TokenType.LEFT_BRACE, "requires block after for loop") return Stmt.Block([Stmt.For(init, cond, incr, self.blockStatement())])
def _statement(self) -> st.Stmt: if self._match(TokenType.BREAK): return self._break_statement() if self._match(TokenType.CONTINUE): return self._continue_statement() if self._match(TokenType.FOR): return self._for_statement() if self._match(TokenType.IF): return self._if_statement() if self._match(TokenType.RETURN): return self._return_statement() if self._match(TokenType.WHILE): return self._while_statement() if self._match(TokenType.LEFT_BRACE): statements = self._block() return st.Block(statements) return self._expression_statement()
def statement(self): if self.match(TokenType.FOR): return self.for_statement() if self.match(TokenType.IF): return self.if_statement() if self.match(TokenType.PRINT): return self.print_statement() if self.match(TokenType.WHILE): return self.while_statement() if self.match(TokenType.LEFT_BRACE): return Stmt.Block(self.block()) return self.expression_statement()
def statement(self): if self.match(TT.FOR): return self.for_statement() if self.match(TT.IF): return self.if_statement() if self.match(TT.PRINT): return self.print_statement() if self.match(TT.WHILE): return self.while_statement() if self.match(TT.RETURN): return self.return_statement() if self.match(TT.INDENT): return stmt.Block(self.block()) if self.match(TT.BREAK): return self.break_statement() if self.match(TT.CONTINUE): return self.continue_statement() return self.expression_statement()
def blockStatement(self): return Stmt.Block(self.block())