2024-06-05 15:55:06 +00:00
|
|
|
# an integrated Ludus interpreter
|
2024-06-06 20:14:04 +00:00
|
|
|
# devised in order to run under wasm
|
|
|
|
# takes a string, returns a string with a json object
|
2024-06-06 22:47:04 +00:00
|
|
|
# (try (os/cd "janet") ([_] nil)) # for REPL
|
|
|
|
(import /src/scanner :as s)
|
|
|
|
(import /src/parser :as p)
|
|
|
|
(import /src/validate :as v)
|
|
|
|
(import /src/interpreter :as i)
|
|
|
|
(import /src/errors :as e)
|
|
|
|
(import /src/base :as b)
|
|
|
|
(import /src/prelude :as prelude)
|
2024-06-07 17:42:11 +00:00
|
|
|
(import /src/json :as j)
|
2024-06-05 17:01:43 +00:00
|
|
|
|
2024-06-06 22:47:04 +00:00
|
|
|
(defn ludus [source]
|
2024-06-06 19:41:33 +00:00
|
|
|
(when (= :error prelude/pkg) (error "could not load prelude"))
|
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-06 20:14:04 +00:00
|
|
|
(set (out :errors) (scanned :errors))
|
2024-06-05 21:47:41 +00:00
|
|
|
(each err (scanned :errors)
|
2024-06-06 20:14:04 +00:00
|
|
|
(e/scan-error err))
|
|
|
|
(break out))
|
2024-06-05 15:55:06 +00:00
|
|
|
(def parsed (p/parse scanned))
|
|
|
|
(when (any? (parsed :errors))
|
2024-06-06 20:14:04 +00:00
|
|
|
(set (out :errors) (parsed :errors))
|
|
|
|
(each err (parsed :errors)
|
|
|
|
(e/parse-error err))
|
|
|
|
(break out))
|
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-06 20:14:04 +00:00
|
|
|
(set (out :errors) (validated :errors))
|
|
|
|
(each err (validated :errors)
|
|
|
|
(e/validation-error err))
|
|
|
|
(break out))
|
|
|
|
(setdyn :out console)
|
2024-06-05 15:55:06 +00:00
|
|
|
(try
|
2024-06-06 19:41:33 +00:00
|
|
|
(set result (i/interpret (parsed :ast) ctx))
|
2024-06-06 20:14:04 +00:00
|
|
|
([err]
|
|
|
|
(e/runtime-error err)
|
|
|
|
(set (out :errors) [err])
|
|
|
|
(break out)))
|
|
|
|
(setdyn :out stdout)
|
2024-06-06 22:47:04 +00:00
|
|
|
(set (out :result) (b/show result))
|
2024-06-06 20:14:04 +00:00
|
|
|
(var post @{})
|
|
|
|
(try
|
|
|
|
(set post (i/interpret prelude/post/ast ctx))
|
|
|
|
([err] (e/runtime-error err)))
|
|
|
|
(set (out :draw) (post :draw))
|
2024-06-07 17:42:11 +00:00
|
|
|
(string (j/encode out)))
|
|
|
|
|
2024-06-06 22:47:04 +00:00
|
|
|
|