2014-03-25 01:32:24 +04:00
|
|
|
import sys, traceback
|
|
|
|
import mal_readline
|
2014-04-03 07:23:37 +04:00
|
|
|
import mal_types as types
|
|
|
|
import reader, printer
|
|
|
|
from env import Env
|
|
|
|
import core
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
# read
|
2024-08-08 17:08:30 +03:00
|
|
|
READ = reader.read_str
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
# eval
|
2022-01-10 02:15:40 +03:00
|
|
|
def EVAL(ast, env):
|
2024-08-08 17:08:30 +03:00
|
|
|
dbgeval = env.get(types._symbol('DEBUG-EVAL'), return_nil=True)
|
2022-01-10 02:15:40 +03:00
|
|
|
if dbgeval is not None and dbgeval is not False:
|
|
|
|
print('EVAL: ' + printer._pr_str(ast))
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
if types._symbol_Q(ast):
|
2014-03-25 01:32:24 +04:00
|
|
|
return env.get(ast)
|
2014-04-03 07:23:37 +04:00
|
|
|
elif types._vector_Q(ast):
|
2024-08-08 17:08:30 +03:00
|
|
|
return types.Vector(EVAL(a, env) for a in ast)
|
2014-04-03 07:23:37 +04:00
|
|
|
elif types._hash_map_Q(ast):
|
2021-08-21 20:08:17 +03:00
|
|
|
return types.Hash_Map((k, EVAL(v, env)) for k, v in ast.items())
|
2022-01-10 02:15:40 +03:00
|
|
|
elif not types._list_Q(ast):
|
2014-03-25 01:32:24 +04:00
|
|
|
return ast # primitive value, return unchanged
|
2022-01-10 02:15:40 +03:00
|
|
|
else:
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# apply list
|
|
|
|
if len(ast) == 0: return ast
|
|
|
|
a0 = ast[0]
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2024-08-08 17:08:30 +03:00
|
|
|
if types._symbol_Q(a0):
|
2014-04-03 07:23:37 +04:00
|
|
|
if "def!" == a0:
|
|
|
|
a1, a2 = ast[1], ast[2]
|
|
|
|
res = EVAL(a2, env)
|
|
|
|
return env.set(a1, res)
|
|
|
|
elif "let*" == a0:
|
|
|
|
a1, a2 = ast[1], ast[2]
|
|
|
|
let_env = Env(env)
|
2024-08-08 17:08:30 +03:00
|
|
|
for k, v in types.asPairs(a1):
|
|
|
|
let_env.set(k, EVAL(v, let_env))
|
2014-04-03 07:23:37 +04:00
|
|
|
return EVAL(a2, let_env)
|
|
|
|
elif "do" == a0:
|
2022-01-10 02:15:40 +03:00
|
|
|
for i in range(1, len(ast)-1):
|
|
|
|
EVAL(ast[i], env)
|
|
|
|
return EVAL(ast[-1], env)
|
2014-04-03 07:23:37 +04:00
|
|
|
elif "if" == a0:
|
|
|
|
a1, a2 = ast[1], ast[2]
|
|
|
|
cond = EVAL(a1, env)
|
|
|
|
if cond is None or cond is False:
|
2024-08-08 17:08:30 +03:00
|
|
|
if len(ast) > 3:
|
|
|
|
return EVAL(ast[3], env)
|
|
|
|
else:
|
|
|
|
return None
|
2014-04-03 07:23:37 +04:00
|
|
|
else:
|
|
|
|
return EVAL(a2, env)
|
|
|
|
elif "fn*" == a0:
|
|
|
|
a1, a2 = ast[1], ast[2]
|
2024-08-08 17:08:30 +03:00
|
|
|
def fn(*args):
|
|
|
|
return EVAL(a2, Env(env, a1, args))
|
|
|
|
return fn
|
|
|
|
|
|
|
|
f = EVAL(a0, env)
|
|
|
|
if types._function_Q(f):
|
2022-01-10 02:15:40 +03:00
|
|
|
args = ast[1:]
|
|
|
|
return f(*(EVAL(a, env) for a in args))
|
2024-08-08 17:08:30 +03:00
|
|
|
else:
|
|
|
|
raise Exception('Can only apply functions')
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
# print
|
2024-08-08 17:08:30 +03:00
|
|
|
PRINT = printer._pr_str
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
# repl
|
|
|
|
repl_env = Env()
|
|
|
|
def REP(str):
|
|
|
|
return PRINT(EVAL(READ(str), repl_env))
|
|
|
|
|
2014-04-17 08:57:50 +04:00
|
|
|
# core.py: defined using python
|
2014-12-19 05:33:49 +03:00
|
|
|
for k, v in core.ns.items(): repl_env.set(types._symbol(k), v)
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-17 08:57:50 +04:00
|
|
|
# core.mal: defined using the language itself
|
2014-03-25 01:32:24 +04:00
|
|
|
REP("(def! not (fn* (a) (if a false true)))")
|
|
|
|
|
2014-04-19 22:04:09 +04:00
|
|
|
# repl loop
|
2014-03-25 01:32:24 +04:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
line = mal_readline.readline("user> ")
|
|
|
|
print(REP(line))
|
2024-08-08 17:08:30 +03:00
|
|
|
except EOFError:
|
|
|
|
print()
|
|
|
|
break
|
2014-04-03 07:23:37 +04:00
|
|
|
except reader.Blank: continue
|
2024-08-08 17:08:30 +03:00
|
|
|
except Exception:
|
2014-04-17 07:42:17 +04:00
|
|
|
print("".join(traceback.format_exception(*sys.exc_info())))
|