many bugfixes; desugar pairs with word shorthand in dicts

This commit is contained in:
Scott Richmond 2024-05-13 20:55:21 -04:00
parent 2cfe9fdffc
commit 68e96bf223

View File

@ -1,20 +1,15 @@
### A recursive descent parser for Ludus ### A recursive descent parser for Ludus
### We still need to scan some things ### We still need to scan some things
(os/cd "janet") # when in repl to do relative imports #(os/cd "janet") # when in repl to do relative imports
(import ./scanner :as s) (import ./scanner :as s)
### First, some mutual recursion helpers
(defn unreachable
"A function that errors out if called."
[&] (error "reached the unreachable"))
(defmacro declare (defmacro declare
"Forward-declares a function name, so that it can be called in a mutually recursive manner." "Forward-declares a function name, so that it can be called in a mutually recursive manner."
[& names] [& names]
(def bindings @[]) (def bindings @[])
(loop [name :in names] (loop [name :in names]
(def binding ~(var ,name unreachable)) (def binding ~(var ,name nil))
(array/push bindings binding)) (array/push bindings binding))
~(upscope ,;bindings)) ~(upscope ,;bindings))
@ -22,7 +17,7 @@
"Defines a function depended on by another function, that has been forward `declare`d." "Defines a function depended on by another function, that has been forward `declare`d."
[name & forms] [name & forms]
(if-not (dyn name) (error "recursive functions must be declared before they are defined")) (if-not (dyn name) (error "recursive functions must be declared before they are defined"))
~(set ,name (defn ,name ,;forms))) ~(set ,name (defn- ,name ,;forms)))
### Next: a data structure for a parser ### Next: a data structure for a parser
(defn- new-parser (defn- new-parser
@ -139,7 +134,7 @@
# atoms # atoms
(defn- bool [parser] (defn- bool [parser]
(expect parser :bool) (expect parser :true :false)
(def curr (-> parser current)) (def curr (-> parser current))
(def ttype (type curr)) (def ttype (type curr))
(def value (if (= ttype :true) true false)) (def value (if (= ttype :true) true false))
@ -194,9 +189,6 @@
{:type :word :data (first-token :lexeme) :token first-token} {:type :word :data (first-token :lexeme) :token first-token}
:else {:type :error :msg "string interpolations/patterns must be single words"})) :else {:type :error :msg "string interpolations/patterns must be single words"}))
(def foo [{:foo :bar}])
(-> foo first (get :foo))
(defn- is-error? [data] (defn- is-error? [data]
(cond (cond
(buffer? data) false (buffer? data) false
@ -360,7 +352,7 @@
(def term (if (check parser :splat) (def term (if (check parser :splat)
(do (do
(advance parser) (advance parser)
(def splatted (try (word-only parser) ([e] e))) (def splatted (capture word-only parser))
{:type :splat :data splatted :token origin} {:type :splat :data splatted :token origin}
) )
(capture nonbinding parser))) (capture nonbinding parser)))
@ -412,7 +404,7 @@
(def key (try (kw parser) ([e] e))) (def key (try (kw parser) ([e] e)))
(def value (capture nonbinding parser)) (def value (capture nonbinding parser))
{:type :pair :data [key value] :token origin}) {:type :pair :data [key value] :token origin})
(try (panic parser (string expect "expected dict term, got " (type origin))) ([e] e)) (try (panic parser (string "expected dict term, got " (type origin))) ([e] e))
)) ))
(array/push (ast :data) term) (array/push (ast :data) term)
(try (separators parser) ([e] (array/push (ast :data) e)))) (try (separators parser) ([e] (array/push (ast :data) e))))
@ -499,12 +491,16 @@
(def origin (current parser)) (def origin (current parser))
(def term (case (type origin) (def term (case (type origin)
:splat {:type :splat :data (when (check (advance parser) :word) (word-only parser)) :token origin} :splat {:type :splat :data (when (check (advance parser) :word) (word-only parser)) :token origin}
:word (capture word-pattern parser) :word (do
(def word (capture word-pattern parser))
(def name (word :data))
(def key {:type :keyword :data (keyword name) :token origin})
{:type :pair :data [key word] :token origin})
:keyword (do :keyword (do
(def key (capture kw parser)) (def key (capture kw parser))
(def value (capture pattern parser)) (def value (capture pattern parser))
{:type :pair :data [key value] :token origin}) {:type :pair :data [key value] :token origin})
(try (panic parser (string expect "expected dict term, got " (type origin))) ([e] e)) (try (panic parser (string "expected dict term, got " (type origin))) ([e] e))
)) ))
(array/push (ast :data) term) (array/push (ast :data) term)
(try (separators parser) ([e] (array/push (ast :data) e)))) (try (separators parser) ([e] (array/push (ast :data) e))))
@ -743,6 +739,7 @@
(if (check parser :eof) (if (check parser :eof)
(error {:type :error :token origin :data data :msg "unclosed brace"})) (error {:type :error :token origin :data data :msg "unclosed brace"}))
(array/push data (capture fn-clause parser))) (array/push data (capture fn-clause parser)))
(advance parser)
data) data)
(defn- lambda [parser] (defn- lambda [parser]
@ -781,6 +778,7 @@
(error {:type :error :token origin :data data :msg "unclosed brace"})) (error {:type :error :token origin :data data :msg "unclosed brace"}))
(array/push data (capture expr parser)) (array/push data (capture expr parser))
(terminator parser)) (terminator parser))
(advance parser)
{:type :block :data data :token origin}) {:type :block :data data :token origin})
### TODO: decide whether this design works ### TODO: decide whether this design works
@ -943,7 +941,7 @@
:pkg-name (pkg-name parser) :pkg-name (pkg-name parser)
:recur (recur parser) :recur (recur parser)
:panic (panicc parser) :panic (panicc parser)
(panic parser (string expect "expected simple expression, got " (type curr))) (panic parser (string "expected simple expression, got " (type curr)))
) )
) )
@ -1017,6 +1015,7 @@
:number (num parser) :number (num parser)
:keyword (kw parser) :keyword (kw parser)
:string (str parser) :string (str parser)
:interpolated (interpolated parser)
:lparen (tup parser) :lparen (tup parser)
:lbracket (list parser) :lbracket (list parser)
:startdict (dict parser) :startdict (dict parser)
@ -1060,6 +1059,12 @@
(capture terminator parser)) (capture terminator parser))
{:type :script :data lines :token origin}) {:type :script :data lines :token origin})
(defn parse [scanned]
(def parser (new-parser scanned))
(def ast (script parser))
(set (parser :ast) ast)
parser)
(defn- indent-by [n] (defn- indent-by [n]
(def indentation @"") (def indentation @"")
(repeat n (buffer/push indentation "..")) (repeat n (buffer/push indentation ".."))
@ -1081,12 +1086,13 @@
(do (do
#(comment #(comment
(def source ` (def source `
panic! foo "foo {bar} baz"
`) `)
(def scanned (s/scan source)) (def scanned (s/scan source))
(def a-parser (new-parser scanned))
(print "\n***NEW PARSE***\n") (print "\n***NEW PARSE***\n")
(def parsed (toplevel a-parser)) (def a-parser (new-parser scanned))
(def parsed (script a-parser))
# (print (pp-ast parsed)) # (print (pp-ast parsed))
(pp parsed) (pp parsed)
) )