コード例 #1
0
    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
コード例 #2
0
ファイル: parse.py プロジェクト: betsyalegria/Lox
    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
コード例 #3
0
ファイル: loxparser.py プロジェクト: AntoniosHadji/pylox
    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
コード例 #4
0
 def _expression_statement(self) -> st.Expression:
     value = self._expression()
     self._consume(TokenType.SEMICOLON, "Expect a ';' after expression.")
     return st.Expression(value)
コード例 #5
0
ファイル: loxparser.py プロジェクト: AntoniosHadji/pylox
 def expressionStatement(self) -> s.Stmt:
     expr: e.Expr = self.expression()
     self.consume(TokenType.SEMICOLON, "Expect ';' after expression.")
     return s.Expression(expr)
コード例 #6
0
 def expressionStatement(self):
     value = self.expression()
     return Stmt.Expression(value)
コード例 #7
0
ファイル: parser.py プロジェクト: ErlendLima/Nebbdyr
 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)
コード例 #8
0
ファイル: parse.py プロジェクト: betsyalegria/Lox
 def expression_statement(self):
     expr = self.expression()
     self.consume(TokenType.SEMICOLON, "Expect ';' after expression.")
     return Stmt.Expression(expr)