Update struct match

This commit is contained in:
Scott Richmond 2023-05-31 11:51:02 -04:00
parent 3bd34f1269
commit 82a539a112

View File

@ -170,7 +170,6 @@
(let [ctx-diff (volatile! @ctx-vol) (let [ctx-diff (volatile! @ctx-vol)
splat? (= :splattern (-> members peek :type)) splat? (= :splattern (-> members peek :type))
length (count kws)] length (count kws)]
(if splat? (println "Pattern has splat!!"))
(loop [i 0] (loop [i 0]
(cond (cond
(> length i) (> length i)
@ -193,11 +192,9 @@
(let [splat (-> members peek) (let [splat (-> members peek)
splat-data (-> splat :data first) splat-data (-> splat :data first)
splat-type (-> splat-data :type)] splat-type (-> splat-data :type)]
(println "!!!!Matching splat")
(if (= :word splat-type) (if (= :word splat-type)
(let [unmatched (apply dissoc dict kws) (let [unmatched (assoc (apply dissoc dict kws) ::data/dict true)
match? (match splat-data unmatched ctx-diff)] match? (match splat-data unmatched ctx-diff)]
(println "Splatting " unmatched "\ninto " )
(if (:success match?) (if (:success match?)
{:success true :ctx (merge @ctx-diff (:ctx match?))} {:success true :ctx (merge @ctx-diff (:ctx match?))}
{:success false {:success false
@ -211,31 +208,59 @@
)))))) ))))))
;; TODO: update this to use new AST representation (defn- match-struct [pattern dict ctx-vol]
(defn- match-struct [pattern value ctx-vol] (let [members (:data pattern)
(cond pattern-map (pattern-to-map pattern)
(not (map? value)) kws (keys pattern-map)]
{:success false :reason "Could not match non-struct value to struct pattern"} (cond
(not (map? dict))
{:success false :reason "Could not match non-struct value to struct pattern"}
(not (::data/dict dict))
{:success false :reason "Cannot match non-struct value to struct pattern"}
(not (::data/struct value)) (empty? members)
{:success false :reason "Cannot match non-struct data types a struct pattern"} {:success true :ctx {}}
:else
(let [ctx-diff (volatile! @ctx-vol)
splat? (= :splattern (-> members peek :type))
length (count kws)]
(loop [i 0]
(cond
(> length i)
(let [kw (nth kws i)
pattern-at (kw pattern-map)
value (kw dict)]
(if (contains? dict kw)
(let [match? (match pattern-at value ctx-diff)]
(if (:success match?)
(do
(vswap! ctx-diff #(merge % (:ctx match?)))
(recur (inc i)))
{:success false
:reason (str "Could not match " pattern " with value " dict " at key " kw " because " (:reason match?))}
))
{:success false
:reason (str "Could not match " pattern " with " dict " at key " kw " because there is no value at " kw)}))
:else splat?
(let [members (:members pattern) (let [splat (-> members peek)
kws (keys members) splat-data (-> splat :data first)
ctx-diff (volatile! @ctx-vol)] splat-type (-> splat-data :type)]
(loop [i (dec (count kws))] (if (= :word splat-type)
(if (> 0 i) (let [unmatched (assoc (apply dissoc dict kws) ::data/dict true)
{:success true :ctx @ctx-diff} match? (match splat-data unmatched ctx-diff)]
(let [kw (nth kws i)] (if (:success match?)
(if (contains? value kw) {:success true :ctx (merge @ctx-diff (:ctx match?))}
(let [match? (match (kw members) (kw value) ctx-diff)] {:success false
(if (:success match?) :reason (str "Could not match " pattern " with value " dict " because " (:reason match?))}
(do ))
(vswap! ctx-diff #(merge % (:ctx match?))) {:success true :ctx @ctx-diff}
(recur (dec i))) ))
{:success false :reason (str "Could not match " pattern " with " value " at key " kw " because " (:reason match?))}))
{:success false :reason (str "Could not match " pattern " with " value " at key " kw ", because there is no value at " kw)}))))))) :else
{:success true :ctx @ctx-diff}))))))
(defn- match-typed [pattern value ctx] (defn- match-typed [pattern value ctx]
(let [data (:data pattern) (let [data (:data pattern)
@ -936,8 +961,8 @@
(do (do
(def source " (def source "
let #{a as :number} = #{:a 1} let #{...x} = #{:a 1}
a x
") ")
(println "") (println "")