Deep stack manipulators. 4tH 4tH Forth compiler
تبلیغات
تبلیغات
CHAPTER 7. 4TH FUNDAMENTALS 67
3 3 2
But this one:
2 3 over . . . cr will give you:
2 3 2
Sometimes you want to discard a number, e.g. you duplicated it to check a condition, but since the test failed, you don’t need it anymore. ’DROP’ is the word we use to discard numbers. So this:
2 3 drop .
will give you "2" instead of "3", since we dropped the "3".
The final one I want to introduce is ’ROT’. Most users find ’ROT’ the most complex one since it has its effects deep in the stack. The thirdmost item to be exact. This item is taken from its place and put on top of the stack. It is ’rotated’, as this small program will show you:
1 2 3
. . . cr
1 2 3 rot
. . . cr
\ 1 is the thirdmost item
\ display all numbers
( This will display ’3 2 1’ as expected)
\ same numbers stacked
\ performs a ’ROT’
\ same operation
( This will display ’1 3 2’!)
7.3
Deep stack manipulators
No, there are no manipulators that can dig deeper into the stack. A stack is NOT an array!
So if there are some Forth-83 users out there, I can only tell you: learn Forth the proper way. Programs that have so many items on the stack are just badly written. Leo Brodie agrees with me.
If you are in ’deep’ trouble you can always use the returnstack manipulators. Check out that section.
7.4
Passing arguments to functions
There is no easier way to pass arguments to functions as in 4tH. Functions have another name in 4tH. We call them "words". Words take their "arguments" from the stack and leave the "result" on the stack.
Other languages, like C, do exactly the same. But they hide the process from you. Because passing data to the stack is made explicit in 4tH it has powerful capabilities. In other languages, you can get back only one result. In 4tH you can get back several!
All words in 4tH have a stack-effect-diagram. It describes what data is passed to the stack in what order and what is returned. The word ’*’ for instance takes numbers from the stack, multiplies them and leaves the result on the stack. It’s stack-effect-diagram is:
دانلود
تبلیغات