showcase24.mod 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. MODULE Showcase24;
  2. // Multi-file session showcase. Combines:
  3. // * ISO generic modules: GStack(T) instantiated as IntStack, and the
  4. // two-parameter GPair(K, V) instantiated as PairIC(INTEGER, CHAR),
  5. // * a Clarion CLASS hierarchy with VIRTUAL dispatch (Shape/Circle/
  6. // Square) held through base-class pointers,
  7. // * stdlib libraries: TextIO, Strings, Conversions, Math,
  8. // * a local helper library, Show24Util.
  9. //
  10. // Pushes each shape's virtual Area() onto the generic stack, pops and
  11. // sums them (9+16+16+1 = 42), builds "showcase24 total=42" in
  12. // Show24Util (Strings + Conversions), reverses it through the same
  13. // generic stack, checks a real sqrt via Math and a pair via PairIC.
  14. // Prints the label and exits 42.
  15. IMPORT TextIO, Strings, IntStack, PairIC, Show24Util;
  16. TYPE
  17. CLASS Shape;
  18. VIRTUAL PROCEDURE Area() : INTEGER;
  19. END Shape;
  20. CLASS Circle (Shape);
  21. r : INTEGER;
  22. PROCEDURE SetR(v : INTEGER);
  23. VIRTUAL PROCEDURE Area() : INTEGER;
  24. END Circle;
  25. CLASS Square (Shape);
  26. s : INTEGER;
  27. PROCEDURE SetS(v : INTEGER);
  28. VIRTUAL PROCEDURE Area() : INTEGER;
  29. END Square;
  30. CLASS IMPLEMENTATION Shape;
  31. PROCEDURE Area() : INTEGER;
  32. BEGIN RETURN 0 END Area;
  33. END Shape;
  34. CLASS IMPLEMENTATION Circle;
  35. PROCEDURE SetR(v : INTEGER);
  36. BEGIN r := v END SetR;
  37. PROCEDURE Area() : INTEGER;
  38. BEGIN RETURN r * r END Area;
  39. END Circle;
  40. CLASS IMPLEMENTATION Square;
  41. PROCEDURE SetS(v : INTEGER);
  42. BEGIN s := v END SetS;
  43. PROCEDURE Area() : INTEGER;
  44. BEGIN RETURN s * s END Area;
  45. END Square;
  46. VAR ExitCode : INTEGER;
  47. VAR a, b : POINTER TO Circle;
  48. VAR c, d : POINTER TO Square;
  49. VAR shapes : ARRAY [0 .. 3] OF POINTER TO Shape;
  50. VAR st : IntStack.Stack;
  51. VAR line, rev : ARRAY [0 .. 63] OF CHAR;
  52. VAR i, n, total, v : INTEGER;
  53. VAR ch : CHAR;
  54. VAR ok : BOOLEAN;
  55. BEGIN
  56. ExitCode := 0;
  57. (* four polymorphic shapes: Circle(3)=9, Circle(4)=16,
  58. Square(4)=16, Square(1)=1 -> 42 *)
  59. NEW(a); a^.SetR(3);
  60. NEW(b); b^.SetR(4);
  61. NEW(c); c^.SetS(4);
  62. NEW(d); d^.SetS(1);
  63. shapes[0] := a; shapes[1] := b; shapes[2] := c; shapes[3] := d;
  64. (* generic stack: push each virtual Area(), then pop and sum *)
  65. IntStack.Init(st);
  66. i := 0;
  67. WHILE i <= 3 DO
  68. IntStack.Push(st, shapes[i]^.Area());
  69. INC(i)
  70. END;
  71. IF IntStack.Depth(st) # 4 THEN ExitCode := 1 END;
  72. total := 0;
  73. WHILE IntStack.Pop(st, v) DO total := total + v END;
  74. (* helper library: line = "showcase24 total=" + IntToStr(total) *)
  75. Show24Util.Label("showcase24 total=", total, line);
  76. (* reverse the label through the same generic stack *)
  77. n := Strings.Length(line);
  78. IntStack.Init(st);
  79. i := 0;
  80. WHILE i < n DO
  81. IntStack.Push(st, ORD(line[i]));
  82. INC(i)
  83. END;
  84. i := 0; ok := TRUE;
  85. WHILE ok DO
  86. ok := IntStack.Pop(st, v);
  87. IF ok THEN rev[i] := CHR(v); INC(i) END
  88. END;
  89. rev[i] := CHR(0);
  90. IF (n # 19) OR (rev[0] # "2") OR (rev[n - 1] # "s") THEN ExitCode := 1 END;
  91. (* two-parameter generic: PairIC = GPair(INTEGER, CHAR) *)
  92. IF PairIC.First(total, "!") # 42 THEN ExitCode := 1 END;
  93. ch := PairIC.Second(total, "!");
  94. IF ch # "!" THEN ExitCode := 1 END;
  95. (* real math through the Math library *)
  96. IF Show24Util.Root(42) # 6 THEN ExitCode := 1 END;
  97. TextIO.WriteString(line);
  98. TextIO.WriteLn;
  99. IF total = 42 THEN ExitCode := ExitCode + 42 ELSE ExitCode := 99 END
  100. END Showcase24.