3 import mal_types
as types
4 from mal_types
import (MalSym
, MalInt
, MalStr
,
5 nil
, true
, false
, _symbol
, _keywordu
,
6 MalList
, _list
, MalVector
, MalHashMap
, MalFunc
)
13 return reader
.read_str(str)
17 return types
._sequential
_Q
(x
) and len(x
) > 0
21 return _list(_symbol(u
"quote"), ast
)
24 if isinstance(a0
, MalSym
):
25 if a0
.value
== u
'unquote':
27 if is_pair(a0
) and isinstance(a0
[0], MalSym
):
29 if (isinstance(a00
, MalSym
) and
30 a00
.value
== u
'splice-unquote'):
31 return _list(_symbol(u
"concat"),
33 quasiquote(ast
.rest()))
34 return _list(_symbol(u
"cons"),
36 quasiquote(ast
.rest()))
39 def eval_ast(ast
, env
):
40 if types
._symbol
_Q
(ast
):
41 assert isinstance(ast
, MalSym
)
43 elif types
._list
_Q
(ast
):
46 res
.append(EVAL(a
, env
))
48 elif types
._vector
_Q
(ast
):
51 res
.append(EVAL(a
, env
))
53 elif types
._hash
_map
_Q
(ast
):
55 for k
in ast
.dct
.keys():
56 new_dct
[k
] = EVAL(ast
.dct
[k
], env
)
57 return MalHashMap(new_dct
)
59 return ast
# primitive value, return unchanged
63 #print("EVAL %s" % printer._pr_str(ast))
64 if not types
._list
_Q
(ast
):
65 return eval_ast(ast
, env
)
68 if len(ast
) == 0: return ast
70 if isinstance(a0
, MalSym
):
76 a1
, a2
= ast
[1], ast
[2]
78 return env
.set(a1
, res
)
79 elif u
"let*" == a0sym
:
80 a1
, a2
= ast
[1], ast
[2]
82 for i
in range(0, len(a1
), 2):
83 let_env
.set(a1
[i
], EVAL(a1
[i
+1], let_env
))
85 env
= let_env
# Continue loop (TCO)
86 elif u
"quote" == a0sym
:
88 elif u
"quasiquote" == a0sym
:
89 ast
= quasiquote(ast
[1]) # Continue loop (TCO)
94 eval_ast(ast
.slice2(1, len(ast
)-1), env
)
95 ast
= ast
[-1] # Continue loop (TCO)
97 a1
, a2
= ast
[1], ast
[2]
99 if cond
is nil
or cond
is false
:
100 if len(ast
) > 3: ast
= ast
[3] # Continue loop (TCO)
103 ast
= a2
# Continue loop (TCO)
104 elif u
"fn*" == a0sym
:
105 a1
, a2
= ast
[1], ast
[2]
106 return MalFunc(None, a2
, env
, a1
, EVAL
)
108 el
= eval_ast(ast
, env
)
110 if isinstance(f
, MalFunc
):
113 env
= f
.gen_env(el
.rest()) # Continue loop (TCO)
115 return f
.apply(el
.rest())
117 raise Exception("%s is not callable" % f
)
121 return printer
._pr
_str
(exp
)
124 class MalEval(MalFunc
):
125 def apply(self
, args
):
126 return self
.EvalFunc(args
[0], self
.env
)
128 def entry_point(argv
):
131 return PRINT(EVAL(READ(str), env
))
133 # core.py: defined using python
134 for k
, v
in core
.ns
.items():
135 repl_env
.set(_symbol(unicode(k
)), MalFunc(v
))
136 repl_env
.set(types
._symbol
(u
'eval'),
137 MalEval(None, env
=repl_env
, EvalFunc
=EVAL
))
140 for a
in argv
[2:]: mal_args
.append(MalStr(unicode(a
)))
141 repl_env
.set(_symbol(u
'*ARGV*'), MalList(mal_args
))
143 # core.mal: defined using the language itself
144 REP("(def! not (fn* (a) (if a false true)))", repl_env
)
145 REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))", repl_env
)
148 REP('(load-file "' + argv
[1] + '")', repl_env
)
153 line
= mal_readline
.readline("user> ")
154 if line
== "": continue
155 print(REP(line
, repl_env
))
156 except EOFError as e
:
160 except types
.MalException
as e
:
161 print(u
"Error: %s" % printer
._pr
_str
(e
.object, False))
162 except Exception as e
:
163 print("Error: %s" % e
)
164 #print("".join(traceback.format_exception(*sys.exc_info())))
167 # _____ Define and setup target ___
171 # Just run entry_point if not RPython compilation
173 if not sys
.argv
[0].endswith('rpython'):
174 entry_point(sys
.argv
)