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