showcase15.mod 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. MODULE Showcase15;
  2. // Session showcase: separate compilation across a DEFINITION /
  3. // IMPLEMENTATION pair (whose signatures are verified by the new
  4. // strict 235 check), string concatenation, and IOChan over files.
  5. //
  6. // It writes a small report to a file through a channel, closes it,
  7. // reopens it for reading, reads it back line by line, and prints a
  8. // summary on stdout. Expected ExitCode: 42.
  9. IMPORT Showcase15Lib, IOChan, TextIO, Strings;
  10. VAR ExitCode : INTEGER;
  11. VAR out, inp : IOChan.ChanId;
  12. VAR name, line : ARRAY [0 .. 63] OF CHAR;
  13. VAR p : Showcase15Lib.Point;
  14. VAR total : INTEGER;
  15. PROCEDURE Emit (cid : IOChan.ChanId; s : ARRAY OF CHAR; n : INTEGER);
  16. (* one "label + number" line on the channel *)
  17. VAR buf : ARRAY [0 .. 63] OF CHAR;
  18. BEGIN
  19. Strings.Assign(s, buf);
  20. Showcase15Lib.AppendInt(buf, n);
  21. IOChan.WriteString(cid, buf);
  22. IOChan.WriteLn(cid)
  23. END Emit;
  24. BEGIN
  25. ExitCode := 0;
  26. Strings.Assign("gen_ssa/_showcase15.txt", name);
  27. Showcase15Lib.InitPoint(p, 20, 22);
  28. total := Showcase15Lib.SumPoint(p);
  29. (* write the report through a file channel *)
  30. out := IOChan.OpenWrite(name);
  31. IF NOT IOChan.IsOpen(out) THEN ExitCode := 90 ELSE
  32. IOChan.WriteString(out, "total");
  33. IOChan.WriteLn(out);
  34. Emit(out, "point ", total);
  35. IOChan.Close(out)
  36. END;
  37. (* read it back through another channel, line by line *)
  38. inp := IOChan.OpenRead(name);
  39. IF NOT IOChan.IsOpen(inp) THEN ExitCode := 91 ELSE
  40. IOChan.ReadString(inp, line); (* "total" *)
  41. IF line = "total" THEN ExitCode := ExitCode + 1 ELSE ExitCode := 92 END;
  42. IOChan.ReadString(inp, line); (* "point 42" *)
  43. IF line = "point 42" THEN ExitCode := ExitCode + 2 ELSE ExitCode := 93 END;
  44. TextIO.WriteString("read back: ");
  45. TextIO.WriteString(line);
  46. TextIO.WriteLn;
  47. IOChan.Close(inp)
  48. END;
  49. IF ExitCode = 3 THEN ExitCode := total END; (* 42 on success *)
  50. TextIO.WriteString("showcase15 total=");
  51. TextIO.WriteInt(total);
  52. TextIO.WriteLn
  53. END Showcase15.