Change quasiquote algorithm
[jackhill/mal.git] / impls / miniMAL / step6_file.json
1 ["do",
2
3 ["load", ["`", "miniMAL-core.json"]],
4 ["load", ["`", "types.json"]],
5 ["load", ["`", "reader.json"]],
6 ["load", ["`", "printer.json"]],
7 ["load", ["`", "env.json"]],
8 ["load", ["`", "core.json"]],
9
10 ["def", "READ", ["fn", ["strng"], ["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 ["if", ["vector?", "ast"],
18 ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]],
19 ["if", ["map?", "ast"],
20 ["let", ["new-hm", ["hash-map"]],
21 ["do",
22 ["map", ["fn", ["k"], ["set", "new-hm",
23 ["EVAL", "k", "env"],
24 ["EVAL", ["get", "ast", "k"], "env"]]],
25 ["keys", "ast"]],
26 "new-hm"]],
27 "ast"]]]]]],
28
29 ["def", "LET", ["fn", ["env", "args"],
30 ["if", [">", ["count", "args"], 0],
31 ["do",
32 ["env-set", "env", ["nth", "args", 0],
33 ["EVAL", ["nth", "args", 1], "env"]],
34 ["LET", "env", ["rest", ["rest", "args"]]]]]]],
35
36 ["def", "EVAL", ["fn", ["ast", "env"],
37 ["if", ["not", ["list?", "ast"]],
38 ["eval-ast", "ast", "env"],
39 ["if", ["empty?", "ast"],
40 "ast",
41 ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]],
42 ["if", ["=", ["`", "def!"], "a0"],
43 ["env-set", "env", ["nth", "ast", 1],
44 ["EVAL", ["nth", "ast", 2], "env"]],
45 ["if", ["=", ["`", "let*"], "a0"],
46 ["let", ["let-env", ["env-new", "env"]],
47 ["do",
48 ["LET", "let-env", ["nth", "ast", 1]],
49 ["EVAL", ["nth", "ast", 2], "let-env"]]],
50 ["if", ["=", ["`", "do"], "a0"],
51 ["do",
52 ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"],
53 ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]],
54 ["if", ["=", ["`", "if"], "a0"],
55 ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]],
56 ["if", ["or", ["=", "cond", null], ["=", "cond", false]],
57 ["if", [">", ["count", "ast"], 3],
58 ["EVAL", ["nth", "ast", 3], "env"],
59 null],
60 ["EVAL", ["nth", "ast", 2], "env"]]],
61 ["if", ["=", ["`", "fn*"], "a0"],
62 ["malfunc",
63 ["fn", ["&", "args"],
64 ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]],
65 ["EVAL", ["nth", "ast", 2], "e"]]],
66 ["nth", "ast", 2], "env", ["nth", "ast", 1]],
67 ["let", ["el", ["eval-ast", "ast", "env"],
68 "f", ["first", "el"],
69 "args", ["rest", "el"]],
70 ["if", ["malfunc?", "f"],
71 ["EVAL", ["get", "f", ["`", "ast"]],
72 ["env-new", ["get", "f", ["`", "env"]],
73 ["get", "f", ["`", "params"]],
74 "args"]],
75 ["apply", "f", "args"]]]]]]]]]]]]],
76
77 ["def", "PRINT", ["fn", ["exp"],
78 ["pr-str", "exp", true]]],
79
80
81 ["def", "repl-env", ["env-new"]],
82
83 ["def", "rep", ["fn", ["strng"],
84 ["try",
85 ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]],
86 ["catch", "exc",
87 ["str", ["`", "Error: "],
88 ["if", ["isa", "exc", "Error"],
89 [".", "exc", ["`", "toString"]],
90 ["pr-str", "exc", true]]]]]]],
91
92 ["`", "core.mal: defined using miniMAL"],
93 ["map", ["fn", ["k"], ["env-set", "repl-env",
94 ["symbol", "k"],
95 ["get", "core-ns", "k"]]],
96 ["keys", "core-ns"]],
97 ["env-set", "repl-env", ["symbol", ["`", "eval"]],
98 ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]],
99 ["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]],
100 ["slice", "ARGS", 1]],
101
102 ["`", "core.mal: defined using mal itself"],
103 ["rep", ["`", "(def! not (fn* (a) (if a false true)))"]],
104 ["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))"]],
105
106 ["if", ["not", ["empty?", "ARGS"]],
107 ["rep", ["str", ["`", "(load-file \""], ["get", "ARGS", 0], ["`", "\")"]]],
108 ["repl", ["`", "user> "], "rep"]],
109
110 null
111
112 ]