home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l3p100
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
107 lines
╔════════════════════════════════════════════════════╗
║ Lesson 3 Part 100 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
If you would like more information on IF ... ELSE ... THEN study
Starting Forth 2nd Edition Chapter 4 pp 80-94. Brodie also has a good
collection of exercises and problems starting on page 94.
We are now going to look at a problem that is usually asked very quickly
by people who are familiar some other computer languages.
┌───────────────────────────────────────────────┐
│ Where is Forth's word for inputting numbers? │
└───────────────────────────────────────────────┘
The answer is quite easy. It doesn't have a word for inputting numbers.
The good news is that we can make a word for inputting numbers quite
easily.
But first you should recall that the numeric input required for any the
small programs ( Forth words ) that we have written so far was taken
care of by first placing the numeric input values on the parameter
stack. Thus when we wanted to calculate the volume of a rectangular
tank we would just type:
3 4 5 TANK <enter>
The length, width and height input were provided to TANK as stack
inputs. This is the Forth way of doing things. Many applications
written in Forth actually provide you with a Forth environment in which
to work so that you can get your work done by executing Forth words that
you provide with stack inputs. But let's suppose that we want to just
type the name of our program ( Forth word) and have the program prompt
us for the data required.
To do this we will have to write a forth word to input numbers. We will
call this new word #IN . To implement #IN we are going to steal some
ideas from the outer interpreter MYQUIT that we implemented back in
lesson 1. Remember the words QUERY and INTERPRET ?
QUERY ( -- ) Get a line of text from the keyboard and save in input
buffer.
INTERPRET ( -- ) Interpret the line of text in the input buffer.
The word for inputting numbers is simply:
: #IN ( -- n ) QUERY INTERPRET ;
That's all.... That's it! Let's use it in a program.
Numeric Input using QUERY and INTERPRET and an example
\ Super simple numeric input.
: #IN ( -- n )
QUERY INTERPRET ;
\ Input length.
: GETL ( -- l )
CR ." Enter tank length " #IN ;
\ Input width.
: GETW ( -- w )
CR ." Enter tank width " #IN ;
\ Input height.
: GETH ( -- h )
CR ." Enter tank height " #IN ;
\ Compute volume.
: .VOLUME ( l w h -- )
* * CR ." Volume " . ." cubic feet." ;
\ Compute surface area.
: .AREA ( l w h -- )
3DUP 5 ROLL * 2* -ROT * 2* + -ROT * 2* +
CR ." Surface area " . ." square feet." ;
\ This is the tank program with prompted input
: TANK ( -- )
GETL GETW GETH
3DUP .VOLUME .AREA ;
Here is what happens when we execute TANK
FLOAD INTANK <enter> ok
TANK <enter>
Enter tank length 5
Enter tank width 4
Enter tank height 3
Volume 60 cubic feet.
Surface area 94 square feet. ok
There is only one problem with this version on #IN . It is too
powerful! You can actually input just about anything when you get the
prompt including Forth words, more than one number and of course
garbage!
╓──────────────╖
║ Problem 3.23 ║
╙──────────────╜
Experiment with the program above and list at least 3 problems with this
definition of #IN. If you can find more than 3 list them all.
┌───────────────────────────────────┐
│ Please move to Lesson 3 Part 110 │
└───────────────────────────────────┘