From 010b584ef1504d85bb2e9cddf0d37ad21b15bb6e Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Sun, 19 May 2024 19:35:41 -0400 Subject: [PATCH] partial function application! --- janet/interpreter.janet | 46 ++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/janet/interpreter.janet b/janet/interpreter.janet index 40ef568..bd761ea 100644 --- a/janet/interpreter.janet +++ b/janet/interpreter.janet @@ -11,8 +11,8 @@ (defn- todo [msg] (error (string "not yet implemented: " msg))) (defn- resolve-name [name ctx] - (print "resolving " name " in:") - (pp ctx) + # (print "resolving " name " in:") + # (pp ctx) (when (not ctx) (break :^not-found)) (if (has-key? ctx name) (ctx name) @@ -20,7 +20,7 @@ (defn- match-word [word value ctx] (def name (word :data)) - (print "matched " (b/show value) " to " name) + # (print "matched " (b/show value) " to " name) (set (ctx name) value) {:success true :ctx ctx}) @@ -321,14 +321,30 @@ (set (ctx name) the-fn)) # TODO -(defn- partial [the-fn args] (todo "partially applied functions")) +(defn- is_placeholder [x] (= x :_)) -(defn- call-fn [the-fn args] +(var call-fn nil) + +(defn- partial [the-fn partial-args] + (print "calling partially applied function") + (def args (partial-args :args)) + (def pos (find-index is_placeholder args)) + (def name (string (the-fn :name) "")) + (fn [missing] + (print "calling function with arg " (b/show missing)) + (pp partial-args) + (def full-args (array/slice args)) + (set (full-args pos) missing) + (print "all args: " (b/show full-args)) + (call-fn the-fn [;full-args]))) + +(defn- call-fn* [the-fn args] (print "calling " (b/show the-fn)) (print "with args " (b/show args)) (when (or (= :function (type the-fn)) (= :cfunction (type the-fn))) + (print "Janet function") (break (the-fn ;args))) (def clauses (the-fn :body)) (def len (length clauses)) @@ -353,11 +369,11 @@ (set (the-fn :match) match-fn) (match-fn 0 args)) +(set call-fn call-fn*) + (defn- apply-synth-term [prev curr] - (print "applying") - (pp curr) - (print "to") - (pp prev) + (print "applying " (b/show prev)) + (print "to" (b/show curr)) (def types [(b/ludus/type prev) (b/ludus/type curr)]) (print "typle:") (pp types) @@ -406,9 +422,7 @@ (defn- recur [ast ctx] (todo "recur")) -(defn- repeatt [ast ctx] (todo "repeat")) - -(defn - testt [ast ctx] (todo "test")) +(defn- testt [ast ctx] (todo "test")) (defn- interpret* [ast ctx] (print "interpreting node " (ast :type)) @@ -494,9 +508,13 @@ (do (set source ` - +fn foobar { + (:foo, :bar) -> :foobar! + (_, _) -> :not_foobar +} +let bar = foobar (:foo, _) +bar (:baz) `) (def result (run)) -(b/show result) )