kanaka / mal

mal - Make a Lisp
Other
10.1k stars 2.57k forks source link

python.2: fix tests, use source checkers and more python3 features #665

Closed asarhaddon closed 3 months ago

asarhaddon commented 3 months ago

I have almost rewritten the python.2 implementation, but in the same direction and based on the work by @epylar. I suggest to merge all the ideas he agrees with. The most intrusive suggestion is the removal of an external parser library. The MAL grammar is so simple that a parser does not help much, and general libraries are difficult to type-check.

Also, the impls/python.2/tests/*.py are out of sync (and were before this suggestion). Are they useful after the initial development? If we update them, Makefile should run them.

By the way, python.2 belongs to the past now. Should we not merge the two python implementations?

Changelog:

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).

kanaka commented 3 months ago

Looks like the new tests have revealed some issues in elisp and powershell. I decided just to merge this and I've filed a ticket to track the failures in those implementations: #667

asarhaddon commented 3 months ago

These two features are quite impressive, especially taking into account that the original language design seemed incompatible. As usual in python, both are the result of compromises and slightly differ from what you could expect in a strongly typed language. For example, case [a,b] and case (a, b) are equivalent and both match any iterable object with two elements, not only lists and tuples respectively.

The authors have always said that python3 was a distinct new language. I fully support the name change.