ludus/janet/ludus.janet
2024-06-05 23:03:08 -04:00

61 lines
1.6 KiB
Plaintext

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