Reindent things; start work on named fns; decide to refactor things

This commit is contained in:
Scott Richmond 2022-03-20 17:37:44 -04:00
parent 0f64307ba8
commit 5da8090d2b

View File

@ -273,13 +273,14 @@
] ]
(let [curr (current parser)] (let [curr (current parser)]
(case (token-type parser) (case (token-type parser)
::token/rbrace (let [es (add-member exprs current_expr)] ::token/rbrace
(if (empty? es) (let [es (add-member exprs current_expr)]
(advance (panic parser "Blocks must have at least one expression")) (if (empty? es)
(assoc (advance parser) ::ast { (advance (panic parser "Blocks must have at least one expression"))
::ast/type ::ast/block (assoc (advance parser) ::ast {
:exprs es ::ast/type ::ast/block
}))) :exprs es
})))
(::token/semicolon ::token/newline) (::token/semicolon ::token/newline)
(recur (recur
@ -306,7 +307,8 @@
current_expr nil current_expr nil
] ]
(case (token-type parser) (case (token-type parser)
::token/eof (let [es (add-member exprs current_expr)] ::token/eof
(let [es (add-member exprs current_expr)]
(if (empty? es) (if (empty? es)
(panic parser "Scripts must have at least one expression") (panic parser "Scripts must have at least one expression")
(assoc parser ::ast {::ast/type ::ast/script :exprs es}))) (assoc parser ::ast {::ast/type ::ast/script :exprs es})))
@ -341,10 +343,9 @@
(let [parsed (parse-tuple parser)] (let [parsed (parse-tuple parser)]
(recur parsed (conj terms (::ast parsed)))) (recur parsed (conj terms (::ast parsed))))
(-> parser (assoc parser ::ast {::ast/type ::ast/synthetic :terms terms})
(assoc ::ast {::ast/type ::ast/synthetic :terms terms})
))))) ))))
(defn- parse-word [parser] (defn- parse-word [parser]
(let [curr (current parser)] (let [curr (current parser)]
@ -505,18 +506,30 @@
(case (::token/type (current first)) (case (::token/type (current first))
::token/lparen ::token/lparen
(let [pattern (parse-tuple-pattern first) (let [pattern (parse-tuple-pattern first)
arrow (expect* ::token/rarrow "Expected arrow after pattern" pattern)] arrow (expect* ::token/rarrow "Expected arrow after pattern" pattern)]
(if (:success arrow) (if (:success arrow)
(let [body (parse-expr (:parser arrow))] (let [body (parse-expr (:parser arrow))]
(assoc body ::ast {::ast/type ::ast/fn (assoc body ::ast {::ast/type ::ast/fn
:clauses [{::ast/type ::ast/clause :name "anonymous"
:pattern (::ast pattern) :clauses [{::ast/type ::ast/clause
:body (::ast body)}]})) :pattern (::ast pattern)
() :body (::ast body)}]}))
(panic pattern "Expected arrow after pattern in fn clause")
) )
) )
::token/word () ;; TODO: finish this
;; right now it's broke
::token/word
(let [name (parse-word first)
pattern (parse-tuple-pattern name)
arrow (expect* ::token/rarrow "Expected arrow after pattern" name)
body (parse-expr (:parser arrow))
]
(
)
)
(panic parser "Expected name or clause after fn") (panic parser "Expected name or clause after fn")
))) )))
@ -637,6 +650,16 @@
* Placeholders * Placeholders
* How much in parser, how much in analysis? * How much in parser, how much in analysis?
Some architectural changes:
* UGH, this code is just kind of a mess and hard to reason about
* Especially sequential forms
* Parsers are hard
* One idea:
* Refactor everything so that it returns a success or failure
* Because this is all stateless, in sequential forms, you can just do all the things
* This lets you do one let (with everything building up) and then a cond with bespoke errors/panics
* This also still lets you encapsulate parsererrors with poisoned nodes
") ")