3 import mal_types
as types
10 return reader
.read_str(str)
13 def eval_ast(ast
, env
):
14 if types
._symbol
_Q
(ast
):
16 elif types
._list
_Q
(ast
):
17 return types
._list
(*map(lambda a
: EVAL(a
, env
), ast
))
18 elif types
._vector
_Q
(ast
):
19 return types
._vector
(*map(lambda a
: EVAL(a
, env
), ast
))
20 elif types
._hash
_map
_Q
(ast
):
23 keyvals
.append(EVAL(k
, env
))
24 keyvals
.append(EVAL(ast
[k
], env
))
25 return types
._hash
_map
(*keyvals
)
27 return ast
# primitive value, return unchanged
30 #print("EVAL %s" % printer._pr_str(ast))
31 if not types
._list
_Q
(ast
):
32 return eval_ast(ast
, env
)
35 if len(ast
) == 0: return ast
39 a1
, a2
= ast
[1], ast
[2]
41 return env
.set(a1
, res
)
43 a1
, a2
= ast
[1], ast
[2]
45 for i
in range(0, len(a1
), 2):
46 let_env
.set(a1
[i
], EVAL(a1
[i
+1], let_env
))
47 return EVAL(a2
, let_env
)
49 el
= eval_ast(ast
[1:], env
)
52 a1
, a2
= ast
[1], ast
[2]
54 if cond
is None or cond
is False:
55 if len(ast
) > 3: return EVAL(ast
[3], env
)
60 a1
, a2
= ast
[1], ast
[2]
61 return types
._function
(EVAL
, Env
, a2
, env
, a1
)
63 el
= eval_ast(ast
, env
)
69 return printer
._pr
_str
(exp
)
74 return PRINT(EVAL(READ(str), repl_env
))
76 # core.py: defined using python
77 for k
, v
in core
.ns
.items(): repl_env
.set(types
._symbol
(k
), v
)
79 # core.mal: defined using the language itself
80 REP("(def! not (fn* (a) (if a false true)))")
85 line
= mal_readline
.readline("user> ")
86 if line
== None: break
87 if line
== "": continue
89 except reader
.Blank
: continue
90 except Exception as e
:
91 print("".join(traceback
.format_exception(*sys
.exc_info())))