From a25ece5a6889d96d5b24eec267776a10976c6dad Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Tue, 4 Jun 2024 16:06:31 -0400 Subject: [PATCH] fixed closure/function contexts work with forward declaration & mutual recursion --- janet/interpreter.janet | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/janet/interpreter.janet b/janet/interpreter.janet index 25a5eeb..9013e6f 100644 --- a/janet/interpreter.janet +++ b/janet/interpreter.janet @@ -359,8 +359,21 @@ # 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 (table/to-struct ctx) :doc doc}) - (set (ctx name) the-fn)) + (print "defining fn " name) + (def closure (table/to-struct ctx)) + (def the-fn @{:name name :^type :fn :body clauses :ctx closure :doc doc}) + (when (not= :^not-found (resolve-name name ctx)) + (print "fn "name" was forward declared") + (def fwd (resolve-name name ctx)) + (set (fwd :body) clauses) + (set (fwd :ctx) closure) + (set (fwd :doc) doc) + (print "fn " name " has been defined") + (pp fwd) + (break fwd)) + (pp the-fn) + (set (ctx name) the-fn) + the-fn) (defn- is_placeholder [x] (= x :_)) @@ -612,17 +625,18 @@ # (when (has-errors? validated) (break (validated :errors))) # (def cleaned (get-in parsed [:ast :data 1])) # (pp cleaned) - (interpret (parsed :ast) @{:^parent b/ctx}) - # (try (interpret (parsed :ast) @{:^parent b/ctx}) - # ([e] (print "Ludus panicked!: " - # (if (struct? e) (error (e :msg)) (error e))))) + # (interpret (parsed :ast) @{:^parent b/ctx}) + (try (interpret (parsed :ast) @{:^parent b/ctx}) + ([e] (print "Ludus panicked!: " + (if (struct? e) (error (e :msg)) (error e))))) ) (do (set source ` -fn myadd (x, y) -> add (x, y) -let add = myadd(2, _) -add (3) +fn foo +fn bar () -> foo () +fn foo () -> :foo +bar () `) (def result (run)) (b/show result)