MODULE SimpleM; (* This is an example of a rudimentary main module for use with COCO/R. It assumes the FileIO/Storage I/O libraries (as supplied with this project) are available. The auxiliary modules S (scanner) and P (parser) are assumed to have been constructed with COCO/R compiler generator. *) FROM SimpleMS IMPORT lst, src, errors, Error, CharAt; FROM SimpleMP IMPORT Parse, Successful; IMPORT Strings, Storage, SYSTEM, FileIO; (* and any others needed *) TYPE INT32 = FileIO.INT32 (* 32 bit integers needed *); MODULE ListHandler; (* ------------------- Source Listing and Error handler -------------- *) FROM FileIO IMPORT CR, LF, EOF, WriteString, Write, WriteLn, WriteInt, Long0; FROM Storage IMPORT ALLOCATE; FROM SYSTEM IMPORT TSIZE; IMPORT lst, CharAt, errors, INT32; EXPORT StoreError, PrintListing; TYPE Err = POINTER TO ErrDesc; ErrDesc = RECORD nr, line, col: INTEGER; next: Err END; CONST tab = 11C; VAR firstErr, lastErr: Err; Extra: INTEGER; PROCEDURE StoreError (nr, line, col: INTEGER; pos: INT32); (* Store an error message for later printing *) VAR nextErr: Err; BEGIN ALLOCATE(nextErr, TSIZE(ErrDesc)); nextErr^.nr := nr; nextErr^.line := line; nextErr^.col := col; nextErr^.next := NIL; IF firstErr = NIL THEN firstErr := nextErr ELSE lastErr^.next := nextErr END; lastErr := nextErr; INC(errors) END StoreError; PROCEDURE GetLine (VAR pos: INT32; VAR line: ARRAY OF CHAR; VAR eof: BOOLEAN); (* Read a source line. Return empty line if eof *) VAR ch: CHAR; i: CARDINAL; BEGIN i := 0; eof := FALSE; ch := CharAt(pos); INC(pos); WHILE (ch # CR) & (ch # LF) & (ch # EOF) DO line[i] := ch; INC(i); ch := CharAt(pos); INC(pos); END; eof := (i = 0) & (ch = EOF); line[i] := 0C; IF ch = CR THEN (* check for MsDos *) ch := CharAt(pos); IF ch = LF THEN INC(pos); Extra := 0 END END END GetLine; PROCEDURE PrintErr (line: ARRAY OF CHAR; nr, col: INTEGER); (* Print an error message *) PROCEDURE Msg (s: ARRAY OF CHAR); BEGIN WriteString(lst, s) END Msg; PROCEDURE Pointer; VAR i: INTEGER; BEGIN WriteString(lst, "***** "); i := 0; WHILE i < col + Extra - 2 DO IF line[i] = tab THEN Write(lst, tab) ELSE Write(lst, ' ') END; INC(i) END; WriteString(lst, "^ ") END Pointer; BEGIN Pointer; CASE nr OF 0: Msg("EOF expected") | 1: Msg("ident expected") | 2: Msg("integer expected") | 3: Msg("real expected") | 4: Msg("string expected") | 5: Msg("'MODULE' expected") | 6: Msg("';' expected") | 7: Msg("'.' expected") | 8: Msg("'FROM' expected") | 9: Msg("'IMPORT' expected") | 10: Msg("',' expected") | 11: Msg("'BEGIN' expected") | 12: Msg("'END' expected") | 13: Msg("'CONST' expected") | 14: Msg("'TYPE' expected") | 15: Msg("'VAR' expected") | 16: Msg("'=' expected") | 17: Msg("':' expected") | 18: Msg("'[' expected") | 19: Msg("'..' expected") | 20: Msg("']' expected") | 21: Msg("'(' expected") | 22: Msg("')' expected") | 23: Msg("'ARRAY' expected") | 24: Msg("'OF' expected") | 25: Msg("'RECORD' expected") | 26: Msg("'SET' expected") | 27: Msg("'POINTER' expected") | 28: Msg("'TO' expected") | 29: Msg("'EXIT' expected") | 30: Msg("':=' expected") | 31: Msg("'IF' expected") | 32: Msg("'THEN' expected") | 33: Msg("'ELSIF' expected") | 34: Msg("'ELSE' expected") | 35: Msg("'CASE' expected") | 36: Msg("'|' expected") | 37: Msg("'WHILE' expected") | 38: Msg("'DO' expected") | 39: Msg("'REPEAT' expected") | 40: Msg("'UNTIL' expected") | 41: Msg("'LOOP' expected") | 42: Msg("'FOR' expected") | 43: Msg("'BY' expected") | 44: Msg("'WITH' expected") | 45: Msg("'^' expected") | 46: Msg("'#' expected") | 47: Msg("'<>' expected") | 48: Msg("'<' expected") | 49: Msg("'<=' expected") | 50: Msg("'>' expected") | 51: Msg("'>=' expected") | 52: Msg("'IN' expected") | 53: Msg("'+' expected") | 54: Msg("'-' expected") | 55: Msg("'OR' expected") | 56: Msg("'*' expected") | 57: Msg("'/' expected") | 58: Msg("'DIV' expected") | 59: Msg("'MOD' expected") | 60: Msg("'AND' expected") | 61: Msg("'&' expected") | 62: Msg("'NOT' expected") | 63: Msg("'~' expected") | 64: Msg("'{' expected") | 65: Msg("'}' expected") | 66: Msg("not expected") | 67: Msg("invalid MulOp") | 68: Msg("invalid Fact") | 69: Msg("invalid AddOp") | 70: Msg("invalid Rel") | 71: Msg("invalid SimpleType") | 72: Msg("invalid Type") | 73: Msg("invalid Declaration") | 74: Msg("invalid Import") (* add customized cases here *) | 200: Msg("duplicate identifier") | 201: Msg("undeclared identifier") | 202: Msg("module name mismatch") | 210: Msg("incompatible assignment") | 211: Msg("arithmetic operand must be numeric") | 212: Msg("boolean operand required") | 213: Msg("incompatible comparison") | 214: Msg("BOOLEAN condition required") | 215: Msg("not a RECORD type") | 216: Msg("unknown field") | 217: Msg("not an ARRAY type") | 218: Msg("array index must be integer") | 219: Msg("not a POINTER type") | 220: Msg("FOR needs integer variable and bounds") | 221: Msg("not a type name") | 222: Msg("set operand mismatch") | 223: Msg("cyclical type definition") | 224: Msg("ordinal type required") ELSE Msg("Error: "); WriteInt(lst, nr, 0); END; WriteLn(lst) END PrintErr; PROCEDURE PrintListing; (* Print a source listing with error messages *) VAR nextErr: Err; eof: BOOLEAN; lnr, errC: INTEGER; srcPos: INT32; line: ARRAY [0 .. 255] OF CHAR; BEGIN WriteString(lst, "Listing:"); WriteLn(lst); WriteLn(lst); srcPos := 0; nextErr := firstErr; GetLine(srcPos, line, eof); lnr := 1; errC := 0; WHILE ~ eof DO WriteInt(lst, lnr, 5); WriteString(lst, " "); WriteString(lst, line); WriteLn(lst); WHILE (nextErr # NIL) & (nextErr^.line = lnr) DO PrintErr(line, nextErr^.nr, nextErr^.col); INC(errC); nextErr := nextErr^.next END; GetLine(srcPos, line, eof); INC(lnr); END; IF nextErr # NIL THEN WriteInt(lst, lnr, 5); WriteLn(lst); WHILE nextErr # NIL DO PrintErr(line, nextErr^.nr, nextErr^.col); INC(errC); nextErr := nextErr^.next END END; WriteLn(lst); WriteInt(lst, errC, 5); WriteString(lst, " error"); IF errC # 1 THEN Write(lst, 's') END; WriteLn(lst); WriteLn(lst); WriteLn(lst); END PrintListing; BEGIN firstErr := NIL; Extra := 1; END ListHandler; (* --------------------------- main module ------------------------------- *) PROCEDURE ChangeExtension (oldName, Ext: ARRAY OF CHAR; VAR newName: ARRAY OF CHAR); (* Constructs newName as complete file name by appending ext to oldName Examples: (assume ext = "EXT") old.any ==> old.EXT old ==> old.EXT This is not a file renaming facility, merely a string manipulation routine. *) VAR i, l: CARDINAL; BEGIN Strings.Assign(oldName, newName); i := LENGTH(oldName); l := i; WHILE (i > 0) & (oldName[i -1] # '.') & (oldName[i -1] # '\') & (oldName[i -1] # '/') DO DEC(i) END; IF (i > 0) & (oldName[i-1] = '.') THEN Strings.Delete(newName, i - 1, l + 1 - i) END; IF Ext[0] = '.' THEN Strings.Delete(Ext, 0, 1) END; Strings.Append(".", newName); Strings.Append(Ext, newName) END ChangeExtension; VAR sourceName, listName: ARRAY [0 .. 255] OF CHAR; BEGIN (* check on correct parameter usage *) FileIO.NextParameter(sourceName); IF sourceName[0] = 0C THEN FileIO.WriteString(FileIO.StdOut, "No input file specified"); HALT END; (* open the source file - Scanner.src *) FileIO.Open(src, sourceName, FALSE); IF ~ FileIO.Okay THEN FileIO.WriteString(FileIO.StdOut, "Could not open input file"); FileIO.WriteLn(FileIO.StdOut); HALT END; (* open the output file for the source listing - Scanner.lst *) ChangeExtension(sourceName, ".LST", listName); FileIO.Open(lst, listName, TRUE); IF ~ FileIO.Okay THEN FileIO.WriteString(FileIO.StdOut, "Could not open listing file"); FileIO.WriteLn(FileIO.StdOut); (* default Scanner.lst to screen *) lst := FileIO.StdOut; END; (* install error reporting procedure - Scanner.Error *) Error := StoreError; (* instigate the compilation - Parser.Parse *) FileIO.WriteString(FileIO.StdOut, "Parsing"); FileIO.WriteLn(FileIO.StdOut); Parse; (* generate the source listing on lst file *) PrintListing; IF lst # FileIO.StdOut THEN FileIO.Close(lst) END; (* examine the outcome *) IF NOT Successful() THEN FileIO.WriteString(FileIO.StdOut, "Incorrect source"); ELSE FileIO.WriteString(FileIO.StdOut, "Parsed correctly"); (* ++++++++ Add further activities if required ++++++++++ *) END; END SimpleM.