python.2: fix tests, use source checkers and more python3 features
Check the whole code with flake8, pylint and mypy.
Report all possible errors with extensive context.
Demonstrate iterators, decorators, functional tools, chain maps,
dataclasses, match statements, assignments expressions.
Implement environments with python chain maps.
Rewrite the reader without external dependencies (but inspired by the
ptk library). The motivation was that no external library is fully
type-checked for now.
Avoid name clashes when possible (print, read, readline, types).
Write the readline history file at exit, not after each prompt.
Replace printer.pr_str as methods of MAL objects. This is idiomatic
python, and improves the error reporting.
Change some representations so that the python equality matches the
MAL equality. The recursion is now implicit.
Remove -O from ./run. It took me a while to understand that run-time
assertions were disabled! MAL is about development, not performance.
Dispatch the special forms from a dict, for readability (pylint
rightfully complains that there are too many return statements in
eval_()).
Copy the recursion overflow fix, the python interaction from the first
python implementation.
Add tests detecting that nil false 0 () [] "" are distinct, and that 0
() are not False when tested (in python False == 0 and an empty
container is tested ).
Add tests checking that metadata does not affect equality (they do
with a naive python dataclass).
2024-08-22 06:56:10 +02:00
|
|
|
import traceback
|
|
|
|
|
from collections.abc import Mapping, Sequence
|
|
|
|
|
|
|
|
|
|
import mal_readline
|
|
|
|
|
|
|
|
|
|
from mal_types import (Error, Fn, Form, List,
|
|
|
|
|
Map, Number, Symbol,
|
|
|
|
|
Vector, pr_seq)
|
2019-08-18 10:05:48 -07:00
|
|
|
|
|
|
|
|
import reader
|
|
|
|
|
|
python.2: fix tests, use source checkers and more python3 features
Check the whole code with flake8, pylint and mypy.
Report all possible errors with extensive context.
Demonstrate iterators, decorators, functional tools, chain maps,
dataclasses, match statements, assignments expressions.
Implement environments with python chain maps.
Rewrite the reader without external dependencies (but inspired by the
ptk library). The motivation was that no external library is fully
type-checked for now.
Avoid name clashes when possible (print, read, readline, types).
Write the readline history file at exit, not after each prompt.
Replace printer.pr_str as methods of MAL objects. This is idiomatic
python, and improves the error reporting.
Change some representations so that the python equality matches the
MAL equality. The recursion is now implicit.
Remove -O from ./run. It took me a while to understand that run-time
assertions were disabled! MAL is about development, not performance.
Dispatch the special forms from a dict, for readability (pylint
rightfully complains that there are too many return statements in
eval_()).
Copy the recursion overflow fix, the python interaction from the first
python implementation.
Add tests detecting that nil false 0 () [] "" are distinct, and that 0
() are not False when tested (in python False == 0 and an empty
container is tested ).
Add tests checking that metadata does not affect equality (they do
with a naive python dataclass).
2024-08-22 06:56:10 +02:00
|
|
|
Env = Mapping[str, Fn]
|
2019-08-18 10:05:48 -07:00
|
|
|
|
|
|
|
|
|
python.2: fix tests, use source checkers and more python3 features
Check the whole code with flake8, pylint and mypy.
Report all possible errors with extensive context.
Demonstrate iterators, decorators, functional tools, chain maps,
dataclasses, match statements, assignments expressions.
Implement environments with python chain maps.
Rewrite the reader without external dependencies (but inspired by the
ptk library). The motivation was that no external library is fully
type-checked for now.
Avoid name clashes when possible (print, read, readline, types).
Write the readline history file at exit, not after each prompt.
Replace printer.pr_str as methods of MAL objects. This is idiomatic
python, and improves the error reporting.
Change some representations so that the python equality matches the
MAL equality. The recursion is now implicit.
Remove -O from ./run. It took me a while to understand that run-time
assertions were disabled! MAL is about development, not performance.
Dispatch the special forms from a dict, for readability (pylint
rightfully complains that there are too many return statements in
eval_()).
Copy the recursion overflow fix, the python interaction from the first
python implementation.
Add tests detecting that nil false 0 () [] "" are distinct, and that 0
() are not False when tested (in python False == 0 and an empty
container is tested ).
Add tests checking that metadata does not affect equality (they do
with a naive python dataclass).
2024-08-22 06:56:10 +02:00
|
|
|
def eval_(ast: Form, env: Env) -> Form:
|
|
|
|
|
# print(f'EVAL: {ast}', repr(ast)
|
|
|
|
|
match ast:
|
|
|
|
|
case Symbol():
|
|
|
|
|
if (value := env.get(ast)) is not None:
|
|
|
|
|
return value
|
|
|
|
|
raise Error(f"'{ast}' not found")
|
|
|
|
|
case Map():
|
|
|
|
|
return Map((k, eval_(v, env)) for k, v in ast.items())
|
|
|
|
|
case Vector():
|
|
|
|
|
return Vector(eval_(x, env) for x in ast)
|
|
|
|
|
case List([first, *args]):
|
|
|
|
|
match eval_(first, env):
|
|
|
|
|
case Fn(call):
|
|
|
|
|
return call(tuple(eval_(x, env) for x in args))
|
|
|
|
|
case not_fun:
|
|
|
|
|
raise Error(f'cannot apply {not_fun}')
|
|
|
|
|
case _:
|
|
|
|
|
return ast
|
2019-08-18 10:05:48 -07:00
|
|
|
|
|
|
|
|
|
python.2: fix tests, use source checkers and more python3 features
Check the whole code with flake8, pylint and mypy.
Report all possible errors with extensive context.
Demonstrate iterators, decorators, functional tools, chain maps,
dataclasses, match statements, assignments expressions.
Implement environments with python chain maps.
Rewrite the reader without external dependencies (but inspired by the
ptk library). The motivation was that no external library is fully
type-checked for now.
Avoid name clashes when possible (print, read, readline, types).
Write the readline history file at exit, not after each prompt.
Replace printer.pr_str as methods of MAL objects. This is idiomatic
python, and improves the error reporting.
Change some representations so that the python equality matches the
MAL equality. The recursion is now implicit.
Remove -O from ./run. It took me a while to understand that run-time
assertions were disabled! MAL is about development, not performance.
Dispatch the special forms from a dict, for readability (pylint
rightfully complains that there are too many return statements in
eval_()).
Copy the recursion overflow fix, the python interaction from the first
python implementation.
Add tests detecting that nil false 0 () [] "" are distinct, and that 0
() are not False when tested (in python False == 0 and an empty
container is tested ).
Add tests checking that metadata does not affect equality (they do
with a naive python dataclass).
2024-08-22 06:56:10 +02:00
|
|
|
def add(args: Sequence[Form]) -> Form:
|
|
|
|
|
match args:
|
|
|
|
|
case [Number(left), Number(right)]:
|
|
|
|
|
return Number(left + right)
|
|
|
|
|
case _:
|
|
|
|
|
raise Error('+: bad arguments' + pr_seq(args))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sub(args: Sequence[Form]) -> Form:
|
|
|
|
|
match args:
|
|
|
|
|
case [Number(left), Number(right)]:
|
|
|
|
|
return Number(left - right)
|
|
|
|
|
case _:
|
|
|
|
|
raise Error('-: bad arguments' + pr_seq(args))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mul(args: Sequence[Form]) -> Form:
|
|
|
|
|
match args:
|
|
|
|
|
case [Number(left), Number(right)]:
|
|
|
|
|
return Number(left * right)
|
|
|
|
|
case _:
|
|
|
|
|
raise Error('*: bad arguments' + pr_seq(args))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def floordiv(args: Sequence[Form]) -> Form:
|
|
|
|
|
match args:
|
|
|
|
|
case [Number(left), Number(right)]:
|
|
|
|
|
return Number(left // right)
|
|
|
|
|
case _:
|
|
|
|
|
raise Error('/: bad arguments' + pr_seq(args))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rep(source: str, env: Env) -> str:
|
|
|
|
|
return str(eval_(reader.read(source), env))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
|
repl_env: Env = {
|
|
|
|
|
'+': Fn(add), '-': Fn(sub), '*': Fn(mul), '/': Fn(floordiv),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while True:
|
2019-08-18 10:05:48 -07:00
|
|
|
try:
|
python.2: fix tests, use source checkers and more python3 features
Check the whole code with flake8, pylint and mypy.
Report all possible errors with extensive context.
Demonstrate iterators, decorators, functional tools, chain maps,
dataclasses, match statements, assignments expressions.
Implement environments with python chain maps.
Rewrite the reader without external dependencies (but inspired by the
ptk library). The motivation was that no external library is fully
type-checked for now.
Avoid name clashes when possible (print, read, readline, types).
Write the readline history file at exit, not after each prompt.
Replace printer.pr_str as methods of MAL objects. This is idiomatic
python, and improves the error reporting.
Change some representations so that the python equality matches the
MAL equality. The recursion is now implicit.
Remove -O from ./run. It took me a while to understand that run-time
assertions were disabled! MAL is about development, not performance.
Dispatch the special forms from a dict, for readability (pylint
rightfully complains that there are too many return statements in
eval_()).
Copy the recursion overflow fix, the python interaction from the first
python implementation.
Add tests detecting that nil false 0 () [] "" are distinct, and that 0
() are not False when tested (in python False == 0 and an empty
container is tested ).
Add tests checking that metadata does not affect equality (they do
with a naive python dataclass).
2024-08-22 06:56:10 +02:00
|
|
|
print(rep(mal_readline.input_('user> '), repl_env))
|
2019-08-18 10:05:48 -07:00
|
|
|
except EOFError:
|
python.2: fix tests, use source checkers and more python3 features
Check the whole code with flake8, pylint and mypy.
Report all possible errors with extensive context.
Demonstrate iterators, decorators, functional tools, chain maps,
dataclasses, match statements, assignments expressions.
Implement environments with python chain maps.
Rewrite the reader without external dependencies (but inspired by the
ptk library). The motivation was that no external library is fully
type-checked for now.
Avoid name clashes when possible (print, read, readline, types).
Write the readline history file at exit, not after each prompt.
Replace printer.pr_str as methods of MAL objects. This is idiomatic
python, and improves the error reporting.
Change some representations so that the python equality matches the
MAL equality. The recursion is now implicit.
Remove -O from ./run. It took me a while to understand that run-time
assertions were disabled! MAL is about development, not performance.
Dispatch the special forms from a dict, for readability (pylint
rightfully complains that there are too many return statements in
eval_()).
Copy the recursion overflow fix, the python interaction from the first
python implementation.
Add tests detecting that nil false 0 () [] "" are distinct, and that 0
() are not False when tested (in python False == 0 and an empty
container is tested ).
Add tests checking that metadata does not affect equality (they do
with a naive python dataclass).
2024-08-22 06:56:10 +02:00
|
|
|
break
|
|
|
|
|
# pylint: disable-next=broad-exception-caught
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
traceback.print_exception(exc, limit=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|