14.3. Adding Entries

Of course, writing to the table directly is pretty poor practice, and not one that will help us much later. What we need is a procedure to add entries to the table. At the same time, we know that we're going to need to test the table, to make sure that we aren't redeclaring a variable that's already in use (easy to do with only 26 choices!). To handle all this, enter the following new procedures:

{ Report Type of a Variable }
function TypeOf(N: char): char;
begin
   TypeOf := ST[N];
end;

{ Report if a Variable is in the Table }
function InTable(N: char): boolean;
begin
   InTable := TypeOf(N) <> '?';
end;

{ Check for a Duplicate Variable Name }
procedure CheckDup(N: char);
begin
   if InTable(N) then Abort('Duplicate Name ' + N);
end;

{ Add Entry to Table }
procedure AddEntry(N, T: char);
begin
   CheckDup(N);
   ST[N] := T;
end;

Now change the three lines in the main program to read:

     AddEntry('A', 'a');
     AddEntry('P', 'b');
     AddEntry('X', 'c');

and run the program again. Did it work? Then we have the symbol table routines needed to support our work on types. In the next section, we'll actually begin to use them.