aboutsummaryrefslogtreecommitdiff
path: root/clojure/src/types.clj
blob: 922cf796d8307f2c26bc15e52e6e2d72b4ab5d04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(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))]])