csb8

Descripción del contenido de la página

Modelo de tampón circular de cadenas de texto para un Forth de ocho bitios.

Etiquetas:

csb8 es una reescritura de csb2 con el objeto de servir de prototipo para la implementación de un tampón circular de cadenas de texto en DZX-Forth. Está escrito pues para una máquina de 8 bitios, con caracteres de un octeto (chars y char+ no se usan en el código), aunque funcionaría sin cambios en la mayoría de sistemas Forth. También está escrito con el menor número de palabras posible, para que sea rápido; en todo caso la implementación definitiva en DZX-Forth iba a estar escrita en Z80.

En la página de csb2 hay una definición de lo que es un tampón (o «almacén» o «búfer») circular de cadenas.

Código fuente

\ 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  ;


Descargas

Páginas relacionadas

csb2
Utilidad en Forth para implementar un almacén circular de cadenas de texto.
sbuffer
Búfer circular de cadenas de texto programado en Forth.