Merge pull request #420 from asarhaddon/load-file-once
[jackhill/mal.git] / miniMAL / step2_eval.json
1 ["do",
2
3 ["load", ["`", "miniMAL-core.json"]],
4 ["load", ["`", "types.json"]],
5 ["load", ["`", "reader.json"]],
6 ["load", ["`", "printer.json"]],
7
8 ["def", "READ", ["fn", ["strng"],
9 ["read-str", "strng"]]],
10
11 ["def", "eval-ast", ["fn", ["ast", "env"],
12 ["if", ["symbol?", "ast"],
13 ["let", ["sym", ["get", "ast", ["`", "val"]]],
14 ["if", ["contains?", "env", "sym"],
15 ["get", "env", "sym"],
16 ["throw", ["str", ["`", "'"], "sym", ["`", "' not found"]]]]],
17 ["if", ["list?", "ast"],
18 ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"],
19 ["if", ["vector?", "ast"],
20 ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]],
21 ["if", ["map?", "ast"],
22 ["let", ["new-hm", ["hash-map"]],
23 ["do",
24 ["map", ["fn", ["k"], ["set", "new-hm",
25 ["EVAL", "k", "env"],
26 ["EVAL", ["get", "ast", "k"], "env"]]],
27 ["keys", "ast"]],
28 "new-hm"]],
29 "ast"]]]]]],
30
31 ["def", "EVAL", ["fn", ["ast", "env"],
32 ["if", ["not", ["list?", "ast"]],
33 ["eval-ast", "ast", "env"],
34 ["if", ["empty?", "ast"],
35 "ast",
36 ["let", ["el", ["eval-ast", "ast", "env"],
37 "f", ["first", "el"],
38 "args", ["rest", "el"]],
39 ["apply", "f", "args"]]]]]],
40
41 ["def", "PRINT", ["fn", ["exp"],
42 ["pr-str", "exp", true]]],
43
44
45 ["def", "repl-env",
46 ["hash-map",
47 ["`", "+"], "+",
48 ["`", "-"], "-",
49 ["`", "*"], "*",
50 ["`", "/"], ["fn", ["a", "b"], ["parseInt", ["/", "a", "b"]]]]],
51
52 ["def", "rep", ["fn", ["strng"],
53 ["try",
54 ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]],
55 ["catch", "exc",
56 ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]],
57
58 ["repl", ["`", "user> "], "rep"],
59
60 null
61
62 ]