3 ms·
short code review: - the `tokens` method returning a variable-length tuple is a very bad idea; modern Python supports the `datatype` decorator which should be
by tomp 3y ago
short code review:
- the `tokens` method returning a variable-length tuple is a very bad idea; modern Python supports the `datatype` decorator which should be used in this case
- I'd strongly recommend using a regex for lexing; much easier to get it right than a long if/elif/else block
- I'd recommend refactoring code and implementing the `next_token` logic so that it works for any generator; an example implementation could be (WARNING: I wrote this in 5min, would require extensive unittests!)
class Peek:
def __init__(self, it):
self.cur, self.nxt = itertools.tee(it, 2)
self.current = next(self.cur)
def __next__(self):
self.current = next(self.cur)
return next(self.nxt)
- statements with a "prefix" (like `print X` or `if Y`) are very easy to parse (just use the lookahead token!) but when you get to parsing expressions, I strongly recommend using a Pratt parser (extensible operator precedence parsing)