if expressions, done

This commit is contained in:
Scott Richmond 2024-05-08 13:50:26 -04:00
parent f3256f7d12
commit c36a140c6b

View File

@ -48,7 +48,8 @@
(= type current-type))
### Parsing functions
(declare nonbinding binding synthetic)
# forward declarations
(declare simple nonbinding expr toplevel synthetic)
# errors
(def terminators [:break :newline :semicolon :eof])
@ -66,7 +67,7 @@
(has-value? breaking ttype))
(defn- panic [parser message]
# (print "Panic in the parser: " message)
(print "Panic in the parser: " message)
(def origin (current parser))
(advance parser)
(def skipped @[origin])
@ -74,7 +75,6 @@
(array/push skipped (current parser))
(advance parser))
(array/push skipped (current parser))
# (advance parser)
(def err {:type :error :data skipped :token origin :msg message})
(update parser :errors array/push err)
(error err))
@ -88,6 +88,9 @@
(defn- expect-ret [parser type]
(try (expect parser type) ([e] e)))
(defn- capture [parse-fn parser]
(try (parse-fn parser) ([e] e)))
# atoms
(defn- bool [parser]
(expect parser :bool)
@ -223,7 +226,7 @@
(advance parser)
ast)
(defn- set [parser]
(defn- sett [parser]
(def origin (current parser))
(advance parser)
(def ast {:type :set :data @[] :token origin})
@ -268,11 +271,13 @@
(defn- iff [parser]
(def ast {:type :if :data @[] :token (current parser)})
(advance parser) #consume the if
(update ast array/push :data (simple parser))
(when-let [err (expect-ret parser :then)] (update ast array/push :data err))
(update ast array/push :data (nonbinding parser))
(when-let [err (expect-ret parser :else)] (update ast array/push :data err))
(update ast array/push :data (nonbinding parser))
(array/push (ast :data) (capture simple parser))
(when-let [err (expect-ret parser :then)] (array/push (ast :data) err))
(advance parser)
(array/push (ast :data) (capture nonbinding parser))
(when-let [err (expect-ret parser :else)] (array/push (ast :data) err))
(advance parser)
(array/push (ast :data) (capture nonbinding parser))
ast)
(defn- condd [parser])
@ -377,7 +382,7 @@
:startdict (dict parser)
:startset (sett parser)
:word (word parser)
:if (unreachable)
:if (iff parser)
:cond (unreachable)
:match (unreachable)
:with (unreachable)
@ -409,7 +414,7 @@
:startdict (dict parser)
:startset (sett parser)
:word (word parser)
:if (unreachable)
:if (iff parser)
:cond (unreachable)
:match (unreachable)
:with (unreachable)
@ -423,8 +428,8 @@
(import ./scanner :as s)
(do
#(comment
(def scanned (s/scan "#{}"))
(def source "if foo then else baz")
(def scanned (s/scan source))
(def a-parser (new-parser scanned))
(def parsed (nonbinding a-parser))
(-> parsed)