Keep working on translating parser

This commit is contained in:
Scott Richmond 2024-01-09 23:42:56 -05:00
parent 70e8763dc5
commit a65b4ba873

View File

@ -1,3 +1,6 @@
### in repl, make sure to (os/cwd) into the janet dir
(import ./clj-loop :prefix "")
(defn ? [val default] (if (nil? val) default val))
(defn ok? [{:status status}]
@ -180,7 +183,7 @@
{:name name
:rule (fn order-fn [tokens]
(let [origin (first tokens)]
(loop [ps parsers
(clj-loop [ps parsers # now we can use my handy macro
results []
ts tokens]
(let [result (apply-parser (first ps) ts)
@ -220,21 +223,23 @@
(:err :none)
(update result :trace |(array/push $ name))))))))})
(defn quiet [parser]
{:name (kw+str (? (get parser :name) parser) "-quiet")
:rule (fn quiet-fn [tokens]
(let [result (apply-parser parser tokens)]
(if (pass? result)
(assoc result :status :quiet)
(put result :status :quiet)
result)))})
### Start here
### How to write variadic functions here that preserve this logic
### Janet doesn't allow for Clojure's multiple clauses, :(
(defn zero+
([parser] (zero+ (pname parser) parser))
([name parser]
{:name (kw+str name "-zero+")
:rule (fn zero+fn [tokens]
(loop [results []
(clj-loop [results []
ts tokens]
(let [result (apply-parser parser ts)]
(case (get result :status)
@ -310,14 +315,16 @@
:rule (fn group-fn [tokens]
(let [result (apply-parser parser tokens)]
(if (= :group (get result :status))
(assoc result :status :ok)
(put result :status :ok)
result)))}))
(defn err-msg [{token :token trace :trace}]
(println "Unexpected token " (get token :type) " on line " (get token :line))
(println "Expected token " (first trace)))
(defn err-msg [{:token token :trace trace}]
(print "Unexpected token " (get token :type) " on line " (get token :line))
(print "Expected token " (first trace)))
(defn butlast [xs] (if (empty? xs) [] (slice xs 0 -2)))
(defmacro defp [name & items]
(let [arg (last items)
fns (into [] (butlast items))]
`(defn ~name [] ((apply comp ~fns) (keyword '~name) ~arg))))
fns (butlast items)]
~(defn ,name [] ((apply comp ,fns) (keyword ',name) ,arg))))