From 6a1906c1aeddcb7f2a295e5f1b137e00cc9d4e97 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Mon, 4 Dec 2023 13:58:29 -0500 Subject: [PATCH] Fix the bugs. Not all of them. --- src/ludus/interpreter.cljc | 13 +++++++++---- src/ludus/node.cljc | 13 ++++++++++++- src/ludus/postlude.ld | 14 ++++++++------ src/ludus/prelude.ld | 27 +++++++++++++++++++++------ 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/ludus/interpreter.cljc b/src/ludus/interpreter.cljc index e2804c2..3d037fb 100644 --- a/src/ludus/interpreter.cljc +++ b/src/ludus/interpreter.cljc @@ -876,8 +876,13 @@ (defn get-line [source line] (if line - (let [lines (clojure.string/split source #"\n")] - (clojure.string/trim (nth lines (dec line)))))) + (let [lines (clojure.string/split source #"\n") + numlines (count lines) + gettable? (> numlines line)] + (if gettable? + (clojure.string/trim (nth lines (dec line))) + nil)) + )) (def runtime-error #?( @@ -895,7 +900,7 @@ interpreted (interpret-ast parsed base-ctx) namespace (dissoc interpreted ::data/type ::data/name ::data/struct) context (ns->ctx namespace)] - (println "Prelude fully loaded.") + ; (println "Prelude fully loaded.") context)) ; ;; 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 ">>> " (get-line source (get-in (ex-data e) [:ast :token :line]))) (println (ex-message e)) - (pp/pprint (ex-data e)) + ;(pp/pprint (ex-data e)) (throw e) )))) diff --git a/src/ludus/node.cljc b/src/ludus/node.cljc index 27f6830..62cccb8 100644 --- a/src/ludus/node.cljc +++ b/src/ludus/node.cljc @@ -39,4 +39,15 @@ clj_result (ld->clj ludus_result) ] #?(:clj clj_result :cljs (clj->js clj_result)) - )) \ No newline at end of file + )) + +(do + (def res (run " +fd! (100) +rt! (0.25) +fd! (100) +pencolor! (200) +fd! (50) + ")) + (:draw res) + ) \ No newline at end of file diff --git a/src/ludus/postlude.ld b/src/ludus/postlude.ld index 261ce3c..024d8a0 100644 --- a/src/ludus/postlude.ld +++ b/src/ludus/postlude.ld @@ -1,20 +1,22 @@ & this file runs after any given interpretation & 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 -let console_msgs = deref (console) -set! (console, []) +let console_msgs = flush! () let (r, g, b, a) = deref (bgcolor) -set! (bgcolor, colors :black) +make! (bgcolor, colors :black) let draw_calls = deref (p5_calls) -set! (p5_calls, []) +make! (p5_calls, []) #{ & :result result is provided elsewhere & :errors [] & if we get here there are no errors :console console_msgs - :draw concat ([(:background, r, g, b, a)], draw_calls) -} \ No newline at end of file + :draw concat ( + [(:background, r, g, b, a), (:stroke, 255, 255, 255, 255)] + draw_calls) +} diff --git a/src/ludus/prelude.ld b/src/ludus/prelude.ld index d6dab87..f78d253 100644 --- a/src/ludus/prelude.ld +++ b/src/ludus/prelude.ld @@ -127,6 +127,15 @@ fn append { ref console = [] +fn flush! { + "Clears the console, and returns the messages." + () -> { + let msgs = deref (console) + make! (console, []) + msgs + } +} + fn add_msg! { "Adds a message to the console." (msgs) -> { @@ -708,7 +717,9 @@ let turtle_angle = 0.375 let turtle_color = (100, 100, 100, 100) fn render_turtle! () -> { + print! ("Rendering turtle") let state = do turtle_states > deref > last + print! ("Collected state.") if state :visible? then { add_call! ((:push)) @@ -739,19 +750,19 @@ fn state/call () -> { let states = deref (turtle_states) let curr = last (states) 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 { :forward -> if curr :pendown? - then make_line (curr :position, prev :position) + then make_line (prev :position, curr :position) else nil :back -> if curr :pendown? - then make_line (curr :position, prev :position) + then make_line (prev :position, curr :position) else nil :home -> if curr :pendown? - then make_line (curr :position, prev :position) + then make_line (prev :position, curr :position) else nil :goto -> if curr :pendown? - then make_line (curr :position, prev :position) + then make_line (prev :position, curr :position) else nil :penwidth -> (:strokeWeight, curr :penwidth) :pencolor -> { @@ -859,6 +870,8 @@ fn apply_command { "Takes a turtle state and a command and calculates a new state." (state, command) -> match command with { (: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)) (:left, turns) -> update (state, :heading, add (_, turns)) (:forward, steps) -> { @@ -928,6 +941,7 @@ ns prelude { inc dec print! + flush! console show prn! @@ -995,6 +1009,7 @@ ns prelude { ceil round range + colors forward!, fd! back!, bk! right!, rt! @@ -1008,6 +1023,6 @@ ns prelude { pencolor, penwidth heading/vector turtle_state - p5_calls, turtle_states, turtle_commands + p5_calls, turtle_states, turtle_commands, bgcolor render_turtle! }