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:

WordsAction
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-DICTIONARY
will cause an irreversible damage to the dictionary if you use the wrong hash, it is a good practice to use
MARK
before that.
RESTORE
Restores the dictionary back to the version previously
MARK
ed.

Here are some examples of using these words.

Get the dictionary

For example,
DUP
is a word. If "dup" is on the stack as a string, we can use
GET-DICTIONARY
to 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 word
PLUS
which functions as the word
+
, we can use
SET-DICTIONARY
to 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 of
SET-DICTIONARY
, try to use
MARK
and
RESTORE
to restore the dictionary back to the original version at the end of the program.



Question 2

Write a word
BIND ( "name" xt -- )
which binds the given name and XT and add it to the dictionary.



Next: Recursive Sequences*