Commit | Line | Data |
---|---|---|
9be6d5a6 JM |
1 | import sys, traceback |
2 | import mal_readline | |
3 | import mal_types as types | |
8855a05a JM |
4 | from mal_types import (MalSym, MalInt, MalStr, |
5 | nil, true, false, _symbol, _keywordu, | |
6 | MalList, _list, MalVector, MalHashMap, MalFunc) | |
9be6d5a6 JM |
7 | import reader, printer |
8 | from env import Env | |
9 | import core | |
10 | ||
11 | # read | |
12 | def READ(str): | |
13 | return reader.read_str(str) | |
14 | ||
15 | # eval | |
16 | def is_pair(x): | |
17 | return types._sequential_Q(x) and len(x) > 0 | |
18 | ||
19 | def quasiquote(ast): | |
20 | if not is_pair(ast): | |
21 | return _list(_symbol(u"quote"), ast) | |
22 | else: | |
23 | a0 = ast[0] | |
24 | if isinstance(a0, MalSym): | |
25 | if a0.value == u'unquote': | |
26 | return ast[1] | |
27 | if is_pair(a0) and isinstance(a0[0], MalSym): | |
28 | a00 = a0[0] | |
29 | if (isinstance(a00, MalSym) and | |
30 | a00.value == u'splice-unquote'): | |
31 | return _list(_symbol(u"concat"), | |
32 | a0[1], | |
33 | quasiquote(ast.rest())) | |
34 | return _list(_symbol(u"cons"), | |
35 | quasiquote(a0), | |
36 | quasiquote(ast.rest())) | |
37 | ||
38 | def is_macro_call(ast, env): | |
39 | if types._list_Q(ast): | |
40 | a0 = ast[0] | |
41 | if isinstance(a0, MalSym): | |
42 | if not env.find(a0) is None: | |
43 | return env.get(a0).ismacro | |
44 | return False | |
45 | ||
46 | def macroexpand(ast, env): | |
47 | while is_macro_call(ast, env): | |
48 | assert isinstance(ast[0], MalSym) | |
49 | mac = env.get(ast[0]) | |
50 | ast = macroexpand(mac.apply(ast.rest()), env) | |
51 | return ast | |
52 | ||
53 | def eval_ast(ast, env): | |
54 | if types._symbol_Q(ast): | |
55 | assert isinstance(ast, MalSym) | |
56 | return env.get(ast) | |
57 | elif types._list_Q(ast): | |
58 | res = [] | |
59 | for a in ast.values: | |
60 | res.append(EVAL(a, env)) | |
61 | return MalList(res) | |
8855a05a JM |
62 | elif types._vector_Q(ast): |
63 | res = [] | |
64 | for a in ast.values: | |
65 | res.append(EVAL(a, env)) | |
66 | return MalVector(res) | |
67 | elif types._hash_map_Q(ast): | |
68 | new_dct = {} | |
69 | for k in ast.dct.keys(): | |
70 | new_dct[k] = EVAL(ast.dct[k], env) | |
71 | return MalHashMap(new_dct) | |
9be6d5a6 JM |
72 | else: |
73 | return ast # primitive value, return unchanged | |
74 | ||
75 | def EVAL(ast, env): | |
76 | while True: | |
77 | #print("EVAL %s" % printer._pr_str(ast)) | |
78 | if not types._list_Q(ast): | |
79 | return eval_ast(ast, env) | |
80 | ||
81 | # apply list | |
82 | ast = macroexpand(ast, env) | |
44aef1f4 JM |
83 | if not types._list_Q(ast): |
84 | return eval_ast(ast, env) | |
9be6d5a6 JM |
85 | if len(ast) == 0: return ast |
86 | a0 = ast[0] | |
87 | if isinstance(a0, MalSym): | |
88 | a0sym = a0.value | |
89 | else: | |
90 | a0sym = u"__<*fn*>__" | |
91 | ||
92 | if u"def!" == a0sym: | |
93 | a1, a2 = ast[1], ast[2] | |
94 | res = EVAL(a2, env) | |
95 | return env.set(a1, res) | |
96 | elif u"let*" == a0sym: | |
97 | a1, a2 = ast[1], ast[2] | |
98 | let_env = Env(env) | |
99 | for i in range(0, len(a1), 2): | |
100 | let_env.set(a1[i], EVAL(a1[i+1], let_env)) | |
101 | ast = a2 | |
102 | env = let_env # Continue loop (TCO) | |
103 | elif u"quote" == a0sym: | |
104 | return ast[1] | |
105 | elif u"quasiquote" == a0sym: | |
106 | ast = quasiquote(ast[1]) # Continue loop (TCO) | |
107 | elif u"defmacro!" == a0sym: | |
108 | func = EVAL(ast[2], env) | |
109 | func.ismacro = True | |
110 | return env.set(ast[1], func) | |
111 | elif u"macroexpand" == a0sym: | |
112 | return macroexpand(ast[1], env) | |
113 | elif u"try*" == a0sym: | |
dd7a4f55 JM |
114 | if len(ast) < 3: |
115 | return EVAL(ast[1], env); | |
9be6d5a6 JM |
116 | a1, a2 = ast[1], ast[2] |
117 | a20 = a2[0] | |
118 | if isinstance(a20, MalSym): | |
119 | if a20.value == u"catch*": | |
120 | try: | |
121 | return EVAL(a1, env); | |
122 | except types.MalException as exc: | |
123 | exc = exc.object | |
124 | catch_env = Env(env, _list(a2[1]), _list(exc)) | |
125 | return EVAL(a2[2], catch_env) | |
126 | except Exception as exc: | |
127 | exc = MalStr(unicode("%s" % exc)) | |
128 | catch_env = Env(env, _list(a2[1]), _list(exc)) | |
129 | return EVAL(a2[2], catch_env) | |
130 | return EVAL(a1, env); | |
131 | elif u"do" == a0sym: | |
132 | if len(ast) == 0: | |
133 | return nil | |
134 | elif len(ast) > 1: | |
135 | eval_ast(ast.slice2(1, len(ast)-1), env) | |
136 | ast = ast[-1] # Continue loop (TCO) | |
137 | elif u"if" == a0sym: | |
138 | a1, a2 = ast[1], ast[2] | |
139 | cond = EVAL(a1, env) | |
140 | if cond is nil or cond is false: | |
141 | if len(ast) > 3: ast = ast[3] # Continue loop (TCO) | |
142 | else: return nil | |
143 | else: | |
144 | ast = a2 # Continue loop (TCO) | |
145 | elif u"fn*" == a0sym: | |
146 | a1, a2 = ast[1], ast[2] | |
147 | return MalFunc(None, a2, env, a1, EVAL) | |
148 | else: | |
149 | el = eval_ast(ast, env) | |
150 | f = el.values[0] | |
151 | if isinstance(f, MalFunc): | |
152 | if f.ast: | |
153 | ast = f.ast | |
154 | env = f.gen_env(el.rest()) # Continue loop (TCO) | |
155 | else: | |
156 | return f.apply(el.rest()) | |
157 | else: | |
158 | raise Exception("%s is not callable" % f) | |
159 | ||
160 | ||
161 | def PRINT(exp): | |
162 | return printer._pr_str(exp) | |
163 | ||
164 | # repl | |
165 | class MalEval(MalFunc): | |
166 | def apply(self, args): | |
167 | return self.EvalFunc(args[0], self.env) | |
168 | ||
169 | def entry_point(argv): | |
170 | repl_env = Env() | |
171 | def REP(str, env): | |
172 | return PRINT(EVAL(READ(str), env)) | |
173 | ||
174 | # core.py: defined using python | |
175 | for k, v in core.ns.items(): | |
176 | repl_env.set(_symbol(unicode(k)), MalFunc(v)) | |
177 | repl_env.set(types._symbol(u'eval'), | |
178 | MalEval(None, env=repl_env, EvalFunc=EVAL)) | |
179 | mal_args = [] | |
180 | if len(argv) >= 3: | |
181 | for a in argv[2:]: mal_args.append(MalStr(unicode(a))) | |
182 | repl_env.set(_symbol(u'*ARGV*'), MalList(mal_args)) | |
183 | ||
184 | # core.mal: defined using the language itself | |
185 | REP("(def! not (fn* (a) (if a false true)))", repl_env) | |
186 | REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env) | |
187 | REP("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))", repl_env) | |
188 | REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))", repl_env) | |
189 | ||
190 | if len(argv) >= 2: | |
191 | REP('(load-file "' + argv[1] + '")', repl_env) | |
192 | return 0 | |
193 | ||
194 | while True: | |
195 | try: | |
196 | line = mal_readline.readline("user> ") | |
197 | if line == "": continue | |
198 | print(REP(line, repl_env)) | |
199 | except EOFError as e: | |
200 | break | |
201 | except reader.Blank: | |
202 | continue | |
203 | except types.MalException as e: | |
204 | print(u"Error: %s" % printer._pr_str(e.object, False)) | |
ab02c5bb JM |
205 | except Exception as e: |
206 | print("Error: %s" % e) | |
9be6d5a6 JM |
207 | #print("".join(traceback.format_exception(*sys.exc_info()))) |
208 | return 0 | |
209 | ||
210 | # _____ Define and setup target ___ | |
211 | def target(*args): | |
212 | return entry_point | |
213 | ||
214 | # Just run entry_point if not RPython compilation | |
215 | import sys | |
216 | if not sys.argv[0].endswith('rpython'): | |
217 | entry_point(sys.argv) |