§1  Tablehandler Definition Module  TABLE HANDLER 1

1. Table Handler. This module is a general-purpose symbol table handler. It is based on the Table-Handler module in Niklaus Wirth’s Programming in Modula-2. It allows a calling program to build a table of symbols with a cardinal value associated with each. This is the definition module; the implementation module is in a separate source file.

definition module table handler ; ⟨ Export List 2 ⟩

const ⟨ Constants of the module 3 ⟩

type ⟨ Global types 4 ⟩

2. These are the symbols we are exporting. They are the only ones visible outside of the module. ⟨ Export List 2 ⟩ ≡

export qualified symbol length, undefined symbols in table, symbol string ptr , symbol string , search symbol table, delete symbol table, create symbol table, set error handler , table proc, error proc, symbol table error code, symbol table, symbol is defined , set value of symbol , get value of symbol ;

This code is used in section 1.

3. ⟨ Constants of the module 3 ⟩ ≡ symbol length = 200;

This code is used in section 1.

4. The following types are defined here, rather than in the implementation module, because they are either needed for the procedure definitions which follow or are to be exported or both. symbol table is defined as an opaque type, since the details of its structure are hidden. The complete definition appears in the implementation module. ⟨ Global types 4 ⟩ ≡

symbol table;

symbol table error code = (undefined symbol , duplicate symbol , memory exhausted ); symbol string = array [0 . . symbol length − 1] of char ; symbol string ptr = pointer to symbol string ;

See also sections 8 and 10. This code is used in section 1.

5. This procedure is used to create a symbol table. An error procedure can be supplied by the user in case errors occur during symbol manipulation. Case-sensitivity is optional for table keys. If case sens = true then JUNK and junk are considered different symbols. procedure create symbol table(var the table : symbol table; case sens : boolean;

user error routine : error proc);

6. An error procedure can be supplied by the user in case errors occur during symbol manipulation. This procedure is used to change the error handler for a particular table after it has been created. procedure set error handler (the table : symbol table; user error routine : error proc);

7. This function procedure checks the integrity of the table by searching for entries that have been referenced, but never defined. If undefined symbols are found, the user-supplied error routine is called for each one and a value of true is returned for the procedure. procedure undefined symbols in table(the table : symbol table;

undefined symbol handling routine : error proc): boolean;

2  TABLE HANDLER  Tablehandler Definition Module §8

8. Modula-2 allows a mechanism whereby a procedure, complete with parameters, can be defined as a data type and stored in memory or passed like any other parameter to another procedure. We define a procedure type table proc, which is used in the definition of search symbol table below. The user is allowed to specify his own procedure to perform some action on each entry in the table. The value store in the entry, a pointer to the symbol, and a boolean variable indicating the symbol is defined are passed as parameters ⟨ Global types 4 ⟩ +≡

table proc = procedure (cardinal , symbol string ptr , boolean);

9. This procedure performs a user-provided operation on every entry in the table. As each entry is processed, a user-provided procedure is called, and the value stored with this name is passed as a parameter. The user procedure is free to perform any operation it wishes. The nodes are processed alphabetically. procedure search symbol table(the table : symbol table; user action routine : table proc);

10. We define a error handler procedure type, which is called whenever an error occurs. When one of these procedures is called, the type of error and the symbol involved are passed as parameters. ⟨ Global types 4 ⟩ +≡

error proc = procedure (symbol table error code, array of char );

11. This is the main symbol table lookup procedure, the one visible to the calling program. If the entry does not exist it is created. procedure get value of symbol ( the table : symbol table; target symbol : array of char ;

this is a definition: boolean; var string ptr : symbol string ptr ) : cardinal ;

12. We provide a procedure to set the table entry to a specific value. procedure set value of symbol ( the table : symbol table; target symbol : array of char ;

this is a definition: boolean; var string ptr : symbol string ptr ; the val : cardinal ) ;

13. We provide a function procedure to determine if a particular symbol has been defined. procedure symbol is defined (the table : symbol table; target symbol : array of char ): boolean;

14. This procedure is used to delete the table and free the memory allocated to it. procedure delete symbol table(var the table : symbol table);

15. Here is the end of the definition module.

end table handler .

§16  Tablehandler Definition Module  INDEX 3

16. Index.

boolean: 5, 7, 8, 11, 12, 13. cardinal : \underline{8}, 11, 12. case sens : \underline{5}. char : 4, 10, 11, 12, 13. create symbol table: 2, \underline{5}. delete symbol table: 2, \underline{14}. duplicate symbol : \underline{4}. error proc: 2, 5, 6, 7, \underline{10}. get value of symbol : 2, \underline{11}. memory exhausted : \underline{4}. search symbol table: 2, 8, \underline{9}. set error handler : 2, \underline{6}. set value of symbol : 2, \underline{12}. string ptr : \underline{11}, \underline{12}. symbol is defined : 2, \underline{13}. symbol length: 2, \underline{3}, 4. symbol string : 2, \underline{4}. symbol string ptr : 2, \underline{4}, 8, 11, 12. symbol table: 2, \underline{4}, 5, 6, 7, 9, 11, 12, 13, 14. symbol table error code: 2, \underline{4}, \underline{10}. table handler : \underline{1}, 15. table proc: 2, \underline{8}, 9. target symbol : \underline{11}, \underline{12}, \underline{13}. the table: \underline{5}, \underline{6}, \underline{7}, \underline{9}, \underline{11}, \underline{12}, \underline{13}, \underline{14}. the val : 12. this is a definition: \underline{11}, \underline{12}. true: 5, 7. undefined symbol : \underline{4}. undefined symbol handling routine: \underline{7}. undefined symbols in table: 2, \underline{7}. user action routine: \underline{9}. user error routine: \underline{5}, \underline{6}.

4  NAMES OF THE SECTIONS  Tablehandler Definition Module §16

⟨ Constants of the module 3 ⟩ Used in section 1.

⟨ Export List 2 ⟩ Used in section 1. ⟨ Global types 4, 8, 10 ⟩ Used in section 1.

Tablehandler Definition Module

Section Page

Table Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3