home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 2 Part 060 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
- ┌────────────────────────────────────────────┐
- │ Some new words to manipulate the stack. │
- └────────────────────────────────────────────┘
-
- Copy the ith stack item to the top of the stack.
- PICK ( ...ni... n1 n0 i -- ...ni...n1 n0 ni )
-
- Rotate the ith stack item to the top of the stack.
- ROLL ( ...ni... n1 n0 i -- ..._... n1 n0 ni )
-
- ROLL is a generalization of ROT.
-
- Let's load our outer interpreter MYQUIT and see what these do!
- FLOAD MYQUIT ok
- MYQUIT
- Stack Empty. > 11 22 33 44
- [4] 11 22 33 44 > 3 PICK
- [5] 22 33 44 11 > 2 PICK
- [6] 33 44 11 33 > 1 PICK <--- hey it's OVER
- [7] 44 11 33 11 > 0 PICK <--- hey it's DUP
- [8] 11 33 11 11 > CLEAR <--- you got CLEAR?
- Stack Empty. > 11 22 33 44
- [4] 11 22 33 44 > 3 ROLL
- [4] 22 33 44 11 > 2 ROLL <--- hey it's ROT
- [4] 22 44 11 33 > 1 ROLL <--- hey it's SWAP
- [4] 22 44 33 11 > 0 ROLL <--- hey it's nothing.
- [4] 22 44 33 11 >
-
- Both PICK and ROLL are zero based indexing. Consider the stack to be an
- array with the 0th array position corresponding to the top of the stack.
-
- Note that we could define some of our common stack operators using PICK
- and ROLL.
-
- : OVER 1 PICK ; <--- Make these definitions and verify
- : DUP 0 PICK ; <--- that they work like the regular versions
- : ROT 2 ROLL ;
- : SWAP 1 ROLL ;
-
- No one would do this... it's just interesting... OVER , DUP , ROT , and
- SWAP are implemented using machine CODE definitions.
-
- More new words. These are not part of the FORTH83 standard but they are
- in F-PC and very useful.
-
- NIP ( a b -- b ) Remove second number from the stack.
- TUCK ( a b -- b a b ) Shove copy of top item under second.
- 3DUP ( a b c -- a b c a b c ) Duplicate top three items.
-
- ╓─────────────╖
- ║ Problem 2.8 ║
- ╙─────────────╜
- a) Try the above words with MYQUIT running and verify their function
- as we did with PICK and ROLL .
-
- b) Write high level colon definitions for NIP TUCK and 3DUP.
- Hint: : NIP SWAP DROP ; Opps... only two left to do!
-
- c) Write colon definitions for the following words whose stack effects
- are indicated below.
- SPIN ( a b c -- c b a )
- HATCH ( a b c -- a b b c )
- SHOVE ( a b c -- c a b c )
-
- Let's write an new word to keep TANK_VOLUME company. Let's write a word
- to compute the surface area of the rectangular tank.
-
- Surface area = 2lw + 2lh + 2wh.
-
- : TANK_AREA ( l w h -- ) \ add stack comments for each line
- 3DUP ( )
- 5 ROLL * 2 * ( )
- -ROT * 2 * ( )
- + ( )
- -ROT * 2 * ( )
- + ( )
- CR ." Surface area of tank is " . ." square" .FT ;
-
- Enter TANK_AREA into your file TANKVOL.SEQ , Test it and debug it if you
- need help determining the stack effects for each line.
-
- ╓──────────────╖
- ║ Problem 2.9 ║
- ╙──────────────╜
- Rectangle area & perimeter
- a) Write a word called .LW that takes two stack items, a
- length and a width and echoes the values on separate lines
- with identifiers. Note .LW should leave stack empty.**
-
- b) Write a word called AREA that takes two stack items, a
- length and a width, and computes and displays the area of a
- rectangle. Note AREA should leave the stack empty.**
-
- c) Write a word call PERIMETER that takes two stack items, a
- length and a width, and computes and displays the perimeter
- of a rectangle. Note PERIMETER should leave stack empty.**
-
- d) Write a word called RECTANGLE that takes two stack items, a
- length and a width, and performs the functions of a b & c .
- Did you use the .LW , AREA , and PERIMETER in your
- definition of RECTANGLE?
- ** It would be better to say: it consumes its stack parameters.
-
- ┌────────────────────────────────────┐
- │ Please move to Lesson 2 Part 070 │
- └────────────────────────────────────┘
-