Parse single-clause match expressions

This commit is contained in:
Scott Richmond 2022-03-19 20:52:13 -04:00
parent 584ddaf705
commit e4e984bacd

View File

@ -446,9 +446,35 @@
(parse-then (accept ::token/newline ast)) (parse-then (accept ::token/newline ast))
)) ))
(defn- parse-match-clause [parser]) (defn- parse-match-clause [parser]
(let [pattern (parse-pattern parser)
rarrow (expect* #{::token/rarrow} "Expected arrow in match clause" pattern)
]
(if (:success rarrow)
(let [body (parse-expr (:parser rarrow))]
(assoc body ::ast {::ast/type ::ast/clause
:pattern (::ast pattern) :body (::ast body)})
)
(panic rarrow "Expected -> in match clause. Clauses must be in the form pattern -> expression")
)
))
(defn- parse-match [parser]) (defn- parse-match [parser]
(let [match-expr (parse-expr (advance parser) #{::token/lbrace ::token/lparen})
match-header (expect* #{::token/with} "Expected with" match-expr)]
(if (:success match-header)
(let [clauses (:parser match-header)]
(if (= (token-type clauses) ::token/lbrace)
(println "one or many clauses")
(let [clause (parse-match-clause clauses)]
(assoc clause ::ast {::ast/type ::ast/match
:expr (::ast match-expr)
:clauses [(::ast clause)]})
)
))
(panic parser "Expected with after match expression")
)))
(defn- parse-expr (defn- parse-expr
([parser] (parse-expr parser sync-on)) ([parser] (parse-expr parser sync-on))
@ -512,11 +538,11 @@
(do (do
(def pp pp/pprint) (def pp pp/pprint)
(def source " (def source "match foo with (foo, bar, 0) -> {
(1 let foo = bar
2 if foo then bar else baz
&three :foo
3)") }")
(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))
@ -528,7 +554,7 @@
(println "*** *** NEW PARSE *** ***") (println "*** *** NEW PARSE *** ***")
(-> p (-> p
(parse-script) (parse-match)
(::ast) (::ast)
(pp) (pp)
) )