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- 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) "<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 "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,8 +422,6 @@
(defn- recur [ast ctx] (todo "recur"))
(defn- repeatt [ast ctx] (todo "repeat"))
(defn- testt [ast ctx] (todo "test"))
(defn- interpret* [ast ctx]
@ -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)
)