home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 3 Part 060 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
- ┌───────────────────────┐
- │ ASCII and CONTROL │
- └───────────────────────┘
-
- Let's start by looking at the words ASCII and CONTROL . We used the
- word ASCII back in lesson 2 part 13 in the definition of TRIANGLE_AREA .
- However we neglected to tell you exactly how ASCII works. Remember
- that if we neglect to explain a word to you or if you have forgotten how
- a word works you may get some hints and clues as to a words function and
- operation by using HELP and VIEW. For example:
-
- HELP ASCII <enter>
-
- ASCII <char> ( -- n )
- Compile the next character in the input stream as a literal ASCII
- integer.
-
- CONTROL <char> ( -- n )
- Compile the next character in the input stream as a literal ASCII
- Control Character.
-
- Well... that wasn't too helpful was it? Do you know what the input
- stream is? Do you know what a literal ASCII integer is? Do you know
- what compile means? And what if you're not compiling?
-
- Let's attempt to answer some of these questions. First the input stream
- is the sequence of characters that you enter at the keyboard. After you
- input a line of text and press <enter> F-PC begins to process the
- "stream" or "line" or "list" of characters that you typed. We say that
- F-PC is processing the "input stream". When you FLOAD a file, F-PC just
- processes the characters in the file line by line and we would say that
- the "input stream" is now coming from the file.
-
- A literal integer is what gets compiled when you put a number in a Forth
- word definition.
-
- : TEN 10 ;
-
- We say that the 10 gets compiled as a literal integer.
-
- Ok.. let's experiment with using ASCII outside of a colon definition.
- What ASCII does is take the first letter of the first word that follows
- it and converts the letter to its corresponding ASCII key code and
- puts this ASCII key code on the top of the parameter stack.
-
- For example:
- ASCII A . <enter> 65 ok
- ASCII B . <enter> 66 ok
- ASCII . . <enter> 46 ok
- ASCII 1 . <enter> 49 ok
- ASCII 0 . <enter> 48 ok
-
- If you follow ASCII with an entire word it will just take the first
- letter of the word and throw the rest of the word away.
-
- ASCII APPLE . <enter> 65 ok
- ASCII BANANA . <enter> 66 ok
-
- Now let's use ASCII in a definition and then decompile the definition to
- see exactly what is happening.
-
- : ATEST ASCII A . ASCII B . ASCII APPLE . ASCII BANANA . ; <enter> ok
- SEE ATEST <enter>
- : ATEST
- 65 . 66 . 65 . 66 . ; ok
- ATEST <enter> 65 66 65 66 ok
-
- So... when ASCII is used within a word definition it compiles the
- literal integer value corresponding to the next non blank character in
- the input stream. If the next non blank character is part of a word it
- just uses the first character of that word and then throws the rest of
- the word away.
-
- Warning, as one of our friends discovered the hard way, F-PC process
- both files and user input one line at a time so if you split the word
- ASCII and the next non blank character across a line boundary you are
- going to be in trouble. ASCII must be followed by the character to be
- converted to a key code must be on the same line.
-
- Now you should be able to write your own tutorial on the use of CONTROL
- but we will provide a few examples for you to ponder.
-
- CONTROL M . <enter> 13 ok <--- This is just the <enter> key code.
- CONTROL H . <enter> 8 ok <--- This is just the backspace key code.
- CONTROL A . <enter> 1 ok <--- This is just control A key code.
-
- Now the definition of KEY_TEST in the last lesson may be easier to follow
- as we used CONTROL M with out any explanation. All CONTROL M does is
- compile the literal integer 13 into the definition for use to test for
- termination of the program by the press of the enter key.
-
- Note... The words ASCII and CONTROL are not part of the Forth83
- standard but they have always been in each of the half dozen Forth
- systems that we have used over the last 5 years.
-
- ┌────────────────────────────────────┐
- │ Please move to Lesson 3 Part 070 │
- └────────────────────────────────────┘
-