Manipulating the stack. 4tH 4tH Forth compiler
Oglas
Oglas
CHAPTER 7. 4TH FUNDAMENTALS 66
The following list gives a few more examples of Reverse Polish expressions. After each, we show the contents of the stack, in parentheses.
7 2 -
2 7 -
12 3 /
-12 3 /
4 5 + 2 *
4 5 2 + *
4 5 2 * -
(5)
(-5)
(4)
(-4)
(18)
(28)
(-6)
7.2
Manipulating the stack
You will often find that the items on the stack are not in the right order or that you need a copy. There are stack-manipulators which can take care of that.
To display a number you use ’.’, pronounced "dot". It takes a number from the stack and displays it. ’SWAP’ reverses the order of two items on the stack. If we enter
2 3 . . cr
4tH answers:
3 2
If you want to display the numbers in the same order as you entered them, you have to enter:
2 3 swap . . cr
In that case 4tH will answer:
2 3
You can duplicate a number using ’DUP’. If you enter:
2 . . cr
4tH will complain that the stack is empty. However, if you enter:
2 dup . . cr
4tH will display:
2 2
Another way to duplicate a number is using ’OVER’. In that case not the topmost number of the stack is duplicated, but the number beneath. E.g.
2 3 dup . . . cr will give you the following result:
Preuzimanje
Oglas