3 import mal_types
as types
4 from mal_types
import (MalSym
, MalInt
, MalStr
, nil
, true
, false
,
5 _symbol
, _keywordu
, MalList
, _list
, MalFunc
)
12 return reader
.read_str(str)
15 def eval_ast(ast
, env
):
16 if types
._symbol
_Q
(ast
):
17 assert isinstance(ast
, MalSym
)
19 elif types
._list
_Q
(ast
):
22 res
.append(EVAL(a
, env
))
24 ## elif types._vector_Q(ast):
25 ## return types._vector(*map(lambda a: EVAL(a, env), ast))
26 ## elif types._hash_map_Q(ast):
28 ## for k in ast.keys():
29 ## keyvals.append(EVAL(k, env))
30 ## keyvals.append(EVAL(ast[k], env))
31 ## return types._hash_map(*keyvals)
33 return ast
# primitive value, return unchanged
37 #print("EVAL %s" % printer._pr_str(ast))
38 if not types
._list
_Q
(ast
):
39 return eval_ast(ast
, env
)
42 if len(ast
) == 0: return ast
44 if isinstance(a0
, MalSym
):
50 a1
, a2
= ast
[1], ast
[2]
52 return env
.set(a1
, res
)
53 elif u
"let*" == a0sym
:
54 a1
, a2
= ast
[1], ast
[2]
56 for i
in range(0, len(a1
), 2):
57 let_env
.set(a1
[i
], EVAL(a1
[i
+1], let_env
))
59 env
= let_env
# Continue loop (TCO)
64 eval_ast(ast
.slice2(1, len(ast
)-1), env
)
65 ast
= ast
[-1] # Continue loop (TCO)
67 a1
, a2
= ast
[1], ast
[2]
69 if cond
is nil
or cond
is false
:
70 if len(ast
) > 3: ast
= ast
[3] # Continue loop (TCO)
73 ast
= a2
# Continue loop (TCO)
75 a1
, a2
= ast
[1], ast
[2]
76 return MalFunc(None, a2
, env
, a1
, EVAL
)
78 el
= eval_ast(ast
, env
)
80 if isinstance(f
, MalFunc
):
83 env
= f
.gen_env(el
.rest()) # Continue loop (TCO)
85 return f
.apply(el
.rest())
87 raise Exception("%s is not callable" % f
)
91 return printer
._pr
_str
(exp
)
94 def entry_point(argv
):
97 return PRINT(EVAL(READ(str), env
))
99 # core.py: defined using python
100 for k
, v
in core
.ns
.items():
101 repl_env
.set(_symbol(unicode(k
)), MalFunc(v
))
103 # core.mal: defined using the language itself
104 REP("(def! not (fn* (a) (if a false true)))", repl_env
)
108 line
= mal_readline
.readline("user> ")
109 if line
== "": continue
110 print(REP(line
, repl_env
))
111 except EOFError as e
:
113 except Exception as e
:
115 #print("".join(traceback.format_exception(*sys.exc_info())))
118 # _____ Define and setup target ___
122 # Just run entry_point if not RPython compilation
124 if not sys
.argv
[0].endswith('rpython'):
125 entry_point(sys
.argv
)