crystal: add seq and string?
[jackhill/mal.git] / nim / step2_eval.nim
1 import rdstdin, tables, sequtils, types, reader, printer
2
3 proc read(str: string): MalType = str.read_str
4
5 proc eval(ast: MalType, env: Table[string, MalType]): MalType
6
7 proc eval_ast(ast: MalType, env: Table[string, MalType]): MalType =
8 case ast.kind
9 of Symbol:
10 if not env.hasKey(ast.str):
11 raise newException(ValueError, "'" & ast.str & "' not found")
12 result = env[ast.str]
13 of List:
14 result = list ast.list.mapIt(MalType, it.eval(env))
15 of Vector:
16 result = vector ast.list.mapIt(MalType, it.eval(env))
17 of HashMap:
18 result = hash_map()
19 for k, v in ast.hash_map.pairs:
20 result.hash_map[k] = v.eval(env)
21 else:
22 result = ast
23
24 proc eval(ast: MalType, env: Table[string, MalType]): MalType =
25 case ast.kind
26 of List:
27 let el = ast.eval_ast(env)
28 el.list[0].fun(el.list[1 .. ^1])
29 else:
30 ast.eval_ast(env)
31
32 proc print(exp: MalType): string = exp.pr_str
33
34 template wrapNumberFun(op: expr): expr =
35 fun proc(xs: varargs[MalType]): MalType = number op(xs[0].number, xs[1].number)
36
37 let repl_env = toTable({
38 "+": wrapNumberFun `+`,
39 "-": wrapNumberFun `-`,
40 "*": wrapNumberFun `*`,
41 "/": wrapNumberFun `div`,
42 })
43
44 proc rep(str: string): string =
45 str.read.eval(repl_env).print
46
47 while true:
48 try:
49 let line = readLineFromStdin("user> ")
50 echo line.rep
51 except:
52 echo getCurrentExceptionMsg()