[comp.lang.forth] comma-free arrays

gamber@cosmo.UUCP (Johannes Teich) (10/13/89)

Forthers,
I have two questions concerning 'comma-free' arrays. (Skip 40 lines.:-)
 
 
If I have to preset a big array, I don't like to use a lot of commas.
Instead, I prefer this way:
 
16 uwarray <name>                            hex
    0001 0002 0004 0008 0010 0020 0040 0080
    0100 0200 0400 0800 1000 2000 4000 8000  decimal
16 <name> !uwarray
 
At load time, the 16 values are stacked and then filled into the array.
 
Here are four versions with counted/uncounted arrays of words/bytes:
 
V d variable <name> -2 allot  1 , 2 , 3 , |
V may be replaced by:  d 3 uwarray <name>   1 2 3   3 <name> !uwarray |
: uwarray   create  0  ?do  0 ,  loop ;
: !uwarray   swap 1- 0 swap  do        tuck i 2* + !
                             -1 +loop  drop ;
 
V d variable <name> -2 allot  1 c, 2 c, 3 c, |
V may be replaced by:  d 3 ubarray <name>   1 2 3   3 <name> !ubarray |
: ubarray   create  0  ?do  0 c,  loop ;
: !ubarray   swap 1- 0 swap  do        tuck i + c!
                             -1 +loop  drop ;
 
V d variable <name> -2 allot  3 c,  1 , 2 , 3 , |
V may be replaced by:  d 3 cwarray <name>   1 2 3   3 <name> !cwarray |
: cwarray   create dup c, 0  ?do  0 ,  loop ;
: !cwarray   1- swap 1 swap  do        tuck i 2* + !
                             -1 +loop  drop ;
 
V d variable <name> -2 allot  3 c,  1 c, 2 c, 3 c, |
V may be replaced by:  d 3 cbarray <name>   1 2 3   3 <name> !cbarray |
: cbarray   create  dup c,  0  ?do  0 c,  loop ;
: !cbarray   swap 1 swap  do        tuck i + c!
                          -1 +loop  drop ;
 
Now my questions are:
     - is there a better solution to create 'comma-free' arrays?
     - are there any naming conventions for them?
                     """"""""""""""""""
 
 
Here is another defining word, creating a 'counted string' array.
 
V >>>--> In LMI Forth,
V d variable <name> -2 allot  3 c, ascii a c, ascii b c, ascii c c, |
V may be replaced by: d " abc" $array <name> |
: $array   create  count dup c,  here swap dup allot  cmove ;
 
In LMI Forth, the word d " | (followed by a string) leaves the address
of the counted string on the stack. I tried it with Tom Zimmer's F-PC,
but the counted string compiles directly to d here |, with no address on
the stack. So it works with d variable <name> -2 allot " abc" | instead.
 
(In real life, LMI Forth needs capital letters.)
 
 
  Cheers  -  Johannes Teich                            Murnau, Bavaria (FRG)
 
+---------------------------------------+------------------------------------+
! UUCP via CosmoNet: unido!cosmo!gamber ! FidoNet via TBUS-BBS: 2:507/414.20 !
+---------------------------------------+------------------------------------+
 

gamber@cosmo.UUCP (Johannes Teich) (10/13/89)

Sorry, reading my last message back (only possible at the next session),
I detected some wrong characters. They evidently were replaced by CosmoNet.
The backslash (hex 5C) echoes as 'V'. The braces (hex 7B and hex 7D) look
like 'd' and the vertical bar, respectively.
I think it is best to repeat the message without these characters. Sorry.
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
Forthers,
I have two questions concerning 'comma-free' arrays. (Skip 40 lines.:-)
 
 
If I have to preset a big array, I don't like to use a lot of commas.
Instead, I prefer this way:
 
16 uwarray <name>                            hex
    0001 0002 0004 0008 0010 0020 0040 0080
    0100 0200 0400 0800 1000 2000 4000 8000  decimal
16 <name> !uwarray
 
At load time, the 16 values are stacked and then filled into the array.
 
