t_variant.mod 977 B

12345678910111213141516171819202122232425262728293031323334
  1. MODULE TVariant;
  2. (* Variant records: plain fields pack before the CASE tag; every
  3. branch's fields overlay in one region after the tag. Exit 31. *)
  4. VAR ExitCode : INTEGER;
  5. TYPE
  6. Kind = (circle, square, rect);
  7. Shape = RECORD
  8. id : INTEGER;
  9. CASE k : Kind OF
  10. circle : radius : INTEGER;
  11. | square : side : INTEGER;
  12. | rect : w, h : INTEGER;
  13. END;
  14. END;
  15. VAR s : Shape;
  16. BEGIN
  17. ExitCode := 0;
  18. s.id := 7;
  19. s.k := rect;
  20. s.w := 3;
  21. s.h := 4;
  22. IF s.id = 7 THEN ExitCode := ExitCode + 1 ELSE ExitCode := 100 END;
  23. IF s.k = rect THEN ExitCode := ExitCode + 2 ELSE ExitCode := 101 END;
  24. IF s.h = 4 THEN ExitCode := ExitCode + 4 ELSE ExitCode := 102 END;
  25. (* w and h share storage: the last store wins *)
  26. s.w := 9;
  27. IF s.h = 9 THEN ExitCode := ExitCode + 8 ELSE ExitCode := 103 END;
  28. (* switching branch reuses the same slot *)
  29. s.k := square;
  30. s.side := 5;
  31. IF s.side = 5 THEN ExitCode := ExitCode + 16 ELSE ExitCode := 104 END
  32. END TVariant.