In step6, test that eval uses Repl not Env. I have not managed to test this before...
[jackhill/mal.git] / nim / step9_try.nim
CommitLineData
8de9f308 1import rdstdin, tables, sequtils, os, types, reader, printer, env, core
2
3proc read(str: string): MalType = str.read_str
4
5proc is_pair(x: MalType): bool =
6 x.kind in {List, Vector} and x.list.len > 0
7
8proc quasiquote(ast: MalType): MalType =
9 if not ast.is_pair:
10 return list(symbol "quote", ast)
11 elif ast.list[0] == symbol "unquote":
12 return ast.list[1]
13 elif ast.list[0].is_pair and ast.list[0].list[0] == symbol "splice-unquote":
14 return list(symbol "concat", ast.list[0].list[1],
3f429bf4 15 quasiquote(list ast.list[1 .. ^1]))
8de9f308 16 else:
3f429bf4 17 return list(symbol "cons", quasiquote(ast.list[0]), quasiquote(list(ast.list[1 .. ^1])))
8de9f308 18
19proc is_macro_call(ast: MalType, env: Env): bool =
dd7a4f55 20 ast.kind == List and ast.list.len > 0 and ast.list[0].kind == Symbol and
c1709fad 21 env.find(ast.list[0].str) != nil and env.get(ast.list[0].str).fun_is_macro
8de9f308 22
23proc macroexpand(ast: MalType, env: Env): MalType =
24 result = ast
25 while result.is_macro_call(env):
26 let mac = env.get(result.list[0].str)
3f429bf4 27 result = mac.malfun.fn(result.list[1 .. ^1]).macroexpand(env)
8de9f308 28
29proc eval(ast: MalType, env: Env): MalType
30
31proc eval_ast(ast: MalType, env: var Env): MalType =
32 case ast.kind
33 of Symbol:
34 result = env.get(ast.str)
35 of List:
36 result = list ast.list.mapIt(MalType, it.eval(env))
37 of Vector:
38 result = vector ast.list.mapIt(MalType, it.eval(env))
39 of HashMap:
40 result = hash_map()
41 for k, v in ast.hash_map.pairs:
42 result.hash_map[k] = v.eval(env)
43 else:
44 result = ast
45
46proc eval(ast: MalType, env: Env): MalType =
47 var ast = ast
48 var env = env
49
50 template defaultApply =
51 let el = ast.eval_ast(env)
52 let f = el.list[0]
53 case f.kind
54 of MalFun:
55 ast = f.malfun.ast
3f429bf4 56 env = initEnv(f.malfun.env, f.malfun.params, list(el.list[1 .. ^1]))
8de9f308 57 else:
3f429bf4 58 return f.fun(el.list[1 .. ^1])
8de9f308 59
60 while true:
61 if ast.kind != List: return ast.eval_ast(env)
62
63 ast = ast.macroexpand(env)
6c94cd3e 64 if ast.kind != List: return ast.eval_ast(env)
8de9f308 65 if ast.list.len == 0: return ast
66
67 let a0 = ast.list[0]
68 case a0.kind
69 of Symbol:
70 case a0.str
71 of "def!":
72 let
73 a1 = ast.list[1]
74 a2 = ast.list[2]
75 res = a2.eval(env)
76 return env.set(a1.str, res)
77
78 of "let*":
79 let
80 a1 = ast.list[1]
81 a2 = ast.list[2]
4dab2092 82 var let_env = env
8de9f308 83 case a1.kind
84 of List, Vector:
85 for i in countup(0, a1.list.high, 2):
86 let_env.set(a1.list[i].str, a1.list[i+1].eval(let_env))
87 else: raise newException(ValueError, "Illegal kind in let*")
88 ast = a2
89 env = let_env
90 # Continue loop (TCO)
91
92 of "quote":
93 return ast.list[1]
94
95 of "quasiquote":
96 ast = ast.list[1].quasiquote
97 # Continue loop (TCO)
98
99 of "defmacro!":
100 var fun = ast.list[2].eval(env)
101 fun.malfun.is_macro = true
102 return env.set(ast.list[1].str, fun)
103
104 of "macroexpand":
105 return ast.list[1].macroexpand(env)
106
107 of "try*":
108 let
109 a1 = ast.list[1]
110 a2 = ast.list[2]
dd7a4f55
JM
111 if ast.list.len <= 2:
112 return a1.eval(env)
8de9f308 113 if a2.list[0].str == "catch*":
114 try:
115 return a1.eval(env)
116 except MalError:
117 let exc = (ref MalError) getCurrentException()
118 var catchEnv = initEnv(env, list a2.list[1], exc.t)
119 return a2.list[2].eval(catchEnv)
120 except:
121 let exc = getCurrentExceptionMsg()
122 var catchEnv = initEnv(env, list a2.list[1], list str(exc))
123 return a2.list[2].eval(catchEnv)
124 else:
125 return a1.eval(env)
126
127 of "do":
128 let last = ast.list.high
4dab2092 129 discard (list ast.list[1 .. <last]).eval_ast(env)
2800f318 130 ast = ast.list[last]
8de9f308 131 # Continue loop (TCO)
132
133 of "if":
134 let
135 a1 = ast.list[1]
136 a2 = ast.list[2]
137 cond = a1.eval(env)
138
139 if cond.kind in {Nil, False}:
140 if ast.list.len > 3: ast = ast.list[3]
141 else: ast = nilObj
142 else: ast = a2
143
144 of "fn*":
145 let
146 a1 = ast.list[1]
147 a2 = ast.list[2]
148 var env2 = env
149 let fn = proc(a: varargs[MalType]): MalType =
150 var newEnv = initEnv(env2, a1, list(a))
151 a2.eval(newEnv)
152 return malfun(fn, a2, a1, env)
153
dd7a4f55 154 else: defaultApply()
8de9f308 155
dd7a4f55 156 else: defaultApply()
8de9f308 157
158proc print(exp: MalType): string = exp.pr_str
159
160var repl_env = initEnv()
161
162for k, v in ns.items:
163 repl_env.set(k, v)
164repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env)))
165var ps = commandLineParams()
166repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str)))
167
168
169# core.nim: defined using nim
170proc rep(str: string): string {.discardable.} =
171 str.read.eval(repl_env).print
172
173# core.mal: defined using mal itself
174rep "(def! not (fn* (a) (if a false true)))"
175rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
176rep "(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)))))))"
177rep "(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))))))))"
178
179if paramCount() >= 1:
180 rep "(load-file \"" & paramStr(1) & "\")"
181 quit()
182
183while true:
184 try:
185 let line = readLineFromStdin("user> ")
186 echo line.rep
187 except Blank: discard
dd7a4f55
JM
188 except IOError: quit()
189 except MalError:
190 let exc = (ref MalError) getCurrentException()
191 echo "Error: " & exc.t.list[0].pr_str
8de9f308 192 except:
dd7a4f55 193 stdout.write "Error: "
8de9f308 194 echo getCurrentExceptionMsg()
195 echo getCurrentException().getStackTrace()