t46_strindex_var.pas 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. program t46;
  2. (* s[i] with a VARIABLE index -- an address the code can only compute at run
  3. time.
  4. Until now a subscript had to fold: ParseSub accepted a constant and
  5. reported ENoLib for anything else, because off+i is not known when the
  6. symbol is. TP3 computes it once, in indstr/ialoop, and holds it in DI
  7. until the byte is loaded or stored (TPSRC9 1100-1111, 1200-1229); the
  8. assignment paths then save it across the right-hand side (TPSRC8 asnscal2
  9. 758-760, asnscal3 786-789), which is what keeps `g[i] := h[j]` storing
  10. where it read from. This mirrors that: LEA DI,[base] / ADD DI,AX, and a
  11. PUSH DI around an assignment's right-hand side.
  12. A STRING is still a length byte at s[0] then the characters, so s[i] is
  13. the one byte at off+i. A second subscript is not a type (ESimpType), and
  14. there is no range check yet -- {$R+} / {$I+} belong to item 2. *)
  15. var
  16. g : string[10] ;
  17. h : string[10] ;
  18. i, j : integer ;
  19. c : char ;
  20. procedure show (ch : char) ;
  21. begin
  22. write (ch)
  23. end ;
  24. procedure p ;
  25. var
  26. l : string[6] ;
  27. k : integer ;
  28. begin
  29. l := 'abcdef' ;
  30. k := 2 ;
  31. writeln (l[k]) ; (* local string: LEA DI,[BP+disp] *)
  32. l[k] := 'Z' ; (* local destination, variable index *)
  33. writeln (l) ;
  34. g[3] := l[k] (* constant destination, variable source *)
  35. end ;
  36. begin
  37. g := 'hello' ;
  38. h := 'world' ;
  39. i := 1 ;
  40. j := 4 ;
  41. writeln (g[i]) ; (* read *)
  42. c := g[j] ;
  43. writeln (c) ; (* read into a char *)
  44. g[i] := 'H' ; (* write *)
  45. writeln (g) ;
  46. if g[i] = 'H' then writeln ('y') else writeln ('n') ;
  47. writeln (g[j], h[i]) ; (* two variable reads in one call *)
  48. show (g[j]) ; (* a subscript as a procedure ARGUMENT *)
  49. writeln ;
  50. g[i] := h[j] ; (* both sides indexed at once *)
  51. writeln (g) ;
  52. writeln (g[i+1]) ; (* computed index: ADD DI,AX *)
  53. p ;
  54. writeln (g)
  55. end.