Do a bunch of work to get things mostly working on ludus-web

This commit is contained in:
Scott Richmond 2023-12-09 20:30:43 -05:00
parent 88e027b240
commit 7a4ad90380
7 changed files with 48 additions and 22 deletions

View File

@ -1,3 +1,6 @@
# start a repl # start a repl
repl: repl:
clj -X:repl clj -X:repl
build:
shadow-cljs release node

View File

@ -1,6 +1,6 @@
{ {
"name": "@ludus/ludus-js-pure", "name": "@ludus/ludus-js-pure",
"version": "0.1.0-alpha.5", "version": "0.1.0-alpha.7.6",
"description": "A Ludus interpreter in a pure JS function.", "description": "A Ludus interpreter in a pure JS function.",
"main": "target/js/ludus.js", "main": "target/js/ludus.js",
"directories": {}, "directories": {},

View File

@ -12,7 +12,7 @@
line-start (reduce (fn [len line] (+ len (count line))) (count lines-before) lines-before) line-start (reduce (fn [len line] (+ len (count line))) (count lines-before) lines-before)
from-start (- start line-start) from-start (- start line-start)
underline-length (count lexeme) underline-length (count lexeme)
padding (string/join (take (+ prefix from-start) (repeat " "))) padding (string/join (take (+ prefix from-start) (repeat "-")))
underline (string/join (take underline-length (repeat "^")))] underline (string/join (take underline-length (repeat "^")))]
(apply str padding underline) (apply str padding underline)
)) ))
@ -31,3 +31,9 @@
(str message "\n" prefix line "\n" underline) (str message "\n" prefix line "\n" underline)
) )
) )
(defn run-error [source {:keys [line message]}]
(if line
(str "Ludus panicked on line " line ":\n" (get-line source {:line line}) "\n" message)
(str "Ludus panicked!\n" message)
))

View File

