ludus/janet/ludus.janet

65 lines
1.7 KiB
Plaintext
Raw Normal View History

# 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 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 [...]}
)
(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))
(when (any? (scanned :errors))
(break (-> :errors scanned (e/scan-error out))))
(def parsed (p/parse scanned))
(when (any? (parsed :errors))
(break (-> :errors parsed (e/parse-error out))))
(def validated (v/valid parsed))
(when (any? (validated :errors))
(break (-> :errors validated (e/validation-error out))))
(setdyn :out console)
(try
(set result (b/show (i/interpret (parsed :ast) @{})))
([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 `
fn foo () -> :foo
foo ()
`)
(-> source run j/decode)