Ejemplo n.º 1
0
 def block_statement(self):
     self.pop_token()
     statements = []
     while not self.at_end_of_tokens() and not self.curr_token_is(
         [TokenType.CLOSE_BRACE]):
         statements.append(self.declaritive_statement())
     if self.at_end_of_tokens():
         raise Exception('Missing }, program ended before block closed')
     self.pop_token()
     return Statement.Block(statements)
Ejemplo n.º 2
0
    def for_statement(self):
        self.pop_token()
        self.pop_token_expect([TokenType.OPEN_PAREN],
                              'For loop needs an opening parenthesis')
        initializer = self.declaritive_statement()
        condition = self.expression()
        self.pop_token_expect(
            [TokenType.SEMICOLON],
            'Need a semicolon after the condition in the for loop')
        after_expression = self.expression()
        self.pop_token_expect([TokenType.CLOSE_PAREN],
                              'For loop needs a closing parenthesis')
        then_branch = self.block_statement()

        wrapped_while_body = Statement.Block([then_branch, after_expression])
        while_statement = Statement.While(condition, wrapped_while_body)
        the_statements = [initializer, while_statement]
        # print(Statement.Block(the_statements).accept(Stringify()))
        return Statement.Block(the_statements)