3 import mal_types
as types
10 return reader
.read_str(str)
14 return types
._sequential
_Q
(x
) and len(x
) > 0
18 return types
._list
(types
._symbol
("quote"),
20 elif ast
[0] == 'unquote':
22 elif is_pair(ast
[0]) and ast
[0][0] == 'splice-unquote':
23 return types
._list
(types
._symbol
("concat"),
27 return types
._list
(types
._symbol
("cons"),
31 def eval_ast(ast
, env
):
32 if types
._symbol
_Q
(ast
):
34 elif types
._list
_Q
(ast
):
35 return types
._list
(*map(lambda a
: EVAL(a
, env
), ast
))
36 elif types
._vector
_Q
(ast
):
37 return types
._vector
(*map(lambda a
: EVAL(a
, env
), ast
))
38 elif types
._hash
_map
_Q
(ast
):
41 keyvals
.append(EVAL(k
, env
))
42 keyvals
.append(EVAL(ast
[k
], env
))
43 return types
._hash
_map
(*keyvals
)
45 return ast
# primitive value, return unchanged
49 #print("EVAL %s" % printer._pr_str(ast))
50 if not types
._list
_Q
(ast
):
51 return eval_ast(ast
, env
)
54 if len(ast
) == 0: return ast
58 a1
, a2
= ast
[1], ast
[2]
60 return env
.set(a1
, res
)
62 a1
, a2
= ast
[1], ast
[2]
64 for i
in range(0, len(a1
), 2):
65 let_env
.set(a1
[i
], EVAL(a1
[i
+1], let_env
))
71 elif "quasiquote" == a0
:
72 ast
= quasiquote(ast
[1]);
75 eval_ast(ast
[1:-1], env
)
79 a1
, a2
= ast
[1], ast
[2]
81 if cond
is None or cond
is False:
82 if len(ast
) > 3: ast
= ast
[3]
88 a1
, a2
= ast
[1], ast
[2]
89 return types
._function
(EVAL
, Env
, a2
, env
, a1
)
91 el
= eval_ast(ast
, env
)
93 if hasattr(f
, '__ast__'):
95 env
= f
.__gen
_env
__(el
[1:])
101 return printer
._pr
_str
(exp
)
106 return PRINT(EVAL(READ(str), repl_env
))
108 # core.py: defined using python
109 for k
, v
in core
.ns
.items(): repl_env
.set(types
._symbol
(k
), v
)
110 repl_env
.set(types
._symbol
('eval'), lambda ast
: EVAL(ast
, repl_env
))
111 repl_env
.set(types
._symbol
('*ARGV*'), types
._list
(*sys
.argv
[2:]))
113 # core.mal: defined using the language itself
114 REP("(def! not (fn* (a) (if a false true)))")
115 REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))")
117 if len(sys
.argv
) >= 2:
118 REP('(load-file "' + sys
.argv
[1] + '")')
124 line
= mal_readline
.readline("user> ")
125 if line
== None: break
126 if line
== "": continue
128 except reader
.Blank
: continue
129 except Exception as e
:
130 print("".join(traceback
.format_exception(*sys
.exc_info())))