1.2 KiB
1.2 KiB
Journal — lex phase
Build run
Implemented calc/lexer.py with:
Tokendataclass withkind(str) andvalue(int | float | str | None)LexError(Exception)for invalid characterstokenize(src: str) -> list[Token]scanning char-by-char
Design choices:
Tokenis a plain dataclass so later phases (parser, evaluator) can pattern-match on.kind- Numbers: scanned greedily while char is digit or
.; cast tointif no.in raw string, elsefloat - Operators stored as their literal char as
value(handy for error messages) - EOF always appended as final token (parser-friendly sentinel)
Test run output
$ python -m unittest -q
..............
----------------------------------------------------------------------
Ran 14 tests in 0.000s
OK
Verify commands output
$ python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('3.5*(1-2)')])"
[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]
$ python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"
Traceback (most recent call last):
...
calc.lexer.LexError: unexpected character '@' at position 2