It is sometimes useful to inspect the internals of a word. There are two ways to do this:
SEEdecompiles a named word,
DECOMPILEdecompiles a given XT to screen.
: simple 1 2 + . ; SEE simpleRunning this program:
 Literal(1)  Literal(2)  +  . okThe listing shows the contents of the code section for the word
SIMPLE. The numbers on the right correspond to each action compiled by Smojo into the word
+word is a primitive word, which adds two numbers.
SEEing it shows that you can't break it down further:
see + + ok
.is a compound word meaning that it is constructed out of primitives and literals. You can
see .  (.)  BL  (.) ok
Using DECOMPILEYou can similarly decompile any XT (since the XT contains the word's code section), with the word
' otherwise DECOMPILE  Literal XT: _  COMPILE,  -> ok
Understanding DecompilationsSome further notes on decompilations:
|Decompilation||What it means|
This puts the literal, whose value is shown between the parentheses, onto the stack. Literal XTs are also indicated by Literal XT: ...
|Primitive & Compound Words||
The actual name of the primitive/compound word is shown. Compound words can be |
SEEn to show their component words.
An Unconditional Jump that moves the program to the given line in that word. For example,
Jump<21>is an instruction that moves instruction to Line #21. Note: any jump past the end of the compound word will exit that word.
A Conditional Jump that moves the program to the given line in that word, if the top of the stack evaluates to false. For example,
CJump<5>is an instruction that moves instruction to Line #5, only if the top of the stack evaluates to false. If it's true, then the program proceeds to the next instruction immediately after the CJump. Note: any jump past the end of the compound word will exit that word.
DOES>that will be explained in later sections.
Question 1The word
SEEis a compound word, which uses the primitive
DECOMPILE. How could you prove this?
see see  BL  PARSE  DOES>  DUP  NULL?  CJump<11>  DROP  Literal(No such word.)  .  CR  Jump<2147483647>  DECOMPILE
DECOMPILEappears in  of the listing.
Question 2Decompile the word
EXIT. Is it primitive or compound?
see exit EXIT ok
exitis a primitive word.
Decompile this word:
: xyz EXIT ;Explain the decompilation listing:
- What happened to the primitive word for
EXIT? Why isn't it in
XYZ's code section? Hint: is
- Were the words
:and ; compiled into the code section of
XYZ? Why not? Hint: Is
:fired before or after
XYZis defined? Is
- What kind of jumps are there in the decompilation? Can you explain what it does?
see xyz  Jump<2147483647>
exitis immediate so the word
exitis not in the code section. When
exitexecutes immediately and put
Jump<2147483647>into the code section, which will unconditionally jump to the end of the word when executed.
:is not an immediate word, but comes before xyz is defined so it is not in the code section.
;is an immediate word, it is executed immediately, which causes Smojo to switch from compilation mode to interpretation mode.
Try this word:
: three ( n -- ) 3 = if "Three!" . else exit then ;What does
THREEdo? Explain every line the decompilation listing. Can
THREEbe simplified? Test out your simplified version if so.
see three  Literal(3)  =  CJump<6>  Literal(Three!)  .  Jump<7>  Jump<2147483647>If the number on the top of the stack is not 3, jump to line 6, then in line 6 jumps past the word. Therefore, essentially if the number is not 3, exit the word. If the number is 3, print Three!, then jump past and exit the word. A simplified version would be:
: threeSimplified ( n -- ) 3 = if "Three!" . then ; see threeSimplified  Literal(3)  =  CJump<5>  Literal(Three!)  .For this version, if the number is not 3, jump past and exit the word. If the number is 3, print Three!, then the word ends.