7

I tried to implement a word that produces a string from an array when given a number on the stack in Forth.

My first naive attempt was:

create myarray s" Alpha" , s" Beta" , s" Charlie" ,

This was accepted, but it did not work as expected — myarray @ type produces inconsistent output (instead of my naive expectation that it might print "Alpha").

When searching the web, I found in Gforth documentation that a string created with s" has a limited lifetime which means that my ansatz is bound to fail from the beginning. On the other hand, even arrays of regular objects seem to be not standardized according to Arrays in Forth section in Len's Forth Tutorial.

<Update> Apparently, this is not a trivial problem with Forth. There are libraries on the web that implement missing string functionality: FFL (str module) and String Functions by Bernd Paysan. This is a good starting point, although it still requires work to go from there to an array of strings. </Update>

So how can I implement a word that returns a string from a given array?

ruvim
  • 7,151
  • 2
  • 27
  • 36
user8472
  • 3,268
  • 3
  • 35
  • 62

2 Answers2

3

To address parts of your code, s" leaves addr u on the stack, an address and the length of the string. , only stores one value so you won't get the desired results that way. 2, might do it as that would store both of the stack items that represent the string. Once you have done that you need to get both values back too so 2@ is what you want.

My rewrite would look like this:

create myarray s" Alpha" 2, s" Beta" 2, s" Charlie" 2,

\ Test
myarray 2@ type Alpha **ok**

Getting at the other elements of your array is a bit trickier. When you type myarray you get the address of the start of the data in that dictionary entry, and you can then use 2@ to get the the things that the first two addresses point to (which are the address and length of "Alpha"). If you want "Beta you need the next pair of addresses. So you can use

myarray 2 cells + \ increment the address by two cells

To get the addresses that point to "Beta" and so on. So in order to access "Beta" you would enter

myarray 2 cells + 2@ type Beta **ok**

I have tested this with gforth and it seems to all work, although I am not sure how to rigorously test for persistence.

Your word would need to be able to do the address incrementing based on what is on the stack to start with. You might want to get into some more create does> stuff. I can give some pointers but I don't want to spoil the fun of discovery.

If I am skipping too many details of what this actually means just say, and I will try again.


Maybe this is too crude, but I had a go at making a "string type" of sorts a while ago.

: string                                    ( addr u "name" -- )
    create 2,                               \ add address and length to dict entry "name"
    does> dup cell+ @ swap @ ;              \ push addr u

\ Example
s" Some Words" string words **ok**
words type Some Words **ok**

It defines a word with a name of your choosing (in this case "words") that will push length and start address of your string (in this case "some words") when it is interpreted. As far as I know when the string is in a definition like this it is persistent.

This doesn't answer you question fully, but it might help.


I have had another go at a persistent string, this one definitely allots memory within a dictionary entry and will be safe as long as that word exists. Before the string "type" only stored the address and length that s" created which is only any good until something else writes over that region of memory. This now copies the string from where s" creates it into a dictionary item called "name" where it is guaranteed to last as long as "name" itself.

: string                                    ( addr u "name" -- )
    create                                  \ create dict entry called "name"
    dup >r here >r                          \ keep copies of string length and start of "name"'s memory
    dup 2 cells + allot                     \ allot memory for the number of chars/bytes of the string plus 2
                                            \ for the new addr u
    r@ 2 cells +                            \ Get the address two cells from the start the space for "name"
    swap cmove                              \ copy the string at addr u into the alloted space for "name"

    \ Now "name" looks like this: "name" -blank1- -blank2- "the text of the string at addr u"
    \ blank1 should be the address of the start of the the text = addr2 and blank2 should be u

    r@ dup 2 cells + swap !                 \ get the address of blank1, copy it, increment by 2 to get addr2
                                            \ and then store that in blank1
    r> cell+ r> swap !                      \ get address of blank1, increment to get address of blank2, then get u and
                                            \ store it in blank2

    \ Now "name" looks like this: "name" addr2 u "the text of the string at addr u"

    does> dup @ swap cell+ @ ;              \ push addr2 u

For amusement, I thought I might show how little sense this makes without helpful formatting

: string-no-comments         ( addr u "name" -- )
    create dup >r here >r dup 2 cells + allot r@
    2 cells + swap cmove r@ dup 2 cells + swap !
    r> cell+ r> swap ! does> dup @ swap cell+ @ ;
sheepez
  • 986
  • 1
  • 10
  • 26
  • 1
    Actually, I can follow until the point where you comment on "`create does>` stuff". I will need to dig deeper to also understand the rest of it. Apart from that I think you solved my problem (with the possible open issue of persistence which is clearly badly documented in the GForth manual). Thank you! – user8472 Jan 06 '12 at 00:24
  • Yeah that was pretty vague, basically I wasn't sure how to do it without thinking about it for a while. It seems that anything in a definition is persistent (that is what I took from the manual). – sheepez Jan 06 '12 at 11:24
  • @user8472 I just wanted to alert you to my edits, the new version is definitely persistent. – sheepez Jan 09 '12 at 23:06
  • 1
    Thanks for the update. I have now taken the time to read through "Starting Forth" (http://home.iae.nl/users/mhx/sf.html) and I understand the issues of strings much better now. In particular, how to allocate the space and how to copy the strings. – user8472 Jan 25 '12 at 10:27
1

Firstly. You must ALLOT permanent storage to the strings. In ciforth (my Forth) there is the word $, that does this in the dictionary space.

S" aap" $,

leaves an address with one cell count, followed by characters. There is no standard word that does similar, you have to write it yourself. This is assuming ALLOCATE is not available. Using this the following code saves string pointers temporarily to the stack:

0 s" Alpha" $, s" Beta" $, s" Charlie" $,

Then you must store there pointers in an array, hence the sentinel 0, at the expense of an extra auxiliary word:

: ttt  BEGIN DUP WHILE , REPEAT DROP ;

And then

( CREATE string-array) ttt
HERE CONSTANT ttt-end

Now you can address strings as follows:

tt-end 2 CELLS -  ( @+ TYPE )

You may want add auxiliary words. This is ugly, cumbersome and far and foremost standard way to do it.