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