properly implement tco and add step7:quote
[jackhill/mal.git] / groovy / step3_env.groovy
1 import reader
2 import printer
3 import types
4 import types.MalException
5 import types.MalSymbol
6 import env.Env
7
8 // READ
9 READ = { str ->
10 reader.read_str str
11 }
12
13 // EVAL
14 eval_ast = { ast, env ->
15 switch (ast) {
16 case MalSymbol: return env.get(ast);
17 case List: return types.vector_Q(ast) ?
18 types.vector(ast.collect { EVAL(it,env) }) :
19 ast.collect { EVAL(it,env) }
20 case Map: def new_hm = [:]
21 ast.each { k,v ->
22 new_hm[EVAL(k, env)] = EVAL(v, env)
23 }
24 return new_hm
25 default: return ast
26 }
27 }
28
29 EVAL = { ast, env ->
30 //println("EVAL: ${printer.pr_str(ast,true)}")
31 if (! types.list_Q(ast)) return eval_ast(ast, env)
32 if (ast.size() == 0) return ast
33
34 switch (ast[0]) {
35 case { it instanceof MalSymbol && it.value == "def!" }:
36 return env.set(ast[1], EVAL(ast[2], env))
37 case { it instanceof MalSymbol && it.value == "let*" }:
38 def let_env = new Env(env)
39 for (int i=0; i < ast[1].size(); i += 2) {
40 let_env.set(ast[1][i], EVAL(ast[1][i+1], let_env))
41 }
42 return EVAL(ast[2], let_env)
43 default:
44 def el = eval_ast(ast, env)
45 def (f, args) = [el[0], el[1..-1]]
46 f(args)
47 }
48 }
49
50 // PRINT
51 PRINT = { exp ->
52 printer.pr_str exp, true
53 }
54
55 // REPL
56 repl_env = new Env();
57 repl_env.set(new MalSymbol("+"), { a -> a[0]+a[1]});
58 repl_env.set(new MalSymbol("-"), { a -> a[0]-a[1]});
59 repl_env.set(new MalSymbol("*"), { a -> a[0]*a[1]});
60 repl_env.set(new MalSymbol("/"), { a -> a[0]/a[1]}); // /
61 REP = { str ->
62 PRINT(EVAL(READ(str), repl_env))
63 }
64
65 while (true) {
66 line = System.console().readLine 'user> '
67 if (line == null) {
68 break;
69 }
70 try {
71 println REP(line)
72 } catch(MalException ex) {
73 println "Error: ${printer.pr_str(ex.obj, true)}"
74 } catch(ex) {
75 println "Error: $ex"
76 ex.printStackTrace()
77 }
78 }