diff options
| author | Joel Martin <github@martintribe.org> | 2014-04-02 22:23:37 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-04-02 22:23:37 -0500 |
| commit | ea81a8087bcd7953b083a2be9db447f75e7ebf56 (patch) | |
| tree | 6cf47a2dbd55d42efc4a901eaabdec952f40ce89 /clojure | |
| parent | 1617910ad342a55762f3ddabb975849d843cff85 (diff) | |
| download | mal-ea81a8087bcd7953b083a2be9db447f75e7ebf56.tar.gz mal-ea81a8087bcd7953b083a2be9db447f75e7ebf56.zip | |
All: split types into types, env, printer, core.
- types: low-level mapping to the implementation language.
- core: functions on types that are exposed directly to mal.
- printer: implementation called by pr-str, str, prn, println.
- env: the environment implementation
- Also, unindent all TCO while loops so that the diff of step4 and
step5 are minimized.
Diffstat (limited to 'clojure')
| -rw-r--r-- | clojure/Makefile | 3 | ||||
| -rw-r--r-- | clojure/src/core.clj | 63 | ||||
| -rw-r--r-- | clojure/src/env.clj | 35 | ||||
| -rw-r--r-- | clojure/src/printer.clj | 7 | ||||
| -rw-r--r-- | clojure/src/step1_read_print.clj | 4 | ||||
| -rw-r--r-- | clojure/src/step2_eval.clj | 7 | ||||
| -rw-r--r-- | clojure/src/step3_env.clj | 20 | ||||
| -rw-r--r-- | clojure/src/step4_if_fn_do.clj | 25 | ||||
| -rw-r--r-- | clojure/src/step5_tco.clj | 27 | ||||
| -rw-r--r-- | clojure/src/step6_file.clj | 29 | ||||
| -rw-r--r-- | clojure/src/step7_quote.clj | 27 | ||||
| -rw-r--r-- | clojure/src/step8_macros.clj | 35 | ||||
| -rw-r--r-- | clojure/src/step9_interop.clj | 35 | ||||
| -rw-r--r-- | clojure/src/stepA_more.clj | 47 | ||||
| -rw-r--r-- | clojure/src/types.clj | 71 |
15 files changed, 238 insertions, 197 deletions
diff --git a/clojure/Makefile b/clojure/Makefile index d18eb50..4bc289e 100644 --- a/clojure/Makefile +++ b/clojure/Makefile @@ -1,7 +1,8 @@ TESTS = -SOURCES = src/types.clj src/readline.clj src/reader.clj src/stepA_more.clj +SOURCES = src/readline.clj src/reader.clj src/printer.clj \ + src/env.clj src/core.clj src/stepA_more.clj all: diff --git a/clojure/src/core.clj b/clojure/src/core.clj new file mode 100644 index 0000000..23dfc55 --- /dev/null +++ b/clojure/src/core.clj @@ -0,0 +1,63 @@ +(ns core) + +;; Errors/exceptions +(defn mal_throw [obj] + (throw (ex-info "mal exception" {:data obj}))) + +;; Atoms +(defn atom? [atm] + (= (type atm) clojure.lang.Atom)) + +;; core_ns is core namespaces functions +(def core_ns + [['= =] + ['throw mal_throw] + ['nil? nil?] + ['true? true?] + ['false? false?] + ['symbol? symbol?] + ['pr-str pr-str] + ['str str] + ['prn prn] + ['println println] + ['< <] + ['<= <=] + ['> >] + ['>= >=] + ['+ +] + ['- -] + ['* *] + ['/ /] + + ['list list] + ['list? seq?] + ['vector vector] + ['vector? vector?] + ['hash-map hash-map] + ['map? map?] + ['assoc assoc] + ['dissoc dissoc] + ['get get] + ['contains? contains?] + ['keys keys] + ['vals vals] + + ['sequential? sequential?] + ['cons cons] + ['concat concat] + ['nth nth] + ['first first] + ['rest rest] + ['empty? empty?] + ['count count] + ['conj conj] + ['apply apply] + ['map #(doall (map %1 %2))] + + ['with-meta with-meta] + ['meta meta] + ['atom atom] + ['atom? atom?] + ['deref deref] + ['reset! reset!] + ['swap! swap!]]) diff --git a/clojure/src/env.clj b/clojure/src/env.clj new file mode 100644 index 0000000..7f1f934 --- /dev/null +++ b/clojure/src/env.clj @@ -0,0 +1,35 @@ +(ns env) + +(defn env [& [outer binds exprs]] + ;;(prn "env" binds exprs) + ;; (when (not= (count binds) (count exprs)) + ;; (throw (Exception. "Arity mistmatch in env call"))) + (atom + (loop [env {:outer outer} + b binds + e exprs] + (cond + (= nil b) + env + + (= '& (first b)) + (assoc env (nth b 1) e) + + :else + (recur (assoc env (first b) (first e)) (next b) (next e)))))) + +(defn env-find [env k] + (cond + (contains? @env k) env + (:outer @env) (env-find (:outer @env) k) + :else nil)) + +(defn env-get [env k] + (let [e (env-find env k)] + (when-not e + (throw (Exception. (str "'" k "' not found")))) + (get @e k))) + +(defn env-set [env k v] + (swap! env assoc k v) + v) diff --git a/clojure/src/printer.clj b/clojure/src/printer.clj new file mode 100644 index 0000000..656914f --- /dev/null +++ b/clojure/src/printer.clj @@ -0,0 +1,7 @@ +(ns printer) + +(defmethod clojure.core/print-method clojure.lang.Atom [a writer] + (.write writer "(atom ") + (.write writer (pr-str @a)) + (.write writer ")")) + diff --git a/clojure/src/step1_read_print.clj b/clojure/src/step1_read_print.clj index a99a0ed..31b2ac6 100644 --- a/clojure/src/step1_read_print.clj +++ b/clojure/src/step1_read_print.clj @@ -1,8 +1,8 @@ (ns step1-read-print (:require [clojure.repl] - [types] [readline] - [reader])) + [reader] + [printer])) ;; read (defn READ [& [strng]] diff --git a/clojure/src/step2_eval.clj b/clojure/src/step2_eval.clj index 6ff9eb3..581bf3f 100644 --- a/clojure/src/step2_eval.clj +++ b/clojure/src/step2_eval.clj @@ -1,10 +1,8 @@ (ns step2-eval (:require [clojure.repl] - [types] [readline] - [reader])) - -(declare EVAL) + [reader] + [printer])) ;; read (defn READ [& [strng]] @@ -12,6 +10,7 @@ (reader/read-string strng))) ;; eval +(declare EVAL) (defn eval-ast [ast env] (cond (symbol? ast) (or (get env ast) diff --git a/clojure/src/step3_env.clj b/clojure/src/step3_env.clj index c0c4e8e..449bf79 100644 --- a/clojure/src/step3_env.clj +++ b/clojure/src/step3_env.clj @@ -1,10 +1,9 @@ (ns step3-env (:require [clojure.repl] - [types] [readline] - [reader])) - -(declare EVAL) + [reader] + [printer] + [env])) ;; read (defn READ [& [strng]] @@ -12,9 +11,10 @@ (reader/read-string strng))) ;; eval +(declare EVAL) (defn eval-ast [ast env] (cond - (symbol? ast) (types/env-get env ast) + (symbol? ast) (env/env-get env ast) (seq? ast) (doall (map #(EVAL % env) ast)) @@ -34,12 +34,12 @@ (let [[a0 a1 a2 a3] ast] (condp = a0 'def! - (types/env-set env a1 (EVAL a2 env)) + (env/env-set env a1 (EVAL a2 env)) 'let* - (let [let-env (types/env env)] + (let [let-env (env/env env)] (doseq [[b e] (partition 2 a1)] - (types/env-set let-env b (EVAL e let-env))) + (env/env-set let-env b (EVAL e let-env))) (EVAL a2 let-env)) ;; apply @@ -52,12 +52,12 @@ (defn PRINT [exp] (pr-str exp)) ;; repl -(def repl-env (types/env)) +(def repl-env (env/env)) (defn rep [strng] (PRINT (EVAL (READ strng), repl-env))) -(defn _ref [k,v] (types/env-set repl-env k v)) +(defn _ref [k,v] (env/env-set repl-env k v)) (_ref '+ +) (_ref '- -) (_ref '* *) diff --git a/clojure/src/step4_if_fn_do.clj b/clojure/src/step4_if_fn_do.clj index 4171848..b36f482 100644 --- a/clojure/src/step4_if_fn_do.clj +++ b/clojure/src/step4_if_fn_do.clj @@ -1,10 +1,10 @@ (ns step4-if-fn-do (:require [clojure.repl] - [types] [readline] - [reader])) - -(declare EVAL) + [reader] + [printer] + [env] + [core])) ;; read (defn READ [& [strng]] @@ -12,9 +12,10 @@ (reader/read-string strng))) ;; eval +(declare EVAL) (defn eval-ast [ast env] (cond - (symbol? ast) (types/env-get env ast) + (symbol? ast) (env/env-get env ast) (seq? ast) (doall (map #(EVAL % env) ast)) @@ -34,12 +35,12 @@ (let [[a0 a1 a2 a3] ast] (condp = a0 'def! - (types/env-set env a1 (EVAL a2 env)) + (env/env-set env a1 (EVAL a2 env)) 'let* - (let [let-env (types/env env)] + (let [let-env (env/env env)] (doseq [[b e] (partition 2 a1)] - (types/env-set let-env b (EVAL e let-env))) + (env/env-set let-env b (EVAL e let-env))) (EVAL a2 let-env)) 'do @@ -55,7 +56,7 @@ 'fn* (fn [& args] - (EVAL a2 (types/env env a1 args))) + (EVAL a2 (env/env env a1 args))) ;; apply (let [el (eval-ast ast env) @@ -67,15 +68,15 @@ (defn PRINT [exp] (pr-str exp)) ;; repl -(def repl-env (types/env)) +(def repl-env (env/env)) (defn rep [strng] (PRINT (EVAL (READ strng), repl-env))) -(defn _ref [k,v] (types/env-set repl-env k v)) +(defn _ref [k,v] (env/env-set repl-env k v)) ;; Import types related functions -(doseq [[k v] types/types_ns] (_ref k v)) +(doseq [[k v] core/core_ns] (_ref k v)) ;; Defined using the language itself (rep "(def! not (fn* [a] (if a false true)))") diff --git a/clojure/src/step5_tco.clj b/clojure/src/step5_tco.clj index 2ed07b4..7739029 100644 --- a/clojure/src/step5_tco.clj +++ b/clojure/src/step5_tco.clj @@ -1,10 +1,10 @@ (ns step5-tco (:require [clojure.repl] - [types] [readline] - [reader])) - -(declare EVAL) + [reader] + [printer] + [env] + [core])) ;; read (defn READ [& [strng]] @@ -12,9 +12,10 @@ (reader/read-string strng))) ;; eval +(declare EVAL) (defn eval-ast [ast env] (cond - (symbol? ast) (types/env-get env ast) + (symbol? ast) (env/env-get env ast) (seq? ast) (doall (map #(EVAL % env) ast)) @@ -36,12 +37,12 @@ (let [[a0 a1 a2 a3] ast] (condp = a0 'def! - (types/env-set env a1 (EVAL a2 env)) + (env/env-set env a1 (EVAL a2 env)) 'let* - (let [let-env (types/env env)] + (let [let-env (env/env env)] (doseq [[b e] (partition 2 a1)] - (types/env-set let-env b (EVAL e let-env))) + (env/env-set let-env b (EVAL e let-env))) (EVAL a2 let-env)) 'do @@ -61,7 +62,7 @@ :environment env :parameters a1} (fn [& args] - (EVAL a2 (types/env env a1 args))) + (EVAL a2 (env/env env a1 args))) ;; apply (let [el (eval-ast ast env) @@ -69,22 +70,22 @@ args (rest el) {:keys [expression environment parameters]} (meta f)] (if expression - (recur expression (types/env environment parameters args)) + (recur expression (env/env environment parameters args)) (apply f args)))))))) ;; print (defn PRINT [exp] (pr-str exp)) ;; repl -(def repl-env (types/env)) +(def repl-env (env/env)) (defn rep [strng] (PRINT (EVAL (READ strng), repl-env))) -(defn _ref [k,v] (types/env-set repl-env k v)) +(defn _ref [k,v] (env/env-set repl-env k v)) ;; Import types related functions -(doseq [[k v] types/types_ns] (_ref k v)) +(doseq [[k v] core/core_ns] (_ref k v)) ;; Defined using the language itself (rep "(def! not (fn* [a] (if a false true)))") diff --git a/clojure/src/step6_file.clj b/clojure/src/step6_file.clj index 2eb4e3d..a65b741 100644 --- a/clojure/src/step6_file.clj +++ b/clojure/src/step6_file.clj @@ -1,10 +1,10 @@ (ns step6-file (:require [clojure.repl] - [types] [readline] - [reader])) - -(declare EVAL) + [reader] + [printer] + [env] + [core])) ;; read (defn READ [& [strng]] @@ -12,9 +12,10 @@ (reader/read-string strng))) ;; eval +(declare EVAL) (defn eval-ast [ast env] (cond - (symbol? ast) (types/env-get env ast) + (symbol? ast) (env/env-get env ast) (seq? ast) (doall (map #(EVAL % env) ast)) @@ -36,12 +37,12 @@ (let [[a0 a1 a2 a3] ast] (condp = a0 'def! - (types/env-set env a1 (EVAL a2 env)) + (env/env-set env a1 (EVAL a2 env)) 'let* - (let [let-env (types/env env)] + (let [let-env (env/env env)] (doseq [[b e] (partition 2 a1)] - (types/env-set let-env b (EVAL e let-env))) + (env/env-set let-env b (EVAL e let-env))) (EVAL a2 let-env)) 'do @@ -61,7 +62,7 @@ :environment env :parameters a1} (fn [& args] - (EVAL a2 (types/env env a1 args))) + (EVAL a2 (env/env env a1 args))) ;; apply (let [el (eval-ast ast env) @@ -69,22 +70,22 @@ args (rest el) {:keys [expression environment parameters]} (meta f)] (if expression - (recur expression (types/env environment parameters args)) + (recur expression (env/env environment parameters args)) (apply f args)))))))) ;; print (defn PRINT [exp] (pr-str exp)) ;; repl -(def repl-env (types/env)) +(def repl-env (env/env)) (defn rep [strng] - (PRINT (EVAL (READ strng), repl-env))) + (PRINT (EVAL (READ strng) repl-env))) -(defn _ref [k,v] (types/env-set repl-env k v)) +(defn _ref [k,v] (env/env-set repl-env k v)) ;; Import types related functions -(doseq [[k v] types/types_ns] (_ref k v)) +(doseq [[k v] core/core_ns] (_ref k v)) ;; Defined using the language itself (_ref 'read-string reader/read-string) diff --git a/clojure/src/step7_quote.clj b/clojure/src/step7_quote.clj index 398b7d2..e68a4cc 100644 --- a/clojure/src/step7_quote.clj +++ b/clojure/src/step7_quote.clj @@ -1,10 +1,10 @@ (ns step7-quote (:require [clojure.repl] - [types] [readline] - [reader])) - -(declare EVAL) + [reader] + [printer] + [env] + [core])) ;; read (defn READ [& [strng]] @@ -12,6 +12,7 @@ (reader/read-string strng))) ;; eval +(declare EVAL) (defn is-pair [x] (and (sequential? x) (> (count x) 0))) @@ -31,7 +32,7 @@ (defn eval-ast [ast env] (cond - (symbol? ast) (types/env-get env ast) + (symbol? ast) (env/env-get env ast) (seq? ast) (doall (map #(EVAL % env) ast)) @@ -53,12 +54,12 @@ (let [[a0 a1 a2 a3] ast] (condp = a0 'def! - (types/env-set env a1 (EVAL a2 env)) + (env/env-set env a1 (EVAL a2 env)) 'let* - (let [let-env (types/env env)] + (let [let-env (env/env env)] (doseq [[b e] (partition 2 a1)] - (types/env-set let-env b (EVAL e let-env))) + (env/env-set let-env b (EVAL e let-env))) (EVAL a2 let-env)) 'quote @@ -84,7 +85,7 @@ :environment env :parameters a1} (fn [& args] - (EVAL a2 (types/env env a1 args))) + (EVAL a2 (env/env env a1 args))) ;; apply (let [el (eval-ast ast env) @@ -92,22 +93,22 @@ args (rest el) {:keys [expression environment parameters]} (meta f)] (if expression - (recur expression (types/env environment parameters args)) + (recur expression (env/env environment parameters args)) (apply f args)))))))) ;; print (defn PRINT [exp] (pr-str exp)) ;; repl -(def repl-env (types/env)) +(def repl-env (env/env)) (defn rep [strng] (PRINT (EVAL (READ strng) repl-env))) -(defn _ref [k,v] (types/env-set repl-env k v)) +(defn _ref [k,v] (env/env-set repl-env k v)) ;; Import types related functions -(doseq [[k v] types/types_ns] (_ref k v)) +(doseq [[k v] core/core_ns] (_ref k v)) ;; Defined using the language itself (_ref 'read-string reader/read-string) diff --git a/clojure/src/step8_macros.clj b/clojure/src/step8_macros.clj index 6b696e7..ed5638b 100644 --- a/clojure/src/step8_macros.clj +++ b/clojure/src/step8_macros.clj @@ -1,11 +1,11 @@ (ns step8-macros (:refer-clojure :exclude [macroexpand]) (:require [clojure.repl] - [types] [readline] - [reader])) - -(declare EVAL) + [reader] + [printer] + [env] + [core])) ;; read (defn READ [& [strng]] @@ -13,6 +13,7 @@ (reader/read-string strng))) ;; eval +(declare EVAL) (defn is-pair [x] (and (sequential? x) (> (count x) 0))) @@ -33,19 +34,19 @@ (defn is-macro-call [ast env] (and (seq? ast) (symbol? (first ast)) - (types/env-find env (first ast)) - (:ismacro (meta (types/env-get env (first ast)))))) + (env/env-find env (first ast)) + (:ismacro (meta (env/env-get env (first ast)))))) (defn macroexpand [ast env] (loop [ast ast] (if (is-macro-call ast env) - (let [mac (types/env-get env (first ast))] + (let [mac (env/env-get env (first ast))] (recur (apply mac (rest ast)))) ast))) (defn eval-ast [ast env] (cond - (symbol? ast) (types/env-get env ast) + (symbol? ast) (env/env-get env ast) (seq? ast) (doall (map #(EVAL % env) ast)) @@ -71,12 +72,12 @@ (let [[a0 a1 a2 a3] ast] (condp = a0 'def! - (types/env-set env a1 (EVAL a2 env)) + (env/env-set env a1 (EVAL a2 env)) 'let* - (let [let-env (types/env env)] + (let [let-env (env/env env)] (doseq [[b e] (partition 2 a1)] - (types/env-set let-env b (EVAL e let-env))) + (env/env-set let-env b (EVAL e let-env))) (EVAL a2 let-env)) 'quote @@ -88,7 +89,7 @@ 'defmacro! (let [func (with-meta (EVAL a2 env) {:ismacro true})] - (types/env-set env a1 func)) + (env/env-set env a1 func)) 'macroexpand (macroexpand a1 env) @@ -110,7 +111,7 @@ :environment env :parameters a1} (fn [& args] - (EVAL a2 (types/env env a1 args))) + (EVAL a2 (env/env env a1 args))) ;; apply (let [el (eval-ast ast env) @@ -118,22 +119,22 @@ args (rest el) {:keys [expression environment parameters]} (meta f)] (if expression - (recur expression (types/env environment parameters args)) + (recur expression (env/env environment parameters args)) (apply f args)))))))))) ;; print (defn PRINT [exp] (pr-str exp)) ;; repl -(def repl-env (types/env)) +(def repl-env (env/env)) (defn rep [strng] (PRINT (EVAL (READ strng) repl-env))) -(defn _ref [k,v] (types/env-set repl-env k v)) +(defn _ref [k,v] (env/env-set repl-env k v)) ;; Import types related functions -(doseq [[k v] types/types_ns] (_ref k v)) +(doseq [[k v] core/core_ns] (_ref k v)) ;; Defined using the language itself (_ref 'read-string reader/read-string) diff --git a/clojure/src/step9_interop.clj b/clojure/src/step9_interop.clj index d78aed7..da37574 100644 --- a/clojure/src/step9_interop.clj +++ b/clojure/src/step9_interop.clj @@ -1,11 +1,11 @@ (ns step9-interop (:refer-clojure :exclude [macroexpand]) (:require [clojure.repl] - [types] [readline] - [reader])) - -(declare EVAL) + [reader] + [printer] + [env] + [core])) ;; read (defn READ [& [strng]] @@ -13,6 +13,7 @@ (reader/read-string strng))) ;; eval +(declare EVAL) (defn is-pair [x] (and (sequential? x) (> (count x) 0))) @@ -33,19 +34,19 @@ (defn is-macro-call [ast env] (and (seq? ast) (symbol? (first ast)) - (types/env-find env (first ast)) - (:ismacro (meta (types/env-get env (first ast)))))) + (env/env-find env (first ast)) + (:ismacro (meta (env/env-get env (first ast)))))) (defn macroexpand [ast env] (loop [ast ast] (if (is-macro-call ast env) - (let [mac (types/env-get env (first ast))] + (let [mac (env/env-get env (first ast))] (recur (apply mac (rest ast)))) ast))) (defn eval-ast [ast env] (cond - (symbol? ast) (types/env-get env ast) + (symbol? ast) (env/env-get env ast) (seq? ast) (doall (map #(EVAL % env) ast)) @@ -71,12 +72,12 @@ (let [[a0 a1 a2 a3] ast] (condp = a0 'def! - (types/env-set env a1 (EVAL a2 env)) + (env/env-set env a1 (EVAL a2 env)) 'let* - (let [let-env (types/env env)] + (let [let-env (env/env env)] (doseq [[b e] (partition 2 a1)] - (types/env-set let-env b (EVAL e let-env))) + (env/env-set let-env b (EVAL e let-env))) (EVAL a2 let-env)) 'quote @@ -88,7 +89,7 @@ 'defmacro! (let [func (with-meta (EVAL a2 env) {:ismacro true})] - (types/env-set env a1 func)) + (env/env-set env a1 func)) 'macroexpand (macroexpand a1 env) @@ -113,7 +114,7 @@ :environment env :parameters a1} (fn [& args] - (EVAL a2 (types/env env a1 args))) + (EVAL a2 (env/env env a1 args))) ;; apply (let [el (eval-ast ast env) @@ -121,22 +122,22 @@ args (rest el) {:keys [expression environment parameters]} (meta f)] (if expression - (recur expression (types/env environment parameters args)) + (recur expression (env/env environment parameters args)) (apply f args)))))))))) ;; print (defn PRINT [exp] (pr-str exp)) ;; repl -(def repl-env (types/env)) +(def repl-env (env/env)) (defn rep [strng] (PRINT (EVAL (READ strng) repl-env))) -(defn _ref [k,v] (types/env-set repl-env k v)) +(defn _ref [k,v] (env/env-set repl-env k v)) ;; Import types related functions -(doseq [[k v] types/types_ns] (_ref k v)) +(doseq [[k v] core/core_ns] (_ref k v)) ;; Defined using the language itself (_ref 'read-string reader/read-string) diff --git a/clojure/src/stepA_more.clj b/clojure/src/stepA_more.clj index d5d80bf..77b8cd7 100644 --- a/clojure/src/stepA_more.clj +++ b/clojure/src/stepA_more.clj @@ -1,11 +1,11 @@ (ns stepA-more (:refer-clojure :exclude [macroexpand]) (:require [clojure.repl] - [types] [readline] - [reader])) - -(declare EVAL) + [reader] + [printer] + [env] + [core])) ;; read (defn READ [& [strng]] @@ -13,6 +13,7 @@ (reader/read-string strng))) ;; eval +(declare EVAL) (defn is-pair [x] (and (sequential? x) (> (count x) 0))) @@ -33,19 +34,19 @@ (defn is-macro-call [ast env] (and (seq? ast) (symbol? (first ast)) - (types/env-find env (first ast)) - (:ismacro (meta (types/env-get env (first ast)))))) + (env/env-find env (first ast)) + (:ismacro (meta (env/env-get env (first ast)))))) (defn macroexpand [ast env] (loop [ast ast] (if (is-macro-call ast env) - (let [mac (types/env-get env (first ast))] + (let [mac (env/env-get env (first ast))] (recur (apply mac (rest ast)))) ast))) (defn eval-ast [ast env] (cond - (symbol? ast) (types/env-get env ast) + (symbol? ast) (env/env-get env ast) (seq? ast) (doall (map #(EVAL % env) ast)) @@ -71,12 +72,12 @@ (let [[a0 a1 a2 a3] ast] (condp = a0 'def! - (types/env-set env a1 (EVAL a2 env)) + (env/env-set env a1 (EVAL a2 env)) 'let* - (let [let-env (types/env env)] + (let [let-env (env/env env)] (doseq [[b e] (partition 2 a1)] - (types/env-set let-env b (EVAL e let-env))) + (env/env-set let-env b (EVAL e let-env))) (EVAL a2 let-env)) 'quote @@ -88,7 +89,7 @@ 'defmacro! (let [func (with-meta (EVAL a2 env) {:ismacro true})] - (types/env-set env a1 func)) + (env/env-set env a1 func)) 'macroexpand (macroexpand a1 env) @@ -101,13 +102,13 @@ (try (EVAL a1 env) (catch clojure.lang.ExceptionInfo ei - (EVAL (nth a2 2) (types/env env - [(nth a2 1)] - [(:data (ex-data ei))]))) + (EVAL (nth a2 2) (env/env env + [(nth a2 1)] + [(:data (ex-data ei))]))) (catch Throwable t - (EVAL (nth a2 2) (types/env env - [(nth a2 1)] - [(.getMessage t)])))) + (EVAL (nth a2 2) (env/env env + [(nth a2 1)] + [(.getMessage t)])))) (EVAL a1 env)) 'do @@ -127,7 +128,7 @@ :environment env :parameters a1} (fn [& args] - (EVAL a2 (types/env env a1 args))) + (EVAL a2 (env/env env a1 args))) ;; apply (let [el (eval-ast ast env) @@ -135,22 +136,22 @@ args (rest el) {:keys [expression environment parameters]} (meta f)] (if expression - (recur expression (types/env environment parameters args)) + (recur expression (env/env environment parameters args)) (apply f args)))))))))) ;; print (defn PRINT [exp] (pr-str exp)) ;; repl -(def repl-env (types/env)) +(def repl-env (env/env)) (defn rep [strng] (PRINT (EVAL (READ strng) repl-env))) -(defn _ref [k,v] (types/env-set repl-env k v)) +(defn _ref [k,v] (env/env-set repl-env k v)) ;; Import types related functions -(doseq [[k v] types/types_ns] (_ref k v)) +(doseq [[k v] core/core_ns] (_ref k v)) ;; Defined using the language itself (_ref 'readline readline/readline) diff --git a/clojure/src/types.clj b/clojure/src/types.clj deleted file mode 100644 index 922cf79..0000000 --- a/clojure/src/types.clj +++ /dev/null @@ -1,71 +0,0 @@ -(ns types) - -;; Custom printing - -(defmethod clojure.core/print-method clojure.lang.Atom [a writer] - (.write writer "(atom ") - (.write writer (pr-str @a)) - (.write writer ")")) - -;; Errors/exceptions -(defn mal_throw [obj] - (throw (ex-info "mal exception" {:data obj}))) - - -;; Atoms -(defn atom? [atm] - (= (type atm) clojure.lang.Atom)) - - -;; env - -(defn env [& [outer binds exprs]] - ;;(prn "env" binds exprs) - ;; (when (not= (count binds) (count exprs)) - ;; (throw (Exception. "Arity mistmatch in env call"))) - (atom - (loop [env {:outer outer} - b binds - e exprs] - (cond - (= nil b) - env - - (= '& (first b)) - (assoc env (nth b 1) e) - - :else - (recur (assoc env (first b) (first e)) (next b) (next e)))))) - -(defn env-find [env k] - (cond - (contains? @env k) env - (:outer @env) (env-find (:outer @env) k) - :else nil)) - -(defn env-get [env k] - (let [e (env-find env k)] - (when-not e - (throw (Exception. (str "'" k "' not found")))) - (get @e k))) - -(defn env-set [env k v] - (swap! env assoc k v) - v) - -(def types_ns - [['pr-str pr-str] ['str str] ['prn prn] ['println println] - ['with-meta with-meta] ['meta meta] ['= =] - ['nil? nil?] ['true? true?] ['false? false?] ['symbol? symbol?] - ['> >] ['>= >=] ['< <] ['<= <=] ['+ +] ['- -] ['* *] ['/ /] - ['hash-map hash-map] ['map? map?] - ['assoc assoc] ['dissoc dissoc] ['get get] - ['contains? contains?] ['keys keys] ['vals vals] - ['throw mal_throw] - ['list list] ['list? seq?] ['vector vector] ['vector? vector?] - ['atom atom] ['atom? atom?] ['deref deref] - ['reset! reset!] ['swap! swap!] - ['sequential? sequential?] ['cons cons] ['nth nth] - ['empty? empty?] ['count count] ['concat concat] - ['conj conj] ['first first] ['rest rest] - ['apply apply] ['map #(doall (map %1 %2))]]) |
