def whileStatement(self) -> s.Stmt: self.consume(TokenType.LEFT_PAREN, "Expect '(' after 'while'.") condition: e.Expr = self.expression() self.consume(TokenType.RIGHT_PAREN, "Expect ')' after condition.") body: s.Stmt = self.statement() return s.While(condition, 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): 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 while_statement(self): self.consume(TokenType.LEFT_PAREN, "Expect '(' after 'while'.") condition = self.expression() self.consume(TokenType.RIGHT_PAREN, "Expect ')' after condition.") body = self.statement() return Stmt.While(condition, body)
def while_statement(self): condition = self.expression() self.consume(TT.COLON, "Expect ':' after while condition.") self.consume(TT.NEWLINE, "Expect newline after ':'.") try: self.loop_level += 1 body = self.statement() return stmt.While(condition, body) finally: self.loop_level -= 1
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 _while_statement(self) -> st.While: self._consume(TokenType.LEFT_PAREN, "Expect '(' after 'while'.") condition = self._expression() self._consume(TokenType.RIGHT_PAREN, "Expect ')' after condition.") body = self._statement() return st.While(condition, body)