|
26 | 26 | TT_STRING = "TT_STRING" |
27 | 27 | TT_LSQUARE = "LSQUARE" |
28 | 28 | TT_RSQUARE = "RSQUARE" |
29 | | -TT_NEWLINE="NEWLINE" |
| 29 | +TT_NEWLINE = "NEWLINE" |
30 | 30 |
|
31 | 31 | KEYWORDS = [ |
32 | 32 | 'var', |
@@ -218,7 +218,7 @@ def make_tokens(self): |
218 | 218 | elif self.current_char == ']': |
219 | 219 | tokens.append(Token(TT_RSQUARE, pos_start=self.pos)) |
220 | 220 | self.advance() |
221 | | - elif self.current_char==";\n": |
| 221 | + elif self.current_char == ";\n": |
222 | 222 | tokens.append(Token(TT_NEWLINE, pos_start=self.pos)) |
223 | 223 | self.advance() |
224 | 224 |
|
@@ -544,6 +544,45 @@ def parse(self): |
544 | 544 |
|
545 | 545 | ################################### |
546 | 546 |
|
| 547 | + def statement(self): |
| 548 | + res = ParseResult() |
| 549 | + statements = [] |
| 550 | + pos_start = self.current_tok.pos_start.copy() |
| 551 | + |
| 552 | + while self.current_tok.type == TT_NEWLINE: |
| 553 | + res.register_advancement() |
| 554 | + self.advance() |
| 555 | + statement = res.register(self.expr()) |
| 556 | + if res.error: return res |
| 557 | + statements.append(statement) |
| 558 | + |
| 559 | + more_statements = True |
| 560 | + |
| 561 | + while True: |
| 562 | + newline_count = 0 |
| 563 | + while self.current_tok.type == TT_NEWLINE: |
| 564 | + res.register_advancement() |
| 565 | + self.advance() |
| 566 | + newline_count += 1 |
| 567 | + |
| 568 | + if newline_count == 0: |
| 569 | + more_statements = False |
| 570 | + |
| 571 | + if not more_statements: |
| 572 | + break |
| 573 | + |
| 574 | + if not statement: |
| 575 | + self.reverse(res.to_reverse_count) |
| 576 | + more_statements = False |
| 577 | + continue |
| 578 | + statements.append(statement) |
| 579 | + |
| 580 | + return res.success(ListNode( |
| 581 | + statements, |
| 582 | + pos_start, |
| 583 | + self.current_tok.pos_end.copy() |
| 584 | + )) |
| 585 | + |
547 | 586 | def if_expr(self): |
548 | 587 | res = ParseResult() |
549 | 588 | cases = [] |
@@ -1610,18 +1649,19 @@ def execute_extend(self, exec_ctx): |
1610 | 1649 |
|
1611 | 1650 | execute_extend.arg_names = ["listA", "listB"] |
1612 | 1651 |
|
1613 | | -BuiltInFunction.print = BuiltInFunction("print") |
1614 | | -BuiltInFunction.print_ret = BuiltInFunction("print_ret") |
1615 | | -BuiltInFunction.input = BuiltInFunction("input") |
1616 | | -BuiltInFunction.input_int = BuiltInFunction("input_int") |
1617 | | -BuiltInFunction.clear = BuiltInFunction("clear") |
1618 | | -BuiltInFunction.is_number = BuiltInFunction("is_number") |
1619 | | -BuiltInFunction.is_string = BuiltInFunction("is_string") |
1620 | | -BuiltInFunction.is_list = BuiltInFunction("is_list") |
| 1652 | + |
| 1653 | +BuiltInFunction.print = BuiltInFunction("print") |
| 1654 | +BuiltInFunction.print_ret = BuiltInFunction("print_ret") |
| 1655 | +BuiltInFunction.input = BuiltInFunction("input") |
| 1656 | +BuiltInFunction.input_int = BuiltInFunction("input_int") |
| 1657 | +BuiltInFunction.clear = BuiltInFunction("clear") |
| 1658 | +BuiltInFunction.is_number = BuiltInFunction("is_number") |
| 1659 | +BuiltInFunction.is_string = BuiltInFunction("is_string") |
| 1660 | +BuiltInFunction.is_list = BuiltInFunction("is_list") |
1621 | 1661 | BuiltInFunction.is_function = BuiltInFunction("is_function") |
1622 | | -BuiltInFunction.append = BuiltInFunction("append") |
1623 | | -BuiltInFunction.pop = BuiltInFunction("pop") |
1624 | | -BuiltInFunction.extend = BuiltInFunction("extend") |
| 1662 | +BuiltInFunction.append = BuiltInFunction("append") |
| 1663 | +BuiltInFunction.pop = BuiltInFunction("pop") |
| 1664 | +BuiltInFunction.extend = BuiltInFunction("extend") |
1625 | 1665 |
|
1626 | 1666 |
|
1627 | 1667 | class SymbolTable: |
@@ -1886,7 +1926,6 @@ def visit_CallNode(self, node, context): |
1886 | 1926 | global_symbol_table.set("exetend", BuiltInFunction.extend) |
1887 | 1927 |
|
1888 | 1928 |
|
1889 | | - |
1890 | 1929 | def run(fn, text): |
1891 | 1930 | # Generate tokens |
1892 | 1931 | lexer = Lexer(fn, text) |
|
0 commit comments