PL0c4.mod 3.8 KB

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