Modes

Smojo works in two different ways:

  1. 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.

  2. 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 words
    DUP
    and
    +
    are not executed, but are instead compiled into the definition of the new word
    DOUBLE
    .

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 word 
IMMEDIATE
after its definition. For example:
: say-hi "hello" . cr ; immediate
creates the word immediate word
SAY-HI
. This means that
SAY-HI
will 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 word 
JELLO
is executed? Try this out. Are you surprised by the result?



Question 2

You can tell if a word is immediate by using the word 
immediate?
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.



Question 3

The available words can be listed using the word:

words
Try it. Use this list to detect two other immediate words.



Next: Comments