Skip to content

Commit

Permalink
MATCH-61: only emit init expressions *once*
Browse files Browse the repository at this point in the history
Add a new dynamic var `*root*` which is set to true. When compiling we
now emit via `root-bind-node` the initial bindings for the match
expression only *once*. Remove binding hack from
`first-row-wildcards-case`.
  • Loading branch information
swannodette committed Jun 20, 2013
1 parent f96177e commit 7b02b83
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/main/clojure/clojure/core/match.clj
Expand Up @@ -70,6 +70,7 @@
`(throw clojure.core.match/backtrack))

(def ^{:dynamic true} *recur-backtrack* nil)
(def ^{:dynamic true} *root* true)

(defn warn [msg]
(if (not @*warned*)
Expand Down Expand Up @@ -651,14 +652,12 @@
[ocr ocr])))
ocrs))

(defn switch-or-bind-node [col ocrs clauses default]
(if (some expression? ocrs)
(let [bs (bind-variables ocrs)
ocr (ocrs col)
node (switch-node ocr clauses default)]
(bind-node bs node))
(let [ocr (ocrs col)]
(switch-node ocr clauses default))))
(defn root-bind-node [matrix]
(let [ocrs (occurrences matrix)
node (compile matrix)]
(if (some expression? ocrs)
(bind-node (bind-variables ocrs) node)
node)))

;; -----------------------------------------------------------------------------
;; # Compilation Cases
Expand Down Expand Up @@ -688,11 +687,8 @@
[rows ocrs]
(let [f (first rows)
a (:action f)
bs (row-bindings f ocrs)
node (leaf-node a bs)]
(if (some expression? ocrs)
(bind-node (bind-variables ocrs) node)
node)))
bs (row-bindings f ocrs)]
(leaf-node a bs)))

(defn expand-matrix [matrix col]
(reduce
Expand All @@ -714,10 +710,10 @@
(let [expanded (expand-matrix matrix col)
[S D B] (split-matrix expanded)]
(if-not *recur-present*
(switch-or-bind-node col ocrs
(switch-node (ocrs col)
(cases S)
(default-case D))
(switch-or-bind-node col ocrs
(switch-node (ocrs col)
(binding [*recur-backtrack* B]
(cases S))
(binding [*recur-backtrack* nil]
Expand All @@ -736,6 +732,10 @@ col with the first column and compile the result"

(defn compile [{:keys [rows ocrs] :as pm}]
(cond
*root*
(binding [*root* false]
(root-bind-node pm))

(empty? rows)
(empty-rows-case)

Expand Down
8 changes: 8 additions & 0 deletions src/test/clojure/clojure/core/match/test/core.clj
Expand Up @@ -827,3 +827,11 @@
[([h & t] :seq)] [h t]
[_] :a1))
:a1)))

(deftest match-61
(is (= (let [q '(a) y '(b) z '(c)]
(match [q (seq y) z]
[([_] :seq) _ _] 'a
[_ _ _] 'b))
'a)))

0 comments on commit 7b02b83

Please sign in to comment.