Hashmap -> dict
This commit is contained in:
parent
a5b1763324
commit
3292c0676c
|
@ -68,13 +68,13 @@
|
||||||
(recur (dec i) (merge ctx (:ctx match?)))
|
(recur (dec i) (merge ctx (:ctx match?)))
|
||||||
{:success false :reason (str "Could not match " pattern " with " value)})))))))
|
{:success false :reason (str "Could not match " pattern " with " value)})))))))
|
||||||
|
|
||||||
(defn- match-hashmap [pattern value ctx-vol]
|
(defn- match-dict [pattern value ctx-vol]
|
||||||
(cond
|
(cond
|
||||||
(not (map? value))
|
(not (map? value))
|
||||||
{:success false :reason "Could not match non-hashmap value to hashmap pattern"}
|
{:success false :reason "Could not match non-dict value to dict pattern"}
|
||||||
|
|
||||||
(not (::data/hashmap value))
|
(not (::data/dict value))
|
||||||
{:success false :reason "Cannot match non-hashmap data types a hashmap pattern"}
|
{:success false :reason "Cannot match non-dict data types a dict pattern"}
|
||||||
|
|
||||||
:else
|
:else
|
||||||
(let [members (:members pattern)
|
(let [members (:members pattern)
|
||||||
|
@ -138,7 +138,7 @@
|
||||||
|
|
||||||
::ast/list (match-list pattern value ctx-vol)
|
::ast/list (match-list pattern value ctx-vol)
|
||||||
|
|
||||||
::ast/hash (match-hashmap pattern value ctx-vol)
|
::ast/hash (match-dict pattern value ctx-vol)
|
||||||
|
|
||||||
::ast/struct (match-struct pattern value ctx-vol)
|
::ast/struct (match-struct pattern value ctx-vol)
|
||||||
|
|
||||||
|
@ -421,22 +421,22 @@
|
||||||
(let [members (:members ast)]
|
(let [members (:members ast)]
|
||||||
(reduce (set-term ctx) #{} members)))
|
(reduce (set-term ctx) #{} members)))
|
||||||
|
|
||||||
(defn- hash-term [ctx]
|
(defn- dict-term [ctx]
|
||||||
(fn [hash member]
|
(fn [dict member]
|
||||||
(if (= (::ast/type member) ::ast/splat)
|
(if (= (::ast/type member) ::ast/splat)
|
||||||
(let [splatted (interpret-ast (:expr member) ctx)
|
(let [splatted (interpret-ast (:expr member) ctx)
|
||||||
splat-map? (and
|
splat-map? (and
|
||||||
(map? splatted)
|
(map? splatted)
|
||||||
(::data/hashmap splatted))]
|
(::data/dict splatted))]
|
||||||
(if splat-map?
|
(if splat-map?
|
||||||
(merge hash splatted)
|
(merge dict splatted)
|
||||||
(throw (ex-info "Cannot splat non-hashmap into hashmap" {:ast member}))))
|
(throw (ex-info "Cannot splat non-dict into dict" {:ast member}))))
|
||||||
(let [k (first member) v (second member)]
|
(let [k (first member) v (second member)]
|
||||||
(assoc hash k (interpret-ast v ctx))))))
|
(assoc dict k (interpret-ast v ctx))))))
|
||||||
|
|
||||||
(defn- interpret-hash [ast ctx]
|
(defn- interpret-dict [ast ctx]
|
||||||
(let [members (:members ast)]
|
(let [members (:members ast)]
|
||||||
(assoc (reduce (hash-term ctx) {} members) ::data/hashmap true)
|
(assoc (reduce (dict-term ctx) {} members) ::data/dict true)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -506,7 +506,7 @@
|
||||||
::ast/set
|
::ast/set
|
||||||
(interpret-set ast ctx)
|
(interpret-set ast ctx)
|
||||||
|
|
||||||
::ast/hash (interpret-hash ast ctx)
|
::ast/hash (interpret-dict ast ctx)
|
||||||
|
|
||||||
::ast/struct
|
::ast/struct
|
||||||
(let [members (:members ast)]
|
(let [members (:members ast)]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user