diff --git a/prelude.ld b/prelude.ld index e6cd5da..c05b897 100644 --- a/prelude.ld +++ b/prelude.ld @@ -1250,6 +1250,15 @@ fn turtle_state { () -> do turtle_states > unbox > last } +fn load_turtle_state! { + "Sets the turtle state to a previously saved state. Returns the state." + (state) -> { + update! (turtle_states, append (_, state)) + let call = state/call () + if call then { add_call! (call); :ok } else :ok + } +} + & position () -> (x, y) fn position { "Returns the turtle's current position." @@ -1348,6 +1357,7 @@ pkg Prelude { last left! list + load_turtle_state! lt! lt? lte? diff --git a/src/base.janet b/src/base.janet index 4645a96..76b6185 100644 --- a/src/base.janet +++ b/src/base.janet @@ -53,6 +53,8 @@ (set stringify stringify*) +(var show nil) + (defn- show-pkg [x] (def tab (struct/to-table x)) (set (tab :^name) nil) @@ -60,41 +62,50 @@ (string "pkg " (x :^name) " {" (stringify tab) "}") ) -(defn show [x] +(defn- dict-show [dict] + (string/join + (map + (fn [[k v]] (string (show k) " " (show v))) + (pairs dict)) + ", ")) + +(defn- show* [x] (case (ludus/type x) :nil "nil" :string (string "\"" x "\"") - :tuple (string "(" (string/join (map show x)) ")") - :list (string "[" (string/join (map show x)) "]") - :dict (string "#{" (string/join (map show x)) "}") - :set (string "${" (string/join (map show (keys x))) "}") + :tuple (string "(" (string/join (map show x) ", ") ")") + :list (string "[" (string/join (map show x) ", ") "]") + :dict (string "#{" (dict-show x) "}") + :set (string "${" (string/join (map show (keys x)) ", ") "}") :box (string "box " (x :name) " [ " (show x) " ]") :pkg (show-pkg x) (stringify x))) -(var json nil) +(set show show*) -(defn- dict-json [dict] - (string/join - (map - (fn [[k v]] (string (json k) ": " (json v))) - (pairs dict)) - ", ")) +# (var json nil) -(defn- json* [x] - (case (ludus/type x) - :nil "\"null\"" - :number (string x) - :bool (if true "\"true\"" "\"false\"") - :string (string "\"" x "\"") - :keyword (string "\"" x "\"") - :tuple (string "[" (string/join (map json x) ", ") "]") - :list (string "[" (string/join (map json x) ", ")"]") - :dict (string "{" (dict-json x) "}") - :set (string "[" (string/join (map json (keys x)) ", ") "]") - (show x))) +# (defn- dict-json [dict] +# (string/join +# (map +# (fn [[k v]] (string (json k) ": " (json v))) +# (pairs dict)) +# ", ")) -(set json json*) +# (defn- json* [x] +# (case (ludus/type x) +# :nil "\"null\"" +# :number (string x) +# :bool (if true "\"true\"" "\"false\"") +# :string (string "\"" x "\"") +# :keyword (string "\"" x "\"") +# :tuple (string "[" (string/join (map json x) ", ") "]") +# :list (string "[" (string/join (map json x) ", ")"]") +# :dict (string "{" (dict-json x) "}") +# :set (string "[" (string/join (map json (keys x)) ", ") "]") +# (show x))) + +# (set json json*) (defn show-patt [x] (case (x :type) diff --git a/src/ludus.janet b/src/ludus.janet index e8a036e..e74c2e6 100644 --- a/src/ludus.janet +++ b/src/ludus.janet @@ -53,4 +53,17 @@ (set (out :draw) (post :draw)) (-> out j/encode string)) - +# (comment +(do +(-> (ludus ` +penup! () +let s = turtle_state () +pendown! () +forward! (100) +rt! (0.25) +fd! (100) +load_turtle_state! (s) +pendown! () +lt! (0.25) +fd! (50) +`)))