Extending Smojo

Smojo is an extensible language, meaning you can (and are encouraged) to extend the language depending on the work at hand. For example, many of the important words in Smojo like 
DO
,
LOOP
,
ELSE
,
POSTPONE
and all the multi-level conditional words have been defined as language extensions.

The first step to extending Smojo is to understand the words
CREATE
,
DOES>
and
(CREATE)
WordAction
CREATE

Creates a new dictionary entry, with the name given at runtime. In other words, a name - XT binding is added to the dictionary.

  1. The XT created this way has a special code section - it just pushes the XT to the stack.
  2. The data section can optionally be defined using
    ,
DOES>
Optionally assigns further functionality to the dictionary entry from
CREATE
. The functionality is ended by
;
(CREATE)
Similar to
CREATE
except that
(CREATE)
receives the name from the top of the stack as text.

Using CREATE

We can create a word 
THREE
, whose data section is initialized to 3:

	
		create THREE 3 ,
		ok

		THREE . 
		com.terraweather.mini.XT@58a07808 ok
		
		THREE ?
		3 ok
	
Be sure you understand this example thoroughly before proceeding. Note that:
  1. CREATE
    defined a new word
    THREE
    on the dictionary; When it is run,
    THREE
    puts its XT on the stack.
  2. After
    CREATE
    defines the new word, the comma operator initializes its data section with a single element, whose value is 3.

The => Keyword

While many language have built-in "keywords" to do things, in Smojo, you can create your own. For example, consider the word
=>
below:
		: => ( n -- )
			CREATE , ; 
	
If you understand how
THREE
works, you should be able to understand
=>
. It creates a new dictionary entry using
CREATE
and initializes its data section using
,
. In other languages, this would be known as a "variable". Some ways to use:
		32 => HELLO
		ok
		
		HELLO ? 
		32 ok
		
		41 HELLO !
		ok 
		
		HELLO ?
		41 ok
	
Indeed, the example above is equivalent to another built-in word
VARIABLE
, try using
VARIABLE
in your own program.

Use DOES>

We can use 
DOES>
to add functionalities to the words. For example, if we want the word
THREE
to print the value out once it is called, we can modify it in the following way:

		create three 3 , does> ? ;
		three
	
		3 ok
	

Create Immediate Words

Similar to defining data sections, you can also mark the word being created immediate by using the word
IMMEDIATE
		create aa immediate does> "Immediate?" . immediate? . ;
		ok

		aa
		Immediate? true ok
	

Note that 
DOES>
always puts the XT of the word being created onto the stack. Again, be sure you understand this example thoroughly before proceeding.

Quiz

Question 1

What is the difference between using
:
and using
CREATE
?



Question 2

Use 
CREATE
to write a word with a data section of multiple elements



Question 3

Different from a variable as shown in the examples, a constant cannot be amended. Write a word
CONSTANT
which declares a constant.
CONSTANT
is also a built-in word, check your answer by using
SEE
.



Question 4

Write a word
::
for word redefinition such that the original definition is automatically saved in its data section.



Next: Deferred Words