From b8e7777841b521b7886d4b73c5a7f44493826580 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Sun, 3 Apr 2022 19:13:54 -0400 Subject: [PATCH] Called KW enforce one arg --- src/ludus/interpreter.clj | 19 +++++++++++-------- src/ludus/parser.clj | 11 +++++++++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/ludus/interpreter.clj b/src/ludus/interpreter.clj index d00488f..f4b3d97 100644 --- a/src/ludus/interpreter.clj +++ b/src/ludus/interpreter.clj @@ -149,11 +149,14 @@ ;; TODO: clean this up ;; TODO: error with a passed tuple longer than 1 (if (= clojure.lang.Keyword (type fn)) - (if (::data/struct (second passed)) - (if (contains? (second passed) fn) - (fn (second passed)) - (throw (ex-info (str "Struct error: no member at " fn) {}))) - (get (second passed) fn)) + (if (= 2 (count passed)) + (let [target (second passed) kw fn] + (if (::data/struct target) + (if (contains? target kw) + (kw target) + (throw (ex-info (str "Struct error: no member at " kw) {}))) + (kw target))) + (throw (ex-info "Called keywords take a single argument" {}))) (throw (ex-info "I don't know how to call that" {:fn fn})))))) ;; TODO: add placeholder partial application @@ -260,9 +263,9 @@ (def source " - fn call (callable, target, thing) -> callable (target, thing) - fn id (x) -> x - call (:foo, nil, nil) + fn call (f, t) -> f (t) + + call (:foo, #{:foo 23}) ") diff --git a/src/ludus/parser.clj b/src/ludus/parser.clj index 68d324f..d46ee94 100644 --- a/src/ludus/parser.clj +++ b/src/ludus/parser.clj @@ -606,7 +606,14 @@ (do (def pp pp/pprint) - (def source "@{:foo :bar, :bar 42}") + (def source " + +fn maybe_foo (mf) -> if eq (mf, :foo) + then (:ok, :foo) + else (:error, mf) + + + ") (def lexed (scanner/scan source)) (def tokens (:tokens lexed)) (def p (parser tokens)) @@ -618,7 +625,7 @@ (println "*** *** NEW PARSE *** ***") (-> p - (parse-expr) + (parse-script) (::ast) (pp)))