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) (def {:body clauses} fnn)
(string/join (map (fn [x] (-> x first show-patt)) clauses) " ")) (string/join (map (fn [x] (-> x first show-patt)) clauses) " "))
(defn doc! [fnn] (defn doc [fnn]
(def {:name name :doc doc :body clauses} fnn) (def {:name name :doc doc} fnn)
(print name) (string/join [name (pretty-patterns fnn) doc] "\n"))
(print (pretty-patterns fnn))
(print doc))
(defn- conj!-set [sett value] (defn- conj!-set [sett value]
(set (sett value) true) (set (sett value) true)
@ -204,7 +202,7 @@
"type" ludus/type "type" ludus/type
"stringify" stringify "stringify" stringify
"show" show "show" show
"doc!" doc! "doc" doc
"concat" concat "concat" concat
"conj" conj "conj" conj
"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 # 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 (try (os/cd "janet") ([_] nil)) # for REPL
(import /scanner :as s) (import /scanner :as s)
(import /parser :as p) (import /parser :as p)
@ -6,20 +8,9 @@
(import /interpreter :as i) (import /interpreter :as i)
(import /errors :as e) (import /errors :as e)
(import /base :as b) (import /base :as b)
(import /load-prelude :as prelude) (import /prelude :as prelude)
(import spork/json :as j) (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] (defn run [source]
(when (= :error prelude/pkg) (error "could not load prelude")) (when (= :error prelude/pkg) (error "could not load prelude"))
(def ctx @{:^parent prelude/ctx}) (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 out @{:errors errors :draw draw :result result :console console})
(def scanned (s/scan source)) (def scanned (s/scan source))
(when (any? (scanned :errors)) (when (any? (scanned :errors))
(break (do (set (out :errors) (scanned :errors))
(each err (scanned :errors) (each err (scanned :errors)
(e/scan-error err))))) (e/scan-error err))
(break out))
(def parsed (p/parse scanned)) (def parsed (p/parse scanned))
(when (any? (parsed :errors)) (when (any? (parsed :errors))
(break (each err (parsed :errors) (set (out :errors) (parsed :errors))
(e/parse-error err)))) (each err (parsed :errors)
(e/parse-error err))
(break out))
(def validated (v/valid parsed ctx)) (def validated (v/valid parsed ctx))
(when (any? (validated :errors)) (when (any? (validated :errors))
(break (each err (validated :errors) (set (out :errors) (validated :errors))
(e/validation-error err)))) (each err (validated :errors)
# (setdyn :out console) (e/validation-error err))
(break out))
(setdyn :out console)
(try (try
(set result (i/interpret (parsed :ast) ctx)) (set result (i/interpret (parsed :ast) ctx))
([err] (comment setdyn :out stdout) (e/runtime-error err))) ([err]
# (setdyn :out stdout) (e/runtime-error err)
(set (out :errors) [err])
(break out)))
(setdyn :out stdout)
(set (out :result) result) (set (out :result) result)
result) (var post @{})
(try
(do (set post (i/interpret prelude/post/ast ctx))
(def source ` ([err] (e/runtime-error err)))
forward! (100) (set (out :draw) (post :draw))
p5_calls (j/encode out))
`)
(-> source run)
)
(comment
Next up:
* postlude!
* testing turtle graphics
)

View File

@ -7,10 +7,10 @@ if turtle_state () :visible? then render_turtle! () else nil
reset_turtle! () reset_turtle! ()
let console_msgs = flush! () & let console_msgs = flush! ()
let (r, g, b, a) = unbox (bgcolor) let (r, g, b, a) = unbox (bgcolor)
make! (bgcolor, colors :black) store! (bgcolor, colors :black)
let draw_calls = unbox (p5_calls) let draw_calls = unbox (p5_calls)
store! (p5_calls, []) store! (p5_calls, [])
@ -18,7 +18,7 @@ store! (p5_calls, [])
#{ #{
& :result result is provided elsewhere & :result result is provided elsewhere
& :errors [] & if we get here there are no errors & :errors [] & if we get here there are no errors
:console console_msgs & :console console_msgs
:draw concat ( :draw concat (
[(:background, r, g, b, a), (:stroke, 255, 255, 255, 255)] [(:background, r, g, b, a), (:stroke, 255, 255, 255, 255)]
draw_calls) 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." "Sends a text representation of Ludus values to the console."
(...args) -> { (...args) -> {
base :print! (args) base :print! (args)
add_msg! (args) & add_msg! (args)
:ok :ok
} }
} }
@ -289,7 +289,7 @@ fn prn! {
"Prints the underlying Clojure data structure of a Ludus value." "Prints the underlying Clojure data structure of a Ludus value."
(x) -> { (x) -> {
base :prn (x) base :prn (x)
add_msg! (x) & add_msg! (x)
:ok :ok
} }
} }
@ -320,11 +320,11 @@ fn string? {
} }
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 as :string) -> x
(x) -> show (x) (x) -> show (x)
(x, ...xs) -> loop (x, xs) with { (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) (out, [x, ...xs]) -> recur (concat (out, show (x)), xs)
} }
} }