CR.LST 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. Listing:
  2. 1 (* =========================================== *)
  3. 2 (* (c) 1990-1992 Clarion Software Corporation *)
  4. 3 (* =========================================== *)
  5. 4
  6. 5 MODULE CR;
  7. 6
  8. 7 (* Prints a cross-reference table of Modula-2 program. All user-defined
  9. 8 words are listed in alphabetical order together with the count of
  10. 9 the number of references and a list of all reference line numbers.
  11. 10
  12. 11 This program will cross reference the words in comments as well
  13. 12 as those in the source code.
  14. 13 *)
  15. 14
  16. 15 IMPORT Table, IO, Str, Lib, FIO;
  17. 16
  18. 17 (* Modula-2 Keywords and Predefined Identifiers *)
  19. 18
  20. 19 CONST
  21. 20
  22. 21 KeyWords =
  23. 22 'BY DO IF IN OF OR TO AND DIV END FOR MOD NOT SET VAR CASE ELSE '+
  24. ***** ^ not supported yet
  25. 23 'EXIT FROM GOTO LOOP THEN TYPE WITH ARRAY BEGIN CONST ELSIF LABEL '+
  26. ***** ^ not supported yet
  27. 24 'UNTIL WHILE EXPORT IMPORT MODULE RECORD REPEAT RETURN FORWARD '+
  28. ***** ^ not supported yet
  29. 25 'POINTER PROCEDURE DEFINITION IMPLEMENTATION QUALIFIED CLASS'+
  30. ***** ^ not supported yet
  31. 26 'SHORTINT INTEGER LONGINT BOOLEAN CHAR BYTE SHORTCARD CARDINAL '+
  32. ***** ^ not supported yet
  33. 27 'WORD LONGCARD LONGWORD REAL LONGREAL ADDRESS SHORTADDR BITSET '+
  34. ***** ^ not supported yet
  35. 28 'TRUE FALSE NIL PROC HALT NULLPROC CAP ABS CHR FLOAT HIGH ODD '+
  36. ***** ^ not supported yet
  37. 29 'TRUNC VAL DEC EXCL INC INCL SIZE VSIZE ADR ORD NEW DISPOSE MAX MIN ';
  38. ***** ^ not supported yet
  39. 30
  40. 31
  41. 32 TYPE
  42. 33 SymTabPtr = POINTER TO SymTab;
  43. ***** ^ undeclared identifier
  44. 34 CLASS SymTab(Table.Element); (* Symbol table entry *)
  45. ***** ^ not supported yet
  46. 35
  47. 36 Count : INTEGER; (* Reference count *)
  48. 37 LRef : Table.TABLE; (* Cross-Ref List *)
  49. 38 Name : ARRAY [0..30] OF CHAR; (* This word *)
  50. 39
  51. 40 VIRTUAL PROCEDURE Compare( p : Table.ElementPtr ) : INTEGER;
  52. 41
  53. 42 END SymTab ;
  54. 43
  55. 44 CLASS IMPLEMENTATION SymTab;
  56. 45
  57. 46 VIRTUAL PROCEDURE Compare( p : Table.ElementPtr ) : INTEGER;
  58. 47 BEGIN
  59. 48 RETURN Str.Compare(Name,SymTabPtr(p)^.Name);
  60. 49 END Compare;
  61. 50
  62. 51 BEGIN
  63. 52 END SymTab ;
  64. 53
  65. 54 TYPE
  66. 55
  67. 56 LinTabPtr = POINTER TO LinTab;
  68. 57
  69. 58 CLASS LinTab(Table.Element); (* Line number table entry *)
  70. 59
  71. 60 RefLn : INTEGER; (* Line number *)
  72. 61
  73. 62 VIRTUAL PROCEDURE Compare( p : Table.ElementPtr ) : INTEGER;
  74. 63
  75. 64 END LinTab ;
  76. 65
  77. 66 CLASS IMPLEMENTATION LinTab; (* Line number table entry *)
  78. 67
  79. 68 VIRTUAL PROCEDURE Compare( p : Table.ElementPtr ) : INTEGER;
  80. 69 BEGIN
  81. 70 IF (RefLn < LinTabPtr(p)^.RefLn) THEN
  82. 71 RETURN -1;
  83. 72 ELSIF (RefLn > LinTabPtr(p)^.RefLn) THEN
  84. 73 RETURN 1;
  85. 74 ELSE
  86. 75 RETURN 0;
  87. 76 END;
  88. 77 END Compare;
  89. 78
  90. 79 BEGIN
  91. 80 END LinTab ;
  92. 81
  93. 82
  94. 83 VAR
  95. 84 ni : CARDINAL; (* Column count for printing routines *)
  96. 85
  97. 86 PROCEDURE PrintLineNo( p : Table.ElementPtr );
  98. 87 (* Prints the line-number information for 'p', which is of
  99. 88 CLASS 'LinTab'.
  100. 89 *)
  101. 90
  102. 91 VAR
  103. 92 t, l : CARDINAL;
  104. 93 BEGIN
  105. 94 (* These first few lines count the number of digits in 'p.val' and
  106. 95 then check if there's enough room on the line for it.
  107. 96 *)
  108. 97 t := LinTabPtr(p)^.RefLn;
  109. 98 l := 1;
  110. 99 REPEAT
  111. 100 INC(l);
  112. 101 t := t DIV 10;
  113. 102 UNTIL t=0; (* l := number of digits in line number *)
  114. 103 IF (ni < l) THEN (* Not enough room *)
  115. 104 ni := 40;
  116. 105 IO.WrLn;
  117. 106 IO.WrCharRep(' ',37);
  118. 107 END;
  119. 108 IO.WrInt(LinTabPtr(p)^.RefLn,1);
  120. 109 IO.WrChar(' ');
  121. 110 DEC(ni,l);
  122. 111 END PrintLineNo;
  123. 112
  124. 113 PROCEDURE PrintSymbol( r : Table.ElementPtr );
  125. 114 (* Prints the symbol, definition line and cross-reference lines
  126. 115 of the symbol 'r' (of type 'SymtabPtr') *)
  127. 116 BEGIN
  128. 117 IO.WrStrAdj(SymTabPtr(r)^.Name,-30);
  129. 118 IO.WrInt(SymTabPtr(r)^.Count,-4);
  130. 119 IO.WrStr(' : ');
  131. 120 ni := 40;
  132. 121 SymTabPtr(r)^.LRef.Apply(PrintLineNo);
  133. 122 (* List line numbers for symbol *)
  134. 123 IO.WrLn;
  135. 124 END PrintSymbol;
  136. 125
  137. 126 VAR
  138. 127
  139. 128 SymbolTable, (* Actual table *)
  140. 129 Predef : Table.TABLE; (* Key word table *)
  141. 130 LineNr : LinTab; (* Work Area *)
  142. 131 i,Item : CARDINAL;
  143. 132 Symbol : SymTab; (* Word Area *)
  144. 133 FileName : ARRAY [0..100] OF CHAR;
  145. 134 File : FIO.File;
  146. 135 Buffer : ARRAY [1..(1024 + FIO.BufferOverhead)] OF CHAR;
  147. 136 Line : ARRAY [0..1023] OF CHAR;
  148. 137
  149. 138
  150. 139 PROCEDURE FillPre;
  151. 140 (* Fill 'PreDef' with keyword from 'KeyWords' *)
  152. 141 VAR
  153. 142 i, j : CARDINAL;
  154. 143 x : SymTab;
  155. 144 BEGIN
  156. 145 Predef.Init; (* Initialize TABLE *)
  157. 146 i := 0;
  158. 147 WHILE (KeyWords[i] # 0C) DO
  159. 148 j := 0;
  160. 149 WHILE (KeyWords[i] # ' ') DO
  161. 150 x.Name[j] := KeyWords[i];
  162. 151 INC(j);
  163. 152 INC(i);
  164. 153 END;
  165. 154 x.Name[j] := 0C;
  166. 155 IF (j # 0) THEN
  167. 156 Predef.Insert(x);
  168. 157 END;
  169. 158 INC(i);
  170. 159 END (* WHILE *);
  171. 160 END FillPre;
  172. 161
  173. 162 BEGIN
  174. 163 (* check parameters *)
  175. 164 IF Lib.ParamCount() <> 1 THEN
  176. 165 IO.WrStr('Try again : cr input-file');
  177. 166 IO.WrLn;
  178. 167 HALT;
  179. 168 END;
  180. 169
  181. 170 SymbolTable.Init; (* Initialize symbol table *)
  182. 171 Lib.ParamStr(FileName,1); (* Get filename from command line *)
  183. 172 File := FIO.OpenRead(FileName); (* Open - only need to read it *)
  184. 173 FIO.AssignBuffer(File,Buffer); (* Associate a buffer with file
  185. 174 to speed up access *)
  186. 175 LineNr.RefLn := 1; (* Initialise line counter *)
  187. 176 LOOP
  188. 177 FIO.RdStr(File,Line); (* Read line from file *)
  189. 178 IF FIO.EOF THEN (* Check for end-of-file *)
  190. 179 EXIT;
  191. 180 END;
  192. 181 Item := 0;
  193. 182 LOOP
  194. 183 Str.Item(Symbol.Name,Line,
  195. 184 Str.CHARSET{ 0C..CHAR(255) } -
  196. 185 Str.CHARSET{ 'A'..'Z','a'..'z','_','0'..'9','.' },Item);
  197. 186 (* Extracts a single word from 'Line'. Each word
  198. 187 is delimited by any characters except a digit, a letter,
  199. 188 an underbar or a '.'.
  200. 189 *)
  201. 190 IF (Symbol.Name[0] = 0C) THEN (* Check that there was a word *)
  202. 191 EXIT;
  203. 192 END;
  204. 193 INC(Item);
  205. 194 IF (Symbol.Name[0] > '9') THEN (* Don't enter numbers in table *)
  206. 195 IF (SymbolTable.Find(Symbol)) THEN
  207. 196 (* Is word already in tabel? *)
  208. 197 INC(Symbol.Count); (* If, yes, increment count *)
  209. 198 ELSE
  210. 199 Symbol.Count := 1;
  211. 200 Symbol.LRef.Init; (* Initailize line number table *)
  212. 201 END;
  213. 202 Symbol.LRef.Insert(LineNr); (* Insert cross-reference *)
  214. 203 SymbolTable.Insert(Symbol); (* Insert/Update symbol entry *)
  215. 204 END;
  216. 205 END (* LOOP through line *);
  217. 206 INC(LineNr.RefLn); (* Next line *)
  218. 207 END (* LOOP - File *);
  219. 208 FillPre; (* Build table of predefined words *)
  220. 209 SymbolTable.Excl(Predef); (* Exclude predefined words from table *)
  221. 210 SymbolTable.Apply(PrintSymbol); (* Display table *)
  222. 211 Predef.Dispose; (* Reclaim storage for predefined words *)
  223. 212 SymbolTable.Dispose; (* Reclaim storage other names *)
  224. 213 END CR.
  225. 10 errors