Post by Krishna MyneniI'm in need of a convenient way to do the following for the fp stack,
a b c -- b a c
Two ways to do this on the data stack are
ROT SWAP
R SWAP R>
( the latter can't be done from the interpreter, portably at least ).
Is there a word I'm not remembering to perform this operation? If not
what name would one give to this stack operation?
SWAP-UNDER
ROTSWAP
RISE
For fp stack manipulation, I had posted mostly portable code for F>R and
FR> to push and pop from the floating point stack to/from the return
stack. A significant reason for suggesting the F>R FR> pair is to deal
with this situation where having to use FROT FSWAP would be inefficient
on non-analytic compilers.
Maybe a generic swap operation at depth u, called RISE, is useful e.g.
0 RISE \ same as SWAP
1 RISE \ i*x a b c -- i*x b a c
2 RISE \ i*x a b c d -- i*x b a c d
etc.
[...]
I'm reading through your programs and I encounter SWAP-UNDER .
It involves stack operations, so there is no way you
can understand the code unless you have looked up
this words.
Looking at various sources, including my own, ROT SWAP is not a common
sequence. Indeed, I would say it is exceptional. Scanning SwiftForth
sources and libs it is used once. In my case it was a setup for CMOVE
which I used on a couple occasions:
( dest src len) <process-source>
( dest src' len') ROT SWAP CMOVE ( save result)
I could have written:
( src len dst) >R <process-source>
( src' len') R> SWAP CMOVE ( save result)
but it would have been costlier. So I've no regrets.
Remember the washing machine example of Brodie?
All stack manipulations are hidden.
The program proceeds with normal words that describe
the actions that are done.
If you mean factoring as a way of improving readability and hiding
'noise words', yes. But we're told define words used only once is
a 'waste' of namespace. This may be a learned response. If one
sees an algorithm written in C in one chunk, the temptation is to
duplicate that.
You already have three items on the stack, that is
too much. Unless it is actally a double item,
say a string, and a single item.
The presence of ROT SWAP ought to raise a red flag as it suggests
something is awry. After examining the code we may conclude its
use in this case was the least worst option.