miniMAL: step3
[jackhill/mal.git] / miniMAL / step3_env.json
1 ["do",
2
3 ["load-file", ["`", "miniMAL-core.json"]],
4 ["load-file", ["`", "types.json"]],
5 ["load-file", ["`", "reader.json"]],
6 ["load-file", ["`", "printer.json"]],
7 ["load-file", ["`", "env.json"]],
8
9 ["def", "READ", ["fn", ["strng"],
10 ["read-str", "strng"]]],
11
12 ["def", "eval-ast", ["fn", ["ast", "env"],
13 ["if", ["symbol?", "ast"],
14 ["env-get", "env", "ast"],
15 ["if", ["list?", "ast"],
16 ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"],
17 "ast"]]]],
18
19 ["def", "LET", ["fn", ["env", "args"],
20 ["if", [">", ["count", "args"], 0],
21 ["do",
22 ["env-set", "env", ["nth", "args", 0],
23 ["EVAL", ["nth", "args", 1], "env"]],
24 ["LET", "env", ["rest", ["rest", "args"]]]]]]],
25
26 ["def", "EVAL", ["fn", ["ast", "env"],
27 ["if", ["not", ["list?", "ast"]],
28 ["eval-ast", "ast", "env"],
29 ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]],
30 ["if", ["=", ["`", "def!"], "a0"],
31 ["env-set", "env", ["nth", "ast", 1],
32 ["EVAL", ["nth", "ast", 2], "env"]],
33 ["if", ["=", ["`", "let*"], "a0"],
34 ["let", ["let-env", ["env-new", "env"]],
35 ["do",
36 ["LET", "let-env", ["nth", "ast", 1]],
37 ["EVAL", ["nth", "ast", 2], "let-env"]]],
38 ["let", ["el", ["eval-ast", "ast", "env"],
39 "f", ["first", "el"],
40 "args", ["rest", "el"]],
41 ["apply", "f", "args"]]]]]]]],
42
43 ["def", "PRINT", ["fn", ["exp"],
44 ["pr-str", "exp", true]]],
45
46
47 ["def", "repl-env", ["env-new"]],
48 ["env-set", "repl-env", ["symbol", ["`", "+"]], "+"],
49 ["env-set", "repl-env", ["symbol", ["`", "-"]], "-"],
50 ["env-set", "repl-env", ["symbol", ["`", "*"]], "*"],
51 ["def", "div", ["fn", ["a", "b"], ["parseInt", ["/", "a", "b"]]]],
52 ["env-set", "repl-env", ["symbol", ["`", "/"]], "div"],
53
54 ["def", "rep", ["fn", ["strng"],
55 ["try",
56 ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]],
57 ["catch", "exc",
58 ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]],
59
60 ["repl", ["`", "user> "], "rep"],
61
62 null
63
64 ]