M2c.mod 11 KB

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