SimpleQ.mod 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. MODULE SimpleQ;
  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 SimpleQS IMPORT lst, src, errors, Error, CharAt;
  8. FROM SimpleQP 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. | 230: Msg("qbe backend: construct not supported in scalar subset")
  184. ELSE Msg("Error: "); WriteInt(lst, nr, 0);
  185. END;
  186. WriteLn(lst)
  187. END PrintErr;
  188. PROCEDURE PrintListing;
  189. (* Print a source listing with error messages *)
  190. VAR
  191. nextErr: Err;
  192. eof: BOOLEAN;
  193. lnr, errC: INTEGER;
  194. srcPos: INT32;
  195. line: ARRAY [0 .. 255] OF CHAR;
  196. BEGIN
  197. WriteString(lst, "Listing:");
  198. WriteLn(lst); WriteLn(lst);
  199. srcPos := 0; nextErr := firstErr;
  200. GetLine(srcPos, line, eof); lnr := 1; errC := 0;
  201. WHILE ~ eof DO
  202. WriteInt(lst, lnr, 5); WriteString(lst, " ");
  203. WriteString(lst, line); WriteLn(lst);
  204. WHILE (nextErr # NIL) & (nextErr^.line = lnr) DO
  205. PrintErr(line, nextErr^.nr, nextErr^.col); INC(errC);
  206. nextErr := nextErr^.next
  207. END;
  208. GetLine(srcPos, line, eof); INC(lnr);
  209. END;
  210. IF nextErr # NIL THEN
  211. WriteInt(lst, lnr, 5); WriteLn(lst);
  212. WHILE nextErr # NIL DO
  213. PrintErr(line, nextErr^.nr, nextErr^.col); INC(errC);
  214. nextErr := nextErr^.next
  215. END
  216. END;
  217. WriteLn(lst);
  218. WriteInt(lst, errC, 5); WriteString(lst, " error");
  219. IF errC # 1 THEN Write(lst, 's') END;
  220. WriteLn(lst); WriteLn(lst); WriteLn(lst);
  221. END PrintListing;
  222. BEGIN
  223. firstErr := NIL; Extra := 1;
  224. END ListHandler;
  225. (* --------------------------- main module ------------------------------- *)
  226. PROCEDURE ChangeExtension (oldName, Ext: ARRAY OF CHAR;
  227. VAR newName: ARRAY OF CHAR);
  228. (* Constructs newName as complete file name by appending ext to oldName
  229. Examples: (assume ext = "EXT")
  230. old.any ==> old.EXT
  231. old ==> old.EXT
  232. This is not a file renaming facility, merely a string manipulation
  233. routine. *)
  234. VAR
  235. i, l: CARDINAL;
  236. BEGIN
  237. Strings.Assign(oldName, newName);
  238. i := LENGTH(oldName); l := i;
  239. WHILE (i > 0) & (oldName[i -1] # '.')
  240. & (oldName[i -1] # '\') & (oldName[i -1] # '/') DO
  241. DEC(i)
  242. END;
  243. IF (i > 0) & (oldName[i-1] = '.') THEN
  244. Strings.Delete(newName, i - 1, l + 1 - i)
  245. END;
  246. IF Ext[0] = '.' THEN Strings.Delete(Ext, 0, 1) END;
  247. Strings.Append(".", newName);
  248. Strings.Append(Ext, newName)
  249. END ChangeExtension;
  250. VAR
  251. sourceName, listName: ARRAY [0 .. 255] OF CHAR;
  252. BEGIN
  253. (* check on correct parameter usage *)
  254. FileIO.NextParameter(sourceName);
  255. IF sourceName[0] = 0C THEN
  256. FileIO.WriteString(FileIO.StdOut, "No input file specified");
  257. HALT
  258. END;
  259. (* open the source file - Scanner.src *)
  260. FileIO.Open(src, sourceName, FALSE);
  261. IF ~ FileIO.Okay THEN
  262. FileIO.WriteString(FileIO.StdOut, "Could not open input file");
  263. FileIO.WriteLn(FileIO.StdOut);
  264. HALT
  265. END;
  266. (* open the output file for the source listing - Scanner.lst *)
  267. ChangeExtension(sourceName, ".LST", listName);
  268. FileIO.Open(lst, listName, TRUE);
  269. IF ~ FileIO.Okay THEN
  270. FileIO.WriteString(FileIO.StdOut, "Could not open listing file");
  271. FileIO.WriteLn(FileIO.StdOut);
  272. (* default Scanner.lst to screen *) lst := FileIO.StdOut;
  273. END;
  274. (* install error reporting procedure - Scanner.Error *)
  275. Error := StoreError;
  276. (* instigate the compilation - Parser.Parse *)
  277. FileIO.WriteString(FileIO.StdOut, "Parsing"); FileIO.WriteLn(FileIO.StdOut);
  278. Parse;
  279. (* generate the source listing on lst file *)
  280. PrintListing;
  281. IF lst # FileIO.StdOut THEN FileIO.Close(lst) END;
  282. (* examine the outcome *)
  283. IF NOT Successful()
  284. THEN
  285. FileIO.WriteString(FileIO.StdOut, "Incorrect source");
  286. ELSE
  287. FileIO.WriteString(FileIO.StdOut, "Parsed correctly");
  288. (* ++++++++ Add further activities if required ++++++++++ *)
  289. END;
  290. END SimpleQ.