philburk / pforth

Portable Forth in C
BSD Zero Clause License
597 stars 98 forks source link

TRACE of words with 2>R and 2R> is wrong. #162

Closed izuk closed 5 months ago

izuk commented 5 months ago

See:

https://github.com/philburk/pforth/blob/6aa808ad3cd326dc7d76ea2b97cef2b100ee8abc/fth/trace.fth#L275

I think there should be a swap there.

philburk commented 5 months ago

I did an experiment.

: FOO 2>R 2R> ;    ok
11 22 foo .s 0sp

That printed "11 22" in the original order. Then I tried TRACE.

11 22 trace foo 
<< FOO +0       <10:2> 11 22             ||  2>R                 >>    ok
s 
<< FOO +8       <10:0>                   ||  2R>                 >>    ok
s 
<< FOO +16      <10:2> 11 22             ||  EXIT                >>    ok

So it behaves the same and puts "11 22" back on the stack.

THEN I defined:

: GOO >r >r 2R> ;

When I execute it normally it swaps the two values. When I execute it using TRACE it does NOT swap. So that is different.

What is the expected behavior for 2R> ?

philburk commented 5 months ago

According to the ANSI spec at https://forth-standard.org/standard/core/TwoRfrom

2>R should be equivalent to "R> R> SWAP".

So I believe that the pForth kernel version of 2>R and 2R> is correct and the TRACE version is incorrect.

philburk commented 5 months ago

2R@ is also backwards in TRACE

To test:

:  T1  swap >r >r   2r@  2r> ;
0sp 11 22 T1 .s

Should print "11 22 11 22". Then TRACE should do the same:

0SP 11 22 trace T1

Keep entering S until you reach EXIT.

philburk commented 5 months ago

@izuk - Thanks for the bug report. Does #163 look OK?

izuk commented 5 months ago

LGTM