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 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 _expression_statement(self) -> st.Expression: value = self._expression() self._consume(TokenType.SEMICOLON, "Expect a ';' after expression.") return st.Expression(value)
def expressionStatement(self) -> s.Stmt: expr: e.Expr = self.expression() self.consume(TokenType.SEMICOLON, "Expect ';' after expression.") return s.Expression(expr)
def expressionStatement(self): value = self.expression() return Stmt.Expression(value)
def expression_statement(self): expr = self.expression() if not self.is_at_end(): self.consume(TT.NEWLINE, "Expect newline after expression.") return stmt.Expression(expr)
def expression_statement(self): expr = self.expression() self.consume(TokenType.SEMICOLON, "Expect ';' after expression.") return Stmt.Expression(expr)