showcase22.mod 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. MODULE Showcase22;
  2. // Tour of this step's additions:
  3. //
  4. // * forward module-level variables — the procedures below use
  5. // `total`, `done` and `label`, all declared after them;
  6. // * expression result suffixes — GetRec()^.a, GetArr()^[i];
  7. // * the channel/time wrapper library — StdChans (current output
  8. // channel), wrapclock (opaque timespec), wraptime (timeval/tm),
  9. // wrapc (rand/signbit/isfinite) and TimeString.
  10. //
  11. // Writes "showcase22 total=42"; expected ExitCode: 42.
  12. IMPORT IOChan, StdChans, TimeString, wrapclock, wraptime, wrapc;
  13. FROM SYSTEM IMPORT ADDRESS;
  14. TYPE
  15. Rec = RECORD a, b : INTEGER END;
  16. RecPtr = POINTER TO Rec;
  17. Arr = ARRAY [0..3] OF INTEGER;
  18. ArrPtr = POINTER TO Arr;
  19. VAR ExitCode : INTEGER;
  20. VAR r : RecPtr;
  21. VAR s : ArrPtr;
  22. VAR ok : INTEGER;
  23. VAR out : IOChan.ChanId;
  24. VAR stamp : ARRAY [0..63] OF CHAR;
  25. (* --- forward references: these globals are declared further down --- *)
  26. PROCEDURE Bump;
  27. BEGIN
  28. total := total + 1;
  29. IF total >= 3 THEN done := TRUE END
  30. END Bump;
  31. (* ------------------------------------------------------------------ *)
  32. PROCEDURE GetRec () : RecPtr;
  33. BEGIN RETURN r END GetRec;
  34. PROCEDURE GetArr () : ArrPtr;
  35. BEGIN RETURN s END GetArr;
  36. VAR total : INTEGER;
  37. VAR done : BOOLEAN;
  38. VAR label : ARRAY [0..15] OF CHAR;
  39. PROCEDURE SetLabel;
  40. BEGIN
  41. label[0] := "o"; label[1] := "k"; label[2] := CHR(0)
  42. END SetLabel;
  43. VAR ts : wrapclock.timespec;
  44. VAR tv : wraptime.timeval;
  45. VAR tm : wraptime.tm;
  46. VAR sec, nano : LONGCARD;
  47. VAR nrand : INTEGER;
  48. BEGIN
  49. ExitCode := 0;
  50. out := StdChans.OutChan();
  51. (* --- forward variables: Bump uses total/done before their VAR --- *)
  52. NEW(r); NEW(s);
  53. r^.a := 40; r^.b := 2;
  54. s^[0] := 1; s^[1] := 2; s^[2] := 2; s^[3] := 4;
  55. Bump; (* total = 1 *)
  56. Bump; (* total = 2 *)
  57. Bump; (* total = 3, done = TRUE *)
  58. total := total + r^.a - r^.b; (* 3 + 40 - 2 = 41 *)
  59. SetLabel;
  60. (* --- expression result suffixes --- *)
  61. total := total + GetRec()^.b - GetArr()^[2]; (* 41 + 2 - 2 = 41 *)
  62. IF GetRec()^.a + GetRec()^.b = 42 THEN INC(ok) END;
  63. IF GetArr()^[1] + GetArr()^[2] = 4 THEN INC(ok) END;
  64. IF done AND (label[0] = "o") THEN total := total + 1 END; (* 42 *)
  65. (* --- wrappers --- *)
  66. ts := wrapclock.InitTimespec();
  67. ok := ok + wrapclock.SetTimespec(ts, 42, 0);
  68. ok := ok + wrapclock.GetTimespec(ts, sec, nano);
  69. IF (sec = 42) AND (nano = 0) THEN INC(ok) END;
  70. ts := wrapclock.KillTimespec(ts);
  71. tv := wraptime.InitTimeval();
  72. tm := wraptime.InitTM();
  73. wraptime.SetTimeval(tv, 0, 0);
  74. IF wraptime.LocalTime(tv, tm) # NIL THEN INC(ok) END;
  75. tm := wraptime.KillTM(tm);
  76. tv := wraptime.KillTimeval(tv);
  77. nrand := wrapc.getrand(10);
  78. IF (nrand >= 0) AND (nrand < 10) THEN INC(ok) END;
  79. IF wrapc.signbit(-1.0) = 1 THEN INC(ok) END;
  80. IF wrapc.isfinite(1.0) = 1 THEN INC(ok) END;
  81. TimeString.GetTimeString(stamp);
  82. IF stamp[0] # CHR(0) THEN INC(ok) END;
  83. (* --- report --- *)
  84. IOChan.WriteString(out, "showcase22 total=");
  85. IOChan.WriteInt(out, total);
  86. IOChan.WriteLn(out);
  87. IF (total = 42) AND (ok >= 5) THEN
  88. ExitCode := 42
  89. ELSE
  90. ExitCode := 1
  91. END
  92. END Showcase22.