Fix the bugs. Not all of them.

This commit is contained in:
Scott Richmond 2023-12-04 13:58:29 -05:00
parent efb33cc1be
commit 6a1906c1ae
4 changed files with 50 additions and 17 deletions

View File

@ -876,8 +876,13 @@
(defn get-line [source line] (defn get-line [source line]
(if line (if line
(let [lines (clojure.string/split source #"\n")] (let [lines (clojure.string/split source #"\n")
(clojure.string/trim (nth lines (dec line)))))) numlines (count lines)
gettable? (> numlines line)]
(if gettable?
(clojure.string/trim (nth lines (dec line)))
nil))
))
(def runtime-error (def runtime-error
#?( #?(
@ -895,7 +900,7 @@
interpreted (interpret-ast parsed base-ctx) interpreted (interpret-ast parsed base-ctx)
namespace (dissoc interpreted ::data/type ::data/name ::data/struct) namespace (dissoc interpreted ::data/type ::data/name ::data/struct)
context (ns->ctx namespace)] context (ns->ctx namespace)]
(println "Prelude fully loaded.") ; (println "Prelude fully loaded.")
context)) context))
; ;; TODO: update this to use new parser pipeline & new AST representation ; ;; TODO: update this to use new parser pipeline & new AST representation
@ -948,7 +953,7 @@
(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)
)))) ))))

View File

@ -40,3 +40,14 @@
] ]
#?(:clj clj_result :cljs (clj->js clj_result)) #?(:clj clj_result :cljs (clj->js clj_result))
)) ))
(do
(def res (run "
fd! (100)
rt! (0.25)
fd! (100)
pencolor! (200)
fd! (50)
"))
(:draw res)
)

View File

@ -1,20 +1,22 @@
& this file runs after any given interpretation & this file runs after any given interpretation
& the goal is to output any global state & the goal is to output any global state
& this does not have base loaded into it: must be pure ludus
if turtle_state() :visible? then render_turtle! () else nil if turtle_state() :visible? then render_turtle! () else nil
let console_msgs = deref (console) let console_msgs = flush! ()
set! (console, [])
let (r, g, b, a) = deref (bgcolor) let (r, g, b, a) = deref (bgcolor)
set! (bgcolor, colors :black) make! (bgcolor, colors :black)
let draw_calls = deref (p5_calls) let draw_calls = deref (p5_calls)
set! (p5_calls, []) make! (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 ([(:background, r, g, b, a)], draw_calls) :draw concat (
[(:background, r, g, b, a), (:stroke, 255, 255, 255, 255)]
draw_calls)
} }

View File

@ -127,6 +127,15 @@ fn append {
ref console = [] ref console = []
fn flush! {
"Clears the console, and returns the messages."
() -> {
let msgs = deref (console)
make! (console, [])
msgs
}
}
fn add_msg! { fn add_msg! {
"Adds a message to the console." "Adds a message to the console."
(msgs) -> { (msgs) -> {
@ -708,7 +717,9 @@ let turtle_angle = 0.375
let turtle_color = (100, 100, 100, 100) let turtle_color = (100, 100, 100, 100)
fn render_turtle! () -> { fn render_turtle! () -> {
print! ("Rendering turtle")
let state = do turtle_states > deref > last let state = do turtle_states > deref > last
print! ("Collected state.")
if state :visible? if state :visible?
then { then {
add_call! ((:push)) add_call! ((:push))
@ -739,19 +750,19 @@ fn state/call () -> {
let states = deref (turtle_states) let states = deref (turtle_states)
let curr = last (states) let curr = last (states)
let prev = nth (states, sub (count (states), 2)) let prev = nth (states, sub (count (states), 2))
print! ("Curr, prev, command", curr, prev, cmd) & print! ("Curr, prev, command", curr, prev, cmd)
match cmd with { match cmd with {
:forward -> if curr :pendown? :forward -> if curr :pendown?
then make_line (curr :position, prev :position) then make_line (prev :position, curr :position)
else nil else nil
:back -> if curr :pendown? :back -> if curr :pendown?
then make_line (curr :position, prev :position) then make_line (prev :position, curr :position)
else nil else nil
:home -> if curr :pendown? :home -> if curr :pendown?
then make_line (curr :position, prev :position) then make_line (prev :position, curr :position)
else nil else nil
:goto -> if curr :pendown? :goto -> if curr :pendown?
then make_line (curr :position, prev :position) then make_line (prev :position, curr :position)
else nil else nil
:penwidth -> (:strokeWeight, curr :penwidth) :penwidth -> (:strokeWeight, curr :penwidth)
:pencolor -> { :pencolor -> {
@ -859,6 +870,8 @@ fn apply_command {
"Takes a turtle state and a command and calculates a new state." "Takes a turtle state and a command and calculates a new state."
(state, command) -> match command with { (state, command) -> match command with {
(:goto, (x, y)) -> assoc (state, :position, (x, y)) (:goto, (x, y)) -> assoc (state, :position, (x, y))
(:home) -> assoc (state, :position, (0, 0))
(:clear) -> assoc (state, :position, (0, 0))
(:right, turns) -> update (state, :heading, sub (_, turns)) (:right, turns) -> update (state, :heading, sub (_, turns))
(:left, turns) -> update (state, :heading, add (_, turns)) (:left, turns) -> update (state, :heading, add (_, turns))
(:forward, steps) -> { (:forward, steps) -> {
@ -928,6 +941,7 @@ ns prelude {
inc inc
dec dec
print! print!
flush!
console console
show show
prn! prn!
@ -995,6 +1009,7 @@ ns prelude {
ceil ceil
round round
range range
colors
forward!, fd! forward!, fd!
back!, bk! back!, bk!
right!, rt! right!, rt!
@ -1008,6 +1023,6 @@ ns prelude {
pencolor, penwidth pencolor, penwidth
heading/vector heading/vector
turtle_state turtle_state
p5_calls, turtle_states, turtle_commands p5_calls, turtle_states, turtle_commands, bgcolor
render_turtle! render_turtle!
} }