| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- MODULE PL0c4;
- (************************************************************)
- (* PL0 compiler in GNU Modula-2 *)
- (* using only ISO Libraries to void mixing *)
- (* generates an asm file to be interpreted by PLO executable*)
- (* Eric Streit <eric@yojik.eu> May 2026 *)
- (************************************************************)
- (* program structure *)
- (* read a file with the text program *)
- (* syntax analysis followed by semantic analysis *)
- (* and code generation *)
- FROM IOResult IMPORT ReadResults, ReadResult;
- FROM TextIO IMPORT ReadRestLine, SkipLine, ReadToken, WriteLn, WriteString, ReadChar;
- FROM SeqFile IMPORT OpenRead, OpenWrite, OpenResults, Close, ChanId, read, write;
- IMPORT STextIO, SWholeIO, Strings, ProgramArgs, TextIO;
- VAR
- theChanId : ChanId;
- theResult : OpenResults;
- theChar : CHAR;
- theLineNumber : CARDINAL;
- theFileName : ARRAY[0..128] OF CHAR;
- thelength : CARDINAL;
- theArgChanId : ChanId;
- theArgument : ARRAY [0..20] OF CHAR;
- BEGIN
- STextIO.WriteString("PL0 compiler from Wirth book 'Algorithms and Data Structures'");
- STextIO.WriteLn;
- STextIO.WriteString("Eric Streit <eric@yojik.eu>");
- STextIO.WriteLn;
- STextIO.WriteLn;
- (* testing command line arguments*)
- (* are there any arguments?*)
- theArgChanId := ProgramArgs.ArgChan ();
- WHILE ProgramArgs.IsArgPresent() DO
- TextIO.ReadToken(ProgramArgs.ArgChan(), theArgument);
- STextIO.WriteString(theArgument);
- STextIO.WriteLn;
- ProgramArgs.NextArg ();
- END;
- (* adding the file name input *)
- theFileName := "";
- STextIO.WriteString("Enter the file name (ENTER to leave)(with an ending '.' to add automatcally the extension) : ");
- STextIO.ReadString(theFileName);
- (* STextIO.WriteString(theFileName); *)
- thelength := Strings.Length(theFileName);
- IF thelength = 0 THEN
- STextIO.WriteString("Leaving .... bye ! ");
- STextIO.WriteLn;
- HALT
- ELSE
- (* adding extension if the name ends by . )*)
- IF theFileName[thelength-1] = "." THEN
- (* adding extension if enough space*)
- IF thelength < 125 THEN
- Strings.Concat(theFileName, "pl0", theFileName);
- ELSE
- STextIO.WriteString("File name too long");
- STextIO.WriteLn;
- STextIO.WriteString("Halting");
- HALT;
- END;
- END;
- END;
- STextIO.WriteString(theFileName);
- STextIO.WriteLn;
- theLineNumber := 1;
- OpenRead(theChanId, "test1.pl0", read, theResult);
- IF theResult = noSuchFile THEN
- STextIO.WriteString("Sorry, this file doesn't exist ; halting");
- STextIO.WriteLn;
- ELSE
- IF theResult=opened THEN
- (* file is open*)
- STextIO.WriteString("file opened!");
- STextIO.WriteLn;
- STextIO.WriteLn;
- (* reading a char*)
- ReadChar(theChanId, theChar);
- (* testing the result *)
- (* if OK we continue *)
- WHILE ReadResult(theChanId) <> endOfInput DO
- (* testing end of line *)
- IF ReadResult(theChanId) = endOfLine THEN
- INC(theLineNumber);
- (* we have to 'skip' the endOfLine ... so strange *)
- SkipLine(theChanId);
- END;
- (* printing the char*)
- STextIO.WriteChar(theChar);
- (* reading next char *)
- ReadChar(theChanId, theChar);
- END;
- Close(theChanId);
- ELSE
- STextIO.WriteString("Error");
- STextIO.WriteLn;
- END;
- END;
-
- END PL0c4.
|