1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-27 14:52:16 +03:00
mal/impls/python.2/stepA_mal.py

253 lines
8.3 KiB
Python
Raw Normal View History

import functools
import readline
import sys
from typing import List, Dict
import core
import reader
from env import Env
from mal_types import (
MalExpression,
MalSymbol,
MalException,
MalList,
MalNil,
MalBoolean,
MalFunctionCompiled,
MalFunctionRaw,
MalVector,
MalHash_map,
MalUnknownSymbolException,
MalInvalidArgumentException,
MalString,
)
def READ(x: str) -> MalExpression:
return reader.read(x)
def qq_loop(acc: MalList, elt: MalExpression) -> MalList:
if isinstance(elt, MalList):
lst = elt.native()
if len(lst) == 2:
fst = lst[0]
if isinstance(fst, MalSymbol) and fst.native() == u"splice-unquote":
return MalList([MalSymbol(u"concat"), lst[1], acc])
return MalList([MalSymbol(u"cons"), quasiquote(elt), acc])
def qq_foldr(xs: List[MalExpression]) -> MalList:
return functools.reduce(qq_loop, reversed(xs), MalList([]))
def quasiquote(ast: MalExpression) -> MalExpression:
if isinstance(ast, MalList):
lst = ast.native()
if len(lst) == 2:
fst = lst[0]
if isinstance(fst, MalSymbol) and fst.native() == u'unquote':
return lst[1]
return qq_foldr(lst)
elif isinstance(ast, MalVector):
return MalList([MalSymbol("vec"), qq_foldr(ast.native())])
elif isinstance(ast, MalSymbol) or isinstance(ast, MalHash_map):
return MalList([MalSymbol("quote"), ast])
else:
return ast
def EVAL(ast: MalExpression, env: Env) -> MalExpression:
while True:
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 02:15:40 +03:00
dbgeval = env.get("DEBUG-EVAL")
if (dbgeval is not None
and not isinstance(dbgeval, MalNil)
and (not isinstance(dbgeval, MalBoolean) or dbgeval.native())):
print("EVAL: " + str(ast))
ast_native = ast.native()
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 02:15:40 +03:00
if isinstance(ast, MalSymbol):
key = str(ast)
val = env.get(key)
if val is None: raise MalUnknownSymbolException(key)
return val
if isinstance(ast, MalVector):
return MalVector([EVAL(x, env) for x in ast_native])
if isinstance(ast, MalHash_map):
new_dict = {} # type: Dict[str, MalExpression]
for key in ast_native:
new_dict[key] = EVAL(ast_native[key], env)
return MalHash_map(new_dict)
if not isinstance(ast, MalList):
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 02:15:40 +03:00
return ast
elif len(ast_native) == 0:
return ast
first_str = str(ast_native[0])
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 02:15:40 +03:00
if first_str == "def!":
name: str = str(ast_native[1])
value: MalExpression = EVAL(ast_native[2], env)
return env.set(name, value)
if first_str == "defmacro!":
name = str(ast_native[1])
value = EVAL(ast_native[2], env)
assert isinstance(value, MalFunctionCompiled) or isinstance(
value, MalFunctionRaw
)
value.make_macro()
return env.set(name, value)
elif first_str == "let*":
assert len(ast_native) == 3
let_env = Env(env)
bindings: MalExpression = ast_native[1]
assert isinstance(bindings, MalList) or isinstance(bindings, MalVector)
bindings_list: List[MalExpression] = bindings.native()
assert len(bindings_list) % 2 == 0
for i in range(0, len(bindings_list), 2):
assert isinstance(bindings_list[i], MalSymbol)
assert isinstance(bindings_list[i + 1], MalExpression)
let_env.set(str(bindings_list[i]), EVAL(bindings_list[i + 1], let_env))
env = let_env
ast = ast_native[2]
continue
elif first_str == "do":
for x in range(1, len(ast_native) - 1):
EVAL(ast_native[x], env)
ast = ast_native[len(ast_native) - 1]
continue
elif first_str == "if":
condition = EVAL(ast_native[1], env)
if isinstance(condition, MalNil) or (
isinstance(condition, MalBoolean) and condition.native() is False
):
if len(ast_native) >= 4:
ast = ast_native[3]
continue
else:
return MalNil()
else:
ast = ast_native[2]
continue
elif first_str == "fn*":
raw_ast = ast_native[2]
raw_params = ast_native[1]
def fn(args: List[MalExpression]) -> MalExpression:
f_ast = raw_ast
f_env = Env(outer=env, binds=raw_params.native(), exprs=args)
return EVAL(f_ast, f_env)
return MalFunctionRaw(fn=fn, ast=raw_ast, params=raw_params, env=env)
elif first_str == "quote":
return (
MalList(ast_native[1].native())
if isinstance(ast_native[1], MalVector)
else ast_native[1]
)
elif first_str == "quasiquote":
ast = quasiquote(ast_native[1])
continue
elif first_str == "try*":
try:
return EVAL(ast_native[1], env)
except MalException as e:
if len(ast_native) < 3:
raise e
catch_block = ast_native[2]
assert (
isinstance(catch_block, MalList)
and isinstance(catch_block.native()[0], MalSymbol)
and str(catch_block.native()[0]) == "catch*"
and len(catch_block.native()) == 3
)
exception_symbol = catch_block.native()[1]
assert isinstance(exception_symbol, MalSymbol)
env = Env(env)
env.set(str(exception_symbol), e.native())
ast = catch_block.native()[2]
continue
else:
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 02:15:40 +03:00
f = EVAL(ast_native[0], env)
if isinstance(f, (MalFunctionCompiled, MalFunctionRaw)) and f.is_macro():
ast = f.call(ast_native[1:])
continue
args = [EVAL(ast_native[i], env) for i in range(1, len(ast_native))]
if isinstance(f, MalFunctionRaw):
ast = f.ast()
env = Env(
outer=f.env(),
binds=f.params().native(),
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 02:15:40 +03:00
exprs=args,
)
continue
elif isinstance(f, MalFunctionCompiled):
return f.call(args)
else:
raise MalInvalidArgumentException(f, "not a function")
def PRINT(x: MalExpression) -> str:
return str(x)
def rep(x: str, env: Env) -> str:
return PRINT(EVAL(READ(x), env))
def init_repl_env() -> Env:
def eval_func(args: List[MalExpression], env: Env) -> MalExpression:
a0 = args[0]
assert isinstance(a0, MalExpression)
return EVAL(a0, env)
env = Env(None)
for key in core.ns:
env.set(key, core.ns[key])
env.set("eval", MalFunctionCompiled(lambda args: eval_func(args, env)))
rep('(def! *host-language* "python.2")', env)
rep(
'(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) "\nnil)")))))',
env,
)
mal_argv = MalList([MalString(x) for x in sys.argv[2:]])
env.set("*ARGV*", mal_argv)
rep(
"(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))",
env,
)
return env
def rep_handling_exceptions(line: str, repl_env: Env) -> str:
try:
return rep(line, repl_env)
except MalUnknownSymbolException as e:
return "'" + e.func + "' not found"
except MalException as e:
return "ERROR: " + str(e)
if __name__ == "__main__":
# repl loop
eof: bool = False
repl_env = init_repl_env()
if len(sys.argv) >= 2:
file_str = sys.argv[1]
rep_handling_exceptions('(load-file "' + file_str + '")', repl_env)
exit(0)
rep('(println (str "Mal [" *host-language* "]"))', repl_env)
while not eof:
try:
line = input("user> ")
readline.add_history(line)
print(rep_handling_exceptions(line, repl_env))
except EOFError:
eof = True