Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #26764 > unrolled thread

Forth200x enhanced locals implementation

Started byanton@mips.complang.tuwien.ac.at (Anton Ertl)
First post2013-10-30 17:03 +0000
Last post2013-11-03 14:22 +0000
Articles 8 — 3 participants

Back to article view | Back to comp.lang.forth


Contents

  Forth200x enhanced locals implementation anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-10-30 17:03 +0000
    Re: Forth200x enhanced locals implementation "Alex McDonald" <blog@rivadpm.com> - 2013-10-30 22:23 +0000
      Re: Forth200x enhanced locals implementation anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-10-31 12:06 +0000
        Re: Forth200x enhanced locals implementation "Alex McDonald" <blog@rivadpm.com> - 2013-10-31 18:01 +0000
          Re: Forth200x enhanced locals implementation anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-11-01 08:46 +0000
            Re: Forth200x enhanced locals implementation "Alex McDonald" <blog@rivadpm.com> - 2013-11-01 10:36 +0000
    Re: Forth200x enhanced locals implementation albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-10-31 14:21 +0000
    Re: Forth200x enhanced locals implementation anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-11-03 14:22 +0000

#26764 — Forth200x enhanced locals implementation

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2013-10-30 17:03 +0000
SubjectForth200x enhanced locals implementation
Message-ID<2013Oct30.180302@mips.complang.tuwien.ac.at>
The proposal for the Forth200x enhanced locals proposal does not
contain a portable implementation, even though implementing the syntax
is possible portably (increasing the number of locals, also contained
in that proposal, requires carnal knowledge).

So I implemented the X:locals syntax portably, and you can find the
result in compat/xlocals.fs in
<http://www.complang.tuwien.ac.at/forth/compat.zip>.

There is an interesting insight in this implementation: I started out
with the <http://www.complang.tuwien.ac.at/forth/anslocal.fs> code
inspired by John Hayes' paper on implementing the { ... } syntax and
added the "|" option and some error checking.

The interesting change is that I switched from BL WORD COUNT to using
PARSE-NAME.  This significantly simplified the implementation, as I
did not need to save and restore >IN, and reparse each local, but
could just keep the parsed string descriptors around.

The central words in the implementations are:

