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:
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.
|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
|Restores the dictionary back to the version previously
Here are some examples of using these words.
Get the dictionaryFor example,
DUPis a word. If "dup" is on the stack as a string, we can use
GET-DICTIONARYto retrieve the XT of
"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 dictionaryFor example, if "plus" is on the stack as a string and we want to create a word
PLUSwhich 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
Question 1In the example of
SET-DICTIONARY, try to use
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 2Write a word
BIND ( "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 ;