From f80aa7a8dcb568a3816287a43669080243b0e6d8 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Tue, 4 Jun 2024 15:52:24 -0400 Subject: [PATCH] function contexts are fixed at declaration --- janet/base.janet | 1 + janet/interpreter.janet | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/janet/base.janet b/janet/base.janet index 9d55281..e1dd742 100644 --- a/janet/base.janet +++ b/janet/base.janet @@ -46,6 +46,7 @@ (string/join (map stringify (keys value)) ", ") :ref (stringify (value :^value)) :fn (string "fn " (value :name)) + :applied (string "fn " (value :name)) :function (string "builtin " (string value)) :pkg (dict-str value) )) diff --git a/janet/interpreter.janet b/janet/interpreter.janet index 942f937..25a5eeb 100644 --- a/janet/interpreter.janet +++ b/janet/interpreter.janet @@ -359,25 +359,30 @@ # For now, this should be enough to tall the thing (defn- fnn [ast ctx] (def {:name name :data clauses :doc doc} ast) - (def the-fn @{:name name :^type :fn :body clauses :ctx ctx :doc doc}) + (def the-fn @{:name name :^type :fn :body clauses :ctx (table/to-struct ctx) :doc doc}) (set (ctx name) the-fn)) (defn- is_placeholder [x] (= x :_)) (var call-fn nil) +(def name "foo") +(eval ~(fn ,(symbol name) [] :foo)) + (defn- partial [the-fn partial-args] (print "calling partially applied function") (def args (partial-args :args)) + (pp args) (def pos (find-index is_placeholder args)) - (def name (string (the-fn :name) "")) - (fn [missing] + (def name (string (the-fn :name) " *partial*")) + (defn 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]))) + (call-fn the-fn [;full-args])) + {:^type :applied :name name :body partial-fn}) (defn- call-fn* [the-fn args] (print "calling " (b/show the-fn)) @@ -414,6 +419,8 @@ (set call-fn call-fn*) +(defn- call-partial [the-fn arg] ((the-fn :body) ;arg)) + (defn- apply-synth-term [prev curr] (print "applying " (b/show prev)) (print "to" (b/show curr)) @@ -424,6 +431,7 @@ [:fn :tuple] (call-fn prev curr) [:fn :partial] (partial prev curr) [:function :tuple] (call-fn prev curr) + [:applied :tuple] (call-partial prev curr) [:keyword :args] (get (first curr) prev :^nil) [:dict :keyword] (get prev curr :^nil) [:nil :keyword] :^nil @@ -457,7 +465,6 @@ (def last-fn (interpret last-term ctx)) (call-fn last-fn [prev])) -# TODO for Computer Class (defn- pkg [ast ctx] (def members (ast :data)) (def the-pkg @{:^name (ast :name) :^type :pkg}) @@ -613,10 +620,9 @@ (do (set source ` -let foo = 42 -let bar = :bar -pkg Baz {foo, bar} -Baz :fool +fn myadd (x, y) -> add (x, y) +let add = myadd(2, _) +add (3) `) (def result (run)) (b/show result)