\ old                                \ new
: {helper ( -- final-offset )        : {helper ( f "name1"..."namen" -- ) 
  >in @                                parse-name1                        
  bl word count                        2dup s" |" compare 0= if           
  2dup s" --" compare 0= if              2drop drop true parse-name1 then 
    2drop [char] } parse 2drop true    2dup s" --" compare 0= if          
  else                                   2drop drop parse-rest exit then  
    s" }" compare 0=                   2dup s" :}" compare 0= if          
  then                                   2drop drop exit then             
  if                                   rot dup if \ we are in the | part  
    drop >in @                           0 postpone literal then          
  else                                 recurse (local) ;                  
    recurse
    swap >in ! local
  then ;

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2013: http://www.euroforth.org/ef13/

[toc] | [next] | [standalone]


#26766

From"Alex McDonald" <blog@rivadpm.com>
Date2013-10-30 22:23 +0000
Message-ID<l4s0sb$mti$1@dont-email.me>
In reply to#26764
on 30/10/2013 17:02:58,  wrote:
> The proposal for the Forth200x enhanced locals proposal does not
> contain a portable implementation, even though implementing the syntax
> is possible portably (increasing the number of locals, also contained
> in that proposal, requires carnal knowledge).
> 
> So I implemented the X:locals syntax portably, and you can find the
> result in compat/xlocals.fs in
> <http://www.complang.tuwien.ac.at/forth/compat.zip>.
> 
> There is an interesting insight in this implementation: I started out
> with the <http://www.complang.tuwien.ac.at/forth/anslocal.fs> code
> inspired by John Hayes' paper on implementing the { ... } syntax and
> added the "|" option and some error checking.
> 
> The interesting change is that I switched from BL WORD COUNT to using
> PARSE-NAME.  This significantly simplified the implementation, as I
> did not need to save and restore >IN, and reparse each local, but
> could just keep the parsed string descriptors around.

From wf32 locals implementation;

: nextword     ( -- addr len ) \ get next word in input stream
    begin parse-name dup 0=
    while 
      drop refill dup 0= if exit then
      2drop
    repeat ;

: definite-word ( -- addr len ) \ find definite word in stream
    nextword dup 0= throw_earlyeof ?throw ;

nextword supports {: across multiple lines; definite-word ensure that the
input stream isn't exhausted until we expect it to be. They are not good
names, I admit. Then;

: {: ( -- )  \ {: :} locals
   compile-only>
     0 to localdcl          \ zero dcl counter, new set of locals
     begin definite-word                    \ get next word
       2dup s" |" str= >r               \ as in {: [...] | ...
       2dup s" --" str= >r              \ as in {: [...] -- ...
       2dup s" :}"  str= 2r> or or not    \ as in {: [...] :} ...
     while (local) repeat                   \ if none, then not done
     0 0 (local)                          \ done all locals
     begin
       2dup s" :}" str= >r          \ here at ... -- or ... :}
            s" |" str= r> or not
     while definite-word repeat ;  \ skip until finish

[toc] | [prev] | [next] | [standalone]


#26769

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2013-10-31 12:06 +0000
Message-ID<2013Oct31.130647@mips.complang.tuwien.ac.at>
In reply to#26766
"Alex McDonald" <blog@rivadpm.com> writes:
>on 30/10/2013 17:02:58,  wrote:
>: {: ( -- )  \ {: :} locals
>   compile-only>
>     0 to localdcl          \ zero dcl counter, new set of locals
>     begin definite-word                    \ get next word
>       2dup s" |" str= >r               \ as in {: [...] | ...
>       2dup s" --" str= >r              \ as in {: [...] -- ...
>       2dup s" :}"  str= 2r> or or not    \ as in {: [...] :} ...
>     while (local) repeat                   \ if none, then not done
>     0 0 (local)                          \ done all locals
>     begin
>       2dup s" :}" str= >r          \ here at ... -- or ... :}
>            s" |" str= r> or not
>     while definite-word repeat ;  \ skip until finish

It seems to me that the locals are defined in the reverse order and
the locals after "|" are not defined at all by this code.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2013: http://www.euroforth.org/ef13/

[toc] | [prev] | [next] | [standalone]


#26777

From"Alex McDonald" <blog@rivadpm.com>
Date2013-10-31 18:01 +0000
Message-ID<l4u5to$la9$1@dont-email.me>
In reply to#26769
on 31/10/2013 12:06:46,  wrote:
> "Alex McDonald" <blog@rivadpm.com> writes:
>>on 30/10/2013 17:02:58,  wrote:
>>: {: ( -- )  \ {: :} locals
>>   compile-only>
>>     0 to localdcl          \ zero dcl counter, new set of locals
>>     begin definite-word                    \ get next word
>>       2dup s" |" str= >r               \ as in {: [...] | ...
>>       2dup s" --" str= >r              \ as in {: [...] -- ...
>>       2dup s" :}"  str= 2r> or or not    \ as in {: [...] :} ...
>>     while (local) repeat                   \ if none, then not done
>>     0 0 (local)                          \ done all locals
>>     begin
>>       2dup s" :}" str= >r          \ here at ... -- or ... :}
>>            s" |" str= r> or not
>>     while definite-word repeat ;  \ skip until finish
> 
> It seems to me that the locals are defined in the reverse order and

: locals| ( -- ) \ ans standard locals
    compile-only> -1 to localord postpone {: ;  \ reversed stack order

The definitions don't generate code until 0 0 (LOCAL). LOCALORD sets the
order.

> the locals after "|" are not defined at all by this code.

True. I don't see a requirement for them; | stops the locals compiler and
they consequently generate error -13 when compiled. The point was to
demonstrate your very valid point re parse-name, and the simplification
it brings.

> 
> - anton

[toc] | [prev] | [next] | [standalone]


#26778

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2013-11-01 08:46 +0000
Message-ID<2013Nov1.094656@mips.complang.tuwien.ac.at>
In reply to#26777
"Alex McDonald" <blog@rivadpm.com> writes:
>on 31/10/2013 12:06:46,  wrote:
>> "Alex McDonald" <blog@rivadpm.com> writes:
>>>on 30/10/2013 17:02:58,  wrote:
>>>: {: ( -- )  \ {: :} locals
>>>   compile-only>
>>>     0 to localdcl          \ zero dcl counter, new set of locals
>>>     begin definite-word                    \ get next word
>>>       2dup s" |" str= >r               \ as in {: [...] | ...
>>>       2dup s" --" str= >r              \ as in {: [...] -- ...
>>>       2dup s" :}"  str= 2r> or or not    \ as in {: [...] :} ...
>>>     while (local) repeat                   \ if none, then not done
>>>     0 0 (local)                          \ done all locals
>>>     begin
>>>       2dup s" :}" str= >r          \ here at ... -- or ... :}
>>>            s" |" str= r> or not
>>>     while definite-word repeat ;  \ skip until finish
>> 
>> It seems to me that the locals are defined in the reverse order and
>
>: locals| ( -- ) \ ans standard locals
>    compile-only> -1 to localord postpone {: ;  \ reversed stack order
>
>The definitions don't generate code until 0 0 (LOCAL). LOCALORD sets the
>order.

Ah, ok.  However, the default for (LOCAL) should be the behaviour with
"-1 to localord", otherwise standard programs that use (LOCAL), like
xlocals.fs, don't work correctly.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2013: http://www.euroforth.org/ef13/

[toc] | [prev] | [next] | [standalone]


#26779

From"Alex McDonald" <blog@rivadpm.com>
Date2013-11-01 10:36 +0000
Message-ID<l5007g$ucl$1@dont-email.me>
In reply to#26778
on 01/11/2013 08:46:53,  wrote:
> "Alex McDonald" <blog@rivadpm.com> writes:
>>on 31/10/2013 12:06:46,  wrote:
>>> "Alex McDonald" <blog@rivadpm.com> writes:
>>>>on 30/10/2013 17:02:58,  wrote:
>>>>: {: ( -- )  \ {: :} locals
>>>>   compile-only>
>>>>     0 to localdcl          \ zero dcl counter, new set of locals
>>>>     begin definite-word                    \ get next word
>>>>       2dup s" |" str= >r               \ as in {: [...] | ...
>>>>       2dup s" --" str= >r              \ as in {: [...] -- ...
>>>>       2dup s" :}"  str= 2r> or or not    \ as in {: [...] :} ...
>>>>     while (local) repeat                   \ if none, then not done
>>>>     0 0 (local)                          \ done all locals
>>>>     begin
>>>>       2dup s" :}" str= >r          \ here at ... -- or ... :}
>>>>            s" |" str= r> or not
>>>>     while definite-word repeat ;  \ skip until finish
>>>
>>> It seems to me that the locals are defined in the reverse order and
>>
>>: locals| ( -- ) \ ans standard locals
>>    compile-only> -1 to localord postpone {: ;  \ reversed stack order
>>
>>The definitions don't generate code until 0 0 (LOCAL). LOCALORD sets the
>>order.
> 
> Ah, ok. However, the default for (LOCAL) should be the behaviour with
> "-1 to localord", otherwise standard programs that use (LOCAL), like
> xlocals.fs, don't work correctly.

Good point; fixed.

> 
> - anton

[toc] | [prev] | [next] | [standalone]


#26772

Fromalbert@spenarnc.xs4all.nl (Albert van der Horst)
Date2013-10-31 14:21 +0000
Message-ID<5272677e$0$3198$e4fe514c@dreader36.news.xs4all.nl>
In reply to#26764
In article <2013Oct30.180302@mips.complang.tuwien.ac.at>,
Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
>The proposal for the Forth200x enhanced locals proposal does not
>contain a portable implementation, even though implementing the syntax
>is possible portably (increasing the number of locals, also contained
>in that proposal, requires carnal knowledge).
>
>So I implemented the X:locals syntax portably, and you can find the
>result in compat/xlocals.fs in
><http://www.complang.tuwien.ac.at/forth/compat.zip>.
>
>There is an interesting insight in this implementation: I started out
>with the <http://www.complang.tuwien.ac.at/forth/anslocal.fs> code
>inspired by John Hayes' paper on implementing the { ... } syntax and
>added the "|" option and some error checking.
>
>The interesting change is that I switched from BL WORD COUNT to using
>PARSE-NAME.  This significantly simplified the implementation, as I
>did not need to save and restore >IN, and reparse each local, but
>could just keep the parsed string descriptors around.

Nice, especially for those system that have made WORD FIND and >IN
loadable extensions.

>The central words in the implementations are:
>
>\ old                                \ new
>: {helper ( -- final-offset )        : {helper ( f "name1"..."namen" -- )
>  >in @                                parse-name1
>  bl word count                        2dup s" |" compare 0= if
>  2dup s" --" compare 0= if              2drop drop true parse-name1 then
>    2drop [char] } parse 2drop true    2dup s" --" compare 0= if
>  else                                   2drop drop parse-rest exit then
>    s" }" compare 0=                   2dup s" :}" compare 0= if
>  then                                   2drop drop exit then
>  if                                   rot dup if \ we are in the | part
>    drop >in @                           0 postpone literal then
>  else                                 recurse (local) ;
>    recurse
>    swap >in ! local
>  then ;

With denotations (recognizers) and a helper function it becomes even
neater:

\ Return:"strings are equal"
: $=  compare 0= ;    (sc sc -- fl )

\ Do something with locals.
: {helper ( f "name1"..."namen" -- )
  parse-name1
  2dup "|"  $= if 2drop drop true parse-name1 then
  2dup "--" $= if 2drop drop parse-rest exit then
  2dup ":}" $= if 2drop drop exit then
  \ we are in the | part
  sdswap dup if 0 postpone literal then    recurse (local)
;

>
>- anton
>--
>M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
-- 
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

[toc] | [prev] | [next] | [standalone]


#26794

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2013-11-03 14:22 +0000
Message-ID<2013Nov3.152200@mips.complang.tuwien.ac.at>
In reply to#26764
anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>So I implemented the X:locals syntax portably, and you can find the
>result in compat/xlocals.fs in
><http://www.complang.tuwien.ac.at/forth/compat.zip>.

Actually a better place is the reference implementations for
Forth200x.  You can find them at

http://www.forth200x.org/reference-implementations/

The new X:locals implementation is

http://www.forth200x.org/reference-implementations/locals.fs

I also saw another implementation there, which I wrote three years ago
(and had mostly forgotten):

http://www.forth200x.org/reference-implementations/extended-locals.fs

Instead of John Hayes recursion-for-reversal approach this
implementation uses the stack and a count-of-locals for achieving the
reversing; it also uses control flow instead of a flag on the stack
for dealing with the locals after "|".  Overall this implementation is
quite a bit more complicated, probably mostly because of the latter
design decision.

You can find the code for all the available reference implementations
and the available tests in

http://www.forth200x.org/forth200x-code.zip

Or if you are only interested in the implementations:

http://www.forth200x.org/extensions.zip

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2013: http://www.euroforth.org/ef13/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web