interpret loop & recur!

This commit is contained in:
Scott Richmond 2024-06-04 14:25:22 -04:00
parent 4da846d8d7
commit 27b688b96d
4 changed files with 62 additions and 24 deletions

View File

@ -151,7 +151,7 @@
(def ctx { (def ctx {
"print!" print! "print!" print!
"prn" prn "prn" prn
"eq" deep= "eq?" deep=
"bool" bool "bool" bool
"and" ludus/and "and" ludus/and
"or" ludus/or "or" ludus/or

View File

@ -460,9 +460,49 @@
# TODO for Computer Class # TODO for Computer Class
(defn- pkg [ast ctx] (todo "pkgs")) (defn- pkg [ast ctx] (todo "pkgs"))
(defn- loopp [ast ctx] (todo "loops")) (defn- loopp [ast ctx]
(print "looping!")
(def data (ast :data))
(def args (interpret (data 0) ctx))
(when (ast :match) (break ((ast :match) 0 args)))
(def clauses (data 1))
(def len (length clauses))
(def loop-ctx @{:^parent ctx})
(defn match-fn [i args]
(print "calling inner loop fn")
(print "for the " i "th time")
(when (= len i)
(error {:node ast :value args :msg "no match"}))
(def clause (clauses i))
(def [patt guard expr] clause)
(def match?
(match-pattern patt args loop-ctx))
(when (not (match? :success))
(print "no match")
(break (match-fn (inc i) args)))
(print "matched!")
(def body-ctx (match? :ctx))
(def guard? (if guard
(b/bool (interpret guard body-ctx)) true))
(print "passed guard")
(when (not guard?)
(break (match-fn (inc i) args)))
(interpret expr body-ctx))
(set (ast :match) match-fn)
(set (loop-ctx :^recur) match-fn)
(print "ATTACHED MATCH-FN")
(match-fn 0 args))
(defn- recur [ast ctx] (todo "recur")) (defn- recur [ast ctx]
(print "recurring!")
(def passed (ast :data))
(def args (interpret passed ctx))
(def match-fn (resolve-name :^recur ctx))
(print "match fn in ctx:")
(pp (ctx :^recur))
(pp match-fn)
(pp ctx)
(match-fn 0 args))
# TODO for 0.1.0 # TODO for 0.1.0
(defn- testt [ast ctx] (todo "test")) (defn- testt [ast ctx] (todo "test"))
@ -553,21 +593,21 @@
# (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 foo { loop (10) with {
"a doc, another doc" (0) -> :done!
() -> :foo (x) -> {
(_) -> :bar print! (x)
recur (dec (x))
}
} }
doc! (foo)
`) `)
(def result (run)) (def result (run))
(b/show result) (b/show result)

View File

@ -935,7 +935,7 @@
(expect parser :loop) (advance parser) (expect parser :loop) (advance parser)
(def args (tup parser)) (def args (tup parser))
(expect parser :with) (advance parser) (expect parser :with) (advance parser)
(def clauses (case (-> parser current type) (def {:clauses clauses} (case (-> parser current type)
:lparen (fn-simple parser) :lparen (fn-simple parser)
:lbrace (fn-clauses parser) :lbrace (fn-clauses parser)
)) ))
@ -1132,14 +1132,14 @@
) )
# (do (do
(comment # (comment
(def source `(foo as :bar, 12, :foo, _thing) (def source `recur (x)
`) `)
(def scanned (s/scan source)) (def scanned (s/scan source))
(print "\n***NEW PARSE***\n") (print "\n***NEW PARSE***\n")
(def a-parser (new-parser scanned)) (def a-parser (new-parser scanned))
(def parsed (pattern a-parser)) (def parsed (recur a-parser))
) )

View File

@ -768,16 +768,14 @@ Deferred until a later iteration of Ludus:
(import ./base :as b) (import ./base :as b)
(do # (do
# (comment (comment
(def source ` (def source `
fn foo dec (12)
fn bar () -> foo ()
fn foo () -> bar ()
`) `)
(def scanned (s/scan source)) (def scanned (s/scan source))
(def parsed (p/parse scanned)) (def parsed (p/parse scanned))
(def validated (valid parsed)) (def validated (valid parsed b/ctx))
# (get-in validated [:status :declared]) # (get-in validated [:status :declared])
# (validated :ctx) # (validated :ctx)
) )