PL0c3.mod 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. MODULE PL0c3;
  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. thelength : CARDINAL;
  23. BEGIN
  24. STextIO.WriteString("PL0 compiler from Wirth book 'Algorithms and Data Structures'");
  25. STextIO.WriteLn;
  26. STextIO.WriteString("Eric Streit <eric@yojik.eu>");
  27. STextIO.WriteLn;
  28. STextIO.WriteLn;
  29. (* adding the file name input *)
  30. theFileName := "";
  31. STextIO.WriteString("Enter the file name (ENTER to leave)(with an ending '.' to add automatcally the extension) : ");
  32. STextIO.ReadString(theFileName);
  33. (* STextIO.WriteString(theFileName); *)
  34. thelength := Strings.Length(theFileName);
  35. IF thelength = 0 THEN
  36. STextIO.WriteString("Leaving .... bye ! ");
  37. STextIO.WriteLn;
  38. HALT
  39. ELSE
  40. (* adding extension if the name ends by . )*)
  41. IF theFileName[thelength-1] = "." THEN
  42. (* adding extension if enough space*)
  43. IF thelength < 125 THEN
  44. Strings.Concat(theFileName, "pl0", theFileName);
  45. ELSE
  46. STextIO.WriteString("File name too long");
  47. STextIO.WriteLn;
  48. STextIO.WriteString("Halting");
  49. HALT;
  50. END;
  51. END;
  52. END;
  53. STextIO.WriteString(theFileName);
  54. STextIO.WriteLn;
  55. theLineNumber := 1;
  56. OpenRead(theChanId, "test1.pl0", read, theResult);
  57. IF theResult = noSuchFile THEN
  58. STextIO.WriteString("Sorry, this file doesn't exist ; halting");
  59. STextIO.WriteLn;
  60. ELSE
  61. IF theResult=opened THEN
  62. (* file is open*)
  63. STextIO.WriteString("file opened!");
  64. STextIO.WriteLn;
  65. STextIO.WriteLn;
  66. (* reading a char*)
  67. ReadChar(theChanId, theChar);
  68. (* testing the result *)
  69. (* if OK we continue *)
  70. WHILE ReadResult(theChanId) <> endOfInput DO
  71. (* testing end of line *)
  72. IF ReadResult(theChanId) = endOfLine THEN
  73. INC(theLineNumber);
  74. (* we have to 'skip' the endOfLine ... so strange *)
  75. SkipLine(theChanId);
  76. END;
  77. (* printing the char*)
  78. STextIO.WriteChar(theChar);
  79. (* reading next char *)
  80. ReadChar(theChanId, theChar);
  81. END;
  82. Close(theChanId);
  83. ELSE
  84. STextIO.WriteString("Error");
  85. STextIO.WriteLn;
  86. END;
  87. END;
  88. END PL0c3.