Making New Words

All words are stored in Smojo's dictionary. Each word has a name and an action. For example, the word 
cr
has the name CR and its action is to cause Smojo to print a carriage return.

To make a new word, you use the words 
:
and
;

For example:

		: say-hello "Hello Janna" . ; 
	

makes a new word named 
SAY-HELLO
in Smojo's dictionary. To run the word, you call it by its name,

say-hello

Try it. What does this new word do?

Using Words

Once a Word is born, it can be used again and again within other Words. This is how Smojo programs are built. Simple words combine to form more complex Words.

Beginners create Messy words -
words that do many things,
words that are hard to change,
words that can't be re-used elsewhere.

Some break down their Messy words -
combinations of simpler, re-usable gems!
This makes building new programs easier and also a joy.

But this takes patience even for the experienced.

Names

You can use almost anything as a name for a word. Three rules:

  1. You can't use blank spaces.
  2. You can't use numbers only.
  3. You shouldn't start names with " , ' or ` .
Apart from these, anything goes. For example, you could have named SAY-HELLO as 你好:

		:  你好 "Hello Janna" . ; 
	

Also, Smojo isn't fussy about how you run words. So, 
say-HELLO
,
say-hello
and
SAY-HELLO
all refer to the same word, but I would recommend you stick with using lowercase in programs.

Changing Actions

You may change the action of a word at any time. For example:

			: greet "Hello Janna" . cr ;
			
			greet
			 
			: greet "Goodbye Janna" . ;
			
			greet
			 
		
The word
GREET
has changed actions. Run this program. Did you notice Smojo's warning to you?

No Favourites

Smojo treats all words the same. You may change the action of any word, even built-in ones like 
:
,
;
,
+
,
cr
and
.

Used sensibly, this ability makes Smojo a very powerful programming language.

Actions are Forever

Once you use a word within another, its action is "bound" permanently into the other. This action isn't lost if the first word is re-defined. For example, we might have a word like this:

			: 1+  1 + ;			 
		
which adds 1 to any number:
			21 1+ . 
		
will show 22. What if we re-define the Word + ?
			: +  - ;			 
		
Now, + is redefined to - (which is probably a bad idea). But if we run the program:
			21 1+ . 
		
The answer remains the same: 22. This is because the original action of + was "bound" into our definition of 1+. So, 1+ remains unchanged even if + changed later.

Now, if we create a new word after re-defining +, 
			
				: minus10 10 + ;			 
			
Then the new action of + is used, and
minus10
will subtract instead of add 10 from a number:
			43 minus10 .
			33 ok 
		
There is no way to recover the older action of + , unless you "save" it in another word.

Quiz

Question 1

What happens when you redefine a word that is used elsewhere? Will those dependent words change too? For example, run this:

			: 1st  "Oh, where have you been, my blue-eyed son?" . cr ; 
			: 2nd  
				1st 
				"And where have you been my darling young one?" . cr 
			;
			
			2nd 
			
			: 1st " Oh, what did you see, my blue eyed son?" . ; 
			
			2nd
		




Be sure you understand what is going on in this example before moving on! After answering this question, you might want to click here to read a more in-depth discussion on this issue.








Question 2

Write a Word that prints a blank line. What will you call it?



Question 3*

How would you "save" the original definition of a word?



Next: Modes