Get if-exprs fully wired up

This commit is contained in:
Scott Richmond 2022-03-19 12:28:13 -04:00
parent e616dcd453
commit aa7f2ac8fc

View File

@ -354,8 +354,33 @@
(let [pattern (parse-pattern (advance parser))] (let [pattern (parse-pattern (advance parser))]
(parse-assignment pattern))) (parse-assignment pattern)))
(defn- parse-else [parser]
(let [ast (::ast parser)
else-kw (expect* ::token/else "Expected else clause after then" parser)
success (:success else-kw)
else-kw-parser (:parser else-kw)]
(if success
(let [expr (parse-expr else-kw-parser)
else-expr (::ast expr)]
(assoc expr ::ast (assoc ast :else else-expr)))
else-kw-parser)))
(defn- parse-then [parser]
(let [ast (::ast parser)
then-kw (expect* ::token/then "Expected then clause after if" parser)
success (:success then-kw)
then-kw-parser (:parser then-kw)]
(if success
(let [expr (parse-expr then-kw-parser (conj sync-on ::token/else))
then-expr (::ast expr)]
(parse-else (accept ::token/newline (assoc expr ::ast (assoc ast :then then-expr)))))
then-kw-parser)))
(defn- parse-if* [parser] (defn- parse-if* [parser]
) (let [if-expr (parse-expr (advance parser) #{::token/newline ::token/then})
ast (assoc if-expr ::ast {::ast/type ::ast/if :if (::ast if-expr)})]
(parse-then (accept ::token/newline ast))
))
;; TODO: Fix failure case here ;; TODO: Fix failure case here
(defn- parse-if [parser] (defn- parse-if [parser]
@ -410,9 +435,9 @@
::token/let (parse-let* parser) ::token/let (parse-let* parser)
::token/if (parse-if parser) ::token/if (parse-if* parser)
::token/error (panic parser (:message token)) ::token/error (panic parser (:message token) sync-on)
(::token/rparen ::token/rbrace ::token/rbracket) (::token/rparen ::token/rbrace ::token/rbracket)
(panic parser (str "Unbalanced enclosure: " (::token/lexeme token))) (panic parser (str "Unbalanced enclosure: " (::token/lexeme token)))
@ -426,9 +451,7 @@
(do (do
(def pp pp/pprint) (def pp pp/pprint)
(def source "if (def source "if {foo} then bar else baz")
then bar
else 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))
@ -440,7 +463,7 @@
(println "*** *** NEW PARSE *** ***") (println "*** *** NEW PARSE *** ***")
(-> p (-> p
(parse-if) (parse-script)
(::ast) (::ast)
(pp) (pp)
) )