home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 2 Part 050 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
- We are going to put you to work right away this time.
-
- ╓──────────────╖
- ║ Problem 2.6 ║
- ╙──────────────╜
- Use HELP , VIEW , Starting Forth etc to produce a table of stack
- comments and descriptions for the following words and upload your
- solution to the BBS as a reference for others reading the tutorial.
- First one to upload the complete list wins! We only want one copy
- posted. Here is the word list:
-
- DROP SWAP DUP OVER ROT -ROT 2DROP 2SWAP 2DUP 2OVER
-
- Example:
- OVER ( a b -- a b a ) Make a copy of the second item to top of stack.
-
- ┌─────────────────────────────┐
- │ Our First Useful Program. │
- └─────────────────────────────┘
-
- Make a Forth word to compute the volume of a rectangular processing
- tank that has length l , width w, and height h ( all measured in feet ).
- Call the word TANK_VOLUME . The word should be used as follows:
-
- <l> <w> <h> TANK_VOLUME <enter>
-
- We could give the stack picture instead:
-
- TANK_VOLUME ( l w h -- )
-
- Here is a sample of the output to be produced by TANK_VOLUME.
-
- 4 3 2 TANK_VOLUME <enter>
- Length 4 feet
- Width 3 feet
- Height 2 feet
- Volume 24 cubic feet. ok
-
- Our first approach will be to write just one word to do everything
- shown above. By the way... Forth programmers commonly create programs,
- tools, and even large applications that are used from within a Forth
- system operation environment so passing the input to the program
- TANK_VOLUME as a set of parameters on the stack is very common.
-
- The first thing our program/word must accomplish is to echo or display
- the input stack parameters on the screen. Note that the first value
- displayed is the length which is the third item on the stack. This can
- be accomplished by the sequence:
-
- CR ROT DUP ." Length" . ." feet" ( l w h -- w l h ) <-- stack!
-
- Here is the code for the word TANK_VOLUME. Enter it into a file
- called TANKVOL.SEQ
-
- \ First Approach: Write one word to do the whole thing!
- : TANK_VOLUME ( l w h -- )
- CR ROT DUP ." Length " . ." feet"
- CR ROT DUP ." Width " . ." feet"
- CR ROT DUP ." Height " . ." feet"
- CR * * ." Volume " . ." cubic feet." ;
-
- ╓─────────────╖
- ║ Problem 2.7 ║
- ╙─────────────╜
- a) Add stack comments to each line of the above definition.
- b) Test the word with some typical values. What is the largest cubical
- tank ( l=w=h ) that can have its volume computed accurately?
- (Remember we are using signed integers). If you changed the
- dot " . " to " U. " what is the largest cubical tank that would
- have its volume computed accurately?
- c) Decompile TANK_VOLUME. Debug TANK_VOLUME.
- d) Please please do this. Leave out the last " mark just before the
- semi colon and recompile the word. Report back to me what happened!
- We predict something weird will happen. Can you discover how to
- recover without resetting you machine? Hint: With the " missing
- semi-colon is gobbled up by ." looking for the terminating " which
- does not exist.
-
- IMPORTANT: At various points we have deliberately been showing you how
- you could crash your Forth system. We have heard many stories from the
- people new to Forth complaining about all the crashes and having to
- reset the machine. If you are aware of some of the things that can go
- wrong it is often possible to recover gracefully without resetting your
- machine.
-
- ┌─────────────────────────────────────────────────┐
- │ Factoring. ( Almost like algebraic factoring ) │
- └─────────────────────────────────────────────────┘
-
- You may have noticed a lot of repetition in the above definition of
- TANK_VOLUME. Every line has CR ROT DUP . and ." feet"
- Here is a new version called TANK-VOLUME that "factors" out the
- repetitions into two new words... .ECHO and .FT.
- \ Add this to your file TANKVOL.SEQ. Verify that TANK-VOL functions.
-
- \ Second Approach: Volume of a tank factored.
- : .ECHO ( a b c -- b c a )
- ROT DUP . ;
-
- : .FT ( -- )
- ." feet." CR ;
-
- : TANK-VOLUME ( l w h -- )
- CR ." Length " .ECHO .FT
- ." Width " .ECHO .FT
- ." Height " .ECHO .FT
- * * ." Volume " . ." cubic" .FT ;
-
- ┌────────────────────────────────────┐
- │ Please move to Lesson 2 Part 060 │
- └────────────────────────────────────┘
-