DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / io / step2_eval.io
1 MalTypes
2 MalReader
3
4 READ := method(str, MalReader read_str(str))
5
6 eval_ast := method(ast, env,
7 (ast type) switch(
8 "MalSymbol", env at(ast val) ifNil(Exception raise("'" .. (ast val) "' not found")),
9 "MalList", MalList with(ast map(a, EVAL(a, env))),
10 "MalVector", MalVector with(ast map(a, EVAL(a, env))),
11 "MalMap",
12 m := MalMap clone
13 ast foreach(k, v,
14 keyObj := MalMap keyToObj(k)
15 m atPut(MalMap objToKey(EVAL(keyObj, env)), EVAL(v, env))
16 )
17 m,
18 ast
19 )
20 )
21
22 EVAL := method(ast, env,
23 if(ast type != "MalList", return(eval_ast(ast, env)))
24 if(ast isEmpty, return ast)
25 el := eval_ast(ast, env)
26 f := el at(0)
27 args := el rest
28 f callWithArgList(args)
29 )
30
31 PRINT := method(exp, exp malPrint(true))
32
33 repl_env := Map with(
34 "+", block(a, b, a + b),
35 "-", block(a, b, a - b),
36 "*", block(a, b, a * b),
37 "/", block(a, b, a / b)
38 )
39
40 RE := method(str, EVAL(READ(str), repl_env))
41
42 REP := method(str, PRINT(RE(str)))
43
44 loop(
45 line := MalReadline readLine("user> ")
46 if(line isNil, break)
47 if(line isEmpty, continue)
48 e := try(REP(line) println)
49 e catch(Exception,
50 ("Error: " .. (e error)) println
51 )
52 )