M2c.mod 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. MODULE M2c;
  2. (* Minimal main module for the M2c Modula-2 compiler (Coco/R).
  3. Assumes the FileIO/Storage I/O libraries and the generated
  4. <Grammar>S (scanner) and <Grammar>P (parser) modules. *)
  5. FROM M2cS IMPORT lst, src, errors, Error, CharAt;
  6. FROM M2cP IMPORT Parse, Successful;
  7. IMPORT
  8. Strings, Storage, SYSTEM, FileIO;
  9. TYPE
  10. INT32 = FileIO.INT32 (* 32 bit integers needed *);
  11. MODULE ListHandler;
  12. (* ------------------- Source Listing and Error handler -------------- *)
  13. FROM FileIO IMPORT CR, LF, EOF, WriteString, Write, WriteLn, WriteInt, Long0;
  14. FROM Storage IMPORT ALLOCATE;
  15. FROM SYSTEM IMPORT TSIZE;
  16. IMPORT lst, CharAt, errors, INT32;
  17. EXPORT StoreError, PrintListing;
  18. TYPE
  19. Err = POINTER TO ErrDesc;
  20. ErrDesc = RECORD
  21. nr, line, col: INTEGER;
  22. next: Err
  23. END;
  24. CONST
  25. tab = 11C;
  26. VAR
  27. firstErr, lastErr: Err;
  28. Extra: INTEGER;
  29. PROCEDURE StoreError (nr, line, col: INTEGER; pos: INT32);
  30. (* Store an error message for later printing *)
  31. VAR
  32. nextErr: Err;
  33. BEGIN
  34. ALLOCATE(nextErr, TSIZE(ErrDesc));
  35. nextErr^.nr := nr; nextErr^.line := line; nextErr^.col := col;
  36. nextErr^.next := NIL;
  37. IF firstErr = NIL
  38. THEN firstErr := nextErr
  39. ELSE lastErr^.next := nextErr
  40. END;
  41. lastErr := nextErr;
  42. INC(errors)
  43. END StoreError;
  44. PROCEDURE GetLine (VAR pos: INT32;
  45. VAR line: ARRAY OF CHAR;
  46. VAR eof: BOOLEAN);
  47. (* Read a source line. Return empty line if eof *)
  48. VAR
  49. ch: CHAR;
  50. i: CARDINAL;
  51. BEGIN
  52. i := 0; eof := FALSE; ch := CharAt(pos); INC(pos);
  53. WHILE (ch # CR) & (ch # LF) & (ch # EOF) DO
  54. line[i] := ch; INC(i); ch := CharAt(pos); INC(pos);
  55. END;
  56. eof := (i = 0) & (ch = EOF); line[i] := 0C;
  57. IF ch = CR THEN (* check for MsDos *)
  58. ch := CharAt(pos);
  59. IF ch = LF THEN INC(pos); Extra := 0 END
  60. END
  61. END GetLine;
  62. PROCEDURE PrintErr (line: ARRAY OF CHAR; nr, col: INTEGER);
  63. (* Print an error message *)
  64. PROCEDURE Msg (s: ARRAY OF CHAR);
  65. BEGIN
  66. WriteString(lst, s)
  67. END Msg;
  68. PROCEDURE Pointer;
  69. VAR
  70. i: INTEGER;
  71. BEGIN
  72. WriteString(lst, "***** ");
  73. i := 0;
  74. WHILE i < col + Extra - 2 DO
  75. IF line[i] = tab
  76. THEN Write(lst, tab)
  77. ELSE Write(lst, ' ')
  78. END;
  79. INC(i)
  80. END;
  81. WriteString(lst, "^ ")
  82. END Pointer;
  83. BEGIN
  84. Pointer;
  85. CASE nr OF
  86. 0: Msg("EOF expected")
  87. | 1: Msg("ident expected")
  88. | 2: Msg("integer expected")
  89. | 3: Msg("real expected")
  90. | 4: Msg("string expected")
  91. | 5: Msg("'MODULE' expected")
  92. | 6: Msg("';' expected")
  93. | 7: Msg("'.' expected")
  94. | 8: Msg("'FROM' expected")
  95. | 9: Msg("'IMPORT' expected")
  96. | 10: Msg("',' expected")
  97. | 11: Msg("'BEGIN' expected")
  98. | 12: Msg("'END' expected")
  99. | 13: Msg("'CONST' expected")
  100. | 14: Msg("'TYPE' expected")
  101. | 15: Msg("'VAR' expected")
  102. | 16: Msg("'=' expected")
  103. | 17: Msg("':' expected")
  104. | 18: Msg("'PROCEDURE' expected")
  105. | 19: Msg("'(' expected")
  106. | 20: Msg("')' expected")
  107. | 21: Msg("'FORWARD' expected")
  108. | 22: Msg("'[' expected")
  109. | 23: Msg("'..' expected")
  110. | 24: Msg("']' expected")
  111. | 25: Msg("'ARRAY' expected")
  112. | 26: Msg("'OF' expected")
  113. | 27: Msg("'RECORD' expected")
  114. | 28: Msg("'SET' expected")
  115. | 29: Msg("'POINTER' expected")
  116. | 30: Msg("'TO' expected")
  117. | 31: Msg("'EXIT' expected")
  118. | 32: Msg("':=' expected")
  119. | 33: Msg("'IF' expected")
  120. | 34: Msg("'THEN' expected")
  121. | 35: Msg("'ELSIF' expected")
  122. | 36: Msg("'ELSE' expected")
  123. | 37: Msg("'CASE' expected")
  124. | 38: Msg("'|' expected")
  125. | 39: Msg("'WHILE' expected")
  126. | 40: Msg("'DO' expected")
  127. | 41: Msg("'REPEAT' expected")
  128. | 42: Msg("'UNTIL' expected")
  129. | 43: Msg("'LOOP' expected")
  130. | 44: Msg("'FOR' expected")
  131. | 45: Msg("'BY' expected")
  132. | 46: Msg("'-' expected")
  133. | 47: Msg("'WITH' expected")
  134. | 48: Msg("'RETURN' expected")
  135. | 49: Msg("'NEW' expected")
  136. | 50: Msg("'DISPOSE' expected")
  137. | 51: Msg("'WriteInt' expected")
  138. | 52: Msg("'WriteString' expected")
  139. | 53: Msg("'^' expected")
  140. | 54: Msg("'#' expected")
  141. | 55: Msg("'<>' expected")
  142. | 56: Msg("'<' expected")
  143. | 57: Msg("'<=' expected")
  144. | 58: Msg("'>' expected")
  145. | 59: Msg("'>=' expected")
  146. | 60: Msg("'IN' expected")
  147. | 61: Msg("'+' expected")
  148. | 62: Msg("'OR' expected")
  149. | 63: Msg("'*' expected")
  150. | 64: Msg("'/' expected")
  151. | 65: Msg("'DIV' expected")
  152. | 66: Msg("'MOD' expected")
  153. | 67: Msg("'AND' expected")
  154. | 68: Msg("'&' expected")
  155. | 69: Msg("'HIGH' expected")
  156. | 70: Msg("'NOT' expected")
  157. | 71: Msg("'~' expected")
  158. | 72: Msg("'{' expected")
  159. | 73: Msg("'}' expected")
  160. | 74: Msg("not expected")
  161. | 75: Msg("invalid MulOp")
  162. | 76: Msg("invalid Fact")
  163. | 77: Msg("invalid AddOp")
  164. | 78: Msg("invalid Rel")
  165. | 79: Msg("invalid ByLit")
  166. | 80: Msg("invalid AssignOrCall")
  167. | 81: Msg("invalid SimpleType")
  168. | 82: Msg("invalid Type")
  169. | 83: Msg("invalid ProcedureDecl")
  170. | 84: Msg("invalid Declaration")
  171. | 85: Msg("invalid Import")
  172. (* add customized cases here *)
  173. | 200: Msg("duplicate identifier")
  174. | 201: Msg("undeclared identifier")
  175. | 202: Msg("module/procedure name mismatch")
  176. | 210: Msg("incompatible assignment")
  177. | 211: Msg("arithmetic operand must be numeric")
  178. | 212: Msg("boolean operand required")
  179. | 213: Msg("incompatible comparison")
  180. | 214: Msg("BOOLEAN condition required")
  181. | 215: Msg("not a RECORD type")
  182. | 216: Msg("unknown field")
  183. | 217: Msg("not an ARRAY type")
  184. | 218: Msg("array index must be integer")
  185. | 219: Msg("not a POINTER type")
  186. | 220: Msg("FOR needs integer variable and bounds")
  187. | 221: Msg("not a type name")
  188. | 222: Msg("set operand mismatch")
  189. | 223: Msg("cyclical type definition")
  190. | 224: Msg("ordinal type required")
  191. | 230: Msg("not supported in this phase")
  192. | 231: Msg("procedure forward mismatch or missing body")
  193. | 232: Msg("bad RETURN")
  194. | 233: Msg("invalid procedure call")
  195. ELSE Msg("Error: "); WriteInt(lst, nr, 0);
  196. END;
  197. WriteLn(lst)
  198. END PrintErr;
  199. PROCEDURE PrintListing;
  200. (* Print a source listing with error messages *)
  201. VAR
  202. nextErr: Err;
  203. eof: BOOLEAN;
  204. lnr, errC: INTEGER;
  205. srcPos: INT32;
  206. line: ARRAY [0 .. 255] OF CHAR;
  207. BEGIN
  208. WriteString(lst, "Listing:");
  209. WriteLn(lst); WriteLn(lst);
  210. srcPos := 0; nextErr := firstErr;
  211. GetLine(srcPos, line, eof); lnr := 1; errC := 0;
  212. WHILE ~ eof DO
  213. WriteInt(lst, lnr, 5); WriteString(lst, " ");
  214. WriteString(lst, line); WriteLn(lst);
  215. WHILE (nextErr # NIL) & (nextErr^.line = lnr) DO
  216. PrintErr(line, nextErr^.nr, nextErr^.col); INC(errC);
  217. nextErr := nextErr^.next
  218. END;
  219. GetLine(srcPos, line, eof); INC(lnr);
  220. END;
  221. IF nextErr # NIL THEN
  222. WriteInt(lst, lnr, 5); WriteLn(lst);
  223. WHILE nextErr # NIL DO
  224. PrintErr(line, nextErr^.nr, nextErr^.col); INC(errC);
  225. nextErr := nextErr^.next
  226. END
  227. END;
  228. WriteLn(lst);
  229. WriteInt(lst, errC, 5); WriteString(lst, " error");
  230. IF errC # 1 THEN Write(lst, 's') END;
  231. WriteLn(lst); WriteLn(lst); WriteLn(lst);
  232. END PrintListing;
  233. BEGIN
  234. firstErr := NIL; Extra := 1;
  235. END ListHandler;
  236. (* --------------------------- main module ------------------------------- *)
  237. PROCEDURE ChangeExtension (oldName, Ext: ARRAY OF CHAR;
  238. VAR newName: ARRAY OF CHAR);
  239. (* Constructs newName by replacing the extension of oldName with Ext. *)
  240. VAR
  241. i, l: CARDINAL;
  242. BEGIN
  243. Strings.Assign(oldName, newName);
  244. i := LENGTH(oldName); l := i;
  245. WHILE (i > 0) & (oldName[i -1] # '.')
  246. & (oldName[i -1] # '\') & (oldName[i -1] # '/') DO
  247. DEC(i)
  248. END;
  249. IF (i > 0) & (oldName[i-1] = '.') THEN
  250. Strings.Delete(newName, i - 1, l + 1 - i)
  251. END;
  252. IF Ext[0] = '.' THEN Strings.Delete(Ext, 0, 1) END;
  253. Strings.Append(".", newName);
  254. Strings.Append(Ext, newName)
  255. END ChangeExtension;
  256. VAR
  257. sourceName, listName: ARRAY [0 .. 255] OF CHAR;
  258. BEGIN
  259. (* check on correct parameter usage *)
  260. FileIO.NextParameter(sourceName);
  261. IF sourceName[0] = 0C THEN
  262. FileIO.WriteString(FileIO.StdOut, "No input file specified");
  263. HALT
  264. END;
  265. (* open the source file - Scanner.src *)
  266. FileIO.Open(src, sourceName, FALSE);
  267. IF ~ FileIO.Okay THEN
  268. FileIO.WriteString(FileIO.StdOut, "Could not open input file");
  269. FileIO.WriteLn(FileIO.StdOut);
  270. HALT
  271. END;
  272. (* open the output file for the source listing - Scanner.lst *)
  273. ChangeExtension(sourceName, ".LST", listName);
  274. FileIO.Open(lst, listName, TRUE);
  275. IF ~ FileIO.Okay THEN
  276. FileIO.WriteString(FileIO.StdOut, "Could not open listing file");
  277. FileIO.WriteLn(FileIO.StdOut);
  278. (* default Scanner.lst to screen *) lst := FileIO.StdOut;
  279. END;
  280. (* install error reporting procedure - Scanner.Error *)
  281. Error := StoreError;
  282. (* instigate the compilation - Parser.Parse *)
  283. FileIO.WriteString(FileIO.StdOut, "Parsing"); FileIO.WriteLn(FileIO.StdOut);
  284. Parse;
  285. (* generate the source listing on lst file *)
  286. PrintListing;
  287. IF lst # FileIO.StdOut THEN FileIO.Close(lst) END;
  288. (* examine the outcome *)
  289. IF NOT Successful()
  290. THEN
  291. FileIO.WriteString(FileIO.StdOut, "Incorrect source");
  292. ELSE
  293. FileIO.WriteString(FileIO.StdOut, "Parsed correctly");
  294. END;
  295. END M2c.