home *** CD-ROM | disk | FTP | other *** search
- \ Lesson 6 Part 3 ( F-PC 3.5 Tutorial by Jack Brown )
- COMMENT:
- We begin with an example and a problem.
- Example: Here are number of ways to make a dashed line using 10 dash
- characters.
- COMMENT;
- : DASHED1 ( -- ) CR ." ----------" ;
- CREATE DASH ," ----------"
- : DASHED2 ( -- ) CR DASH COUNT TYPE ;
- : DASHED3 ( -- ) CR " ----------" TYPE ;
- : DASHED4 ( -- ) CR 10 0 DO ASCII - EMIT LOOP ;
- COMMENT: **
- Problem 6.3 ****
- Use each of the four techniques ******
- above to produce the display ********
- shown at the right. **********
- **
-
- The Forth words FILL and ERASE which were studied earlier are often
- used as string initialization primitives.
-
- FILL ( addr n c -- ) Fill string at addr with n copies of c .
- ERASE ( addr n -- ) Fill string at addr with n null's or 0's.
-
- Try the following:
- NAME$ ," George Smith" <enter> ok
- NAME$ COUNT TYPE <enter> George Smith ok
- NAME$ COUNT ASCII * FILL <enter> ok
- NAME$ COUNT TYPE <enter> ************ ok
- NAME$ COUNT BL FILL <enter>
- NAME$ COUNT TYPE <enter> ok
-
- Forth String Input Techniques.
-
- The Forth word EXPECT is used to input a string of up to length n to a
- buffer at address, addr . The actual number of characters entered is
- stored in a variable called SPAN for use by the application program. The
- Forth 83 implementation of EXPECT will return control to the calling
- program when the nth character has been received. Pressing <enter>
- before the nth character will also return control to the calling program
- The string placed in the buffer at addr by EXPECT is not a counted
- string, the application program must determine the actual number of
- characters entered by the user by interrogating the variable SPAN.
-
- EXPECT ( addr n -- ) \ Input up to n characters to buffer at addr
- COMMENT;
- CREATE BUFFER1 80 C, 80 ALLOT VARIABLE LEN
-
- \ Accept a string up to 80 characters long from the console.
- : READLINE ( -- )
- BUFFER1 COUNT BL FILL \ Clear BUFFER1 to blanks.
- CR BUFFER1 COUNT EXPECT \ Input up to 80 characters
- \ SPAN @ BUFFER1 C! \ Alternate to below method.
- SPAN @ LEN ! ; \ Save actual character count
- COMMENT:
- Note: Actual character count is returned in variable SPAN and is saved
- for our own use in the VARIABLE LEN . This must be done because the
- Forth system itself uses EXPECT to input characters and our value stored
- in SPAN would be lost.
- COMMENT;
- \ Display string stored in BUFFER1
- : SHOWLINE ( -- )
- CR BUFFER1 1+ \ skip of buffer length of 80
- LEN @ TYPE ; \ display actual number entered.
-
- COMMENT:
- Problem 6.4
- Write a simple ELIZA program that will interact with a user. Your
- program should use EXPECT for string input. Your program should find
- and save at least the following information about the user: NAME SEX
- and EYE-COLOUR saving the information. Your program should ask at least
- two questions that can be answered with a yes or no answer. Save the
- yes/now response in a string called ANSWER . You program should only
- check the first letter of the entered string and case should not matter
- so that Y YES yes yah etc would all be interpreted as a YES and No
- no N Nah Nyet etc would all be taken as a NO. Your program should
- provide alternate paths depending upon the answers given.
-
- Hints:
- COMMENT;
- CREATE EYE-COLOUR ," **********"
- : EYE-COLOUR? ( -- )
- ." What colour are your eyes? "
- EYE-COLOUR COUNT BL FILL
- EYE-COLOUR COUNT EXPECT
- SPAN @ EYE-COLOUR C! ;
-
-