Page 1 of 3
extra stack operators
Posted: Sun May 04, 2014 3:28 pm
by chitselb
on the OLPC wiki, there's NIP ( a b -- b ) and TUCK ( a b -- b a b ) and -ROT ( a b c -- c a b ) which I call LROT, but what is the word which has the stack diagram ( a b c -- b a c ) ? Is it UNDER?
Re: extra stack operators
Posted: Sun May 04, 2014 7:10 pm
by GARTHWILSON
I haven't heard of that or of LROT, but NIP, TUCK, and -ROT are pretty standard.
Re: extra stack operators
Posted: Sun May 04, 2014 9:19 pm
by barrym95838
Ooh, may I try? (utter noob)
: UNDER >R SWAP R> ;
... or ...
: UNDER ROT SWAP ;
I don't know what to call it, but UNDER sounds as good as any, as long as that name isn't habitually used for something different.
Mike
Re: extra stack operators
Posted: Mon May 05, 2014 4:02 am
by chitselb
Mike,
Both of your definitions have the equivalent and proper stack effect ( a b c -- b a c ). I would choose the latter because it's two bytes shorter. In Brodie's "Thinking Forth" one of the things he says, to paraphrase badly from a book I read 30 years ago, "Naming the word properly is half the work of writing the word." In this case, it would be greatly preferred to write code that another Forth programmer would understand so I'm seeking the recognizable name for what I'm now calling UNDER.
I've really taken myself to school this past couple days, on the short bus. Stupid mistake after stupid mistake, violating principles that were once well known to me. I wrote this pretty short word (Copy) and then a monolithic word (Delete) and then a huge monolithic word (Paste) where everything fell apart with the line wrap table. Yes, that thing you suggested I dispense with entirely on the other thread. I'm keeping it in mostly because of the "nativeness" of it, and because of the long string literals. Today I did a fair amount of (literal) gardening and redesigned everything relating to copy/delete/paste into tiny reusable chunks of code. It's all headerless anyway, I don't know why I resisted this path. I see light at the end of the tunnel.
Update: I just learned on comp.lang.forth that UNDER is taken, and it does something else. It's probably not RSWAP either, which I'm pretty sure swaps two words on the return stack ( R: a b -- R: b a ).
: UNDER ( a b -- a a b ) >R DUP R> ;
Re: extra stack operators
Posted: Mon May 05, 2014 5:49 am
by barrym95838
... : UNDER ( a b -- a a b ) >R DUP R> ;
Ah, it figures.
Using your criteria for compactness, we could successfully change this to
: UNDER ( a b -- a a b ) OVER SWAP ;
right? I'm sure that actual execution speed is a toss-up, since OVER and SWAP are probably more complex primitives than >R DUP and R>, but it's definitely a cell shorter, all else being equal. ROTSWAP would be most efficient as a primitive on my 'm32, with no ITC size penalty ... assuming that it would be used often enough to be preferred over ROT SWAP in a real program, which I have yet to write.
Code: Select all
UNDER: ; ( a b -- a a b )
.dw .+1
ldb 0,x
stb 0,-x
bra _next
ROTSWAP: ; ( a b c -- b a c )
.dw .+1
exa 0,x ; a c b
exa 1,x ; b c a
bra SWAP+1 ; b a c
SWAP: ; ( a b -- b a )
.dw .+1
exa 0,x
_next:
ldu 0,y+
jmp (0,u+)
Mike
Re: extra stack operators
Posted: Mon May 05, 2014 5:15 pm
by chitselb
Seems to me that it would be useful to have a stack manipulator word for every permutation of stack diagram, and these should have universally accepted names.
( a --
dup ( a -- a a )
?dup ( a -- a a | 0 )
drop ( a -- )
( a b --
over ( a b -- a b a )
swap ( a b -- b a )
under ( a b -- a a b )
nip ( a b -- b )
tuck ( a b -- b a b )
2dup ( a b -- a b a b )
( a b c --
rot ( a b c -- b c a )
FLIP ( a b c -- b a c )
-rot ( a b c -- c a b )
FLOP ( a b c -- c b a )
I'm going to call them FLIP and FLOP for now, until I find out what the names are supposed to be.
Re: extra stack operators
Posted: Wed Jun 11, 2014 4:27 am
by chitselb
Ooh, may I try? (utter noob)
: UNDER >R SWAP R> ;
... or ...
: UNDER ROT SWAP ;
I don't know what to call it, but UNDER sounds as good as any, as long as that name isn't habitually used for something different.
Mike
I went with : UNDER ( a b -- a a b ) >R DUP R> ;
Re: extra stack operators
Posted: Wed Dec 16, 2015 8:12 pm
by programandala.net
: UNDER ( a b -- a a b ) OVER SWAP ;
It's called also
nup. It seems there's no accepted name yet. Some Forthers dislike those too specific stack manipulations, except the standard
nip and
tuck; others find them useful.
Re: extra stack operators
Posted: Wed Dec 16, 2015 8:26 pm
by programandala.net
Seems to me that it would be useful to have a stack manipulator word for every permutation of stack diagram, and these should have universally accepted names.
I don't think it would be practical, even feasible. There are many different stack operations you can do, and most of them can be defined using others. The standard operations have names related to their functions, because the functions are simple.
FLIP ( a b c -- b a c )
FLOP ( a b c -- c b a )
I'm going to call them FLIP and FLOP for now, until I find out what the names are supposed to be.
That's the problem I just mentioned. It would be hard to remember not only which one is `flop` and which one is `flip`, but also what they actually do!
Re: extra stack operators
Posted: Wed Dec 16, 2015 10:59 pm
by nyef
I don't think it would be practical, even feasible. There are many different stack operations you can do, and most of them can be defined using others. The standard operations have names related to their functions, because the functions are simple.
FLIP ( a b c -- b a c )
FLOP ( a b c -- c b a )
I'm going to call them FLIP and FLOP for now, until I find out what the names are supposed to be.
That's the problem I just mentioned. It would be hard to remember not only which one is `flop` and which one is `flip`, but also what they actually do!
Even within the standard, I have a hard time remembering which way round ROT goes. It seems like every time I need to use it, I either look it up or I do "1 2 3 ROT .S DROP 2DROP" to figure it out.
Re: extra stack operators
Posted: Wed Dec 16, 2015 11:44 pm
by barrym95838
I would be tempted to name them
bca,
bac,
cab, and
cba, but I have a perverse affinity for three-letter mnemonics.
Mike B.
Re: extra stack operators
Posted: Wed Dec 16, 2015 11:54 pm
by Dr Jefyll
Mike, the TLM's you propose are valid hex numbers. Could get (a little too) interesting...

Re: extra stack operators
Posted: Wed Dec 16, 2015 11:59 pm
by programandala.net
Even within the standard, I have a hard time remembering which way round ROT goes. It seems like every time I need to use it, I either look it up or I do "1 2 3 ROT .S DROP 2DROP" to figure it out.
I suggest this mnemonic: `rot` is the "normal", so it's "clockwise"; `-rot` is the opposite.
Re: extra stack operators
Posted: Thu Dec 17, 2015 12:30 am
by barrym95838
Mike, the TLM's you propose are valid hex numbers. Could get (a little too) interesting...

Ugh ... foiled again! How about:
: wxy drop ;
: yzz dup ;
: yzx rot ;
: yzy over ;
: zwx 2over ;
... meh, could cause brain damage in large quantities.

Plus the fact that those are valid base-36 numbers.
Mike B.
Re: extra stack operators
Posted: Thu Dec 17, 2015 7:08 am
by theGSman
I don't think it would be practical, even feasible. There are many different stack operations you can do, and most of them can be defined using others. The standard operations have names related to their functions, because the functions are simple.
I think that what chitselb means is that every stack manipulation should have a name, not that every conceivable stack manipulation (no matter how rarely used) should be implemented in a Forth system. It is a simple matter to write a CODE word for any manipulation that isn't in your Forth system but is likely to be used a lot in a program. I have often found 2PICK to be such a word.
That's the problem I just mentioned. It would be hard to remember not only which one is `flop` and which one is `flip`, but also what they actually do!
for "flop" I would probably use ROTSWAP if I needed the word or SWAPUNDER if the former name was not descriptive enough.
"flip" would probably sound better as 3REVERSE. (I hope I didn't get "flip" and "flop" mixed up.

)