Nim: step8
[jackhill/mal.git] / nim / step8_macros.nim
CommitLineData
f5cf5237 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],
15 quasiquote(list ast.list[1 .. -1]))
16 else:
17 return list(symbol "cons", quasiquote(ast.list[0]), quasiquote(list(ast.list[1 .. -1])))
18
19proc is_macro_call(ast: MalType, env: Env): bool =
20 ast.kind == List and ast.list[0].kind == Symbol and
21 env.find(ast.list[0].str) != nil and env.get(ast.list[0].str).macro_q
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)
27 result = mac.malfun.fn(result.list[1 .. -1]).macroexpand(env)
28
29proc eval(ast: MalType, env: var 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: var Env): MalType =
47 template defaultApply =
48 let el = ast.eval_ast(env)
49 let f = el.list[0]
50 case f.kind
51 of MalFun:
52 ast = f.malfun.ast
53 env = initEnv(env, f.malfun.params, list(el.list[1 .. -1]))
54 else:
55 return f.fun(el.list[1 .. -1])
56
57 var ast = ast
58 while true:
59 if ast.kind != List: return ast.eval_ast(env)
60
61 ast = ast.macroexpand(env)
62 if ast.kind != List: return ast
63 if ast.list.len == 0: return ast
64
65 let a0 = ast.list[0]
66 case a0.kind
67 of Symbol:
68 case a0.str
69 of "def!":
70 let
71 a1 = ast.list[1]
72 a2 = ast.list[2]
73 return env.set(a1.str, a2.eval(env))
74
75 of "let*":
76 let
77 a1 = ast.list[1]
78 a2 = ast.list[2]
79 var let_env = Env(env)
80 case a1.kind
81 of List, Vector:
82 for i in countup(0, a1.list.high, 2):
83 let_env.set(a1.list[i].str, a1.list[i+1].eval(let_env))
84 else: raise newException(ValueError, "Illegal kind in let*")
85 ast = a2
86 env = let_env
87 # Continue loop (TCO)
88
89 of "quote":
90 return ast.list[1]
91
92 of "quasiquote":
93 ast = ast.list[1].quasiquote
94 # Continue loop (TCO)
95
96 of "defmacro!":
97 var fun = ast.list[2].eval(env)
98 fun.malfun.is_macro = true
99 return env.set(ast.list[1].str, fun)
100
101 of "macroexpand":
102 return ast.list[1].macroexpand(env)
103
104 of "do":
105 let last = ast.list.high
106 let el = (list ast.list[1 .. <last]).eval_ast(env)
107 ast = ast.list[last].eval(env)
108 # Continue loop (TCO)
109
110 of "if":
111 let
112 a1 = ast.list[1]
113 a2 = ast.list[2]
114 cond = a1.eval(env)
115
116 if cond.kind in {Nil, False}:
117 if ast.list.len > 3: ast = ast.list[3]
118 else: ast = nilObj
119 else: ast = a2
120
121 of "fn*":
122 let
123 a1 = ast.list[1]
124 a2 = ast.list[2]
125 var env2 = env
126 let fn = proc(a: varargs[MalType]): MalType =
127 var newEnv = initEnv(env2, a1, list(a))
128 a2.eval(newEnv)
129 return malfun(fn, a2, a1, env2)
130
131 else:
132 defaultApply()
133
134 else:
135 defaultApply()
136
137proc print(exp: MalType): string = exp.pr_str
138
139var repl_env = initEnv()
140
141for k, v in ns.items:
142 repl_env.set(k, v)
143repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env)))
144var ps = commandLineParams()
145repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str)))
146
147
148# core.nim: defined using nim
149proc rep(str: string): string {.discardable.} =
150 str.read.eval(repl_env).print
151
152# core.mal: defined using mal itself
153rep "(def! not (fn* (a) (if a false true)))"
154rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
155rep "(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)))))))"
156rep "(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))))))))"
157
158if paramCount() >= 1:
159 rep "(load-file \"" & paramStr(1) & "\")"
160 quit()
161
162while true:
163 try:
164 let line = readLineFromStdin("user> ")
165 echo line.rep
166 except Blank: discard
167 except:
168 echo getCurrentExceptionMsg()
169 echo getCurrentException().getStackTrace()