4.1 Introducing the Text Interpreter

When you invoke the Forth image, you will see a startup banner printed and nothing else (if you have Gforth installed on your system, try invoking it now, by typing gforthRET). Forth is now running its command line interpreter, which is called the Text Interpreter (also known as the Outer Interpreter). (You will learn a lot about the text interpreter as you read through this chapter, for more detail see The Text Interpreter).

Although it’s not obvious, Forth is actually waiting for your input. Type a number and press the RET key:

45RET  ok

Rather than give you a prompt to invite you to input something, the text interpreter prints a status message after it has processed a line of input. The status message in this case (“ ok” followed by carriage-return) indicates that the text interpreter was able to process all of your input successfully. Now type something illegal:

*the terminal*:2: Undefined word
$2A95B42A20 throw 
$2A95B57FB8 no.extensions 

The exact text, other than the “Undefined word” may differ slightly on your system, but the effect is the same; when the text interpreter detects an error, it discards any remaining text on a line, resets certain internal state and prints an error message. For a detailed description of error messages see Error messages.

The text interpreter waits for you to press carriage-return, and then processes your input line. Starting at the beginning of the line, it breaks the line into groups of characters separated by spaces. For each group of characters in turn, it makes two attempts to do something:

If the text interpreter is unable to do either of these things with any group of characters, it discards the group of characters and the rest of the line, then prints an error message. If the text interpreter reaches the end of the line without error, it prints the status message “ ok” followed by carriage-return.

This is the simplest command we can give to the text interpreter:

RET  ok

The text interpreter did everything we asked it to do (nothing) without an error, so it said that everything is “ ok”. Try a slightly longer command:

12 dup fred dupRET
*the terminal*:3: Undefined word
12 dup >>>fred<<< dup
$2A95B42A20 throw 
$2A95B57FB8 no.extensions 

When you press the carriage-return key, the text interpreter starts to work its way along the line:

At this point, the text interpreter gives up and prints an error message. The error message shows exactly how far the text interpreter got in processing the line. In particular, it shows that the text interpreter made no attempt to do anything with the final character group, dup, even though we have good reason to believe that the text interpreter would have no problem looking that word up and executing it a second time.



We can’t tell if it found them or not, but assume for now that it did not