next up previous contents
: Using sm1 to teach : kan/examples : Function definition   目次

Dictionaries and contexts

The def or set operators associate a key with a value and that key-value pair is stored in the current dictionary. They key may starts with any printable character except ( ) [ ] { } $ % and numbers and be followed by any printable characters except the special characters. For example,

  foo  test   Test!   foo?59
are accepted as names for keys.

A key-value pair is stored in the current dictionary when you use the operator def or the operator set. For example, when you input the line

   /foo  15   def
then the key-value pair (foo, 15) is stored in the current dictionary. We can generate several dictionaries in sm1. Each dictionary must have its parent dictionary. When you input a token (key) that is not a number or a string or a literal, sm1 looks up the current dictionary to find the value of the key. If the value is an executable array, then it will be executed. If the value is not an executable array, then the value is put on the stack as an object. If the looking-up fails, then it tries to find the value in the parent dictionary. If it fails again, then it tries to find the value in the grandparent dictionary and so on. This mechanism enables us to write an object oriented system. When the system starts, there are two dictionaries: primitive dictionary and the standard user dictionary. For example, the input ? makes sm1 to look up the standard user dictionary and sm1 finds the value of ?, which is an executable array that displays all keys in the primitive dictionary.

A new dictionary can be created by the operator newcontext. Here is an example of creating a new dictionary.

/abc { (Bye) message } def 
/aaa 20 def 
abc  aaa ::
The key-value pairs (abc, { (Bye) message } and (aaa, 20 ) are stored in the current dictionary (StandardContextp). Here is the output from the system.
Bye
20
(mycontext) StandardContextp newcontext /nc set ;
nc setcontext ;
Create a new dictionary and change the current dictionary by setcontext.
/abc { (Hello) message } def ;
abc aaa ::
Store a new key-value pair in the new dictionary. Here is the output of the system.
Hello
20
The key abc was found in the current dictionary, so the system outputs Hello. The key aaa was not found in the current dictionary, so the system looked for it in the parent dictionary and outputs the value 20.

It is sometimes preferable to protect the key-value pairs from unexpected rewriting. If you input the following lines, then all pairs in the current dictionary except arg1, arg2, arg3, v1, v2, `@=11@.usages will become readonly pairs.

[(Strict2) 1] system_variable  %% from var.sm1
[(chattrs) 2] extension
[(chattr) 0 /arg1] extension
[(chattr) 0 /arg2] extension
[(chattr) 0 /arg3] extension
[(chattr) 0 /v1] extension  %% used in join.
[(chattr) 0 /v2] extension
[(chattr) 0 /`@=11@.usages] extension


next up previous contents
: Using sm1 to teach : kan/examples : Function definition   目次
Nobuki Takayama 平成20年1月30日