tw2178.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. type
  2. stlpos=^stlink;
  3. stlink=
  4. packed object
  5. next,prev:stlpos;
  6. data:string;
  7. procedure save(var f:file);
  8. procedure load(var f:file);
  9. end;
  10. list_of_string=
  11. packed object
  12. parent:pointer;
  13. anchor:stlpos;
  14. last:stlpos;
  15. len:longint;
  16. constructor init(p:pointer);
  17. function first:stlpos;
  18. function next(p:stlpos):stlpos;
  19. function prev(p:stlpos):stlpos;
  20. function ret(p:stlpos):string;
  21. function pret(p:stlpos):pointer;
  22. function insert(p:stlpos;d:string):stlpos;
  23. function remove(p:stlpos):stlpos;
  24. function empty:boolean;
  25. destructor term;
  26. procedure save(var f:file);
  27. procedure load(var f:file);
  28. end;
  29. stack_of_string=
  30. packed object (list_of_string)
  31. procedure pop(var s:string);
  32. procedure push(const s:string);
  33. end;
  34. procedure writestring(var f:file;s:string);
  35. begin
  36. blockwrite(f,s,length(s)+1);
  37. end;
  38. procedure readstring(var f:file;var s:string);
  39. var b:byte;
  40. begin
  41. blockread(f,b,1);
  42. seek(f,filepos(f)-1);
  43. blockread(f,s,b+1);
  44. end;
  45. constructor list_of_string.init(p:pointer);
  46. begin
  47. parent:=p;
  48. new(anchor);
  49. anchor^.next:=nil;
  50. anchor^.prev:=nil;
  51. last:=anchor;
  52. len:=0;
  53. end;
  54. function list_of_string.first:stlpos;
  55. begin
  56. first:=anchor^.next;
  57. end;
  58. function list_of_string.next(p:stlpos):stlpos;
  59. begin
  60. next:=p^.next;
  61. end;
  62. function list_of_string.prev(p:stlpos):stlpos;
  63. begin
  64. prev:=p^.prev;
  65. end;
  66. function list_of_string.ret(p:stlpos):string;
  67. begin
  68. ret:=p^.data;
  69. end;
  70. function list_of_string.pret(p:stlpos):pointer;
  71. begin
  72. pret:=@(p^.data);
  73. end;
  74. function list_of_string.insert(p:stlpos;d:string):stlpos;
  75. var t:stlpos;
  76. begin
  77. new(t);
  78. t^.prev:=p;
  79. t^.next:=p^.next;
  80. p^.next:=t;
  81. if t^.next<>nil then
  82. t^.next^.prev:=t
  83. else
  84. last:=t;
  85. t^.data:=d;
  86. inc(len);
  87. insert:=t;
  88. end;
  89. function list_of_string.remove(p:stlpos):stlpos;
  90. begin
  91. if p^.prev<>nil then
  92. p^.prev^.next:=p^.next;
  93. if p^.next<>nil then
  94. p^.next^.prev:=p^.prev
  95. else
  96. last:=p^.prev;
  97. dispose(p);
  98. dec(len);
  99. remove:=p^.next;
  100. end;
  101. function list_of_string.empty:boolean;
  102. begin
  103. empty:=(last=anchor);
  104. end;
  105. destructor list_of_string.term;
  106. begin
  107. while not empty do
  108. remove(last);
  109. dispose(anchor);
  110. last:=nil;
  111. end;
  112. procedure stlink.save(var f:file);
  113. begin
  114. writestring(f,data);
  115. end;
  116. procedure list_of_string.save(var f:file);
  117. var
  118. l,i:longint;
  119. p:stlpos;
  120. begin
  121. l:=len;
  122. blockwrite(f,l,sizeof(longint));
  123. p:=first;
  124. for i:=1 to l do
  125. begin
  126. p^.save(f);
  127. p:=next(p);
  128. end;
  129. end;
  130. procedure stlink.load(var f:file);
  131. begin
  132. readstring(f,data);
  133. end;
  134. procedure list_of_string.load(var f:file);
  135. var
  136. l,i:longint;
  137. d:stlink;
  138. begin
  139. blockread(f,l,sizeof(longint));
  140. for i:=1 to l do
  141. begin
  142. d.load(f);
  143. insert(last,d.data);
  144. end;
  145. end;
  146. procedure stack_of_string.pop(var s:string);
  147. begin
  148. if not empty then
  149. begin
  150. s:=first^.data;
  151. remove(first);
  152. end;
  153. end;
  154. procedure stack_of_string.push(const s:string);
  155. begin
  156. insert(anchor,s);
  157. end;
  158. var
  159. gs:stack_of_string;
  160. opaddr : ^string;
  161. begin
  162. gs.init(nil);
  163. gs.push('test');
  164. {perfectly compiles}
  165. opaddr:=@((gs.first)^.data);
  166. writeln(opaddr^);
  167. if (opaddr^<>'test') then
  168. halt(1);
  169. {reports error ") expected ^ but found"}
  170. opaddr:=@(gs.first^.data);
  171. writeln(opaddr^);
  172. if (opaddr^<>'test') then
  173. halt(1);
  174. end.