renamed some functions in stdlib/arr.sl and made examples/snake.sl use the functions from stdlib/arr.sl instead of custom ones

This commit is contained in:
IgorCielniak
2026-03-21 14:03:39 +01:00
parent 5dd361e563
commit 2055aa3b1f
5 changed files with 85 additions and 96 deletions

View File

@@ -1,7 +1,8 @@
# Terminal Snake (classic real-time: WASD steer, q quit)
import stdlib/stdlib.sl
import stdlib/linux.sl
import stdlib.sl
import arr.sl
import linux.sl
macro WIDTH 0 20 ;
macro HEIGHT 0 12 ;
@@ -40,18 +41,6 @@ macro DIR_DOWN 0 1 ;
macro DIR_LEFT 0 2 ;
macro DIR_UP 0 3 ;
#arr_get [*, arr | idx] -> [* | value]
word arr_get
8 * + @
end
#arr_set [*, arr, idx | value] -> [*]
word arr_set
>r
8 * +
r> !
end
#xy_idx [*, x | y] -> [* | idx]
word xy_idx
WIDTH * +
@@ -60,14 +49,14 @@ end
#board_get [*, board, x | y] -> [* | value]
word board_get
xy_idx
arr_get
1 - arr_get
end
#board_set [*, board, x, y | value] -> [*]
word board_set
>r
xy_idx
r> arr_set
r> swap 1 - arr_set
end
#state_dir@ [* | state] -> [* | dir]
@@ -304,16 +293,16 @@ word init_snake
WIDTH 2 /
HEIGHT 2 /
with cx cy in
xs 0 cx arr_set
ys 0 cy arr_set
xs 0 cx swap 1 - arr_set
ys 0 cy swap 1 - arr_set
b cx cy 1 board_set
xs 1 cx 1 - arr_set
ys 1 cy arr_set
xs 1 cx 1 - swap 1 - arr_set
ys 1 cy swap 1 - arr_set
b cx 1 - cy 1 board_set
xs 2 cx 2 - arr_set
ys 2 cy arr_set
xs 2 cx 2 - swap 1 - arr_set
ys 2 cy swap 1 - arr_set
b cx 2 - cy 1 board_set
end
end
@@ -328,7 +317,7 @@ word spawn_food
with start tried found in
while tried CELLS < do
start tried + CELLS %
dup b swap arr_get 0 == if
dup b swap 1 - arr_get 0 == if
dup WIDTH % s swap state_food_x!
dup WIDTH / s swap state_food_y!
drop
@@ -367,12 +356,12 @@ word draw_game
42 putc
else
over WIDTH * over +
b swap arr_get
b swap 1 - arr_get
if 111 putc else 46 putc end
end
else
over WIDTH * over +
b swap arr_get
b swap 1 - arr_get
if 111 putc else 46 putc end
end
1 +
@@ -458,8 +447,8 @@ end
#step_game [*, board, xs, ys | state] -> [*]
word step_game
with b xs ys s in
xs 0 arr_get
ys 0 arr_get
xs 0 1 - arr_get
ys 0 1 - arr_get
with hx hy in
hx
hy
@@ -507,8 +496,8 @@ word step_game
grow 0 == if
s state_len@ 1 -
with ti in
xs ti arr_get
ys ti arr_get
xs ti 1 - arr_get
ys ti 1 - arr_get
with tx ty in
b tx ty 0 board_set
end
@@ -528,16 +517,16 @@ word step_game
end
while dup 0 > do
dup >r
xs r@ xs r@ 1 - arr_get arr_set
ys r@ ys r@ 1 - arr_get arr_set
xs r@ xs r@ 2 - arr_get swap 1 - arr_set
ys r@ ys r@ 2 - arr_get swap 1 - arr_set
rdrop
1 -
end
drop
# write new head
xs 0 nx arr_set
ys 0 ny arr_set
xs 0 nx swap 1 - arr_set
ys 0 ny swap 1 - arr_set
b nx ny 1 board_set
grow if

View File

@@ -39,8 +39,8 @@ word arr_cap 8 + @ end
#arr_data [* | arr] -> [* | ptr]
word arr_data 16 + @ end
#arr_free [* | arr] -> [*]
word arr_free
#dyn_arr_free [* | arr] -> [*]
word dyn_arr_free
dup arr_cap 8 * 24 + free
end
@@ -81,7 +81,7 @@ word arr_reserve
arr_copy_elements
# Free old and return new
swap arr_free
swap dyn_arr_free
nip
end
end
@@ -116,15 +116,15 @@ word arr_pop
end
end
#arr_get [*, arr | i] -> [* | x]
#dyn_arr_get [*, arr | i] -> [* | x]
# Get element at index i
word arr_get
word dyn_arr_get
swap arr_data swap 8 * + @
end
#arr_set [*, arr, x | i] -> [*]
#dyn_arr_set [*, arr, x | i] -> [*]
# Set element at index i to x
word arr_set
word dyn_arr_set
rot arr_data swap 8 * + swap !
end
@@ -149,21 +149,21 @@ word arr_item_ptr
swap 8 * swap 8 + +
end
#arr_get_static [*, arr | i] -> [* | x]
#arr_get [*, arr | i] -> [* | x]
# Get element from built-in static array
word arr_get_static
word arr_get
swap arr_item_ptr @
end
#arr_set_static [*, arr, x | i] -> [*]
#arr_set [*, arr, x | i] -> [*]
# Set element in built-in static array
word arr_set_static
word arr_set
rot arr_item_ptr swap !
end
#arr_static_free [* | arr] -> [*]
#arr_free [* | arr] -> [*]
# Free built-in static array allocation produced by list literals.
word arr_static_free
word arr_free
dup @ 1 + 8 * free
end

