csb8
Description of the page content
Model of circular string buffer for an 8-bit Forth.
csb8 is a rewrote of csb2 in order to use it as a prototype to be implemented in DZX-Forth. It is written for an 8-bit machine with 1-byte chars (chars
and char+
are not used), but it should work without change in most Forth systems. Beside, it is written with few words in order to make it faster, though the final implementation in DZX-Forth was to be written in Z80.
Source code
\ csb8
\ csb8 is an implementation of a circular string buffer, to be used as
\ a prototype for an 8-bit Forth system with 1-byte characters.
\ Copyright (C) 2015 Marcos Cruz (programandala.net)
\
\ csb8 is free software distributed under the terms of the
\ Programandala License
\ See: <http://programandala.net/licence>
\ NO WARRANTY OF ANY KIND
\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Acknowledgment
\ csb8 is based on csb2
\ (http://programandala.net/es.programa.csb2.html). csb2 was partly
\ based on code by Wil Baden (published in Forth Dimensions,
\ 1996-07/08, volume 18, issue 2, page 6, http://forth.org).
\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ History
\ 2015-01-29: Start.
\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Circular string buffer
512 constant /csb \ buffer size
/csb create csb-unused , \ unused buffer space
here /csb allot constant csb0 \ bottom address
\ XXX TODO -- rewrite, make it faster:
: ?csb ( len -- )
\ Make sure there's room for the given characters.
dup csb-unused @ > if /csb csb-unused ! then
negate csb-unused +! ;
: string-allocate ( len -- ca )
?csb csb0 csb-unused @ + ;
: save-string ( ca1 len1 -- ca2 len1 )
dup string-allocate swap 2dup 2>r move 2r> ;
: save-counted-string ( ca1 len1 -- ca2 )
dup 1+ string-allocate dup >r place r> ;
\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ String tools
: (s) ( compilation: ca len -- ) ( run-time: ca len -- ca2 len )
state @ if postpone sliteral else save-string then ;
: s| ( "ccc<|>" -- ca len ) [char] | parse (s) ; immediate
: s" ( "ccc<quote>" -- ca len ) [char] " parse (s) ; immediate
: lengths ( ca1 len1 ca2 len2 -- ca1 len1 ca2 len2 len1 len2 )
2 pick over ;
: smove ( ca1 len ca2 -- ) swap move ;
: s+ ( ca1 len1 ca2 len2 -- ca3 len3 )
\ Append the string ca2 len2 to the end of string ca1 len1
\ returning the resulting string ca2 len3.
lengths + >r ( ca1 len2 ca2 len2 ) ( R: len3 )
r@ string-allocate >r ( R: len3 ca3 )
2 pick r@ + ( ca1 len1 ca2 len2 len1+ca3 )
smove ( ca1 len1 ) \ second string to buffer
r@ smove \ first string to buffer
r> r> ;
: count ( ca1 -- ca2 len ) count save-string ;