continue integration work: basic framework

This commit is contained in:
Scott Richmond 2024-06-05 13:01:43 -04:00
parent 721594823d
commit 6a4e2ccd17
2 changed files with 58 additions and 24 deletions

10
janet/errors.janet Normal file
View File

@ -0,0 +1,10 @@
(import spork/json :as j)
(import /base :as b)
(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 validation-error [e out] (set (out :errors) e) (j/encode out))
(defn runtime-error [e out] (set (out :errors) e) (j/encode out))

View File

@ -1,40 +1,64 @@
# an integrated Ludus interpreter # an integrated Ludus interpreter
(try (os/cd "janet") ([_] nil)) # for REPL (try (os/cd "janet") ([_] nil)) # for REPL
(import ./scanner :as s) (import /scanner :as s)
(import ./parser :as p) (import /parser :as p)
(import ./validate :as v) (import /validate :as v)
(import ./interpreter :as i) (import /interpreter :as i)
(import /errors :as e)
(import /base :as b)
(import spork/json :as j)
# (defn run [] (comment
# (def scanned (s/scan source)) The API from the old Clojure Ludus interpreter returns an object with four fields:
# (when (has-errors? scanned) (break (scanned :errors))) * `console`: an array of lines to be printed to the console
# (def parsed (p/parse scanned)) * `result`: a string representation of the result of running the script
# (when (has-errors? parsed) (break (parsed :errors))) * `draw`: an array of arrays that represent p5 calls, e.g. `["line", 0, 0, 100, 100]`
# (def validated (v/valid parsed b/ctx)) * `errors`: an array of errors, which are just strings
# # (when (has-errors? validated) (break (validated :errors)))
# # (def cleaned (get-in parsed [:ast :data 1]))
# # # (pp cleaned)
# # (interpret (parsed :ast) @{:^parent b/ctx})
# (try (interpret (parsed :ast) @{:^parent b/ctx})
# ([e] (if (struct? e) (error (e :msg)) (error e)))))
(defn main [source] This new scene will have to return a JSON POJSO:
{:console [...] :result "..." :draw [...] :errors [...]}
)
(def console @"")
(setdyn :out console)
(print "foo")
(pp {:a 1 :b 2})
(setdyn :out stdout)
(print "collected out")
(print console)
(defn run [source]
(def errors @[])
(def draw @[])
(var result @"")
(def console @"")
(def out @{:errors errors :draw draw :result result :console console})
(def scanned (s/scan source)) (def scanned (s/scan source))
(when (any? (scanned :errors)) (when (any? (scanned :errors))
(break (scanned :errors))) (break (-> :errors scanned (e/scan-error out))))
(def parsed (p/parse scanned)) (def parsed (p/parse scanned))
(when (any? (parsed :errors)) (when (any? (parsed :errors))
(break (parsed :errors))) (break (-> :errors parsed (e/parse-error out))))
(def validated (v/valid parsed)) (def validated (v/valid parsed))
(when (any? (validated :errors)) (when (any? (validated :errors))
(break (validated :errors))) (break (-> :errors validated (e/validation-error out))))
(setdyn :out console)
(try (try
(i/interpret (parsed :ast) @{}) (set result (b/show (i/interpret (parsed :ast) @{})))
([e] (if (struct? e) (error (e :msg)) (error e))))) ([err] (e/runtime-error err out)))
(set (out :result) result)
(j/encode out))
(defn test [source])
(defn run-script [filename]
(def source (slurp filename))
(run source))
(def source ` (def source `
fn foo () -> :foo fn foo () -> :foo
fool () foo ()
`) `)
(main source) (-> source run j/decode)