Modes
Smojo works in two different ways:
- Interpretation Mode: This is the default, and words are executed as they occur, from left to right. For example, the program:
"Hello" .
is entirely in interpretation mode. Smojo encounters the Literal "Hello" and puts that text on the stack, then it runs the word.
, printing what's on the top of the stack. - Compilation Mode: This mode is entered into after the word
:
and is ended by the word;
. When Smojo is in compilation mode, it does not execute words, except for special immediate words (see below). Instead, it adds them into the action part of the new word's dictionary entry. This process is known as compilation. For example, in this program:: double dup + ;
the wordsDUP
and+
are not executed, but are instead compiled into the definition of the new wordDOUBLE
.
Immediate Words
Immediate words are special because they are always executed when they are encountered, regardless of what the mode is. To create an immediate word, simply use the wordIMMEDIATEafter its definition. For example:
: say-hi "hello" . cr ; immediatecreates the word immediate word
SAY-HI. This means that
SAY-HIwill execute even when we're in compilation mode:
: jello "Have some Jello" . cr say-hi ;will cause the text "hello" to be displayed.
Quiz
Question 1
In the example above, what happens when the wordJELLOis executed? Try this out. Are you surprised by the result?
"hello" is printed immediately after word definition of
JELLO. When we run
JELLO, only "Have some Jello" is printed.
Question 2
You can tell if a word is immediate by using the wordimmediate?and the word
', known as TICK. For example:
' ; immediate? .should display "true ok", because the word
;is indeed immediate. Can you explain why
;is immediate out of necessity?
NB: TICK is a word we will encounter many times later. It simply suppresses the execution of a word, rather putting that word's action, called an XT (for eXecuTable) on the stack.
;is immediate so that Smojo can exit from compilation mode
Question 3
The available words can be listed using the word:
wordsTry it. Use this list to detect two other immediate words.
Some immediate words:
( | |. loop