The Dictionary
In Smojo, all the words are stored in a dictionary, which is a hash. The keys of this dictionary are the names of the words in lower case and the values are the XTs. The following words can help you get and set this dictionary:
Words | Action |
---|---|
GET-DICTIONARY ( -- # ) |
Returns the entire dictionary as a hash and puts it on the stack. |
SET-DICTIONARY ( # -- ) |
Replaces the current dictionary with the given hash. |
MARK |
Takes a snapshot of the dictionary. Since SET-DICTIONARYwill cause an irreversible damage to the dictionary if you use the wrong hash, it is a good practice to use MARKbefore that. |
RESTORE |
Restores the dictionary back to the version previously MARKed. |
Here are some examples of using these words.
Get the dictionary
For example,DUPis a word. If "dup" is on the stack as a string, we can use
GET-DICTIONARYto retrieve the XT of
DUP
"dup" get-dictionary #@ . com.terraweather.mini.XT@1969ea60 ok
Be reminded that the returned XT will be different when you try the example above
Set the dictionary
For example, if "plus" is on the stack as a string and we want to create a wordPLUSwhich functions as the word
+, we can use
SET-DICTIONARYto do so:
"plus" ' + get-dictionary >R ok R@ #! \ add "plus" and XT of + key-value pair into the dictionary hash ok R> set-dictionary ok 3 5 plus . 8 ok
Quiz
Question 1
In the example ofSET-DICTIONARY, try to use
MARKand
RESTOREto restore the dictionary back to the original version at the end of the program.
"plus" ' + get-dictionary >R mark R@ #! R> set-dictionary restore
Question 2
Write a wordBIND ( "name" xt -- )which binds the given name and XT and add it to the dictionary.
: BIND ( "name" xt -- ) get-dictionary >R R@ #! R> set-dictionary ;