Commit | Line | Data |
---|---|---|
31690700 JM |
1 | import sys, traceback |
2 | import mal_readline | |
ea81a808 JM |
3 | import mal_types as types |
4 | import reader, printer | |
5 | from env import Env | |
6 | import core | |
31690700 JM |
7 | |
8 | # read | |
9 | def READ(str): | |
ea81a808 | 10 | return reader.read_str(str) |
31690700 JM |
11 | |
12 | # eval | |
13 | def is_pair(x): | |
ea81a808 | 14 | return types._sequential_Q(x) and len(x) > 0 |
31690700 JM |
15 | |
16 | def quasiquote(ast): | |
17 | if not is_pair(ast): | |
ea81a808 JM |
18 | return types._list(types._symbol("quote"), |
19 | ast) | |
31690700 JM |
20 | elif ast[0] == 'unquote': |
21 | return ast[1] | |
22 | elif is_pair(ast[0]) and ast[0][0] == 'splice-unquote': | |
ea81a808 JM |
23 | return types._list(types._symbol("concat"), |
24 | ast[0][1], | |
25 | quasiquote(ast[1:])) | |
31690700 | 26 | else: |
ea81a808 JM |
27 | return types._list(types._symbol("cons"), |
28 | quasiquote(ast[0]), | |
29 | quasiquote(ast[1:])) | |
31690700 JM |
30 | |
31 | def is_macro_call(ast, env): | |
ea81a808 JM |
32 | return (types._list_Q(ast) and |
33 | types._symbol_Q(ast[0]) and | |
31690700 JM |
34 | env.find(ast[0]) and |
35 | hasattr(env.get(ast[0]), '_ismacro_')) | |
36 | ||
37 | def macroexpand(ast, env): | |
38 | while is_macro_call(ast, env): | |
39 | mac = env.get(ast[0]) | |
40 | ast = macroexpand(mac(*ast[1:]), env) | |
41 | return ast | |
42 | ||
43 | def eval_ast(ast, env): | |
ea81a808 | 44 | if types._symbol_Q(ast): |
31690700 | 45 | return env.get(ast) |
ea81a808 JM |
46 | elif types._list_Q(ast): |
47 | return types._list(*map(lambda a: EVAL(a, env), ast)) | |
48 | elif types._vector_Q(ast): | |
49 | return types._vector(*map(lambda a: EVAL(a, env), ast)) | |
50 | elif types._hash_map_Q(ast): | |
31690700 JM |
51 | keyvals = [] |
52 | for k in ast.keys(): | |
53 | keyvals.append(EVAL(k, env)) | |
54 | keyvals.append(EVAL(ast[k], env)) | |
ea81a808 | 55 | return types._hash_map(*keyvals) |
31690700 JM |
56 | else: |
57 | return ast # primitive value, return unchanged | |
58 | ||
59 | def EVAL(ast, env): | |
60 | while True: | |
86b689f3 | 61 | #print("EVAL %s" % printer._pr_str(ast)) |
ea81a808 | 62 | if not types._list_Q(ast): |
31690700 | 63 | return eval_ast(ast, env) |
ea81a808 | 64 | |
31690700 JM |
65 | # apply list |
66 | ast = macroexpand(ast, env) | |
ea81a808 | 67 | if not types._list_Q(ast): return ast |
31690700 | 68 | if len(ast) == 0: return ast |
ea81a808 | 69 | a0 = ast[0] |
31690700 | 70 | |
31690700 JM |
71 | if "def!" == a0: |
72 | a1, a2 = ast[1], ast[2] | |
73 | res = EVAL(a2, env) | |
74 | return env.set(a1, res) | |
75 | elif "let*" == a0: | |
76 | a1, a2 = ast[1], ast[2] | |
77 | let_env = Env(env) | |
78 | for i in range(0, len(a1), 2): | |
79 | let_env.set(a1[i], EVAL(a1[i+1], let_env)) | |
6301e0b6 JM |
80 | ast = a2 |
81 | env = let_env | |
82 | # Continue loop (TCO) | |
31690700 JM |
83 | elif "quote" == a0: |
84 | return ast[1] | |
85 | elif "quasiquote" == a0: | |
6301e0b6 JM |
86 | ast = quasiquote(ast[1]); |
87 | # Continue loop (TCO) | |
31690700 JM |
88 | elif 'defmacro!' == a0: |
89 | func = EVAL(ast[2], env) | |
90 | func._ismacro_ = True | |
91 | return env.set(ast[1], func) | |
92 | elif 'macroexpand' == a0: | |
93 | return macroexpand(ast[1], env) | |
94 | elif "do" == a0: | |
95 | eval_ast(ast[1:-1], env) | |
96 | ast = ast[-1] | |
97 | # Continue loop (TCO) | |
98 | elif "if" == a0: | |
99 | a1, a2 = ast[1], ast[2] | |
100 | cond = EVAL(a1, env) | |
101 | if cond is None or cond is False: | |
102 | if len(ast) > 3: ast = ast[3] | |
103 | else: ast = None | |
104 | else: | |
105 | ast = a2 | |
106 | # Continue loop (TCO) | |
107 | elif "fn*" == a0: | |
108 | a1, a2 = ast[1], ast[2] | |
ea81a808 | 109 | return types._function(EVAL, Env, a2, env, a1) |
31690700 JM |
110 | else: |
111 | el = eval_ast(ast, env) | |
112 | f = el[0] | |
a34b0200 JM |
113 | if hasattr(f, '__ast__'): |
114 | ast = f.__ast__ | |
115 | env = f.__gen_env__(el[1:]) | |
31690700 JM |
116 | else: |
117 | return f(*el[1:]) | |
118 | ||
119 | ||
120 | def PRINT(exp): | |
ea81a808 | 121 | return printer._pr_str(exp) |
31690700 JM |
122 | |
123 | # repl | |
124 | repl_env = Env() | |
125 | def REP(str): | |
126 | return PRINT(EVAL(READ(str), repl_env)) | |
31690700 | 127 | |
8cb5cda4 JM |
128 | # core.py: defined using python |
129 | for k, v in core.ns.items(): repl_env.set(k, v) | |
130 | repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) | |
86b689f3 | 131 | repl_env.set('*ARGV*', types._list(*sys.argv[2:])) |
31690700 | 132 | |
8cb5cda4 | 133 | # core.mal: defined using the language itself |
31690700 | 134 | REP("(def! not (fn* (a) (if a false true)))") |
1617910a | 135 | REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))") |
8cb5cda4 JM |
136 | 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)))))))") |
137 | 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))))))))") | |
31690700 JM |
138 | |
139 | if len(sys.argv) >= 2: | |
140 | REP('(load-file "' + sys.argv[1] + '")') | |
86b689f3 JM |
141 | sys.exit(0) |
142 | ||
143 | # repl loop | |
144 | while True: | |
145 | try: | |
146 | line = mal_readline.readline("user> ") | |
147 | if line == None: break | |
148 | if line == "": continue | |
149 | print(REP(line)) | |
150 | except reader.Blank: continue | |
151 | except Exception as e: | |
152 | print("".join(traceback.format_exception(*sys.exc_info()))) |