import string ; strcat, strpack, strrev, strunpack
; prints the character at the top of the stack until terminating zero ; [0 … C] => [] print: :strunpack _print_loop:
dup jz _print_done ochr jump _print_loop
_print_done: pop ret
; print with newline ; [0 … C] => [] println: :print push 10 ochr ret
; reads a line of input onto the top of the stack as a packed string ; ! clobbers heap address -1 ; [] => [L] getline: push 0 ; terminator for strpack _getline_loop:
push -1 dup ichr load dup jn _getline_eof dup push 10 sub jz _getline_done jump _getline_loop
_getline_eof: pop _getline_done: :strpack :strrev ret
; displays the string S then reads a line of input ; [S] => [L] prompt: :print :getline ret
; consume stdin until EOF into the string S ; [] => [S] readall: push 0 ; accumulated string _readall_loop:
:getline dup jz _readall_done :strcat jump _readall_loop
_readall_done: pop ret
; returns the contents C of the file at path P (a string) ; NON-STANDARD! This function makes use of the `shell` instruction, which is ; only(?) available in the Spiceweight Whitespace interpreter. ; [P] => [C] readfile: push “cat ” swap :strcat shell ret