SimpleM.mod 10 KB

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