Compare commits

..

2 Commits

Author SHA1 Message Date
Scott Richmond
20cb689d12 prelude now passes validator 2024-06-05 15:52:03 -04:00
Scott Richmond
5874a56090 pull in most recent prelude 2024-06-05 13:21:48 -04:00
7 changed files with 1452 additions and 22 deletions

View File

@ -44,7 +44,7 @@
:dict (dict-str value) :dict (dict-str value)
:set :set
(string/join (map stringify (keys value)) ", ") (string/join (map stringify (keys value)) ", ")
:ref (stringify (value :^value)) :box (stringify (value :^value))
:fn (string "fn " (value :name)) :fn (string "fn " (value :name))
:applied (string "fn " (value :name)) :applied (string "fn " (value :name))
:function (string "builtin " (string value)) :function (string "builtin " (string value))
@ -68,7 +68,7 @@
:list (string "[" (stringify x) "]") :list (string "[" (stringify x) "]")
:dict (string "#{" (stringify x) "}") :dict (string "#{" (stringify x) "}")
:set (string "${" (stringify x) "}") :set (string "${" (stringify x) "}")
:ref (string "box " (x :name) " [ " (stringify x) " ]") :box (string "box " (x :name) " [ " (stringify x) " ]")
:pkg (show-pkg x) :pkg (show-pkg x)
(stringify x))) (stringify x)))

View File

@ -1,10 +1,34 @@
(import spork/json :as j) (import spork/json :as j)
(import /base :as b) (import /base :as b)
(defn- get-line [source line]
((string/split "\n" source) (dec line)))
(defn scan-error [e out] (set (out :errors) e) (j/encode out)) (defn scan-error [e out] (set (out :errors) e) (j/encode out))
(defn parse-error [e out] (set (out :errors) e) (j/encode out)) (defn parse-error [e]
(def msg (e :msg))
(def line-num (get-in e [:token :line]))
(def source (get-in e [:token :source]))
(def source-line (get-line source line-num))
(print "Parsing error: " msg)
(print "On line " line-num ":")
(print source-line))
(defn validation-error [e out] (set (out :errors) e) (j/encode out))
(defn validation-error [e]
(def msg (e :msg))
(def line-num (get-in e [:node :token :line]))
(def source (get-in e [:node :token :source]))
(def source-line (get-line source line-num))
(case msg
"unbound name"
(do
(print "Validation error: " msg " " (get-in e [:node :data]))
(print "on line " line-num)
(print source-line)
)
)
)
(defn runtime-error [e out] (set (out :errors) e) (j/encode out)) (defn runtime-error [e out] (set (out :errors) e) (j/encode out))

View File

@ -340,7 +340,7 @@
(defn- ref [ast ctx] (defn- ref [ast ctx]
(def {:data value-ast :name name} ast) (def {:data value-ast :name name} ast)
(def value (interpret value-ast ctx)) (def value (interpret value-ast ctx))
(def box @{:^type :ref :^value value :name name}) (def box @{:^type :box :^value value :name name})
(set (ctx name) box) (set (ctx name) box)
box) box)

View File

@ -16,16 +16,16 @@ The API from the old Clojure Ludus interpreter returns an object with four field
* `errors`: an array of errors, which are just strings * `errors`: an array of errors, which are just strings
This new scene will have to return a JSON POJSO: This new scene will have to return a JSON POJSO:
{:console [...] :result "..." :draw [...] :errors [...]} {:console "..." :result "..." :draw [...] :errors [...]}
) )
(def console @"") (def prelude-src (slurp "prelude.ld"))
(setdyn :out console) (def prelude-scanned (s/scan prelude-src))
(print "foo") (def prelude-parsed (p/parse prelude-scanned))
(pp {:a 1 :b 2}) (def parse-errors (prelude-parsed :errors))
(setdyn :out stdout) (when (any? parse-errors) (each err parse-errors (e/parse-error err)))
(print "collected out") (def prelude-validated (v/valid prelude-parsed @{"base" b/base}))
(print console) (each err (prelude-validated :errors) (e/validation-error err))
(defn run [source] (defn run [source]
(def errors @[]) (def errors @[])
@ -44,7 +44,7 @@ This new scene will have to return a JSON POJSO:
(break (-> :errors validated (e/validation-error out)))) (break (-> :errors validated (e/validation-error out))))
(setdyn :out console) (setdyn :out console)
(try (try
(set result (b/show (i/interpret (parsed :ast) @{}))) (set result (b/show (i/interpret (parsed :ast) @{:^parent b/base})))
([err] (e/runtime-error err out))) ([err] (e/runtime-error err out)))
(set (out :result) result) (set (out :result) result)
(j/encode out)) (j/encode out))
@ -56,7 +56,7 @@ This new scene will have to return a JSON POJSO:
(run source)) (run source))
(def source ` (def source `
fn foo () -> :foo let foo = fn () -> :bar
foo () foo ()
`) `)

View File

@ -1,7 +1,7 @@
### 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
# (try (os/cd "janet") ([_] nil)) # when in repl to do relative imports (try (os/cd "janet") ([_] nil)) # when in repl to do relative imports
(import ./scanner :as s) (import ./scanner :as s)
(defmacro declare (defmacro declare
@ -773,7 +773,7 @@
(defn- lambda [parser] (defn- lambda [parser]
(def origin (current parser)) (def origin (current parser))
(expect parser :fn) (advance parser) (expect parser :fn) (advance parser)
@{:type :fn :data (fn-simple parser) :token origin}) @{:type :fn :data ((fn-simple parser) :clauses) :token origin})
(defn- fnn [parser] (defn- fnn [parser]
(if (= :lparen (-> parser peek type)) (break (lambda parser))) (if (= :lparen (-> parser peek type)) (break (lambda parser)))
@ -1104,6 +1104,7 @@
(def origin (current parser)) (def origin (current parser))
(def lines @[]) (def lines @[])
(while (not (check parser :eof)) (while (not (check parser :eof))
(accept-many parser :newline)
(array/push lines (capture toplevel parser)) (array/push lines (capture toplevel parser))
(capture terminator parser)) (capture terminator parser))
{:type :script :data lines :token origin}) {:type :script :data lines :token origin})
@ -1116,10 +1117,10 @@
# (do # (do
(comment (comment
(def source `pkg Foo {} (def source `fn () -> 42
`) `)
(def scanned (s/scan source)) (def scanned (s/scan source))
# (print "\n***NEW PARSE***\n") # (print "\n***NEW PARSE***\n")
(def a-parser (new-parser scanned)) (def a-parser (new-parser scanned))
(def parsed (pkg a-parser)) (def parsed (fnn a-parser))
) )

1405
janet/prelude.ld Normal file

File diff suppressed because it is too large Load Diff

View File

@ -173,7 +173,7 @@ Deferred until a later iteration of Ludus:
:dict :dict
:list :list
:fn :fn
:ref :box
:pkg :pkg
]) ])
@ -296,7 +296,7 @@ Deferred until a later iteration of Ludus:
(defn- fnn [validator] (defn- fnn [validator]
(def ast (validator :ast)) (def ast (validator :ast))
(def name (ast :name)) (def name (ast :name))
# (print "function name: " name) (print "function name: " name)
(def status (validator :status)) (def status (validator :status))
(def tail? (status :tail)) (def tail? (status :tail))
(set (status :tail) true) (set (status :tail) true)