PL0c1.mod 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. MODULE PL0c1;
  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;
  16. VAR
  17. theChanId : ChanId;
  18. theResult : OpenResults;
  19. theChar : CHAR;
  20. theLineNumber : CARDINAL;
  21. BEGIN
  22. STextIO.WriteString("PL0 compiler from Wirth book 'Algorithms and Data Structures'");
  23. STextIO.WriteLn;
  24. STextIO.WriteString("Eric Streit <eric@yojik.eu>");
  25. STextIO.WriteLn;
  26. STextIO.WriteLn;
  27. theLineNumber := 1;
  28. OpenRead(theChanId, "test1.pl0", read, theResult);
  29. IF theResult = noSuchFile THEN
  30. STextIO.WriteString("Sorry, this file doesn't exist ; halting");
  31. STextIO.WriteLn;
  32. ELSE
  33. IF theResult=opened THEN
  34. (* file is open*)
  35. STextIO.WriteString("file opened!");
  36. STextIO.WriteLn;
  37. STextIO.WriteLn;
  38. (* reading a char*)
  39. ReadChar(theChanId, theChar);
  40. (* testing the result *)
  41. (* if OK we continue *)
  42. WHILE ReadResult(theChanId) <> endOfInput DO
  43. (* testing end of line *)
  44. IF ReadResult(theChanId) = endOfLine THEN
  45. INC(theLineNumber);
  46. (* we have to 'skip' the endOfLine ... so strange *)
  47. SkipLine(theChanId);
  48. END;
  49. (* printing the char*)
  50. STextIO.WriteChar(theChar);
  51. (* reading next char *)
  52. ReadChar(theChanId, theChar);
  53. END;
  54. Close(theChanId);
  55. ELSE
  56. STextIO.WriteString("Error");
  57. STextIO.WriteLn;
  58. END;
  59. END;
  60. END PL0c1.