partial function application!

This commit is contained in:
Scott Richmond 2024-05-19 19:35:41 -04:00
parent ba1aa8ed03
commit 010b584ef1

View File

@ -11,8 +11,8 @@
(defn- todo [msg] (error (string "not yet implemented: " msg))) (defn- todo [msg] (error (string "not yet implemented: " msg)))
(defn- resolve-name [name ctx] (defn- resolve-name [name ctx]
(print "resolving " name " in:") # (print "resolving " name " in:")
(pp ctx) # (pp ctx)
(when (not ctx) (break :^not-found)) (when (not ctx) (break :^not-found))
(if (has-key? ctx name) (if (has-key? ctx name)
(ctx name) (ctx name)
@ -20,7 +20,7 @@
(defn- match-word [word value ctx] (defn- match-word [word value ctx]
(def name (word :data)) (def name (word :data))
(print "matched " (b/show value) " to " name) # (print "matched " (b/show value) " to " name)
(set (ctx name) value) (set (ctx name) value)
{:success true :ctx ctx}) {:success true :ctx ctx})
@ -321,14 +321,30 @@
(set (ctx name) the-fn)) (set (ctx name) the-fn))
# TODO # 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) "<partial>"))
(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 "calling " (b/show the-fn))
(print "with args " (b/show args)) (print "with args " (b/show args))
(when (or (when (or
(= :function (type the-fn)) (= :function (type the-fn))
(= :cfunction (type the-fn))) (= :cfunction (type the-fn)))
(print "Janet function")
(break (the-fn ;args))) (break (the-fn ;args)))
(def clauses (the-fn :body)) (def clauses (the-fn :body))
(def len (length clauses)) (def len (length clauses))
@ -353,11 +369,11 @@
(set (the-fn :match) match-fn) (set (the-fn :match) match-fn)
(match-fn 0 args)) (match-fn 0 args))
(set call-fn call-fn*)
(defn- apply-synth-term [prev curr] (defn- apply-synth-term [prev curr]
(print "applying") (print "applying " (b/show prev))
(pp curr) (print "to" (b/show curr))
(print "to")
(pp prev)
(def types [(b/ludus/type prev) (b/ludus/type curr)]) (def types [(b/ludus/type prev) (b/ludus/type curr)])
(print "typle:") (print "typle:")
(pp types) (pp types)
@ -406,8 +422,6 @@
(defn- recur [ast ctx] (todo "recur")) (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] (defn- interpret* [ast ctx]
@ -494,9 +508,13 @@
(do (do
(set source ` (set source `
fn foobar {
(:foo, :bar) -> :foobar!
(_, _) -> :not_foobar
}
let bar = foobar (:foo, _)
bar (:baz)
`) `)
(def result (run)) (def result (run))
(b/show result)
) )