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
31 #print("EVAL %s" % printer._pr_str(ast))
32 if not types
._list
_Q
(ast
):
33 return eval_ast(ast
, env
)
36 if len(ast
) == 0: return ast
40 a1
, a2
= ast
[1], ast
[2]
42 return env
.set(a1
, res
)
44 a1
, a2
= ast
[1], ast
[2]
46 for i
in range(0, len(a1
), 2):
47 let_env
.set(a1
[i
], EVAL(a1
[i
+1], let_env
))
52 eval_ast(ast
[1:-1], env
)
56 a1
, a2
= ast
[1], ast
[2]
58 if cond
is None or cond
is False:
59 if len(ast
) > 3: ast
= ast
[3]
65 a1
, a2
= ast
[1], ast
[2]
66 return types
._function
(EVAL
, Env
, a2
, env
, a1
)
68 el
= eval_ast(ast
, env
)
70 if hasattr(f
, '__ast__'):
72 env
= f
.__gen
_env
__(el
[1:])
78 return printer
._pr
_str
(exp
)
83 return PRINT(EVAL(READ(str), repl_env
))
85 # core.py: defined using python
86 for k
, v
in core
.ns
.items(): repl_env
.set(types
._symbol
(k
), v
)
87 repl_env
.set(types
._symbol
('eval'), lambda ast
: EVAL(ast
, repl_env
))
88 repl_env
.set(types
._symbol
('*ARGV*'), types
._list
(*sys
.argv
[2:]))
90 # core.mal: defined using the language itself
91 REP("(def! not (fn* (a) (if a false true)))")
92 REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
94 if len(sys
.argv
) >= 2:
95 REP('(load-file "' + sys
.argv
[1] + '")')
101 line
= mal_readline
.readline("user> ")
102 if line
== None: break
103 if line
== "": continue
105 except reader
.Blank
: continue
106 except Exception as e
:
107 print("".join(traceback
.format_exception(*sys
.exc_info())))