added some functions to the stdlib

This commit is contained in:
igor
2026-03-18 15:27:01 +01:00
parent 78bf6c132f
commit cda34e61bd
3 changed files with 82 additions and 0 deletions

View File

@@ -442,6 +442,14 @@
} }
; ;
:asm rswap {
mov rax, [r13] ; get top
mov rbx, [r13 + 8] ; get second
mov [r13], rbx ; swap
mov [r13 + 8], rax
}
;
#pick [* | n] -> [* | x] #pick [* | n] -> [* | x]
:asm pick { :asm pick {
mov rcx, [r12] ; get index mov rcx, [r12] ; get index

View File

@@ -58,6 +58,13 @@ word memdump
end end
end end
#memdump_bytes [*, len | addr] -> [* | addr]
word memdump_bytes
for
dup c@ puti cr 1 +
end
end
#realloc [*, addr, old_len | new_len] -> [* | new_addr] #realloc [*, addr, old_len | new_len] -> [* | new_addr]
word realloc word realloc
2 pick swap alloc 2 pick swap alloc

View File

@@ -378,3 +378,70 @@ word format
drop # drop counter (0) drop # drop counter (0)
end end
end end
# rotate N elements of the top of the stack
# nrot [*, x1 ... xN - 1 | xN] -> [*, xN, xN - 1 ... x2 | x1]
word nrot
dup 1 + 1 swap for
dup pick swap 2 +
end
1 - 2 / pick
dup for
swap >r rswap
end
1 + for
nip
end
for
rswap r>
end
end
# convert a string to a sequence of ascii codes of its characters and push the codes on to the stack,
# Warning! the sequence is reversed so the ascii code of the last character ends up first on the stack
# toascii [*, addr | LEN] -> [*, x, x1 ... xLEN - 1 | xLEN + 1]
word toascii
0 swap
for
2dup + c@
-rot
1 +
end
2drop
end
word splitby_str
"split_str isn't implemented yet" puts
end
# splitby [*, addr, len, addr1 | len1] -> [*, addr1, len1 ... addrN | lenN]
word splitby
dup 1 == if
splitby_char
else
splitby_str
end
end
# splitby_char [*, addr, len, addr1 | len] -> [*, addr1, len1 ... addrN | lenN]
word splitby_char
>r >r 2dup r> r> 2swap 2dup
>r >r toascii 1 rpick nrot r> r>
dup 3 + pick c@
0 >r
swap for
dup
3 pick == if rswap r> 1 + >r rswap over 0 c! end
swap 1 + swap >r nip r>
end
2drop 2drop drop
r> 1 + for
dup strlen 2dup
1 + +
end
drop
end