fixed closure/function contexts work with forward declaration & mutual recursion

This commit is contained in:
Scott Richmond 2024-06-04 16:06:31 -04:00
parent f80aa7a8dc
commit a25ece5a68

View File

@ -359,8 +359,21 @@
# For now, this should be enough to tall the thing # For now, this should be enough to tall the thing
(defn- fnn [ast ctx] (defn- fnn [ast ctx]
(def {:name name :data clauses :doc doc} ast) (def {:name name :data clauses :doc doc} ast)
(def the-fn @{:name name :^type :fn :body clauses :ctx (table/to-struct ctx) :doc doc}) (print "defining fn " name)
(set (ctx name) the-fn)) (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 :_)) (defn- is_placeholder [x] (= x :_))
@ -612,17 +625,18 @@
# (when (has-errors? validated) (break (validated :errors))) # (when (has-errors? validated) (break (validated :errors)))
# (def cleaned (get-in parsed [:ast :data 1])) # (def cleaned (get-in parsed [:ast :data 1]))
# (pp cleaned) # (pp cleaned)
(interpret (parsed :ast) @{:^parent b/ctx}) # (interpret (parsed :ast) @{:^parent b/ctx})
# (try (interpret (parsed :ast) @{:^parent b/ctx}) (try (interpret (parsed :ast) @{:^parent b/ctx})
# ([e] (print "Ludus panicked!: " ([e] (print "Ludus panicked!: "
# (if (struct? e) (error (e :msg)) (error e))))) (if (struct? e) (error (e :msg)) (error e)))))
) )
(do (do
(set source ` (set source `
fn myadd (x, y) -> add (x, y) fn foo
let add = myadd(2, _) fn bar () -> foo ()
add (3) fn foo () -> :foo
bar ()
`) `)
(def result (run)) (def result (run))
(b/show result) (b/show result)