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 {
"print!" print!
"prn" prn
"eq" deep=
"eq?" deep=
"bool" bool
"and" ludus/and
"or" ludus/or

View File

@ -460,9 +460,49 @@
# TODO for Computer Class
(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
(defn- testt [ast ctx] (todo "test"))
@ -553,21 +593,21 @@
# (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 foo {
"a doc, another doc"
() -> :foo
(_) -> :bar
loop (10) with {
(0) -> :done!
(x) -> {
print! (x)
recur (dec (x))
}
}
doc! (foo)
`)
(def result (run))
(b/show result)

View File

@ -935,7 +935,7 @@
(expect parser :loop) (advance parser)
(def args (tup parser))
(expect parser :with) (advance parser)
(def clauses (case (-> parser current type)
(def {:clauses clauses} (case (-> parser current type)
:lparen (fn-simple parser)
:lbrace (fn-clauses parser)
))
@ -1132,14 +1132,14 @@
)
# (do
(comment
(def source `(foo as :bar, 12, :foo, _thing)
(do
# (comment
(def source `recur (x)
`)
(def scanned (s/scan source))
(print "\n***NEW PARSE***\n")
(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)
(do
# (comment
# (do
(comment
(def source `
fn foo
fn bar () -> foo ()
fn foo () -> bar ()
dec (12)
`)
(def scanned (s/scan source))
(def parsed (p/parse scanned))
(def validated (valid parsed))
(def validated (valid parsed b/ctx))
# (get-in validated [:status :declared])
# (validated :ctx)
)