Interpret match expressions
This commit is contained in:
parent
069e3b4a7b
commit
11a51cf708
|
@ -97,6 +97,30 @@
|
||||||
(interpret else-expr ctx)
|
(interpret else-expr ctx)
|
||||||
)))
|
)))
|
||||||
|
|
||||||
|
(defn- interpret-match [ast ctx]
|
||||||
|
(let [match-expr (:expr ast)
|
||||||
|
expr (interpret match-expr ctx)
|
||||||
|
clauses (:clauses ast)]
|
||||||
|
(loop [clause (first clauses)
|
||||||
|
clauses (rest clauses)]
|
||||||
|
(if clause
|
||||||
|
(let [pattern (:pattern clause)
|
||||||
|
body (:body clause)
|
||||||
|
new-ctx (atom {::parent ctx})
|
||||||
|
match? (match pattern expr new-ctx)
|
||||||
|
success (:success match?)
|
||||||
|
clause-ctx (:ctx match?)]
|
||||||
|
(if success
|
||||||
|
(do
|
||||||
|
(swap! new-ctx #(merge % clause-ctx))
|
||||||
|
(interpret body new-ctx))
|
||||||
|
(recur (first clauses) (rest clauses))
|
||||||
|
))
|
||||||
|
(throw (ex-info "Match Error: No match found" {}))
|
||||||
|
))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
(defn interpret [ast ctx]
|
(defn interpret [ast ctx]
|
||||||
(case (::ast/type ast)
|
(case (::ast/type ast)
|
||||||
|
|
||||||
|
@ -108,6 +132,8 @@
|
||||||
|
|
||||||
::ast/if (interpret-if ast ctx)
|
::ast/if (interpret-if ast ctx)
|
||||||
|
|
||||||
|
::ast/match (interpret-match ast ctx)
|
||||||
|
|
||||||
::ast/block
|
::ast/block
|
||||||
(let [exprs (:exprs ast)
|
(let [exprs (:exprs ast)
|
||||||
inner (pop exprs)
|
inner (pop exprs)
|
||||||
|
@ -154,8 +180,12 @@
|
||||||
(do
|
(do
|
||||||
|
|
||||||
(def source "
|
(def source "
|
||||||
let (foo, (_, baz)) = (1, (2, 3))
|
let foo = (1, 2)
|
||||||
baz
|
match foo with {
|
||||||
|
0 -> :zero
|
||||||
|
(_, 3) -> :one
|
||||||
|
& baz -> baz
|
||||||
|
}
|
||||||
")
|
")
|
||||||
|
|
||||||
(println "")
|
(println "")
|
||||||
|
|
|
@ -466,7 +466,7 @@
|
||||||
(let [curr (current parser)]
|
(let [curr (current parser)]
|
||||||
(case (::token/type curr)
|
(case (::token/type curr)
|
||||||
::token/rbrace
|
::token/rbrace
|
||||||
(assoc parser ::ast {::ast/type ::ast/clauses :clauses clauses})
|
(assoc (advance parser) ::ast {::ast/type ::ast/clauses :clauses clauses})
|
||||||
|
|
||||||
::token/newline
|
::token/newline
|
||||||
(recur (accept-many #{::token/newline} parser) clauses)
|
(recur (accept-many #{::token/newline} parser) clauses)
|
||||||
|
@ -566,10 +566,10 @@
|
||||||
(do
|
(do
|
||||||
(def pp pp/pprint)
|
(def pp pp/pprint)
|
||||||
(def source "match foo with {
|
(def source "match foo with {
|
||||||
_ -> foo
|
0 -> foo
|
||||||
foo () -> bar
|
}
|
||||||
() -> baz
|
|
||||||
}")
|
")
|
||||||
(def lexed (scanner/scan source))
|
(def lexed (scanner/scan source))
|
||||||
(def tokens (:tokens lexed))
|
(def tokens (:tokens lexed))
|
||||||
(def p (parser tokens))
|
(def p (parser tokens))
|
||||||
|
@ -581,7 +581,7 @@
|
||||||
(println "*** *** NEW PARSE *** ***")
|
(println "*** *** NEW PARSE *** ***")
|
||||||
|
|
||||||
(-> p
|
(-> p
|
||||||
(parse-match)
|
(parse-script)
|
||||||
(::ast)
|
(::ast)
|
||||||
(pp)
|
(pp)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user