2024-06-05 15:55:06 +00:00
|
|
|
# an integrated Ludus interpreter
|
|
|
|
(try (os/cd "janet") ([_] nil)) # for REPL
|
2024-06-05 17:01:43 +00:00
|
|
|
(import /scanner :as s)
|
|
|
|
(import /parser :as p)
|
|
|
|
(import /validate :as v)
|
|
|
|
(import /interpreter :as i)
|
|
|
|
(import /errors :as e)
|
|
|
|
(import /base :as b)
|
2024-06-06 00:16:29 +00:00
|
|
|
(import /load-prelude :as prelude)
|
2024-06-05 17:01:43 +00:00
|
|
|
(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:
|
2024-06-05 19:52:03 +00:00
|
|
|
{:console "..." :result "..." :draw [...] :errors [...]}
|
2024-06-05 17:01:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
(defn run [source]
|
2024-06-06 00:16:29 +00:00
|
|
|
(def ctx @{:^parent prelude/ctx})
|
2024-06-05 17:01:43 +00:00
|
|
|
(def errors @[])
|
|
|
|
(def draw @[])
|
|
|
|
(var result @"")
|
|
|
|
(def console @"")
|
|
|
|
(def out @{:errors errors :draw draw :result result :console console})
|
2024-06-05 15:55:06 +00:00
|
|
|
(def scanned (s/scan source))
|
|
|
|
(when (any? (scanned :errors))
|
2024-06-05 21:47:41 +00:00
|
|
|
(break (do
|
|
|
|
(each err (scanned :errors)
|
|
|
|
(e/scan-error err)))))
|
2024-06-05 15:55:06 +00:00
|
|
|
(def parsed (p/parse scanned))
|
|
|
|
(when (any? (parsed :errors))
|
2024-06-05 21:47:41 +00:00
|
|
|
(break (each err (parsed :errors)
|
|
|
|
(e/parse-error err))))
|
2024-06-06 00:16:29 +00:00
|
|
|
(def validated (v/valid parsed ctx))
|
2024-06-05 15:55:06 +00:00
|
|
|
(when (any? (validated :errors))
|
2024-06-05 21:47:41 +00:00
|
|
|
(break (each err (validated :errors)
|
|
|
|
(e/validation-error err))))
|
2024-06-05 17:01:43 +00:00
|
|
|
(setdyn :out console)
|
2024-06-06 00:16:29 +00:00
|
|
|
(print "starting ludus run")
|
2024-06-05 15:55:06 +00:00
|
|
|
(try
|
2024-06-06 00:16:29 +00:00
|
|
|
(set result (b/show (i/interpret (parsed :ast) ctx)))
|
2024-06-05 21:47:41 +00:00
|
|
|
([err] (setdyn :out stdout) (e/runtime-error err)))
|
|
|
|
(setdyn :out stdout)
|
2024-06-05 17:01:43 +00:00
|
|
|
(set (out :result) result)
|
2024-06-06 00:16:29 +00:00
|
|
|
result)
|
2024-06-05 15:55:06 +00:00
|
|
|
|
2024-06-06 00:16:29 +00:00
|
|
|
(do
|
2024-06-05 15:55:06 +00:00
|
|
|
(def source `
|
2024-06-06 03:03:08 +00:00
|
|
|
|
2024-06-05 15:55:06 +00:00
|
|
|
`)
|
|
|
|
|
2024-06-06 00:16:29 +00:00
|
|
|
(-> source run)
|
|
|
|
)
|
2024-06-05 17:01:43 +00:00
|
|
|
|
2024-06-06 03:03:08 +00:00
|
|
|
|