;; read (def! READ (fn* [strng] (read-string strng))) ;; eval (def! eval-ast (fn* [ast env] (do ;;(do (prn "eval-ast" ast "/" (keys env)) ) (cond (symbol? ast) (let* [res (get env (str ast))] (if res res (throw (str ast " not found")))) (list? ast) (map (fn* [exp] (EVAL exp env)) ast) (vector? ast) (apply vector (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! EVAL (fn* [ast env] (do ;;(do (prn "EVAL" ast "/" (keys @env)) ) (if (not (list? ast)) (eval-ast ast env) ;; apply list (if (empty? ast) ast (let* [el (eval-ast ast env) f (first el) args (rest el)] (apply f args))))))) ;; print (def! PRINT (fn* [exp] (pr-str exp))) ;; repl (def! repl-env {"+" + "-" - "*" * "/" /}) (def! rep (fn* [strng] (PRINT (EVAL (READ strng) repl-env)))) ;; repl loop (def! repl-loop (fn* [] (let* [line (readline "mal-user> ")] (if line (do (if (not (= "" line)) (try* (println (rep line)) (catch* exc (println "Uncaught exception:" exc)))) (repl-loop)))))) (def! -main (fn* [& args] (repl-loop))) (-main)