Merge branch 'master' into fsharp
[jackhill/mal.git] / nim / step6_file.nim
1 import rdstdin, tables, sequtils, os, types, reader, printer, env, core
2
3 proc read(str: string): MalType = str.read_str
4
5 proc eval(ast: MalType, env: var Env): MalType
6
7 proc eval_ast(ast: MalType, env: var Env): MalType =
8 case ast.kind
9 of Symbol:
10 result = env.get(ast.str)
11 of List:
12 result = list ast.list.mapIt(MalType, it.eval(env))
13 of Vector:
14 result = vector ast.list.mapIt(MalType, it.eval(env))
15 of HashMap:
16 result = hash_map()
17 for k, v in ast.hash_map.pairs:
18 result.hash_map[k] = v.eval(env)
19 else:
20 result = ast
21
22 proc eval(ast: MalType, env: var Env): MalType =
23 var ast = ast
24
25 template defaultApply =
26 let el = ast.eval_ast(env)
27 let f = el.list[0]
28 case f.kind
29 of MalFun:
30 ast = f.malfun.ast
31 env = initEnv(f.malfun.env, f.malfun.params, list(el.list[1 .. -1]))
32 else:
33 return f.fun(el.list[1 .. -1])
34
35 while true:
36 if ast.kind != List: return ast.eval_ast(env)
37
38 let a0 = ast.list[0]
39 case a0.kind
40 of Symbol:
41 case a0.str
42 of "def!":
43 let
44 a1 = ast.list[1]
45 a2 = ast.list[2]
46 return env.set(a1.str, a2.eval(env))
47
48 of "let*":
49 let
50 a1 = ast.list[1]
51 a2 = ast.list[2]
52 var let_env = Env(env)
53 case a1.kind
54 of List, Vector:
55 for i in countup(0, a1.list.high, 2):
56 let_env.set(a1.list[i].str, a1.list[i+1].eval(let_env))
57 else: raise newException(ValueError, "Illegal kind in let*")
58 ast = a2
59 env = let_env
60 # Continue loop (TCO)
61
62 of "do":
63 let last = ast.list.high
64 let el = (list ast.list[1 .. <last]).eval_ast(env)
65 ast = ast.list[last]
66 # Continue loop (TCO)
67
68 of "if":
69 let
70 a1 = ast.list[1]
71 a2 = ast.list[2]
72 cond = a1.eval(env)
73
74 if cond.kind in {Nil, False}:
75 if ast.list.len > 3: ast = ast.list[3]
76 else: ast = nilObj
77 else: ast = a2
78
79 of "fn*":
80 let
81 a1 = ast.list[1]
82 a2 = ast.list[2]
83 var env2 = env
84 let fn = proc(a: varargs[MalType]): MalType =
85 var newEnv = initEnv(env2, a1, list(a))
86 a2.eval(newEnv)
87 return malfun(fn, a2, a1, env)
88
89 else:
90 defaultApply()
91
92 else:
93 defaultApply()
94
95 proc print(exp: MalType): string = exp.pr_str
96
97 var repl_env = initEnv()
98
99 for k, v in ns.items:
100 repl_env.set(k, v)
101 repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env)))
102 var ps = commandLineParams()
103 repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str)))
104
105
106 # core.nim: defined using nim
107 proc rep(str: string): string {.discardable.} =
108 str.read.eval(repl_env).print
109
110 # core.mal: defined using mal itself
111 rep "(def! not (fn* (a) (if a false true)))"
112 rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
113
114 if paramCount() >= 1:
115 rep "(load-file \"" & paramStr(1) & "\")"
116 quit()
117
118 while true:
119 try:
120 let line = readLineFromStdin("user> ")
121 echo line.rep
122 except Blank: discard
123 except:
124 echo getCurrentExceptionMsg()
125 echo getCurrentException().getStackTrace()