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 eval_ast(ast, env): | |
ea81a808 | 32 | if types._symbol_Q(ast): |
31690700 | 33 | return env.get(ast) |
ea81a808 JM |
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): | |
31690700 JM |
39 | keyvals = [] |
40 | for k in ast.keys(): | |
41 | keyvals.append(EVAL(k, env)) | |
42 | keyvals.append(EVAL(ast[k], env)) | |
ea81a808 | 43 | return types._hash_map(*keyvals) |
31690700 JM |
44 | else: |
45 | return ast # primitive value, return unchanged | |
46 | ||
47 | def EVAL(ast, env): | |
48 | while True: | |
86b689f3 | 49 | #print("EVAL %s" % printer._pr_str(ast)) |
ea81a808 | 50 | if not types._list_Q(ast): |
31690700 | 51 | return eval_ast(ast, env) |
ea81a808 | 52 | |
31690700 JM |
53 | # apply list |
54 | if len(ast) == 0: return ast | |
ea81a808 JM |
55 | a0 = ast[0] |
56 | ||
31690700 JM |
57 | if "def!" == a0: |
58 | a1, a2 = ast[1], ast[2] | |
59 | res = EVAL(a2, env) | |
60 | return env.set(a1, res) | |
61 | elif "let*" == a0: | |
62 | a1, a2 = ast[1], ast[2] | |
63 | let_env = Env(env) | |
64 | for i in range(0, len(a1), 2): | |
65 | let_env.set(a1[i], EVAL(a1[i+1], let_env)) | |
6301e0b6 JM |
66 | ast = a2 |
67 | env = let_env | |
68 | # Continue loop (TCO) | |
31690700 JM |
69 | elif "quote" == a0: |
70 | return ast[1] | |
71 | elif "quasiquote" == a0: | |
6301e0b6 JM |
72 | ast = quasiquote(ast[1]); |
73 | # Continue loop (TCO) | |
31690700 JM |
74 | elif "do" == a0: |
75 | eval_ast(ast[1:-1], env) | |
76 | ast = ast[-1] | |
77 | # Continue loop (TCO) | |
78 | elif "if" == a0: | |
79 | a1, a2 = ast[1], ast[2] | |
80 | cond = EVAL(a1, env) | |
81 | if cond is None or cond is False: | |
82 | if len(ast) > 3: ast = ast[3] | |
83 | else: ast = None | |
84 | else: | |
85 | ast = a2 | |
86 | # Continue loop (TCO) | |
87 | elif "fn*" == a0: | |
88 | a1, a2 = ast[1], ast[2] | |
ea81a808 | 89 | return types._function(EVAL, Env, a2, env, a1) |
31690700 JM |
90 | else: |
91 | el = eval_ast(ast, env) | |
92 | f = el[0] | |
a34b0200 JM |
93 | if hasattr(f, '__ast__'): |
94 | ast = f.__ast__ | |
95 | env = f.__gen_env__(el[1:]) | |
31690700 JM |
96 | else: |
97 | return f(*el[1:]) | |
98 | ||
99 | ||
100 | def PRINT(exp): | |
ea81a808 | 101 | return printer._pr_str(exp) |
31690700 JM |
102 | |
103 | # repl | |
104 | repl_env = Env() | |
105 | def REP(str): | |
106 | return PRINT(EVAL(READ(str), repl_env)) | |
31690700 | 107 | |
8cb5cda4 | 108 | # core.py: defined using python |
b8ee29b2 JM |
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:])) | |
31690700 | 112 | |
8cb5cda4 | 113 | # core.mal: defined using the language itself |
31690700 | 114 | REP("(def! not (fn* (a) (if a false true)))") |
e6d41de4 | 115 | REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))") |
31690700 JM |
116 | |
117 | if len(sys.argv) >= 2: | |
118 | REP('(load-file "' + sys.argv[1] + '")') | |
86b689f3 JM |
119 | sys.exit(0) |
120 | ||
121 | # repl loop | |
122 | while True: | |
123 | try: | |
124 | line = mal_readline.readline("user> ") | |
125 | if line == None: break | |
126 | if line == "": continue | |
127 | print(REP(line)) | |
128 | except reader.Blank: continue | |
129 | except Exception as e: | |
130 | print("".join(traceback.format_exception(*sys.exc_info()))) |