(load-file "../mal/env.mal") (load-file "../mal/core.mal") ;; read (def! READ read-string) ;; eval (def! eval-ast (fn* [ast env] ;; (do (prn "eval-ast" ast "/" (keys env)) ) (cond (symbol? ast) (env-get env ast) (list? ast) (map (fn* [exp] (EVAL exp env)) ast) (vector? ast) (vec (map (fn* [exp] (EVAL exp env)) ast)) (map? ast) (apply hash-map (apply concat (map (fn* [k] [k (EVAL (get ast k) env)]) (keys ast)))) "else" ast))) (def! LET (fn* [env binds form] (if (empty? binds) (EVAL form env) (do (env-set env (first binds) (EVAL (nth binds 1) env)) (LET env (rest (rest binds)) form))))) (def! EVAL (fn* [ast env] ;; (do (prn "EVAL" ast "/" (keys @env)) ) (if (not (list? ast)) (eval-ast ast env) ;; apply list (let* [a0 (first ast)] (cond (empty? ast) ast (= 'def! a0) (env-set env (nth ast 1) (EVAL (nth ast 2) env)) (= 'let* a0) (LET (new-env env) (nth ast 1) (nth ast 2)) (= 'do a0) (let* [el (eval-ast (rest ast) env)] (nth el (- (count el) 1))) (= 'if a0) (if (EVAL (nth ast 1) env) (EVAL (nth ast 2) env) (if (> (count ast) 3) (EVAL (nth ast 3) env))) (= 'fn* a0) (fn* [& args] (EVAL (nth ast 2) (new-env env (nth ast 1) args))) "else" (let* [el (eval-ast ast env)] (apply (first el) (rest el)))))))) ;; print (def! PRINT pr-str) ;; repl (def! repl-env (new-env)) (def! rep (fn* [strng] (PRINT (EVAL (READ strng) repl-env)))) ;; core.mal: defined directly using mal (map (fn* [data] (apply env-set repl-env data)) core_ns) (env-set repl-env 'eval (fn* [ast] (EVAL ast repl-env))) (env-set repl-env '*ARGV* (rest *ARGV*)) ;; core.mal: defined using the new language itself (rep "(def! not (fn* [a] (if a false true)))") (rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))") ;; repl loop (def! repl-loop (fn* [line] (if line (do (if (not (= "" line)) (try* (println (rep line)) (catch* exc (println "Uncaught exception:" exc)))) (repl-loop (readline "mal-user> ")))))) ;; main (if (empty? *ARGV*) (repl-loop "") (rep (str "(load-file \"" (first *ARGV*) "\")")))