| 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", "pair?", ["fn", ["x"], |
| 13 | ["if", ["sequential?", "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", ["and", ["symbol?", ["nth", "ast", 0]], |
| 21 | ["=", ["`", "unquote"], ["get", ["nth", "ast", 0], ["`", "val"]]]], |
| 22 | ["nth", "ast", 1], |
| 23 | ["if", ["and", ["pair?", ["nth", "ast", 0]], |
| 24 | ["=", ["`", "splice-unquote"], |
| 25 | ["get", ["nth", ["nth", "ast", 0], 0], ["`", "val"]]]], |
| 26 | ["list", ["symbol", ["`", "concat"]], |
| 27 | ["nth", ["nth", "ast", 0], 1], |
| 28 | ["quasiquote", ["rest", "ast"]]], |
| 29 | ["list", ["symbol", ["`", "cons"]], |
| 30 | ["quasiquote", ["nth", "ast", 0]], |
| 31 | ["quasiquote", ["rest", "ast"]]]]]]]], |
| 32 | |
| 33 | ["def", "macro?", ["fn", ["ast", "env"], |
| 34 | ["and", ["list?", "ast"], |
| 35 | ["symbol?", ["first", "ast"]], |
| 36 | ["not", ["=", null, ["env-find", "env", ["first", "ast"]]]], |
| 37 | ["let", ["fn", ["env-get", "env", ["first", "ast"]]], |
| 38 | ["and", ["malfunc?", "fn"], |
| 39 | ["get", "fn", ["`", "macro?"]]]]]]], |
| 40 | |
| 41 | ["def", "macroexpand", ["fn", ["ast", "env"], |
| 42 | ["if", ["macro?", "ast", "env"], |
| 43 | ["let", ["mac", ["get", ["env-get", "env", ["first", "ast"]], ["`", "fn"]]], |
| 44 | ["macroexpand", ["apply", "mac", ["rest", "ast"]], "env"]], |
| 45 | "ast"]]], |
| 46 | |
| 47 | ["def", "eval-ast", ["fn", ["ast", "env"], |
| 48 | ["if", ["symbol?", "ast"], |
| 49 | ["env-get", "env", "ast"], |
| 50 | ["if", ["list?", "ast"], |
| 51 | ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"], |
| 52 | ["if", ["vector?", "ast"], |
| 53 | ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], |
| 54 | ["if", ["map?", "ast"], |
| 55 | ["let", ["new-hm", ["hash-map"]], |
| 56 | ["do", |
| 57 | ["map", ["fn", ["k"], ["set", "new-hm", |
| 58 | ["EVAL", "k", "env"], |
| 59 | ["EVAL", ["get", "ast", "k"], "env"]]], |
| 60 | ["keys", "ast"]], |
| 61 | "new-hm"]], |
| 62 | "ast"]]]]]], |
| 63 | |
| 64 | ["def", "LET", ["fn", ["env", "args"], |
| 65 | ["if", [">", ["count", "args"], 0], |
| 66 | ["do", |
| 67 | ["env-set", "env", ["nth", "args", 0], |
| 68 | ["EVAL", ["nth", "args", 1], "env"]], |
| 69 | ["LET", "env", ["rest", ["rest", "args"]]]]]]], |
| 70 | |
| 71 | ["def", "EVAL", ["fn", ["ast", "env"], |
| 72 | ["if", ["not", ["list?", "ast"]], |
| 73 | ["eval-ast", "ast", "env"], |
| 74 | ["let", ["ast", ["macroexpand", "ast", "env"]], |
| 75 | ["if", ["not", ["list?", "ast"]], |
| 76 | ["eval-ast", "ast", "env"], |
| 77 | ["if", ["empty?", "ast"], |
| 78 | "ast", |
| 79 | ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]], |
| 80 | ["if", ["=", ["`", "def!"], "a0"], |
| 81 | ["env-set", "env", ["nth", "ast", 1], |
| 82 | ["EVAL", ["nth", "ast", 2], "env"]], |
| 83 | ["if", ["=", ["`", "let*"], "a0"], |
| 84 | ["let", ["let-env", ["env-new", "env"]], |
| 85 | ["do", |
| 86 | ["LET", "let-env", ["nth", "ast", 1]], |
| 87 | ["EVAL", ["nth", "ast", 2], "let-env"]]], |
| 88 | ["if", ["=", ["`", "quote"], "a0"], |
| 89 | ["nth", "ast", 1], |
| 90 | ["if", ["=", ["`", "quasiquote"], "a0"], |
| 91 | ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"], |
| 92 | ["if", ["=", ["`", "defmacro!"], "a0"], |
| 93 | ["let", ["func", ["EVAL", ["nth", "ast", 2], "env"]], |
| 94 | ["do", |
| 95 | ["set", "func", ["`", "macro?"], true], |
| 96 | ["env-set", "env", ["nth", "ast", 1], "func"]]], |
| 97 | ["if", ["=", ["`", "macroexpand"], "a0"], |
| 98 | ["macroexpand", ["nth", "ast", 1], "env"], |
| 99 | ["if", ["=", ["`", "do"], "a0"], |
| 100 | ["do", |
| 101 | ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"], |
| 102 | ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]], |
| 103 | ["if", ["=", ["`", "if"], "a0"], |
| 104 | ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]], |
| 105 | ["if", ["or", ["=", "cond", null], ["=", "cond", false]], |
| 106 | ["if", [">", ["count", "ast"], 3], |
| 107 | ["EVAL", ["nth", "ast", 3], "env"], |
| 108 | null], |
| 109 | ["EVAL", ["nth", "ast", 2], "env"]]], |
| 110 | ["if", ["=", ["`", "fn*"], "a0"], |
| 111 | ["malfunc", |
| 112 | ["fn", ["&", "args"], |
| 113 | ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]], |
| 114 | ["EVAL", ["nth", "ast", 2], "e"]]], |
| 115 | ["nth", "ast", 2], "env", ["nth", "ast", 1]], |
| 116 | ["let", ["el", ["eval-ast", "ast", "env"], |
| 117 | "f", ["first", "el"], |
| 118 | "args", ["rest", "el"]], |
| 119 | ["if", ["malfunc?", "f"], |
| 120 | ["EVAL", ["get", "f", ["`", "ast"]], |
| 121 | ["env-new", ["get", "f", ["`", "env"]], |
| 122 | ["get", "f", ["`", "params"]], |
| 123 | "args"]], |
| 124 | ["apply", "f", "args"]]]]]]]]]]]]]]]]]]], |
| 125 | |
| 126 | ["def", "PRINT", ["fn", ["exp"], |
| 127 | ["pr-str", "exp", true]]], |
| 128 | |
| 129 | |
| 130 | ["def", "repl-env", ["env-new"]], |
| 131 | |
| 132 | ["def", "rep", ["fn", ["strng"], |
| 133 | ["try", |
| 134 | ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], |
| 135 | ["catch", "exc", |
| 136 | ["str", ["`", "Error: "], |
| 137 | ["if", ["isa", "exc", "Error"], |
| 138 | [".", "exc", ["`", "toString"]], |
| 139 | ["pr-str", "exc", true]]]]]]], |
| 140 | |
| 141 | ["`", "core.mal: defined using miniMAL"], |
| 142 | ["map", ["fn", ["k"], ["env-set", "repl-env", |
| 143 | ["symbol", "k"], |
| 144 | ["get", "core-ns", "k"]]], |
| 145 | ["keys", "core-ns"]], |
| 146 | ["env-set", "repl-env", ["symbol", ["`", "eval"]], |
| 147 | ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]], |
| 148 | ["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]], |
| 149 | ["slice", "ARGS", 1]], |
| 150 | |
| 151 | ["`", "core.mal: defined using mal itself"], |
| 152 | ["rep", ["`", "(def! not (fn* (a) (if a false true)))"]], |
| 153 | ["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]], |
| 154 | ["rep", ["`", "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"]], |
| 155 | ["rep", ["`", "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"]], |
| 156 | |
| 157 | ["if", ["not", ["empty?", "ARGS"]], |
| 158 | ["rep", ["str", ["`", "(load-file \""], ["get", "ARGS", 0], ["`", "\")"]]], |
| 159 | ["repl", ["`", "user> "], "rep"]], |
| 160 | |
| 161 | null |
| 162 | |
| 163 | ] |