Skip to content

Commit

Permalink
analyzer: analyze empty collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronsa committed Apr 20, 2013
1 parent b8962c7 commit b6bcdf7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
:url "https://githubl.com/carthy/beard"
:license {:name "LGPL"
:url "http://www.gnu.org/copyleft/lesser.html"}
:dependencies [[org.clojure/clojure "1.4.0"]])
:dependencies [[org.clojure/clojure "1.5.1"]])
37 changes: 28 additions & 9 deletions src/brows/analyzer.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns brows.analyzer
(:refer-clojure :exclude [macroexpand-1 ns find-ns])
(:import (clojure.lang IPersistentVector IPersistentMap Keyword IDeref
ISeq IPersistentSet PersistentQueue Symbol LazySeq)))
ISeq IPersistentSet PersistentQueue Symbol LazySeq
IPersistentCollection)))

;; name: symbol name of the ns
;; aliases: map of sym -> ns (or maybe ns-sym?)
Expand Down Expand Up @@ -63,11 +64,29 @@
:else
:complex))

;; must check for empty
(defprotocol AnalyzableColl
(-analyze-coll [this env]))

(defn analyze-empty [form env]
{:op (case (class form)
IPersistentVector :empty-vector
IPersistentMap :empty-map
IPersistentSet :empty-set
IPeristentQueue :empty-queue
IPersistentList :empty-list)})

(extend-protocol Analyzable

IPersistentVector
IPersistentCollection
(-analyze [form env]
(if (empty? form)
(analyze-empty form env)
(-analyze-coll form env)))) ;; IRecord && IType?

(extend-protocol AnalyzableColl

IPersistentVector
(-analyze-coll [form env]
(let [items-env (or-eval env :expr)
items (mapv (analyze-in-env env) form)]
{:op :vector
Expand All @@ -76,7 +95,7 @@
(not (meta form)))})) ; if we support metadata for every type

IPersistentMap
(-analyze [form env]
(-analyze-coll [form env]
(let [kv-env (or-eval env :expr)
keys (keys env)
vals (vals form)
Expand All @@ -91,14 +110,14 @@
(not (meta form)))}))

IPersistentSet
(-analyze [form env]
(assoc (-analyze (vec form) env)
(-analyze-coll [form env]
(assoc (-analyze-coll (vec form) env)
:op :set))

PersistentQueue
(-analyze [form env]
(assoc (-analyze (vec form) env)
:op :queue))) ;; IRecord && IType?
(-analyze-coll [form env]
(assoc (-analyze-coll (vec form) env)
:op :queue)))

(defn find-ns [ns sym]
(let [curr-ns (get @namespaces ns)
Expand Down

0 comments on commit b6bcdf7

Please sign in to comment.