Permalink
Browse files

remove PatternRow accessors, implement ILookup instead

  • Loading branch information...
1 parent 5983814 commit 14b7ee5e02d0301497558e8ec926643fc9277d4c @swannodette swannodette committed May 6, 2013
Showing with 20 additions and 18 deletions.
  1. +18 −16 src/main/clojure/clojure/core/match.clj
  2. +2 −2 src/main/clojure/clojure/core/match/debug.clj
@@ -295,9 +295,6 @@
;;
(defprotocol IPatternRow
- (patterns [this])
- (action [this])
- (bindings [this])
(update-pattern [this i p])
(all-wildcards? [this])
(drop-nth-bind [this n bind-expr])) ;; TODO: needs better name - David
@@ -306,11 +303,8 @@
(deftype PatternRow [ps action bindings]
IPatternRow
- (action [_] action)
- (patterns [_] ps)
(update-pattern [_ i p]
(PatternRow. (assoc ps i p) action bindings))
- (bindings [_] bindings)
(all-wildcards? [this]
(every? wildcard-pattern? ps))
(drop-nth-bind [this n ocr]
@@ -355,14 +349,23 @@
(seq ps))
(count [_]
(count ps))
+ clojure.lang.ILookup
+ (valAt [this k]
+ (.valAt this k nil))
+ (valAt [this k not-found]
+ (case k
+ :ps ps
+ :action action
+ :bindings bindings
+ not-found))
clojure.lang.IFn
(invoke [_ n]
(nth ps n))
clojure.lang.IPersistentCollection
(cons [_ x]
(PatternRow. (conj ps x) action bindings)))
-(defn ^PatternRow pattern-row
+(defn pattern-row
([ps action]
{:pre [(vector? ps)]}
(PatternRow. ps action nil))
@@ -538,9 +541,9 @@
"Case 2: If the first row is empty then matching always succeeds
and yields the first action."
[rows ocr]
- (let [^PatternRow f (first rows)
- a (action f)
- bs (bindings f)
+ (let [f (first rows)
+ a (:action f)
+ bs (:bindings f)
_ (trace-dag "Empty row, add leaf-node."
"Could not find match for: " ocr
"Action:" a
@@ -555,14 +558,13 @@
(letfn [(row-bindings
;; Returns bindings usable by leaf-node
[f ocrs]
- (let [ps (.ps ^PatternRow f)
+ (let [ps (:ps f)
wc-syms (map :sym ps)
wc-bindings (map vector wc-syms
(map leaf-bind-expr ocrs))]
- (concat (bindings f)
- wc-bindings)))]
+ (concat (:bindings f) wc-bindings)))]
(let [f (first rows)
- a (action f)
+ a (:action f)
bs (row-bindings f ocrs)
_ (trace-dag (str "First row all wildcards, add leaf-node." a bs))]
(leaf-node a bs))))
@@ -725,7 +727,7 @@
(zero? i))
(empty-row? [row]
- (let [ps (patterns row)]
+ (let [ps (:ps row)]
(and (not (nil? ps))
(empty? ps))))]
(cond
@@ -782,7 +784,7 @@
(occurrences [_] ocrs)
(action-for-row [_ j]
- (action (rows j)))
+ (:action (rows j)))
IVecMod
(drop-nth [_ i]
@@ -2,7 +2,7 @@
(:refer-clojure :exclude [compile])
(:use [clojure.core.match
:only [emit-matrix compile occurrences
- rows patterns action-for-row n-to-clj clj-form]])
+ rows action-for-row n-to-clj clj-form]])
(:require [clojure.pprint :as pp]))
(defn source-pprint [source]
@@ -47,7 +47,7 @@
(prn)
(doseq [[i row] (map-indexed (fn [p i] [p i]) (rows pm))]
(print "|")
- (doseq [p (patterns row)]
+ (doseq [p (:ps row)]
(pp/cl-format true "~4D~7,vT" (str p) col-width))
(print "|")
(print " " (action-for-row pm i))

0 comments on commit 14b7ee5

Please sign in to comment.