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

pforth return stack strangeness

53 views
Skip to first unread message

ad...@simple.gopher

unread,
May 27, 2017, 1:04:00 PM5/27/17
to
Maybe there are some pforth users here..

QUADRATIC example from page 112 in Starting Forth book:
% pforth
/ compute a * X^2 + b * X + c
: QUAD ( a b c X -- n) >R SWAP ROT I * + R> * + ; ok
2 7 9 3 QUAD . ==> 30 ok

The answer should be 48. TRACE suggests stuff is getting drop/corrupted:

2 7 9 3 TRACE QUAD .
<< QUAD +0 <10:4> 2 7 9 3 || >R >> 3 ok
s
<< QUAD +4 <10:2> 2 7 || SWAP >> ok
s
<< QUAD +8 <10:2> 7 2 || ROT >> ok
s
<< QUAD +12 <10:2> 2 0 || I >> ok
s
<< QUAD +16 <10:3> 2 0 1515870810 || * >> ok
s
<< QUAD +20 <10:2> 2 0 || + >> ok
s
<< QUAD +24 <10:1> 2 || R> >> ok
s
<< QUAD +28 <10:2> 2 9 || * >> ok
s
<< QUAD +32 <10:1> 18 || + >> ok
s
<< QUAD +36 <10:0> || EXIT >> ok
s Finished.
ok

For reference, same thing in gforth:
% gforth -e ": quad >r swap rot i * + r> * + ; 2 7 9 3 quad . bye"
48

pforth documentation doesn't suggest limited return stack functionality; not
sure why it doesn't work..

Julian Fondren

unread,
May 27, 2017, 1:22:28 PM5/27/17
to
On Saturday, May 27, 2017 at 12:04:00 PM UTC-5, ad...@simple.gopher wrote:
> Maybe there are some pforth users here..
>
> QUADRATIC example from page 112 in Starting Forth book:
> % pforth
> / compute a * X^2 + b * X + c
> : QUAD ( a b c X -- n) >R SWAP ROT I * + R> * + ; ok
> 2 7 9 3 QUAD . ==> 30 ok
>
> The answer should be 48.

The answer's also wrong under SwiftForth.

You can get the intended answer under both by accessing the top
of the return stack with R@ , rather than I :

$ ./pforth_standalone
PForth V27-LE/64, built May 27 2017 12:18:53 (static)

: quad >r swap rot r@ * + r> * + ; 2 7 9 3 quad . 48 ok
Stack<10>

Roelf Toxopeus

unread,
May 27, 2017, 3:11:31 PM5/27/17
to
It's like Julian said, use R@ instead of I. Has to do with loop
implementation strategies (and standards). Use I as the current index
for the innermost loop. Use R@ to copy the top of the return stack to
data stack.

Both the second Starting Forth edition (p.100) and the online version
use R@ (it's now also called QUADRATIC).
: QUADRATIC ( a b c x -- n ) >R SWAP ROT R@ * + R> * + ;

In case you run into more issues using the first edition and whatever
Forth, you could consult/try the online updated version:
http://home.iae.nl/users/mhx/sf.html
https://www.forth.com/starting-forth/

or just ask here of course.




ad...@simple.gopher

unread,
May 27, 2017, 3:15:40 PM5/27/17
to
Hey nice! Seems to also work with 'ff'[1] forth:

% echo ": quad >r swap rot r@ * + r> * + ; 2 7 9 3 quad . cr bye" | ff
ok
48

Thanks ; it was probably a very novice question..

J.

[1] http://bitbucket.org/bunny351/ff

Julian Fondren

unread,
May 27, 2017, 3:37:46 PM5/27/17
to
On Saturday, May 27, 2017 at 2:15:40 PM UTC-5, ad...@simple.gopher wrote:
> Hey nice! Seems to also work with 'ff'[1] forth:
>
> % echo ": quad >r swap rot r@ * + r> * + ; 2 7 9 3 quad . cr bye" | ff
> ok
> 48

Neat, don't think I've seen this one.

README:

`2nip ( x y -- y )`

Drops the 2nd item on the stack.

README (later):

2nip ( x y z -- z )

core.f:

: 2nip ( x y z q -- z q ) rot drop rot drop ;

At least the implementation does what I'd expect :p

>
> Thanks ; it was probably a very novice question..
>

don't sweat it. First the code in the book is rotting a bit and this
is a natural question to have about it. Second, there aren't so many
novice questions around here that people are having to ask for
directions to the loony threads.
0 new messages