Making New WordsAll words are stored in Smojo's dictionary. Each word has a name and an action. For example, the word
crhas the name CR and its action is to cause Smojo to print a carriage return. To make a new word, you use the words
: say-hello "Hello Janna" . ;makes a new word named
SAY-HELLOin Smojo's dictionary. To run the word, you call it by its name,
Try it. What does this new word do?
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.
You can use almost anything as a name for a word. Three rules:
- You can't use blank spaces.
- You can't use numbers only.
- You shouldn't start names with " , ' or ` .
: 你好 "Hello Janna" . ;Also, Smojo isn't fussy about how you run words. So,
SAY-HELLOall refer to the same word, but I would recommend you stick with using lowercase in programs.
You may change the action of a word at any time. For example:
: greet "Hello Janna" . cr ; greet : greet "Goodbye Janna" . ; greetThe word
GREEThas changed actions. Run this program. Did you notice Smojo's warning to you?
No FavouritesSmojo treats all words the same. You may change the action of any word, even built-in ones like
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
minus10will subtract instead of add 10 from a number:
43 minus10 . 33 okThere is no way to recover the older action of + , unless you "save" it in another word.
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
Oh, where have you been, my blue-eyed son? And where have you been my darling young one? 1st redefined Oh, where have you been, my blue-eyed son? And where have you been my darling young one? ok
2ndis called twice, before and after redefinition of
1st, but the output are the same.
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.
Write a Word that prints a blank line. What will you call it?
: blank-line cr ;
How would you "save" the original definition of a word?
: greet "Hello Janna" . cr ; \ original definition greet \ display Hello Janna : old-greet greet ; \ save original definition : greet "Goodbye Janna" . cr ; \ new definition greet \ use new definition => display Goodbye Janna old-greet \ use original definition => display Hello JannaNote that
\is a line comment. Everything after
\until a new line is ignored by the program.