Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The implementation of S, in GForth

132 views
Skip to first unread message

lehs

unread,
Oct 22, 2016, 9:55:50 AM10/22/16
to
In GForth S, is implemented with an final align

see s,
: S,
here noop 1+ allot place align ; ok

that seems unnecessary. Suppose you want to store several counted strings in the same array, then there would be an easy way to recall the i:th string in the array by

: nextword \ ad -- ad'
dup c@ + 1+ ;

: word# \ array i -- ad n
over @ mod 0
rot cell+ -rot
?do nextword loop count ;

if the strings would have been stored without trailing blanks.
So why this ALIGN?

HAA

unread,
Oct 23, 2016, 1:45:33 AM10/23/16
to
If it's any comfort VFX, SWF, Win32F all have their own variations on this.




Doug Hoffman

unread,
Oct 23, 2016, 5:11:36 AM10/23/16
to
On 10/22/16 9:55 AM, lehs wrote:
> In GForth S, is implemented with an final align
>
> see s,
> : S,
> here noop 1+ allot place align ; ok
>
> Suppose you want to store several counted strings in the same array,
> then there would be an easy way to recall the i:th string in the
> array by
>
> : nextword \ ad -- ad'
> dup c@ + 1+ ;
>
> : word# \ array i -- ad n
> over @ mod 0
> rot cell+ -rot
> ?do nextword loop count ;
>
> if the strings would have been stored without trailing blanks.
> So why this ALIGN?

In the Gforth 0.7.3 that I have S, is defined a bit differently.
Regardless, by modifying that to return the address of the string one
could simply store those addresses in a cell-based array.

: s, ( caddr len -- addr )
here dup >r over char+ allot place align r> ;

\ example use:

5 array a
s" Hello" s, 0 a !
s" foo" s, 1 a !
s" hello world" s, 2 a !

0 a @ count type \ => Hello
etc.

Or some variation of the above to do exactly what you want.

-Doug

Albert van der Horst

unread,
Oct 23, 2016, 11:54:29 AM10/23/16
to
In article <580c7df3$0$339$1472...@news.sunsite.dk>,
Doug Hoffman <glid...@gmail.com> wrote:
>On 10/22/16 9:55 AM, lehs wrote:
>> In GForth S, is implemented with an final align
>>
>> see s,
>> : S,
>> here noop 1+ allot place align ; ok
>>
>> Suppose you want to store several counted strings in the same array,
>> then there would be an easy way to recall the i:th string in the
>> array by
>>
>> : nextword \ ad -- ad'
>> dup c@ + 1+ ;
>>
>> : word# \ array i -- ad n
>> over @ mod 0
>> rot cell+ -rot
>> ?do nextword loop count ;
>>
>> if the strings would have been stored without trailing blanks.
>> So why this ALIGN?
>
>In the Gforth 0.7.3 that I have S, is defined a bit differently.
>Regardless, by modifying that to return the address of the string one
>could simply store those addresses in a cell-based array.
>
>: s, ( caddr len -- addr )
> here dup >r over char+ allot place align r> ;

A word like this is indispensible to implement a Forth kernel,
so probably every Forth has it. In ciforth it is called $, .

However all strings in ciforth have a cell count.
Using bytes really restrict the usability.
>
>\ example use:
>
>5 array a
>s" Hello" s, 0 a !
>s" foo" s, 1 a !
>s" hello world" s, 2 a !
>
>0 a @ count type \ => Hello
>etc.

With denotations (recognizers) I don't think it useful to have
volatile strings. So then you have

"Hello" 0 a 2!

(And if your strings are bd it isn't even visible)

>
>Or some variation of the above to do exactly what you want.
>
>-Doug

Groetjes Albert
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

lehs

unread,
Oct 24, 2016, 5:10:24 AM10/24/16
to
If you want an align you could put it there after, it's more convenient.

HAA

unread,
Oct 25, 2016, 6:40:37 AM10/25/16
to
lehs wrote:
> If you want an align you could put it there after, it's more convenient.

If your system's S, aligns do the same in your address calculation:

: nextword \ ad -- ad'
dup c@ + 1+ ALIGNED ;




lehs

unread,
Oct 25, 2016, 8:59:57 AM10/25/16
to
Yes that's also a solution. Thanks!
0 new messages