From 34da4dd6fe9dd2ed2d15a3bbc735442828f62a4f Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Thu, 19 May 2022 20:00:16 -0400 Subject: [PATCH] Actually add hashmap patterns. Whoops. --- src/ludus/interpreter.clj | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/ludus/interpreter.clj b/src/ludus/interpreter.clj index e71a231..d33cb79 100644 --- a/src/ludus/interpreter.clj +++ b/src/ludus/interpreter.clj @@ -70,10 +70,24 @@ (not (map? value)) {:success false :reason "Could not match non-hashmap value to hashmap pattern"} - (not) + (not (::data/hashmap value)) + {:success false :reason "Cannot match non-hashmap data types a hashmap pattern"} - ) - ) + :else + (let [members (:members pattern) + kws (keys members)] + (loop [i (dec (count kws)) ctx {}] + (if (> 0 i) + {:success true :ctx ctx} + (let [kw (nth kws i)] + (if (contains? value kw) + (let [match? (match (kw members) (kw value) ctx-vol)] + (if (:success match?) + (recur (dec i) (merge ctx (:ctx match?))) + {:success false :reason (str "Could not match " pattern " with " value " at key " kw)} + )) + {:success false :reason (str "Could not match " pattern " with " value " at key " kw)} + ))))))) (defn- match [pattern value ctx-vol] (let [ctx @ctx-vol] @@ -97,6 +111,8 @@ ::ast/list (match-list pattern value ctx-vol) + ::ast/hash (match-hashmap pattern value ctx-vol) + (throw (ex-info "Unknown pattern on line " {:pattern pattern}))))) (defn- update-ctx [ctx new-ctx] @@ -445,7 +461,7 @@ (def source " - + let #{foo, bar} = @{:foo :bar, :bar nil} ")