t77_with.pas 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. program t77_with;
  2. { t77 -- WITH, and 2-D array subscripts.
  3. CALC.INC:158 is
  4. with Sheet[I,J] do
  5. begin
  6. CellStatus := [Txt];
  7. Contents := '';
  8. ...
  9. end;
  10. Sheet is `array['A'..'G',1..FYMax] of CellRec', so the WITH base is an
  11. element at a RUN-TIME address: the row stride is (hi1-lo1+1)*elemsize and
  12. both indices vary. The base address is parked in a data slot because the
  13. body may clobber DI; each field access reloads it and adds the field's
  14. offset.
  15. Also covers a WITH over a plain record variable (symbol+offset, no slot), a
  16. WITH over a constant-index element, a field read back inside the body, and a
  17. mixed constant/variable 2-D subscript. Field types here are INTEGER and
  18. CHAR: SET and STRING fields need their own assignments and are the next
  19. rung (CALC's CellStatus is the first one). }
  20. type
  21. Rec = record
  22. a : integer;
  23. b : char;
  24. end;
  25. var
  26. g : array [1..2, 1..3] of Rec;
  27. i, j : integer;
  28. r : Rec;
  29. begin
  30. with r do
  31. begin
  32. a := 3;
  33. b := 'q'
  34. end;
  35. writeln (r.a, r.b);
  36. i := 2;
  37. j := 3;
  38. with g[i,j] do
  39. begin
  40. a := 42;
  41. b := 'x'
  42. end;
  43. writeln (g[2,3].a, g[2,3].b);
  44. with g[i,j] do
  45. writeln (a);
  46. with g[1,2] do
  47. a := 7;
  48. writeln (g[1,2].a);
  49. (* The stride is pinned INDEPENDENTLY: write with constant indices (folded
  50. at compile time) and read with the run-time formula. A write and a read
  51. that share the same wrong stride would agree with each other. *)
  52. g[2,3].a := 99;
  53. writeln (g[i,j].a);
  54. j := 2;
  55. g[i,j].a := 11;
  56. writeln (g[2,2].a);
  57. (* A wrong row stride makes different index pairs ALIAS. With the correct
  58. stride (9, 3), (1,2) is offset 3 and (2,1) is 9; with a uniform element
  59. stride they are both 3. Writing two values and reading the first is the
  60. observation no self-consistent formula can fake. *)
  61. g[1,2].a := 71;
  62. g[2,1].a := 81;
  63. writeln (g[1,2].a)
  64. end.