PL0c2.mod 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. MODULE PL0c2;
  2. (************************************************************)
  3. (* PL0 compiler in GNU Modula-2 *)
  4. (* using only ISO Libraries to void mixing *)
  5. (* generates an asm file to be interpreted by PLO executable*)
  6. (* Eric Streit <eric@yojik.eu> May 2026 *)
  7. (************************************************************)
  8. (* program structure *)
  9. (* read a file with the text program *)
  10. (* syntax analysis followed by semantic analysis *)
  11. (* and code generation *)
  12. FROM IOResult IMPORT ReadResults, ReadResult;
  13. FROM TextIO IMPORT ReadRestLine, SkipLine, ReadToken, WriteLn, WriteString, ReadChar;
  14. FROM SeqFile IMPORT OpenRead, OpenWrite, OpenResults, Close, ChanId, read, write;
  15. IMPORT STextIO, SWholeIO, Strings;
  16. VAR
  17. theChanId : ChanId;
  18. theResult : OpenResults;
  19. theChar : CHAR;
  20. theLineNumber : CARDINAL;
  21. theFileName : ARRAY[0..128] OF CHAR;
  22. BEGIN
  23. STextIO.WriteString("PL0 compiler from Wirth book 'Algorithms and Data Structures'");
  24. STextIO.WriteLn;
  25. STextIO.WriteString("Eric Streit <eric@yojik.eu>");
  26. STextIO.WriteLn;
  27. STextIO.WriteLn;
  28. (* adding the file name input *)
  29. theFileName := "";
  30. STextIO.WriteString("Enter the file name (ENTER to leave): ");
  31. STextIO.ReadString(theFileName);
  32. STextIO.WriteString(theFileName);
  33. IF Strings.Length(theFileName) = 0 THEN
  34. STextIO.WriteString("Leaving .... bye ! ");
  35. STextIO.WriteLn;
  36. HALT
  37. END;
  38. theLineNumber := 1;
  39. OpenRead(theChanId, "test1.pl0", read, theResult);
  40. IF theResult = noSuchFile THEN
  41. STextIO.WriteString("Sorry, this file doesn't exist ; halting");
  42. STextIO.WriteLn;
  43. ELSE
  44. IF theResult=opened THEN
  45. (* file is open*)
  46. STextIO.WriteString("file opened!");
  47. STextIO.WriteLn;
  48. STextIO.WriteLn;
  49. (* reading a char*)
  50. ReadChar(theChanId, theChar);
  51. (* testing the result *)
  52. (* if OK we continue *)
  53. WHILE ReadResult(theChanId) <> endOfInput DO
  54. (* testing end of line *)
  55. IF ReadResult(theChanId) = endOfLine THEN
  56. INC(theLineNumber);
  57. (* we have to 'skip' the endOfLine ... so strange *)
  58. SkipLine(theChanId);
  59. END;
  60. (* printing the char*)
  61. STextIO.WriteChar(theChar);
  62. (* reading next char *)
  63. ReadChar(theChanId, theChar);
  64. END;
  65. Close(theChanId);
  66. ELSE
  67. STextIO.WriteString("Error");
  68. STextIO.WriteLn;
  69. END;
  70. END;
  71. END PL0c2.