MODULE Pascal; (* 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 PascalS IMPORT lst, src, errors, Error, CharAt; FROM PascalP 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("identifier expected") | 2: Msg("integer expected") | 3: Msg("real expected") | 4: Msg("string expected") | 5: Msg("'PROGRAM' expected") | 6: Msg("';' expected") | 7: Msg("'.' expected") | 8: Msg("'(' expected") | 9: Msg("')' expected") | 10: Msg("'LABEL' expected") | 11: Msg("',' expected") | 12: Msg("'CONST' expected") | 13: Msg("'=' expected") | 14: Msg("'+' expected") | 15: Msg("'-' expected") | 16: Msg("'TYPE' expected") | 17: Msg("'PACKED' expected") | 18: Msg("'^' expected") | 19: Msg("'..' expected") | 20: Msg("'ARRAY' expected") | 21: Msg("'[' expected") | 22: Msg("']' expected") | 23: Msg("'OF' expected") | 24: Msg("'RECORD' expected") | 25: Msg("'END' expected") | 26: Msg("'SET' expected") | 27: Msg("'FILE' expected") | 28: Msg("':' expected") | 29: Msg("'CASE' expected") | 30: Msg("'VAR' expected") | 31: Msg("'PROCEDURE' expected") | 32: Msg("'FUNCTION' expected") | 33: Msg("'FORWARD' expected") | 34: Msg("'BEGIN' expected") | 35: Msg("':=' expected") | 36: Msg("'GOTO' expected") | 37: Msg("'WHILE' expected") | 38: Msg("'DO' expected") | 39: Msg("'REPEAT' expected") | 40: Msg("'UNTIL' expected") | 41: Msg("'IF' expected") | 42: Msg("'THEN' expected") | 43: Msg("'ELSE' expected") | 44: Msg("'FOR' expected") | 45: Msg("'TO' expected") | 46: Msg("'DOWNTO' expected") | 47: Msg("'WITH' expected") | 48: Msg("'<' expected") | 49: Msg("'>' expected") | 50: Msg("'<=' expected") | 51: Msg("'>=' expected") | 52: Msg("'<>' expected") | 53: Msg("'IN' expected") | 54: Msg("'OR' expected") | 55: Msg("'*' expected") | 56: Msg("'/' expected") | 57: Msg("'DIV' expected") | 58: Msg("'MOD' expected") | 59: Msg("'AND' expected") | 60: Msg("'NOT' expected") | 61: Msg("'NIL' expected") | 62: Msg("not expected") | 63: Msg("invalid UnsignedLiteral") | 64: Msg("invalid MulOp") | 65: Msg("invalid Factor") | 66: Msg("invalid AddOp") | 67: Msg("invalid RelOp") | 68: Msg("invalid SimpleExpression") | 69: Msg("invalid ForStatement") | 70: Msg("invalid AssignmentOrCall") | 71: Msg("invalid ParamType") | 72: Msg("invalid FormalSection") | 73: Msg("invalid Body") | 74: Msg("invalid StructType") | 75: Msg("invalid SimpleType") | 76: Msg("invalid Type") | 77: Msg("invalid UnsignedNumber") | 78: Msg("invalid Constant") | 79: Msg("invalid Constant") | 80: Msg("invalid ProcDeclarations") (* add customized cases here *) 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 Pascal.