@ -302,7 +302,7 @@
(if (= match-value value) (if (= match-value value)
{:success true :ctx {}} {:success true :ctx {}}
{:success false {:success false
:reason (str "No match: Could not match " (show/show-pattern match-value) " with " (show/show value))})) :reason (str "No match: Could not match " (show/show-pattern pattern) " with " (show/show value))}))
:word :word
(let [word (-> pattern :data first)] (let [word (-> pattern :data first)]
@ -965,10 +965,10 @@
;(println "Running source: " source) ;(println "Running source: " source)
(interpret-ast parsed base-ctx) (interpret-ast parsed base-ctx)
(catch #?(:clj Throwable :cljs js/Object) e (catch #?(:clj Throwable :cljs js/Object) e
(println "Ludus panicked!") ;(println "Ludus panicked!")
(println "On line" (get-in (ex-data e) [:ast :token :line])) ;(println "On line" (get-in (ex-data e) [:ast :token :line]))
(println ">>> " (get-line source (get-in (ex-data e) [:ast :token :line]))) ;(println ">>> " (get-line source (get-in (ex-data e) [:ast :token :line])))
(println (ex-message e)) ;(println (ex-message e))
;(pp/pprint (ex-data e)) ;(pp/pprint (ex-data e))
;(throw e) ;(throw e)
{::data/error true {::data/error true

View File

@ -51,7 +51,7 @@
(clean-out {:errors [(error/parse-error source user_parsed)]}) (clean-out {:errors [(error/parse-error source user_parsed)]})
(::data/error user_result) (::data/error user_result)
(clean-out {:errors [user_result]}) (clean-out (assoc (ld->clj post_result) :errors [(error/run-error source user_result)]))
:else :else
(clean-out clj_result) (clean-out clj_result)

View File

@ -256,7 +256,10 @@ 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 differen kinds of values."
(x as :string) -> x (x as :string) -> x
(x) -> show (x) (x) -> show (x)
(x, ...xs) -> fold (string, xs, x) (x, ...xs) -> loop (x, xs) with {
(out, [x]) -> concat (out, show(x))
(out, [x, ...xs]) -> recur (concat (out, show (x)), xs)
}
} }
fn join { fn join {
@ -887,31 +890,32 @@ fn make_line ((x1, y1), (x2, y2)) -> (:line, x1, y1, x2, y2)
let turtle_radius = 20 let turtle_radius = 20
let turtle_angle = 0.375 let turtle_angle = 0.385
let turtle_color = (150, 150, 150, 200) let turtle_color = (255, 255, 255, 150)
fn render_turtle! () -> { fn render_turtle! () -> {
let state = do turtle_states > deref > last let state = do turtle_states > deref > last
if state :visible? if state :visible?
then { then {
add_call! ((:push))
let (r, g, b, a) = turtle_color let (r, g, b, a) = turtle_color
add_call! ((:fill, r, g, b, a)) add_call! ((:fill, r, g, b, a))
add_call! ((:noStroke))
let #{heading, :position (x, y)} = state let #{heading, :position (x, y)} = state
add_call! ((:translate, x, y)) let first = mult ((0, 1), turtle_radius)
add_call! ((:rotate, turn/rad (add (heading, 0.25))))
add_call! ((:beginShape))
let head_unit = heading/vector (heading)
let first = mult (head_unit, turtle_radius)
let (x1, y1) = first let (x1, y1) = first
let (x2, y2) = rotate (first, turtle_angle) let (x2, y2) = rotate (first, turtle_angle)
let (x3, y3) = rotate (first, neg (turtle_angle)) let (x3, y3) = rotate (first, neg (turtle_angle))
add_call! ((:push))
add_call! ((:translate, x, y))
add_call! ((:rotate, turn/rad (heading)))
add_call! ((:noStroke))
add_call! ((:beginShape))
add_call! ((:vertex, x1, y1)) add_call! ((:vertex, x1, y1))
add_call! ((:vertex, x2, y2)) add_call! ((:vertex, x2, y2))
add_call! ((:vertex, x3, y3)) add_call! ((:vertex, x3, y3))
add_call! ((:endShape)) add_call! ((:endShape))
add_call! ((:stroke, 0))
add_call! ((:line, 0, 0, x1, y1))
add_call! ((:pop)) add_call! ((:pop))
:ok :ok
} }
@ -1046,8 +1050,8 @@ fn apply_command {
(:goto, (x, y)) -> assoc (state, :position, (x, y)) (:goto, (x, y)) -> assoc (state, :position, (x, y))
(:home) -> assoc (state, :position, (0, 0)) (:home) -> assoc (state, :position, (0, 0))
(:clear) -> assoc (state, :position, (0, 0)) (:clear) -> assoc (state, :position, (0, 0))
(:right, turns) -> update (state, :heading, sub (_, turns)) (:right, turns) -> update (state, :heading, add (_, turns))
(:left, turns) -> update (state, :heading, add (_, turns)) (:left, turns) -> update (state, :heading, sub (_, turns))
(:forward, steps) -> { (:forward, steps) -> {
let #{heading, position} = state let #{heading, position} = state
let unit = heading/vector (heading) let unit = heading/vector (heading)
@ -1208,6 +1212,7 @@ ns prelude {
penup!, pu! penup!, pu!
pendown!, pd! pendown!, pd!
pencolor!, pc! pencolor!, pc!
background!, bg!
penwidth!, pw! penwidth!, pw!
home!, clear!, goto!, home!, clear!, goto!,
heading, position, pendown? heading, position, pendown?

View File

@ -1,6 +1,10 @@
(ns ludus.show (ns ludus.show
(:require (:require
[ludus.data :as data] [ludus.data :as data]
; [ludus.scanner :as s]
; [ludus.parser :as p]
; [ludus.grammar :as g]
; [ludus.interpreter :as i]
[clojure.pprint :as pp])) [clojure.pprint :as pp]))
(declare show show-linear show-keyed) (declare show show-linear show-keyed)
@ -75,7 +79,7 @@
:else "else" :else "else"
(:word :number :keyword :true :false :nil :string) (-> pattern :data first) (:word :number :keyword :true :false :nil :string) (-> pattern :data first show)
:typed :typed
(let [word (-> pattern :data first :data first) (let [word (-> pattern :data first :data first)
@ -100,3 +104,11 @@
:struct-pattern (show-coll-pattern pattern ["@{" "}"]) :struct-pattern (show-coll-pattern pattern ["@{" "}"])
)) ))
(comment
(def source "let 1 = 0")
(def tokens (-> source s/scan :tokens))
(def ast (p/apply-parser g/script tokens))
(println (i/prettify-ast ast))
(println (-> ast :data first :data first show-pattern))
)