1 (load-file "../mal/env.mal")
2 (load-file "../mal/core.mal")
5 (def! READ (fn* [strng]
10 (def! is-pair (fn* [x]
15 (def! QUASIQUOTE (fn* [ast]
20 (= 'unquote (first ast))
23 (if (is-pair (first ast))
24 (if (= 'splice-unquote (first (first ast)))
26 (list 'concat (nth (first ast) 1) (QUASIQUOTE (rest ast)))
29 (list 'cons (QUASIQUOTE (first ast)) (QUASIQUOTE (rest ast))))))
31 (def! eval-ast (fn* [ast env] (do
32 ;;(do (prn "eval-ast" ast "/" (keys env)) )
34 (symbol? ast) (env-get env ast)
36 (list? ast) (map (fn* [exp] (EVAL exp env)) ast)
38 (vector? ast) (apply vector (map (fn* [exp] (EVAL exp env)) ast))
40 (map? ast) (apply hash-map
42 (map (fn* [k] [k (EVAL (get ast k) env)])
47 (def! LET (fn* [env args]
48 (if (> (count args) 0)
50 (env-set env (nth args 0) (EVAL (nth args 1) env))
51 (LET env (rest (rest args)))))))
53 (def! EVAL (fn* [ast env] (do
54 ;;(do (prn "EVAL" ast "/" (keys @env)) )
59 (let* [a0 (first ast)]
65 (env-set env (nth ast 1) (EVAL (nth ast 2) env))
68 (let* [let-env (new-env env)]
70 (LET let-env (nth ast 1))
71 (EVAL (nth ast 2) let-env)))
77 (let* [a1 (nth ast 1)]
78 (EVAL (QUASIQUOTE a1) env))
81 (let* [el (eval-ast (rest ast) env)]
82 (nth el (- (count el) 1)))
85 (let* [cond (EVAL (nth ast 1) env)]
86 (if (or (= cond nil) (= cond false))
88 (EVAL (nth ast 3) env)
90 (EVAL (nth ast 2) env)))
94 (EVAL (nth ast 2) (new-env env (nth ast 1) args)))
97 (let* [el (eval-ast ast env)
100 (apply f args))))))))
104 (def! PRINT (fn* [exp] (pr-str exp)))
107 (def! repl-env (new-env))
108 (def! rep (fn* [strng]
109 (PRINT (EVAL (READ strng) repl-env))))
111 ;; core.mal: defined directly using mal
112 (map (fn* [data] (env-set repl-env (nth data 0) (nth data 1))) core_ns)
113 (env-set repl-env 'eval (fn* [ast] (EVAL ast repl-env)))
114 (env-set repl-env '*ARGV* (rest *ARGV*))
116 ;; core.mal: defined using the new language itself
117 (rep "(def! not (fn* [a] (if a false true)))")
118 (rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
121 (def! repl-loop (fn* []
122 (let* [line (readline "mal-user> ")]
125 (if (not (= "" line))
129 (println "Uncaught exception:" exc))))
132 (def! -main (fn* [& args]
133 (if (> (count args) 0)
134 (rep (str "(load-file \"" (first args) "\")"))