home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / dtx9202 / pretty / prhash.mod < prev    next >
Encoding:
Modula Implementation  |  1991-02-18  |  5.5 KB  |  181 lines

  1.  
  2. IMPLEMENTATION MODULE PRHASH;
  3. (*****************************************************************************)
  4. (*                          Modula-2 Hash-Tabellen                           *)
  5. (*                       (c) Peter Engels       1990                         *)
  6. (*****************************************************************************)
  7.  
  8. FROM PRSCAN IMPORT TokenTyp,SymbolTyp,str;
  9.  
  10. FROM Str IMPORT Length,Copy,Compare;
  11.  
  12. FROM SYSTEM IMPORT ADDRESS,ADR;
  13.  
  14. FROM Lib IMPORT Move,HashString;
  15.  
  16. FROM Storage IMPORT ALLOCATE,DEALLOCATE;
  17.  
  18. CONST tablesize = 100;
  19.  
  20. TYPE hashptr = POINTER TO hashentrys;
  21.      hashentrys = RECORD
  22.                     symbol : SymbolTyp;
  23.                     value : POINTER TO str;
  24.                     next : hashptr
  25.                   END;
  26.      indizes = [0..tablesize - 1];
  27.      HashTables = ARRAY indizes OF hashptr;
  28.      HashTable = POINTER TO HashTables;
  29.  
  30. VAR table : HashTable;
  31.  
  32.   PROCEDURE InsertInHashTable (VAR token : TokenTyp);
  33.  
  34.   VAR help : hashptr;
  35.       index : indizes;
  36.  
  37.   BEGIN
  38.     NEW (help);
  39.     token.length := Length (token.value);
  40.     ALLOCATE (help^.value,token.length + 1);
  41.     Move (ADR (token.value),help^.value,token.length);
  42.     help^.value^ [token.length] := 0C;
  43.     help^.symbol := token.symbol;
  44.     index := HashString (token.value,tablesize);
  45.     help^.next := table^ [index];
  46.     table^ [index] := help
  47.   END InsertInHashTable;
  48.  
  49.   PROCEDURE SearchInHashTable (VAR token : TokenTyp);
  50.  
  51.   VAR index : indizes;
  52.       help : hashptr;
  53.  
  54.   BEGIN
  55.     index := HashString (token.value,tablesize);
  56.     help := table^ [index];
  57.     WHILE (help # NIL) & (Compare (token.value,help^.value^) # 0) DO
  58.       help := help^.next
  59.     END;
  60.     IF help # NIL THEN
  61.       token.symbol := help^.symbol
  62.     END
  63.   END SearchInHashTable;
  64.  
  65.   PROCEDURE CreateHashTable;
  66.  
  67.   VAR index : indizes;
  68.  
  69.   BEGIN
  70.     NEW (table);
  71.     FOR index := MIN (indizes) TO MAX (indizes) DO
  72.       table^ [index] := NIL
  73.     END
  74.   END CreateHashTable;
  75.  
  76.   PROCEDURE InitKeyWordTable;
  77.  
  78.     PROCEDURE InsertInTable (symbol : SymbolTyp;
  79.                              value : ARRAY OF CHAR);
  80.  
  81.     VAR token : TokenTyp;
  82.  
  83.     BEGIN
  84.       token.symbol := symbol;
  85.       Copy (token.value,value);
  86.       InsertInHashTable (token)
  87.     END InsertInTable;
  88.  
  89.   BEGIN
  90.     CreateHashTable;
  91.     InsertInTable (abssy,'ABS');
  92.     InsertInTable (andsy,'AND');
  93.     InsertInTable (arraysy,'ARRAY');
  94.     InsertInTable (beginsy,'BEGIN');
  95.     InsertInTable (bitsetsy,'BITSET');
  96.     InsertInTable (booleansy,'BOOLEAN');
  97.     InsertInTable (bysy,'BY');
  98.     InsertInTable (capsy,'CAP');
  99.     InsertInTable (casesy,'CASE');
  100.     InsertInTable (cardsy,'CARD');
  101.     InsertInTable (cardinalsy,'CARDINAL');
  102.     InsertInTable (charsy,'CHAR');
  103.     InsertInTable (chrsy,'CHR');
  104.     InsertInTable (classsy,'CLASS');
  105.     InsertInTable (constsy,'CONST');
  106.     InsertInTable (decsy,'DEC');
  107.     InsertInTable (defsy,'DEFINITION');
  108.     InsertInTable (disposesy,'DISPOSE');
  109.     InsertInTable (divsy,'DIV');
  110.     InsertInTable (dosy,'DO');
  111.     InsertInTable (elsesy,'ELSE');
  112.     InsertInTable (elsifsy,'ELSIF');
  113.     InsertInTable (endsy,'END');
  114.     InsertInTable (exclsy,'EXCL');
  115.     InsertInTable (exitsy,'EXIT');
  116.     InsertInTable (exportsy,'EXPORT');
  117.     InsertInTable (falsesy,'FALSE');
  118.     InsertInTable (fieldofssy,'FieldOfs');
  119.     InsertInTable (floatsy,'FLOAT');
  120.     InsertInTable (forsy,'FOR');
  121.     InsertInTable (forwardsy,'FORWARD');
  122.     InsertInTable (fromsy,'FROM');
  123.     InsertInTable (gotosy,'GOTO');
  124.     InsertInTable (haltsy,'HALT');
  125.     InsertInTable (highsy,'HIGH');
  126.     InsertInTable (ifsy,'IF');
  127.     InsertInTable (implsy,'IMPLEMENTATION');
  128.     InsertInTable (importsy,'IMPORT');
  129.     InsertInTable (insy,'IN');
  130.     InsertInTable (incsy,'INC');
  131.     InsertInTable (inclsy,'INCL');
  132.     InsertInTable (inlinesy,'INLINE');
  133.     InsertInTable (intsy,'INT');
  134.     InsertInTable (integersy,'INTEGER');
  135.     InsertInTable (labelsy,'LABEL');
  136.     InsertInTable (longcardsy,'LONGCARD');
  137.     InsertInTable (longintsy,'LONGINT');
  138.     InsertInTable (longrealsy,'LONGREAL');
  139.     InsertInTable (loopsy,'LOOP');
  140.     InsertInTable (maxsy,'MAX');
  141.     InsertInTable (minsy,'MIN');
  142.     InsertInTable (modsy,'MOD');
  143.     InsertInTable (mtasy,'MTA');
  144.     InsertInTable (modulesy,'MODULE');
  145.     InsertInTable (newsy,'NEW');
  146.     InsertInTable (nilsy,'NIL');
  147.     InsertInTable (notsy,'NOT');
  148.     InsertInTable (nullprocsy,'NULLPROC');
  149.     InsertInTable (oddsy,'ODD');
  150.     InsertInTable (ofsy,'OF');
  151.     InsertInTable (orsy,'OR');
  152.     InsertInTable (ordsy,'ORD');
  153.     InsertInTable (pointersy,'POINTER');
  154.     InsertInTable (proctypsy,'PROC');
  155.     InsertInTable (procsy,'PROCEDURE');
  156.     InsertInTable (qualifiedsy,'QUALIFIED');
  157.     InsertInTable (realsy,'REAL');
  158.     InsertInTable (recordsy,'RECORD');
  159.     InsertInTable (repeatsy,'REPEAT');
  160.     InsertInTable (returnsy,'RETURN');
  161.     InsertInTable (setsy,'SET');
  162.     InsertInTable (shortaddrsy,'SHORTADR');
  163.     InsertInTable (shortcardsy,'SHORTCARD');
  164.     InsertInTable (shortintsy,'SHORTINT');
  165.     InsertInTable (thensy,'THEN');
  166.     InsertInTable (tosy,'TO');
  167.     InsertInTable (truesy,'TRUE');
  168.     InsertInTable (truncsy,'TRUNC');
  169.     InsertInTable (typesy,'TYPE');
  170.     InsertInTable (untilsy,'UNTIL');
  171.     InsertInTable (valsy,'VAL');
  172.     InsertInTable (varsy,'VAR');
  173.     InsertInTable (virtualsy,'VIRTUAL');
  174.     InsertInTable (vsizesy,'VSIZE');
  175.     InsertInTable (whilesy,'WHILE');
  176.     InsertInTable (withsy,'WITH');
  177.   END InitKeyWordTable;
  178.  
  179. BEGIN
  180.   InitKeyWordTable
  181. END PRHASH.