Compare commits

...

2 Commits

Author SHA1 Message Date
Scott Richmond
df274799be pretty print patterns 2024-06-04 13:00:34 -04:00
Scott Richmond
3081af60b2 parse docstrings 2024-06-04 12:13:40 -04:00
3 changed files with 40 additions and 21 deletions

View File

@ -47,7 +47,7 @@
:ref (stringify (value :^value))
:fn (string "fn " (value :name))
:function (string "builtin " (string value))
# XXX: pkg, fn
# XXX: pkg
))
(set stringify stringify*)
@ -63,6 +63,25 @@
:ref (string "ref:" (x :name) "{" (x :value) "}")
(stringify x)))
(defn- show-patt [x]
(case (x :type)
:nil "nil"
:bool (string (x :data))
:number (string (x :data))
:keyword (string ":" (x :data))
:word (x :data)
:placeholder (get-in x [:token :lexeme])
:tuple (string "(" (string/join (map show-patt (x :data)) ", ") ")")
:list (string "[" (string/join (map show-patt (x :data)) ", ")"]")
:dict (string "#{" (string/join (map show-patt (x :data)) ", ") "}")
:pair (string (show-patt (get-in x [:data 0])) " " (show-patt (get-in x [:data 1])))
:typed (string (show-patt (get-in x [:data 1])) " as " (show-patt (get-in x [:data 0])))
:interpolated (get-in x [:token :lexeme])
:string (get-in x [:token :lexeme])
(error "cannot show pattern of unknown type " (x :type))))
(defn- docstring [fnn] :todo)
(defn- conj-set [sett value]
(def new (merge sett))
(set (new value) true)

View File

@ -561,10 +561,12 @@
(do
(set source `
fn foo
fn bar () -> foo ()
fn foo () -> :foo
bar ()
fn foo {
"a foo, a bar"
() -> :foo
(_) -> :bar
}
foo ()
`)
(def result (run))
# (b/show result)

View File

@ -725,7 +725,7 @@
(accept-many parser :newline)
(def rhs (nonbinding parser))
(print "parsed rhs")
[[lhs guard rhs]]
{:clauses [[lhs guard rhs]]}
)
([err] err)
)
@ -757,13 +757,18 @@
(def origin (current parser))
(expect parser :lbrace) (advance parser)
(accept-many parser ;terminators)
(def doc (when (= :string ((current parser) :type))
(def docstring ((current parser) :literal))
(advance parser)
(accept-many parser ;terminators)
docstring))
(def data @[])
(while (not (check parser :rbrace))
(if (check parser :eof)
(error {:type :error :token origin :data data :msg "unclosed brace"}))
(array/push data (capture fn-clause parser)))
(advance parser)
data)
{:clauses data :doc doc})
(defn- lambda [parser]
(def origin (current parser))
@ -783,11 +788,11 @@
(def name (-> parser word-only (get :data)))
(print "function name: ")
(pp name)
(def data (case (-> parser current type)
(def {:clauses data :doc doc} (case (-> parser current type)
:lbrace (fn-clauses parser)
:lparen (fn-simple parser)
:nothing))
@{:type :fn :name name :data data :token origin})
{:clauses :nothing}))
@{:type :fn :name name :data data :token origin :doc doc})
([err] err)))
### compoound forms
@ -1129,25 +1134,18 @@
(do
# (comment
(def source `
fn foo
fn bar
fn () -> :baz
(def source `(foo as :bar, 12, :foo, _thing)
`)
(def scanned (s/scan source))
(print "\n***NEW PARSE***\n")
(def a-parser (new-parser scanned))
(def parsed (script a-parser))
# (print (pp-ast parsed))
# (pp scanned)
(pp parsed)
# (def cleaned (get-in parsed [:data 2]))
# (pp cleaned)
(def parsed (pattern a-parser))
(print (show-patt parsed))
)
# FIXME:
# TODO:
# DECIDE:
# - when to use a flat try/catch format, and when to use capture/expect-ret to get values instead of errors