showcase12.mod 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. MODULE Showcase12;
  2. // Step-8.2/8.3 showcase. Exercises, in one small session:
  3. // - named-CONST folding in an array bound (M = N*2-1)
  4. // - a forward pointer type (Link = POINTER TO Node; Node later)
  5. // - a trailing ';' before a record's END
  6. // - empty statements (';;' and a lone ';')
  7. // - 1-character string literals passed to ARRAY OF CHAR
  8. // - a qualified type name (Showcase12Lib.Point) and qualified call
  9. // Expected ExitCode: 42.
  10. IMPORT Showcase12Lib;
  11. CONST
  12. N = 4; // folded: N used below
  13. M = N * 2 - 1; // folds to 7 — the bound of Vec
  14. TYPE
  15. Vec = ARRAY [0 .. M] OF INTEGER; // 8 elements
  16. Link = POINTER TO Node; // forward: Node is declared later
  17. Node = RECORD
  18. value : INTEGER;
  19. next : Link; // trailing ';' before END is accepted
  20. END;
  21. VAR
  22. ExitCode : INTEGER;
  23. v : Vec;
  24. head : Link;
  25. pt : Showcase12Lib.Point; // qualified type name
  26. i, sum : INTEGER;
  27. BEGIN
  28. sum := 0;
  29. FOR i := 0 TO M DO v[i] := i; sum := sum + v[i] END;; // 0+..+7 = 28
  30. ; // empty statement
  31. NEW(head); head^.value := 9; head^.next := NIL;
  32. sum := sum + head^.value; // 37
  33. sum := sum + Showcase12Lib.Score("A")
  34. + Showcase12Lib.Score("B"); // +2+3 = 42
  35. pt.x := 42;
  36. IF sum = pt.x THEN ExitCode := sum ELSE ExitCode := 1 END
  37. END Showcase12.