radhios / pforth

Automatically exported from code.google.com/p/pforth
0 stars 0 forks source link

Deferred compilation of immediate compile-only words doesn't work #10

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
For example:

: comp   postpone if postpone then ;

Expected:
: test  [ comp ] ;   ok
see test 
( 6473C )    IF or WHILE 
( 64744 )        THEN 
( 64744 )    ;

Seen:
: test  [ comp ] ; Stack<10> UNDERFLOW!
THROW code = -4 
Stack underflow!
see test 
( 645CC )    ;
   ok

Here is a more complex example (taken from the gforth manual):

: compile-map-array ( compilation: xt -- ; run-time: ... addr u -- ... )
     \ at run-time, execute xt ( ... x -- ... ) for each element of the
     \ array beginning at addr and containing u elements
       { xt }
       POSTPONE cells POSTPONE over POSTPONE + POSTPONE swap POSTPONE ?do
         POSTPONE i POSTPONE @ xt compile,
       1 cells POSTPONE literal POSTPONE +loop ;

Expected:
: sum-array ( addr u -- n )
      0 rot rot [ ' + compile-map-array ] ;
here   1 , 2 , 3 , 4 ,   4 sum-array .
     10 ok
see sum-array
( 64798 )    0 ROT ROT CELLS OVER + SWAP 
( 647BC )    ?DO 
( 647C0 )        I @ + 4 
( 647D8 )    +LOOP
( 647DC )    ;
   ok

Seen:
: sum-array ( addr u -- n )       0 rot rot [ ' + compile-map-array ] ; 
Stack<10> UNDERFLOW!
THROW code = -4 
Stack underflow!
    ok
Stack<10> 
here   1 , 2 , 3 , 4 ,   4 sum-array .
     411264    ok
Stack<10> 0 411280 

The problem seems to stem from ?comp testing in the definition of if, begin, 
do, ?do. Without ?comp, everything seems to work properly.

What version of the product are you using? On what operating system?
pforth_v27_20101121
Mac OS 10.4 PPC

Original issue reported on code.google.com by rthstr...@gmail.com on 25 Jan 2011 at 1:24

GoogleCodeExporter commented 9 years ago
This may be due to the ability to use IF THEN DO LOOP and other conditionals 
outside of a colon definition. This capability is added in "smart_if.fth".  IF 
THEN are running in between the square brackets.

Try building without including "smart_if.fth".

Also I recommend making COMP immediate. Then it works.

: COMP postpone if postpone then ; immediate

: TEST COMP ;

see test 
( 100823268 )    IF or WHILE 
( 100823278 )        THEN 
( 100823278 )    ;

Original comment by burkp...@gmail.com on 26 Jan 2011 at 6:11

GoogleCodeExporter commented 9 years ago
> Try building without including "smart_if.fth".

No, this doesn't work either.

> Also I recommend making COMP immediate. Then it works.

Yep.

Well, I don't see which practical use such non-immediate compiling of immediate 
words might have: though, apparently, it's in the standard.

Here is an excerpt from gforth's test/postpone.fs

: postpone-."
    postpone ." ;

: pdq2 [ postpone-." you should see this later. " ] cr ;
: pdq1 [ postpone-." you should see this first. " ] cr ;
{ pdq1 pdq2 -> }

: postpone-begin
    postpone begin ;
{ : PB3 [ POSTPONE-BEGIN ] DUP 5 < WHILE DUP 1+ REPEAT ; -> }

The former example fails without crashing, the latter crashes.

Regards,
Reinhold Straub

Original comment by rthstr...@gmail.com on 27 Jan 2011 at 4:15