XTs
In Smojo, a word can be treated like a Literal: It is possible to put them on the stack, print their contents and of course, execute them. In actuality, we don't really put the Word on the stack, but rather something called an XT (from eXecuTable).
An XT contains the following information:
- The Word's code section that performs the action,
- The Word's data section, which I'll discuss later,
- An indicator if the Word is immediate.
'(called TICK) mainly in interpretation mode or
['](called BRACKET-TICK) in compilation mode only. (NB:
'can be also used in compilation mode - see the Quiz below.). You can run an XT using the word
EXECUTE.
An Example
Recall that.Ssimply displays the contents of the stack. This program:
' * .Swill display the XT like so:
<1> com.terraweather.mini.XT@7a01fdb2 okThis XT has the action of multiplying two numbers. We can demonstrate this:
>R 2 3 R> .SIt helps to check the stack again:
<3> 2 3 com.terraweather.mini.XT@7a01fdb2 okAnd we finally run the XT:
execute . 6 ok
Quiz
Question 1
What is the difference between the tick'and the bracket-tick
[']? Write programs to test out your answer.
Unlike tick
', bracket-tick
[']is an immediate word. Also, bracket-tick
[']can be used in compilation mode only.
Question 2
In the example, what is the alternative to using>Rand
R>? Rewrite the demonstration to test your answer.
2 3 ' * .S execute .
Question 3
Write a program to test if a given XT's word is immediate.
: isImmediate ( xt -- true | false ) immediate? if "Yes" else "No" then . cr ; ' ; isImmediate \ Yes ' + isImmediate \ No
Question 4
What does the wordXXdo?
: xx ' immediate? . ;Test your answer with a program using the word
XX. Hint: (a) Try
xx DUPand
xx IFCan you use the results to explain how
XXworks?
: xx ' immediate? . ; xx dup \ false, same as ' dup immediate? . xx if \ true, same as ' if immediate? .
xxchecks if the word following it is immediate. From this question, you can see that tick
'used in compilation mode has different meaning. As tick
'is not immediate, it executes during the call to
xxand operates on the next word in the command (
dupand
ifin this case). The execution then proceeds on to the next word in the
xxdefinition (
immediate?in this case).