Merge pull request #478 from rjtobin/zig
[jackhill/mal.git] / nim / step7_quote.nim
CommitLineData
dc7f0b6a 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]))
dc7f0b6a 16 else:
3f429bf4 17 return list(symbol "cons", quasiquote(ast.list[0]), quasiquote(list(ast.list[1 .. ^1])))
dc7f0b6a 18
fc7f8a4b 19proc eval(ast: MalType, env: Env): MalType
dc7f0b6a 20
21proc eval_ast(ast: MalType, env: var Env): MalType =
22 case ast.kind
23 of Symbol:
24 result = env.get(ast.str)
25 of List:
26 result = list ast.list.mapIt(MalType, it.eval(env))
27 of Vector:
28 result = vector ast.list.mapIt(MalType, it.eval(env))
29 of HashMap:
30 result = hash_map()
31 for k, v in ast.hash_map.pairs:
32 result.hash_map[k] = v.eval(env)
33 else:
34 result = ast
35
fc7f8a4b 36proc eval(ast: MalType, env: Env): MalType =
2800f318 37 var ast = ast
fc7f8a4b 38 var env = env
2800f318 39
dc7f0b6a 40 template defaultApply =
41 let el = ast.eval_ast(env)
42 let f = el.list[0]
43 case f.kind
44 of MalFun:
45 ast = f.malfun.ast
3f429bf4 46 env = initEnv(f.malfun.env, f.malfun.params, list(el.list[1 .. ^1]))
dc7f0b6a 47 else:
3f429bf4 48 return f.fun(el.list[1 .. ^1])
dc7f0b6a 49
dc7f0b6a 50 while true:
2800f318 51 if ast.kind != List: return ast.eval_ast(env)
920963d6 52 if ast.list.len == 0: return ast
dc7f0b6a 53
54 let a0 = ast.list[0]
55 case a0.kind
56 of Symbol:
57 case a0.str
58 of "def!":
59 let
60 a1 = ast.list[1]
61 a2 = ast.list[2]
62 return env.set(a1.str, a2.eval(env))
63
64 of "let*":
65 let
66 a1 = ast.list[1]
67 a2 = ast.list[2]
fc7f8a4b 68 var let_env = initEnv(env)
dc7f0b6a 69 case a1.kind
70 of List, Vector:
71 for i in countup(0, a1.list.high, 2):
f5cf5237 72 let_env.set(a1.list[i].str, a1.list[i+1].eval(let_env))
73 else: raise newException(ValueError, "Illegal kind in let*")
74 ast = a2
75 env = let_env
dc7f0b6a 76 # Continue loop (TCO)
77
78 of "quote":
79 return ast.list[1]
80
81 of "quasiquote":
82 ast = ast.list[1].quasiquote
83 # Continue loop (TCO)
84
85 of "do":
86 let last = ast.list.high
4dab2092 87 discard (list ast.list[1 .. <last]).eval_ast(env)
2800f318 88 ast = ast.list[last]
dc7f0b6a 89 # Continue loop (TCO)
90
91 of "if":
92 let
93 a1 = ast.list[1]
94 a2 = ast.list[2]
95 cond = a1.eval(env)
96
97 if cond.kind in {Nil, False}:
98 if ast.list.len > 3: ast = ast.list[3]
99 else: ast = nilObj
100 else: ast = a2
101
102 of "fn*":
103 let
104 a1 = ast.list[1]
105 a2 = ast.list[2]
106 var env2 = env
107 let fn = proc(a: varargs[MalType]): MalType =
108 var newEnv = initEnv(env2, a1, list(a))
109 a2.eval(newEnv)
2800f318 110 return malfun(fn, a2, a1, env)
dc7f0b6a 111
112 else:
113 defaultApply()
114
115 else:
116 defaultApply()
117
118proc print(exp: MalType): string = exp.pr_str
119
120var repl_env = initEnv()
121
122for k, v in ns.items:
123 repl_env.set(k, v)
124repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env)))
125var ps = commandLineParams()
126repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str)))
127
128
129# core.nim: defined using nim
130proc rep(str: string): string {.discardable.} =
131 str.read.eval(repl_env).print
132
133# core.mal: defined using mal itself
134rep "(def! not (fn* (a) (if a false true)))"
e6d41de4 135rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))"
dc7f0b6a 136
137if paramCount() >= 1:
138 rep "(load-file \"" & paramStr(1) & "\")"
139 quit()
140
141while true:
142 try:
143 let line = readLineFromStdin("user> ")
144 echo line.rep
145 except Blank: discard
146 except:
147 echo getCurrentExceptionMsg()
148 echo getCurrentException().getStackTrace()