Interpret loop/recur
This commit is contained in:
parent
b62fa6d074
commit
a5914076b0
|
@ -248,9 +248,8 @@
|
||||||
{::data/struct true ::data/type ::data/ns ::data/name name}
|
{::data/struct true ::data/type ::data/ns ::data/name name}
|
||||||
(map-values #(interpret-ast % ctx))
|
(map-values #(interpret-ast % ctx))
|
||||||
members)]
|
members)]
|
||||||
(do
|
|
||||||
(vswap! ctx update-ctx {name ns})
|
(vswap! ctx update-ctx {name ns})
|
||||||
ns)))))
|
ns))))
|
||||||
|
|
||||||
(defn- interpret-import [ast ctx]
|
(defn- interpret-import [ast ctx]
|
||||||
(let [path (:path ast)
|
(let [path (:path ast)
|
||||||
|
@ -277,6 +276,33 @@
|
||||||
(vswap! ctx update-ctx {name ref})
|
(vswap! ctx update-ctx {name ref})
|
||||||
ref)))
|
ref)))
|
||||||
|
|
||||||
|
(defn- interpret-loop [ast ctx]
|
||||||
|
(let [tuple (interpret-ast (:expr ast) ctx)
|
||||||
|
clauses (:clauses ast)]
|
||||||
|
(loop [input tuple]
|
||||||
|
(let [output (loop [clause (first clauses)
|
||||||
|
clauses (rest clauses)]
|
||||||
|
(if clause
|
||||||
|
(let [pattern (:pattern clause)
|
||||||
|
body (:body clause)
|
||||||
|
new-ctx (volatile! {::parent ctx})
|
||||||
|
match? (match pattern input new-ctx)
|
||||||
|
success (:success match?)
|
||||||
|
clause-ctx (:ctx match?)]
|
||||||
|
(if success
|
||||||
|
(do
|
||||||
|
(vswap! new-ctx #(merge % clause-ctx))
|
||||||
|
(interpret-ast body new-ctx))
|
||||||
|
(recur (first clauses) (rest clauses))))
|
||||||
|
|
||||||
|
(throw (ex-info (str "Match Error: No match found in loop for " input) {}))))]
|
||||||
|
(if (::data/recur output)
|
||||||
|
(recur (:tuple output))
|
||||||
|
output
|
||||||
|
))
|
||||||
|
))
|
||||||
|
)
|
||||||
|
|
||||||
(defn interpret-ast [ast ctx]
|
(defn interpret-ast [ast ctx]
|
||||||
(case (::ast/type ast)
|
(case (::ast/type ast)
|
||||||
|
|
||||||
|
@ -306,6 +332,11 @@
|
||||||
|
|
||||||
::ast/ref (interpret-ref ast ctx)
|
::ast/ref (interpret-ref ast ctx)
|
||||||
|
|
||||||
|
::ast/recur
|
||||||
|
{::data/recur true :tuple (interpret-ast (:tuple ast) ctx)}
|
||||||
|
|
||||||
|
::ast/loop (interpret-loop ast ctx)
|
||||||
|
|
||||||
::ast/block
|
::ast/block
|
||||||
(let [exprs (:exprs ast)
|
(let [exprs (:exprs ast)
|
||||||
inner (pop exprs)
|
inner (pop exprs)
|
||||||
|
@ -366,15 +397,10 @@
|
||||||
(println (ex-message e))
|
(println (ex-message e))
|
||||||
(pp/pprint (ex-data e)))))
|
(pp/pprint (ex-data e)))))
|
||||||
|
|
||||||
(do
|
(comment
|
||||||
|
|
||||||
(def source "
|
(def source "
|
||||||
|
|
||||||
ref foo = nil
|
|
||||||
ref bar = nil
|
|
||||||
|
|
||||||
eq (foo, bar)
|
|
||||||
|
|
||||||
")
|
")
|
||||||
|
|
||||||
(println "")
|
(println "")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user