start packaging things up for wasm

This commit is contained in:
Scott Richmond 2024-06-06 16:14:04 -04:00
parent 03128441a6
commit baba0f4977
7 changed files with 37 additions and 94 deletions

View File

@ -93,11 +93,9 @@
(def {:body clauses} fnn)
(string/join (map (fn [x] (-> x first show-patt)) clauses) " "))
(defn doc! [fnn]
(def {:name name :doc doc :body clauses} fnn)
(print name)
(print (pretty-patterns fnn))
(print doc))
(defn doc [fnn]
(def {:name name :doc doc} fnn)
(string/join [name (pretty-patterns fnn) doc] "\n"))
(defn- conj!-set [sett value]
(set (sett value) true)
@ -204,7 +202,7 @@
"type" ludus/type
"stringify" stringify
"show" show
"doc!" doc!
"doc" doc
"concat" concat
"conj" conj
"conj!" conj!

View File

@ -1,43 +0,0 @@
(try (os/cd "janet") ([_] nil))
(import /base :as b)
(import /scanner :as s)
(import /parser :as p)
(import /validate :as v)
(import /interpreter :as i)
(import /errors :as e)
(def pkg (do
(def pre-ctx @{:^parent {"base" b/base}})
(def pre-src (slurp "prelude.ld"))
(def pre-scanned (s/scan pre-src :prelude))
(def pre-parsed (p/parse pre-scanned))
(def parse-errors (pre-parsed :errors))
(when (any? parse-errors) (each err parse-errors (e/parse-error err)) (break :error))
(def pre-validated (v/valid pre-parsed pre-ctx))
(def validation-errors (pre-validated :errors))
(when (any? validation-errors) (each err validation-errors (e/validation-error err)) (break :error))
(try
(i/interpret (pre-parsed :ast) pre-ctx)
([err] (e/runtime-error err) :error))))
(def ctx (do
(def ctx @{})
(each [k v] (pairs pkg)
(set (ctx (string k)) v))
(set (ctx "^name") nil)
(set (ctx "^type") nil)
ctx))
# (def post/src (slurp "postlude.ld"))
# (def post/ast (do
# (def post-ctx @{:^parent ctx})
# (def post-scanned (s/scan post/src :postlude))
# (def post-parsed (p/parse post-scanned))
# (def parse-errors (post-parsed :errors))
# (when (any? parse-errors) (each err parse-errors (e/parse-error err)) (break :error))
# (def post-validated (v/valid post-parsed post-ctx))
# (def validation-errors (post-validated :errors))
# (when (any? validation-errors) (each err validation-errors (e/validation-error err)) (break :error))
# post-parsed))

View File

@ -1,4 +1,6 @@
# an integrated Ludus interpreter
# 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 /scanner :as s)
(import /parser :as p)
@ -6,20 +8,9 @@
(import /interpreter :as i)
(import /errors :as e)
(import /base :as b)
(import /load-prelude :as prelude)
(import /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]
(when (= :error prelude/pkg) (error "could not load prelude"))
(def ctx @{:^parent prelude/ctx})
@ -30,38 +21,35 @@ This new scene will have to return a JSON POJSO:
(def out @{:errors errors :draw draw :result result :console console})
(def scanned (s/scan source))
(when (any? (scanned :errors))
(break (do
(set (out :errors) (scanned :errors))
(each err (scanned :errors)
(e/scan-error err)))))
(e/scan-error err))
(break out))
(def parsed (p/parse scanned))
(when (any? (parsed :errors))
(break (each err (parsed :errors)
(e/parse-error err))))
(set (out :errors) (parsed :errors))
(each err (parsed :errors)
(e/parse-error err))
(break out))
(def validated (v/valid parsed ctx))
(when (any? (validated :errors))
(break (each err (validated :errors)
(e/validation-error err))))
# (setdyn :out console)
(set (out :errors) (validated :errors))
(each err (validated :errors)
(e/validation-error err))
(break out))
(setdyn :out console)
(try
(set result (i/interpret (parsed :ast) ctx))
([err] (comment setdyn :out stdout) (e/runtime-error err)))
# (setdyn :out stdout)
([err]
(e/runtime-error err)
(set (out :errors) [err])
(break out)))
(setdyn :out stdout)
(set (out :result) result)
result)
(do
(def source `
forward! (100)
p5_calls
`)
(-> source run)
)
(comment
Next up:
* postlude!
* testing turtle graphics
)
(var post @{})
(try
(set post (i/interpret prelude/post/ast ctx))
([err] (e/runtime-error err)))
(set (out :draw) (post :draw))
(j/encode out))

View File

@ -7,10 +7,10 @@ if turtle_state () :visible? then render_turtle! () else nil
reset_turtle! ()
let console_msgs = flush! ()
& let console_msgs = flush! ()
let (r, g, b, a) = unbox (bgcolor)
make! (bgcolor, colors :black)
store! (bgcolor, colors :black)
let draw_calls = unbox (p5_calls)
store! (p5_calls, [])
@ -18,7 +18,7 @@ store! (p5_calls, [])
#{
& :result result is provided elsewhere
& :errors [] & if we get here there are no errors
:console console_msgs
& :console console_msgs
:draw concat (
[(:background, r, g, b, a), (:stroke, 255, 255, 255, 255)]
draw_calls)

Binary file not shown.

Binary file not shown.

View File

@ -275,7 +275,7 @@ fn print! {
"Sends a text representation of Ludus values to the console."
(...args) -> {
base :print! (args)
add_msg! (args)
& add_msg! (args)
:ok
}
}
@ -289,7 +289,7 @@ fn prn! {
"Prints the underlying Clojure data structure of a Ludus value."
(x) -> {
base :prn (x)
add_msg! (x)
& add_msg! (x)
:ok
}
}
@ -320,11 +320,11 @@ fn string? {
}
fn string {
"Converts a value to a string by using `show`. If it is a string, returns it unharmed. Use this to build up strings of differen kinds of values."
"Converts a value to a string by using `show`. If it is a string, returns it unharmed. Use this to build up strings of different kinds of values."
(x as :string) -> x
(x) -> show (x)
(x, ...xs) -> loop (x, xs) with {
(out, [x]) -> concat (out, show(x))
(out, [x]) -> concat (out, show (x))
(out, [x, ...xs]) -> recur (concat (out, show (x)), xs)
}
}