interpret forward-declared functions, allowing mutual recursion

This commit is contained in:
Scott Richmond 2024-06-04 11:54:29 -04:00
parent bc1eac46b8
commit bbd41a0f74

View File

@ -362,7 +362,6 @@
(def the-fn @{:name name :^type :fn :body clauses :ctx ctx}) (def the-fn @{:name name :^type :fn :body clauses :ctx ctx})
(set (ctx name) the-fn)) (set (ctx name) the-fn))
# TODO
(defn- is_placeholder [x] (= x :_)) (defn- is_placeholder [x] (= x :_))
(var call-fn nil) (var call-fn nil)
@ -389,6 +388,8 @@
(print "Janet function") (print "Janet function")
(break (the-fn ;args))) (break (the-fn ;args)))
(def clauses (the-fn :body)) (def clauses (the-fn :body))
(when (= :nothing clauses)
(error {:node the-fn :value args :msg "cannot call function before it is defined"}))
(def len (length clauses)) (def len (length clauses))
(when (the-fn :match) (break ((the-fn :match) 0 args))) (when (the-fn :match) (break ((the-fn :match) 0 args)))
(defn match-fn [i args] (defn match-fn [i args]
@ -552,15 +553,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) @{}) (try (interpret (parsed :ast) @{})
# ([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 `
let #{:a ay, :b (:ok, bee), ...c} = #{:a 1, :b (:ok, 42), :c 3} fn foo
fn bar () -> foo ()
fn foo () -> :foo
bar ()
`) `)
(def result (run)) (def result (run))
# (b/show result) # (b/show result)