ludus/src/ludus.janet

95 lines
2.3 KiB
Plaintext
Raw Normal View History

# 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
# (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)
(import /src/json :as j)
(defn ludus [source]
(when (= :error prelude/pkg) (error "could not load prelude"))
2024-06-06 00:16:29 +00:00
(def ctx @{:^parent prelude/ctx})
(def errors @[])
(def draw @[])
(var result @"")
(def console @"")
2024-06-14 18:53:23 +00:00
(setdyn :out console)
(def out @{:errors errors :draw draw :result result :console console})
(def scanned (s/scan source))
(when (any? (scanned :errors))
(each err (scanned :errors)
2024-06-06 20:14:04 +00:00
(e/scan-error err))
(break (-> out j/encode string)))
(def parsed (p/parse scanned))
(when (any? (parsed :errors))
2024-06-06 20:14:04 +00:00
(each err (parsed :errors)
(e/parse-error err))
(break (-> out j/encode string)))
2024-06-06 00:16:29 +00:00
(def validated (v/valid parsed ctx))
(when (any? (validated :errors))
2024-06-06 20:14:04 +00:00
(each err (validated :errors)
(e/validation-error err))
2024-06-10 22:26:48 +00:00
(break (-> out j/encode string)))
(try
(set result (i/interpret (parsed :ast) ctx))
2024-06-06 20:14:04 +00:00
([err]
(e/runtime-error err)
(break (-> out j/encode string))))
2024-06-06 20:14:04 +00:00
(setdyn :out stdout)
(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))
(-> out j/encode string))
# (comment
(do
2024-06-14 18:53:23 +00:00
(def source `
fn strip_punctuation {
("{x},{y}") -> strip_punctuation ("{x}{y}")
("{x}.{y}") -> strip_punctuation ("{x}{y}")
("{x};{y}") -> strip_punctuation ("{x}{y}")
("{x}:{y}") -> strip_punctuation ("{x}{y}")
("{x}?{y}") -> strip_punctuation ("{x}{y}")
("{x}!{y}") -> strip_punctuation ("{x}{y}")
(x) -> x
2024-06-14 21:17:23 +00:00
}
fn trim_left {
(" {x}") -> trim_left ("{x}")
("\n{x}") -> trim_left ("{x}")
("\t{x}") -> trim_left ("{x}")
(x) -> x
}
fn trim_right {
("{x} ") -> trim_right ("{x}")
("{x}\n") -> trim_right ("{x}")
("{x}\t") -> trim_right ("{x}")
(x) -> x
}
fn trim (x) -> do x > trim_left > trim_right
trim_left ("
foo")
2024-06-14 18:53:23 +00:00
`)
2024-06-10 22:26:48 +00:00
(def out (-> source
2024-06-14 21:17:23 +00:00
ludus
2024-06-14 18:53:23 +00:00
j/decode
))
(setdyn :out stdout)
2024-06-10 22:26:48 +00:00
(def console (out "console"))
(print console)
(def result (out "result"))
(print result)
)