1 load "../logo/readline.lg
2 load "../logo/reader.lg
3 load "../logo/printer.lg
13 output and sequentialp :obj ((_count :obj) > 0)
17 if not pairp :ast [output (mal_list symbol_new "quote :ast)]
18 localmake "a0 nth :ast 0
19 if symbolnamedp "unquote :a0 [output nth :ast 1]
21 localmake "a00 nth :a0 0
22 if symbolnamedp "splice-unquote :a00 [
23 localmake "a01 nth :a0 1
24 output (mal_list symbol_new "concat :a01 (mal_list symbol_new "quasiquote rest :ast))
27 output (mal_list symbol_new "cons (mal_list symbol_new "quasiquote :a0) (mal_list symbol_new "quasiquote rest :ast))
30 to macrocallp :ast :env
31 if (obj_type :ast) = "list [
32 if (_count :ast) > 0 [
33 localmake "a0 nth :ast 0
34 if (obj_type :a0) = "symbol [
35 if not emptyp env_find :env :a0 [
36 localmake "f env_get :env :a0
37 if (obj_type :f) = "fn [
47 to _macroexpand :ast :env
48 if not macrocallp :ast :env [output :ast]
49 localmake "a0 nth :ast 0
50 localmake "f env_get :env :a0
51 output _macroexpand invoke_fn :f rest :ast :env
55 output case (obj_type :ast) [
56 [[symbol] env_get :env :ast]
57 [[list] obj_new "list map [_eval ? :env] obj_val :ast]
58 [[vector] obj_new "vector map [_eval ? :env] obj_val :ast]
59 [[hashmap] obj_new "hashmap map [_eval ? :env] obj_val :ast]
64 to _eval :a_ast :a_env
68 if (obj_type :ast) <> "list [output eval_ast :ast :env]
69 make "ast _macroexpand :ast :env
70 if (obj_type :ast) <> "list [output eval_ast :ast :env]
71 if emptyp obj_val :ast [output :ast]
72 localmake "a0 nth :ast 0
73 case list obj_type :a0 obj_val :a0 [
75 localmake "a1 nth :ast 1
76 localmake "a2 nth :ast 2
77 output env_set :env :a1 _eval :a2 :env ]
80 localmake "a1 nth :ast 1
81 localmake "letenv env_new :env [] []
83 while [:i < _count :a1] [
84 ignore env_set :letenv nth :a1 :i _eval nth :a1 (:i + 1) :letenv
88 make "ast nth :ast 2 ] ; TCO
93 [[[symbol quasiquote]]
94 make "ast quasiquote nth :ast 1 ] ; TCO
97 localmake "a1 nth :ast 1
98 localmake "a2 nth :ast 2
99 localmake "macro_fn _eval :a2 :env
100 fn_set_macro :macro_fn
101 output env_set :env :a1 :macro_fn ]
103 [[[symbol macroexpand]]
104 output _macroexpand nth :ast 1 :env ]
107 localmake "result nil_new
108 catch "error [make "result _eval nth :ast 1 :env]
109 localmake "exception error
110 ifelse or emptyp :exception ((_count :ast) < 3) [
113 localmake "e first butfirst :exception
114 localmake "exception_obj ifelse :e = "_mal_exception_ [:global_exception] [obj_new "string :e]
115 localmake "a2 nth :ast 2
116 localmake "catchenv env_new :env [] []
117 ignore env_set :catchenv nth :a2 1 :exception_obj
118 output _eval nth :a2 2 :catchenv
123 while [:i < ((_count :ast) - 1)] [
124 ignore _eval nth :ast :i :env
127 make "ast last obj_val :ast ] ; TCO
130 localmake "a1 nth :ast 1
131 localmake "cond _eval :a1 :env
132 case obj_type :cond [
133 [[nil false] ifelse (_count :ast) > 3 [
134 make "ast nth :ast 3 ; TCO
138 [else make "ast nth :ast 2] ; TCO
142 output fn_new nth :ast 1 :env nth :ast 2 ]
145 localmake "el eval_ast :ast :env
146 localmake "f nth :el 0
149 output apply obj_val :f butfirst obj_val :el ]
151 make "env env_new fn_env :f fn_args :f rest :el
152 make "ast fn_body :f ] ; TCO
154 (throw "error [Wrong type for apply])]
161 output pr_str :exp "true
165 output _eval _read :str :repl_env
169 output _print re :str
172 to print_exception :exception
173 if not emptyp :exception [
174 localmake "e first butfirst :exception
175 ifelse :e = "_mal_exception_ [
176 (print "Error: pr_str :global_exception "false)
184 localmake "running "true
186 localmake "line readline word "user> :space_char
191 if not emptyp :line [
192 catch "error [print rep :line]
193 print_exception error
200 output _eval :a :repl_env
204 localmake "argv ifelse emptyp :command.line [[]] [butfirst :command.line]
205 output obj_new "list map [obj_new "string ?] :argv
208 make "repl_env env_new [] [] []
210 ignore env_set :repl_env first ? first butfirst ?
212 ignore env_set :repl_env [symbol eval] [nativefn mal_eval]
213 ignore env_set :repl_env [symbol *ARGV*] argv_list
215 ; core.mal: defined using the language itself
216 ignore re "|(def! *host-language* "logo")|
217 ignore re "|(def! not (fn* (a) (if a false true)))|
218 ignore re "|(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))|
219 ignore re "|(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)))))))|
220 ignore re "|(def! *gensym-counter* (atom 0))|
221 ignore re "|(def! gensym (fn* [] (symbol (str "G__" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))|
222 ignore re "|(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))|
224 if not emptyp :command.line [
225 catch "error [ignore re (word "\(load-file :space_char "\" first :command.line "\"\) )]
226 print_exception error
230 ignore re "|(println (str "Mal [" *host-language* "]"))|