allow defprotocolpath to be defined with no params argument for consistency with declarepath

This commit is contained in:
Nathan Marz 2016-01-31 10:01:08 -08:00
parent b16dbdfdd2
commit 8293f68696
2 changed files with 80 additions and 78 deletions

View file

@ -2,6 +2,7 @@
* ALL on maps auto-coerces MapEntry to vector, enabling smoother transformation of map keys * ALL on maps auto-coerces MapEntry to vector, enabling smoother transformation of map keys
* declarepath can now be parameterized * declarepath can now be parameterized
* Added params-reset which calls its path with the params index reset to 0. This enables recursive parameterized paths * Added params-reset which calls its path with the params index reset to 0. This enables recursive parameterized paths
* Added convenience syntax for defprotocolpath with no params, e.g. (defprotocolpath foo)
## 0.9.2 ## 0.9.2
* Added VOID selector which navigates nowhere * Added VOID selector which navigates nowhere

View file

@ -205,91 +205,92 @@
(defn- protpath-sym [name] (defn- protpath-sym [name]
(-> name (str "-prot") symbol)) (-> name (str "-prot") symbol))
(defmacro defprotocolpath [name params] (defmacro defprotocolpath
(let [prot-name (protpath-sym name) ([name]
m (-> name (str "-retrieve") symbol) `(defprotocolpath ~name []))
num-params (count params) ([name params]
ssym (gensym "structure") (let [prot-name (protpath-sym name)
rargs [(gensym "params") (gensym "pidx") (gensym "vals") ssym (gensym "next-fn")] m (-> name (str "-retrieve") symbol)
retrieve `(~m ~ssym) num-params (count params)
] ssym (gensym "structure")
`(do rargs [(gensym "params") (gensym "pidx") (gensym "vals") ssym (gensym "next-fn")]
(defprotocol ~prot-name (~m [structure#])) retrieve `(~m ~ssym)
(def ~name ]
(if (= ~num-params 0) `(do
(no-params-compiled-path (defprotocol ~prot-name (~m [structure#]))
(->TransformFunctions (def ~name
RichPathExecutor (if (= ~num-params 0)
(fn ~rargs (no-params-compiled-path
(let [path# ~retrieve (->TransformFunctions
selector# (compiled-selector path#)] RichPathExecutor
(selector# ~@rargs) (fn ~rargs
)) (let [path# ~retrieve
(fn ~rargs selector# (compiled-selector path#)]
(let [path# ~retrieve (selector# ~@rargs)
transformer# (compiled-transformer path#)] ))
(transformer# ~@rargs) (fn ~rargs
)))) (let [path# ~retrieve
(->ParamsNeededPath transformer# (compiled-transformer path#)]
(->TransformFunctions (transformer# ~@rargs)
RichPathExecutor ))))
(fn ~rargs (->ParamsNeededPath
(let [path# ~retrieve (->TransformFunctions
selector# (params-needed-selector path#)] RichPathExecutor
(selector# ~@rargs) (fn ~rargs
)) (let [path# ~retrieve
(fn ~rargs selector# (params-needed-selector path#)]
(let [path# ~retrieve (selector# ~@rargs)
transformer# (params-needed-transformer path#)] ))
(transformer# ~@rargs) (fn ~rargs
))) (let [path# ~retrieve
~num-params transformer# (params-needed-transformer path#)]
) (transformer# ~@rargs)
))))) )))
~num-params
)
))))))
(defn declared-name [name] (defn declared-name [name]
(symbol (str name "-declared"))) (symbol (str name "-declared")))
(defn- declarepath* [name num-params]
(let [declared (declared-name name)
rargs [(gensym "params") (gensym "pidx") (gensym "vals")
(gensym "structure") (gensym "next-fn")]]
`(do
(declare ~declared)
(def ~name
(if (= ~num-params 0)
(no-params-compiled-path
(->TransformFunctions
RichPathExecutor
(fn ~rargs
(let [selector# (compiled-selector ~declared)]
(selector# ~@rargs)
))
(fn ~rargs
(let [transformer# (compiled-transformer ~declared)]
(transformer# ~@rargs)
))))
(->ParamsNeededPath
(->TransformFunctions
RichPathExecutor
(fn ~rargs
(let [selector# (params-needed-selector ~declared)]
(selector# ~@rargs)
))
(fn ~rargs
(let [transformer# (params-needed-transformer ~declared)]
(transformer# ~@rargs)
)))
~num-params
)
)))))
(defmacro declarepath (defmacro declarepath
([name] (declarepath* name 0)) ([name]
`(declarepath ~name []))
([name params] ([name params]
(declarepath* name (count params)))) (let [num-params (count params)
declared (declared-name name)
rargs [(gensym "params") (gensym "pidx") (gensym "vals")
(gensym "structure") (gensym "next-fn")]]
`(do
(declare ~declared)
(def ~name
(if (= ~num-params 0)
(no-params-compiled-path
(->TransformFunctions
RichPathExecutor
(fn ~rargs
(let [selector# (compiled-selector ~declared)]
(selector# ~@rargs)
))
(fn ~rargs
(let [transformer# (compiled-transformer ~declared)]
(transformer# ~@rargs)
))))
(->ParamsNeededPath
(->TransformFunctions
RichPathExecutor
(fn ~rargs
(let [selector# (params-needed-selector ~declared)]
(selector# ~@rargs)
))
(fn ~rargs
(let [transformer# (params-needed-transformer ~declared)]
(transformer# ~@rargs)
)))
~num-params
)
))))))
(defmacro providepath [name apath] (defmacro providepath [name apath]
`(let [comped# (comp-paths* ~apath) `(let [comped# (comp-paths* ~apath)