Here are four versions with counted/uncounted arrays of words/bytes:
 
( variable <name> -2 allot  1 , 2 , 3 ,                             )
( may be replaced by:  3 uwarray <name>   1 2 3   3 <name> !uwarray )
: uwarray   create  0  ?do  0 ,  loop ;
: !uwarray   swap 1- 0 swap  do        tuck i 2* + !
                             -1 +loop  drop ;
 
( variable <name> -2 allot  1 c, 2 c, 3 c,                          )
( may be replaced by:  3 ubarray <name>   1 2 3   3 <name> !ubarray )
: ubarray   create  0  ?do  0 c,  loop ;
: !ubarray   swap 1- 0 swap  do        tuck i + c!
                             -1 +loop  drop ;
 
( variable <name> -2 allot  3 c,  1 , 2 , 3 ,                       )
( may be replaced by:  3 cwarray <name>   1 2 3   3 <name> !cwarray )
: cwarray   create dup c, 0  ?do  0 ,  loop ;
: !cwarray   1- swap 1 swap  do        tuck i 2* + !
                             -1 +loop  drop ;
 
( variable <name> -2 allot  3 c,  1 c, 2 c, 3 c,                    )
( may be replaced by:  3 cbarray <name>   1 2 3   3 <name> !cbarray )
: cbarray   create  dup c,  0  ?do  0 c,  loop ;
: !cbarray   swap 1 swap  do        tuck i + c!
                          -1 +loop  drop ;
 
Now my questions are:
     - is there a better solution to create 'comma-free' arrays?
     - are there any naming conventions for them?
                     """"""""""""""""""
 
 
Here is another defining word, creating a 'counted string' array.
 
( >>>--> In LMI Forth,                                              )
( variable <name> -2 allot  3 c, ascii a c, ascii b c, ascii c c,   )
( may be replaced by:  " abc" $array <name>                         )
: $array   create  count dup c,  here swap dup allot  cmove ;
 
In LMI Forth, the word  "  (followed by a string) leaves the address
of the counted string on the stack. I tried it with Tom Zimmer's F-PC,
but the counted string compiles directly to  here , with no address on
the stack. So it works with  variable <name> -2 allot " abc"  instead.
 
(In real life, LMI Forth needs capital letters.)
 
 
  Cheers  -  Johannes Teich                            Murnau, Bavaria (FRG)
 
+---------------------------------------+------------------------------------+
! UUCP via CosmoNet: unido!cosmo!gamber ! FidoNet via TBUS-BBS: 2:507/414.20 !
+---------------------------------------+------------------------------------+
 

peter@ficc.uu.net (Peter da Silva) (10/16/89)

In article <4075@cosmo2.UUCP> gamber@cosmo.UUCP (Johannes Teich) writes:
>      - is there a better solution to create 'comma-free' arrays?

You can use a sentinal:

	-1 data data data data data data data -1 addr array! (or arrayc!)

You can use a stack counter:

variable stackhold
: [ sp@ stackhold ! ;
: ] sp@ stackhold @ swap - ;

	[ data data data data data ] addr array!

For strings, I do this:

	" stuff" ( use " as terminator , like in ." )

	$ *stuff* ( use next char as terminator )
	.$ *stuff* ( ditto )

In FIG, " is easy:

: ($) r> count 2dup + =cells r> ;
: [$]
  word
  state @ if compile ($) here c@ else here count dup then
  1+ =cells allot ;
: $ tib @ in @ dup 1+ in ! + c@ [$] ; immediate
: " 34 [$] ; immediate
: [.$] state @ if compile type else type then ;
: .$ [compile] $ [.$] ; immediate
: ." [compile] " [.$] ; immediate
-- 
Peter da Silva, *NIX support guy @ Ferranti International Controls Corporation.
Biz: peter@ficc.uu.net, +1 713 274 5180. Fun: peter@sugar.hackercorp.com. `-_-'
                                                                           'U`
Quote: Structured Programming is a discipline -- not a straitjacket.      (wuf)