mirror of
https://github.com/kanaka/mal.git
synced 2024-09-20 01:57:09 +03:00
8a19f60386
- Reorder README to have implementation list after "learning tool" bullet. - This also moves tests/ and libs/ into impls. It would be preferrable to have these directories at the top level. However, this causes difficulties with the wasm implementations which need pre-open directories and have trouble with paths starting with "../../". So in lieu of that, symlink those directories to the top-level. - Move the run_argv_test.sh script into the tests directory for general hygiene.
125 lines
3.5 KiB
Python
125 lines
3.5 KiB
Python
#import sys, traceback
|
|
import mal_readline
|
|
import mal_types as types
|
|
from mal_types import (MalSym, MalInt, MalStr,
|
|
_symbol, _keywordu,
|
|
MalList, _list, MalVector, MalHashMap, MalFunc)
|
|
import reader, printer
|
|
from env import Env
|
|
|
|
# read
|
|
def READ(str):
|
|
return reader.read_str(str)
|
|
|
|
# eval
|
|
def eval_ast(ast, env):
|
|
if types._symbol_Q(ast):
|
|
assert isinstance(ast, MalSym)
|
|
return env.get(ast)
|
|
elif types._list_Q(ast):
|
|
res = []
|
|
for a in ast.values:
|
|
res.append(EVAL(a, env))
|
|
return MalList(res)
|
|
elif types._vector_Q(ast):
|
|
res = []
|
|
for a in ast.values:
|
|
res.append(EVAL(a, env))
|
|
return MalVector(res)
|
|
elif types._hash_map_Q(ast):
|
|
new_dct = {}
|
|
for k in ast.dct.keys():
|
|
new_dct[k] = EVAL(ast.dct[k], env)
|
|
return MalHashMap(new_dct)
|
|
else:
|
|
return ast # primitive value, return unchanged
|
|
|
|
def EVAL(ast, env):
|
|
#print("EVAL %s" % printer._pr_str(ast))
|
|
if not types._list_Q(ast):
|
|
return eval_ast(ast, env)
|
|
|
|
# apply list
|
|
if len(ast) == 0: return ast
|
|
a0 = ast[0]
|
|
if not isinstance(a0, MalSym):
|
|
raise Exception("attempt to apply on non-symbol")
|
|
|
|
if u"def!" == a0.value:
|
|
a1, a2 = ast[1], ast[2]
|
|
res = EVAL(a2, env)
|
|
return env.set(a1, res)
|
|
elif u"let*" == a0.value:
|
|
a1, a2 = ast[1], ast[2]
|
|
let_env = Env(env)
|
|
for i in range(0, len(a1), 2):
|
|
let_env.set(a1[i], EVAL(a1[i+1], let_env))
|
|
return EVAL(a2, let_env)
|
|
else:
|
|
el = eval_ast(ast, env)
|
|
f = el.values[0]
|
|
if isinstance(f, MalFunc):
|
|
return f.apply(el.values[1:])
|
|
else:
|
|
raise Exception("%s is not callable" % f)
|
|
|
|
# print
|
|
def PRINT(exp):
|
|
return printer._pr_str(exp)
|
|
|
|
# repl
|
|
repl_env = Env()
|
|
def REP(str, env):
|
|
return PRINT(EVAL(READ(str), env))
|
|
|
|
def plus(args):
|
|
a, b = args[0], args[1]
|
|
assert isinstance(a, MalInt)
|
|
assert isinstance(b, MalInt)
|
|
return MalInt(a.value+b.value)
|
|
def minus(args):
|
|
a, b = args[0], args[1]
|
|
assert isinstance(a, MalInt)
|
|
assert isinstance(b, MalInt)
|
|
return MalInt(a.value-b.value)
|
|
def multiply(args):
|
|
a, b = args[0], args[1]
|
|
assert isinstance(a, MalInt)
|
|
assert isinstance(b, MalInt)
|
|
return MalInt(a.value*b.value)
|
|
def divide(args):
|
|
a, b = args[0], args[1]
|
|
assert isinstance(a, MalInt)
|
|
assert isinstance(b, MalInt)
|
|
return MalInt(int(a.value/b.value))
|
|
repl_env.set(_symbol(u'+'), MalFunc(plus))
|
|
repl_env.set(_symbol(u'-'), MalFunc(minus))
|
|
repl_env.set(_symbol(u'*'), MalFunc(multiply))
|
|
repl_env.set(_symbol(u'/'), MalFunc(divide))
|
|
|
|
def entry_point(argv):
|
|
while True:
|
|
try:
|
|
line = mal_readline.readline("user> ")
|
|
if line == "": continue
|
|
print(REP(line, repl_env))
|
|
except EOFError as e:
|
|
break
|
|
except reader.Blank:
|
|
continue
|
|
except types.MalException as e:
|
|
print(u"Error: %s" % printer._pr_str(e.object, False))
|
|
except Exception as e:
|
|
print("Error: %s" % e)
|
|
#print("".join(traceback.format_exception(*sys.exc_info())))
|
|
return 0
|
|
|
|
# _____ Define and setup target ___
|
|
def target(*args):
|
|
return entry_point
|
|
|
|
# Just run entry_point if not RPython compilation
|
|
import sys
|
|
if not sys.argv[0].endswith('rpython'):
|
|
entry_point(sys.argv)
|