Delete prints; next -> peek; remove loop in parse-expr

This commit is contained in:
Scott Richmond 2022-02-15 22:48:36 -05:00
parent 96b30ece05
commit a71db0e640

View File

@ -12,7 +12,7 @@
(defn- current [parser] (defn- current [parser]
(nth (::tokens parser) (::token parser) nil)) (nth (::tokens parser) (::token parser) nil))
(defn- next [parser] (defn- peek [parser]
(nth (::tokens parser) (inc (::token parser)) nil)) (nth (::tokens parser) (inc (::token parser)) nil))
(defn- at-end? [parser] (defn- at-end? [parser]
@ -60,10 +60,6 @@
(loop [parser (advance parser) (loop [parser (advance parser)
members [] members []
current_member nil] current_member nil]
(println "***parsing tuple with ")
(print "parser ") (pp/pprint parser)
(print "members ") (pp/pprint members)
(print "current member ")(pp/pprint current_member)
(let [curr (current parser)] (let [curr (current parser)]
(case (::token/type curr) (case (::token/type curr)
::token/rparen (let [ms (add-member members current_member)] ::token/rparen (let [ms (add-member members current_member)]
@ -74,8 +70,6 @@
(::token/comma ::token/newline) (recur (advance parser) (add-member members current_member) nil) (::token/comma ::token/newline) (recur (advance parser) (add-member members current_member) nil)
(let [parsed (parse-expr parser)] (let [parsed (parse-expr parser)]
(print "Got to expr with")
(pp/pprint (::ast parsed))
(recur parsed members (::ast parsed))) (recur parsed members (::ast parsed)))
)))) ))))
@ -84,10 +78,6 @@
(loop [parser (advance parser) (loop [parser (advance parser)
members [] members []
current_member nil] current_member nil]
(println "***parsing list with ")
(print "parser ") (pp/pprint parser)
(print "members ") (pp/pprint members)
(print "current member ")(pp/pprint current_member)
(let [curr (current parser)] (let [curr (current parser)]
(case (::token/type curr) (case (::token/type curr)
::token/rbracket (let [ms (add-member members current_member)] ::token/rbracket (let [ms (add-member members current_member)]
@ -97,7 +87,6 @@
(::token/comma ::token/newline) (recur (advance parser) (add-member members current_member) nil) (::token/comma ::token/newline) (recur (advance parser) (add-member members current_member) nil)
(let [parsed (parse-expr parser)] (let [parsed (parse-expr parser)]
(print "Got to expr with ") (pp/pprint (::ast parsed))
(recur parsed members (::ast parsed))) (recur parsed members (::ast parsed)))
)))) ))))
@ -106,10 +95,6 @@
(loop [parser (advance parser) (loop [parser (advance parser)
members [] members []
current_member nil] current_member nil]
(println "***parsing set with ")
(print "parser ") (pp/pprint parser)
(print "members ") (pp/pprint members)
(print "current member ")(pp/pprint current_member)
(let [curr (current parser)] (let [curr (current parser)]
(case (::token/type curr) (case (::token/type curr)
::token/rbrace (let [ms (add-member members current_member)] ::token/rbrace (let [ms (add-member members current_member)]
@ -119,17 +104,14 @@
(::token/comma ::token/newline) (recur (advance parser) (add-member members current_member) nil) (::token/comma ::token/newline) (recur (advance parser) (add-member members current_member) nil)
(let [parsed (parse-expr parser)] (let [parsed (parse-expr parser)]
(print "Got to expr with ") (pp/pprint (::ast parsed))
(recur parsed members (::ast parsed))) (recur parsed members (::ast parsed)))
)))) ))))
(defn- parse-block [parser] (defn- parse-block [parser]
(println "*** *** parsing block")
(loop [parser (advance parser) (loop [parser (advance parser)
exprs [] exprs []
current_expr nil] current_expr nil]
(print "current ")(pp/pprint (current parser))
(case (::token/type (current parser)) (case (::token/type (current parser))
::token/rbrace ::token/rbrace
(assoc (advance parser) ::ast (assoc (advance parser) ::ast
@ -149,7 +131,6 @@
))) )))
(defn- parse-script [parser] (defn- parse-script [parser]
(println "*** *** *** parsing script")
(loop [parser parser (loop [parser parser
exprs [] exprs []
current_expr nil] current_expr nil]
@ -240,7 +221,6 @@
expr (parse-expr equals) expr (parse-expr equals)
results (map #(get-in % [::ast ::ast/type]) [pattern equals expr]) results (map #(get-in % [::ast ::ast/type]) [pattern equals expr])
] ]
(println "!!!!!!!! let results")
(if (some #(= ::ast/poison %) results) (if (some #(= ::ast/poison %) results)
(println ::poison) (println ::poison)
(assoc expr ::ast { (assoc expr ::ast {
@ -258,7 +238,6 @@
else-expr (parse-expr else) else-expr (parse-expr else)
results (map #(get-in % [::ast ::ast/type]) [if-expr then then-expr else else-expr]) results (map #(get-in % [::ast ::ast/type]) [if-expr then then-expr else else-expr])
] ]
(println "//////////// if results")
(if (some #(= ::ast/poison %) results) (if (some #(= ::ast/poison %) results)
(println ::ast/poison) (println ::ast/poison)
(assoc else-expr ::ast { (assoc else-expr ::ast {
@ -270,66 +249,73 @@
)) ))
(defn- parse-expr [parser] (defn- parse-expr [parser]
(loop [parser parser] (let [token (current parser)]
(let [token (current parser)] (case (::token/type token)
(println "Parsing expr " (::token/type token))
(case (::token/type token)
(::token/number ::token/string) (::token/number ::token/string)
(parse-atom parser token) (parse-atom parser token)
::token/keyword (let [next (next parser) ::token/keyword (let [next (peek parser)
type (::token/type next)] type (::token/type next)]
(if (= type ::token/lparen) (if (= type ::token/lparen)
(parse-synthetic parser) (parse-synthetic parser)
(parse-atom parser token))) (parse-atom parser token)))
::token/word (let [next (next parser) ::token/word (let [next (peek parser)
type (::token/type next)] type (::token/type next)]
(case type (case type
(::token/lparen ::token/keyword) (parse-synthetic parser) (::token/lparen ::token/keyword) (parse-synthetic parser)
(parse-word parser))) (parse-word parser)))
(::token/nil ::token/true ::token/false) (::token/nil ::token/true ::token/false)
(parse-atomic-word parser token) (parse-atomic-word parser token)
::token/lparen (parse-tuple parser) ::token/lparen (parse-tuple parser)
::token/lbracket (parse-list parser) ::token/lbracket (parse-list parser)
::token/startset (parse-set parser) ::token/startset (parse-set parser)
::token/lbrace (parse-block parser) ::token/lbrace (parse-block parser)
::token/let (parse-let parser) ::token/let (parse-let parser)
::token/if (parse-if parser) ::token/if (parse-if parser)
(-> parser (-> parser
(advance) (advance)
(assoc ::ast {::ast/type ::ast/poison :message "Expected expression"})) (assoc ::ast {::ast/type ::ast/poison :message "Expected expression"}))
)))) )))
(do (do
(def source "if let foo = :foo (def source "if let foo = :foo
then { then {
bar (baz) :quux bar (baz) :quux
} }
else (42)") else [
(42)
12
:twenty-three
foo (bar) (baz) :quux
(false, nil, ())
]")
(def tokens (:tokens (scanner/scan source))) (def tokens (:tokens (scanner/scan source)))
(def p (parser tokens)) (def p (parser tokens))
(-> (parse-script p) (-> (parse-script p)
(::ast) (::ast)
(pp/pprint))) (pp/pprint)))
(comment " (comment "
Further thoughts/still to do: Further thoughts/still to do:
* Clean up the parsing functions:
- use accept-many in blocks and scripts
- parse-atom (and other parse functions) should take only a parser
- ast nodes should include their tokens
-
Other quick thoughts: Other quick thoughts:
* Once I get this far, then it's time to wire up the interpreter (with hard-coded functions, and the beginning of static analysis) * Once I get this far, then it's time to wire up the interpreter (with hard-coded functions, and the beginning of static analysis)