View File

@@ -19,22 +19,22 @@ word main
dup arr_len puti cr
dup arr_cap puti cr
# arr_get
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
# dyn_arr_get
dup 0 dyn_arr_get puti cr
dup 1 dyn_arr_get puti cr
dup 2 dyn_arr_get puti cr
# arr_set
dup 99 1 arr_set
dup 1 arr_get puti cr
# dyn_arr_set
dup 99 1 dyn_arr_set
dup 1 dyn_arr_get puti cr
# arr_reserve (with len > 0 so element copy path is exercised)
dup 8 arr_reserve
dup arr_cap puti cr
dup arr_len puti cr
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
dup 0 dyn_arr_get puti cr
dup 1 dyn_arr_get puti cr
dup 2 dyn_arr_get puti cr
# arr_pop (including empty pop)
arr_pop puti cr
@@ -43,16 +43,16 @@ word main
arr_pop puti cr
dup arr_len puti cr
arr_free
dyn_arr_free
# arr_to_dyn (convert std list to dynamic array)
[ 7 8 9 ] dup arr_to_dyn
dup arr_len puti cr
dup arr_cap puti cr
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
arr_free
dup 0 dyn_arr_get puti cr
dup 1 dyn_arr_get puti cr
dup 2 dyn_arr_get puti cr
dyn_arr_free
# free list allocation: bytes = (len + 1) * 8
dup @ 1 + 8 * free
@@ -64,21 +64,21 @@ word main
dup 2 arr_push
dup dyn_arr_sorted
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
arr_free
dup 0 dyn_arr_get puti cr
dup 1 dyn_arr_get puti cr
dup 2 dyn_arr_get puti cr
dyn_arr_free
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
dup 0 dyn_arr_get puti cr
dup 1 dyn_arr_get puti cr
dup 2 dyn_arr_get puti cr
# dyn_arr_sort (alias) sorts in place
dyn_arr_sort
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
arr_free
dup 0 dyn_arr_get puti cr
dup 1 dyn_arr_get puti cr
dup 2 dyn_arr_get puti cr
dyn_arr_free
# dyn_arr_sorted (alias) returns a sorted copy
5 arr_new
@@ -87,13 +87,13 @@ word main
dup 6 arr_push
dup dyn_arr_sorted
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
arr_free
dup 0 dyn_arr_get puti cr
dup 1 dyn_arr_get puti cr
dup 2 dyn_arr_get puti cr
dyn_arr_free
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
arr_free
dup 0 dyn_arr_get puti cr
dup 1 dyn_arr_get puti cr
dup 2 dyn_arr_get puti cr
dyn_arr_free
end

View File

@@ -4,22 +4,22 @@ import ../stdlib/arr.sl
word main
[ 4 1 3 2 ] dup arr_sort
dup 0 arr_get_static puti cr
dup 1 arr_get_static puti cr
dup 2 arr_get_static puti cr
dup 3 arr_get_static puti cr
arr_static_free
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
dup 3 arr_get puti cr
arr_free
[ 9 5 7 ] dup arr_sorted
dup 0 arr_get_static puti cr
dup 1 arr_get_static puti cr
dup 2 arr_get_static puti cr
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
swap
dup 0 arr_get_static puti cr
dup 1 arr_get_static puti cr
dup 2 arr_get_static puti cr
dup 0 arr_get puti cr
dup 1 arr_get puti cr
dup 2 arr_get puti cr
arr_static_free
arr_static_free
arr_free
arr_free
end

View File

@@ -5,13 +5,13 @@ import ../stdlib/arr.sl
# Get element from static array, preserving the array pointer
# [*, arr | i] -> [*, arr | value]
word aget
over swap arr_get_static
over swap arr_get
end
# Set element in static array, preserving the array pointer
# [*, arr, value | i] -> [* | arr]
word aset
rot dup >r -rot arr_set_static r>
rot dup >r -rot arr_set r>
end
# Swap elements at indices i and j in a static array
@@ -86,7 +86,7 @@ end
word print_arr
dup @ 0
while 2dup > do
2 pick over arr_get_static puti cr
2 pick over arr_get puti cr
1 +
end
2drop drop