h2ptypes.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. unit h2ptypes;
  2. // {$mode ObjFPC}
  3. {$inline on}
  4. {$modeswitch result}
  5. interface
  6. uses
  7. Classes, SysUtils;
  8. type
  9. Char=system.ansichar;
  10. ttyp = (
  11. t_id,
  12. { p contains the string }
  13. t_arraydef,
  14. { }
  15. t_pointerdef,
  16. { p1 contains the definition
  17. if in type overrider
  18. or nothing for args
  19. }
  20. t_addrdef,
  21. t_void,
  22. { no field }
  23. t_dec,
  24. { }
  25. t_declist,
  26. { p1 is t_dec
  27. next if exists }
  28. t_memberdec,
  29. { p1 is type specifier
  30. p2 is declarator_list }
  31. t_structdef,
  32. { }
  33. t_memberdeclist,
  34. { p1 is memberdec
  35. next is next if it exist }
  36. t_procdef,
  37. { }
  38. t_uniondef,
  39. { }
  40. t_enumdef,
  41. { }
  42. t_enumlist,
  43. { }
  44. t_preop,
  45. { p contains the operator string
  46. p1 contains the right expr }
  47. t_bop,
  48. { p contains the operator string
  49. p1 contains the left expr
  50. p2 contains the right expr }
  51. t_arrayop,
  52. {
  53. p1 contains the array expr
  54. p2 contains the index expressions }
  55. t_callop,
  56. {
  57. p1 contains the proc expr
  58. p2 contains the index expressions }
  59. t_arg,
  60. {
  61. p1 contain the typedef
  62. p2 the declarator (t_dec)
  63. }
  64. t_arglist,
  65. { }
  66. t_funexprlist,
  67. { }
  68. t_exprlist,
  69. { p1 contains the expr
  70. next contains the next if it exists }
  71. t_ifexpr,
  72. { p1 contains the condition expr
  73. p2 contains the if branch
  74. p3 contains the else branch }
  75. t_funcname,
  76. { p1 contains the function dname
  77. p2 contains the funexprlist
  78. p3 possibly contains the return type }
  79. t_typespec,
  80. { p1 is the type itself
  81. p2 the typecast expr }
  82. t_size_specifier,
  83. { p1 expr for size }
  84. t_default_value,
  85. { p1 expr for value }
  86. t_statement_list,
  87. { p1 is the statement
  88. next is next if it exist }
  89. t_whilenode,
  90. t_fornode,
  91. t_dowhilenode,
  92. t_switchnode,
  93. t_gotonode,
  94. t_continuenode,
  95. t_breaknode
  96. );
  97. const
  98. ttypstr: array[ttyp] of string =
  99. (
  100. 't_id',
  101. 't_arraydef',
  102. 't_pointerdef',
  103. 't_addrdef',
  104. 't_void',
  105. 't_dec',
  106. 't_declist',
  107. 't_memberdec',
  108. 't_structdef',
  109. 't_memberdeclist',
  110. 't_procdef',
  111. 't_uniondef',
  112. 't_enumdef',
  113. 't_enumlist',
  114. 't_preop',
  115. 't_bop',
  116. 't_arrayop',
  117. 't_callop',
  118. 't_arg',
  119. 't_arglist',
  120. 't_funexprlist',
  121. 't_exprlist',
  122. 't_ifexpr',
  123. 't_funcname',
  124. 't_typespec',
  125. 't_size_specifier',
  126. 't_default_value',
  127. 't_statement_list',
  128. 't_whilenode',
  129. 't_fornode',
  130. 't_dowhilenode',
  131. 't_switchnode',
  132. 't_gotonode',
  133. 't_continuenode',
  134. 't_breaknode'
  135. );
  136. type
  137. presobject = ^tresobject;
  138. tresobject = object
  139. typ : ttyp;
  140. p : pansichar;
  141. next : presobject;
  142. p1,p2,p3 : presobject;
  143. { name of int/real, then no T prefix is required }
  144. intname : boolean;
  145. constructor init_no(t : ttyp);
  146. constructor init_one(t : ttyp;_p1 : presobject);
  147. constructor init_two(t : ttyp;_p1,_p2 : presobject);
  148. constructor init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  149. constructor init_id(const s : string);
  150. constructor init_intid(const s : string);
  151. constructor init_bop(const s : string;_p1,_p2 : presobject);
  152. constructor init_preop(const s : string;_p1 : presobject);
  153. procedure setstr(const s:string);
  154. function str : string;
  155. function strlength : byte;
  156. function get_copy : presobject;
  157. { can this ve considered as a constant ? }
  158. function is_const : boolean;
  159. destructor done;
  160. end;
  161. tblocktype = (bt_type,bt_const,bt_var,bt_func,bt_no);
  162. Function NewUnaryOp(aop : string; aright : presobject) : presobject; inline;
  163. Function NewBinaryOp(aop : string; aleft,aright : presobject) : presobject; inline;
  164. Function NewVoid : presobject; inline;
  165. Function NewID(aID : string) : presobject; inline;
  166. Function NewType1(aType : ttyp; aID : presobject) : presobject; inline;
  167. Function NewType2(aType : ttyp; aID,aID2 : presobject) : presobject; inline;
  168. Function NewType3(aType : ttyp; aID,aID2,aID3 : presobject) : presobject; inline;
  169. Function NewIntID(aIntID : string) : presobject; inline;
  170. function strpnew(const s : ansistring) : pansichar; inline;
  171. implementation
  172. uses strings;
  173. Function NewVoid : presobject;
  174. begin
  175. Result:=new(presobject,init_no(t_void));
  176. end;
  177. Function NewBinaryOp(aop : string; aleft,aright : presobject) : presobject;
  178. begin
  179. Result:=new(presobject,init_bop(aop,aleft,aright));
  180. end;
  181. Function NewUnaryOp(aop : string; aright : presobject) : presobject; inline;
  182. begin
  183. Result:=new(presobject,init_preop(aop,aright));
  184. end;
  185. Function NewID(aID : string) : presobject;
  186. begin
  187. Result:=new(presobject,init_id(aID));
  188. end;
  189. Function NewIntID(aIntID : string) : presobject;
  190. begin
  191. Result:=new(presobject,init_intid(aIntID));
  192. end;
  193. Function NewType1(aType : ttyp; aID : presobject) : presobject; inline;
  194. begin
  195. Result:=new(presobject,init_one(atype,aID));
  196. end;
  197. Function NewType2(aType : ttyp; aID,aID2 : presobject) : presobject; inline;
  198. begin
  199. Result:=new(presobject,init_two(atype,aID,aID2));
  200. end;
  201. Function NewType3(aType : ttyp; aID,aID2,aID3 : presobject) : presobject; inline;
  202. begin
  203. Result:=new(presobject,init_three(atype,aID,aID2,aID3));
  204. end;
  205. function strpnew(const s : ansistring) : pansichar;
  206. var
  207. p : pansichar;
  208. begin
  209. getmem(p,length(s)+1);
  210. strpcopy(p,s);
  211. strpnew:=p;
  212. end;
  213. constructor tresobject.init_preop(const s : string;_p1 : presobject);
  214. begin
  215. typ:=t_preop;
  216. p:=strpnew(s);
  217. p1:=_p1;
  218. p2:=nil;
  219. p3:=nil;
  220. next:=nil;
  221. intname:=false;
  222. end;
  223. constructor tresobject.init_bop(const s : string;_p1,_p2 : presobject);
  224. begin
  225. typ:=t_bop;
  226. p:=strpnew(s);
  227. p1:=_p1;
  228. p2:=_p2;
  229. p3:=nil;
  230. next:=nil;
  231. intname:=false;
  232. end;
  233. constructor tresobject.init_id(const s : string);
  234. begin
  235. typ:=t_id;
  236. p:=strpnew(s);
  237. p1:=nil;
  238. p2:=nil;
  239. p3:=nil;
  240. next:=nil;
  241. intname:=false;
  242. end;
  243. constructor tresobject.init_intid(const s : string);
  244. begin
  245. typ:=t_id;
  246. p:=strpnew(s);
  247. p1:=nil;
  248. p2:=nil;
  249. p3:=nil;
  250. next:=nil;
  251. intname:=true;
  252. end;
  253. constructor tresobject.init_two(t : ttyp;_p1,_p2 : presobject);
  254. begin
  255. typ:=t;
  256. p1:=_p1;
  257. p2:=_p2;
  258. p3:=nil;
  259. p:=nil;
  260. next:=nil;
  261. intname:=false;
  262. end;
  263. constructor tresobject.init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  264. begin
  265. typ:=t;
  266. p1:=_p1;
  267. p2:=_p2;
  268. p3:=_p3;
  269. p:=nil;
  270. next:=nil;
  271. intname:=false;
  272. end;
  273. constructor tresobject.init_one(t : ttyp;_p1 : presobject);
  274. begin
  275. typ:=t;
  276. p1:=_p1;
  277. p2:=nil;
  278. p3:=nil;
  279. next:=nil;
  280. p:=nil;
  281. intname:=false;
  282. end;
  283. constructor tresobject.init_no(t : ttyp);
  284. begin
  285. typ:=t;
  286. p:=nil;
  287. p1:=nil;
  288. p2:=nil;
  289. p3:=nil;
  290. next:=nil;
  291. intname:=false;
  292. end;
  293. procedure tresobject.setstr(const s : string);
  294. begin
  295. if assigned(p) then
  296. strdispose(p);
  297. p:=strpnew(s);
  298. end;
  299. function tresobject.str : string;
  300. begin
  301. str:=strpas(p);
  302. end;
  303. function tresobject.strlength : byte;
  304. begin
  305. if assigned(p) then
  306. strlength:=strlen(p)
  307. else
  308. strlength:=0;
  309. end;
  310. { can this ve considered as a constant ? }
  311. function tresobject.is_const : boolean;
  312. begin
  313. case typ of
  314. t_id,t_void :
  315. is_const:=true;
  316. t_preop :
  317. is_const:= ((str='-') or (str=' not ')) and p1^.is_const;
  318. t_bop :
  319. is_const:= p2^.is_const and p1^.is_const;
  320. else
  321. is_const:=false;
  322. end;
  323. end;
  324. function tresobject.get_copy : presobject;
  325. var
  326. newres : presobject;
  327. begin
  328. newres:=new(presobject,init_no(typ));
  329. newres^.intname:=intname;
  330. if assigned(p) then
  331. newres^.p:=strnew(p);
  332. if assigned(p1) then
  333. newres^.p1:=p1^.get_copy;
  334. if assigned(p2) then
  335. newres^.p2:=p2^.get_copy;
  336. if assigned(p3) then
  337. newres^.p3:=p3^.get_copy;
  338. if assigned(next) then
  339. newres^.next:=next^.get_copy;
  340. get_copy:=newres;
  341. end;
  342. destructor tresobject.done;
  343. begin
  344. (* writeln('disposing ',byte(typ)); *)
  345. if assigned(p)then strdispose(p);
  346. if assigned(p1) then
  347. dispose(p1,done);
  348. if assigned(p2) then
  349. dispose(p2,done);
  350. if assigned(p3) then
  351. dispose(p3,done);
  352. if assigned(next) then
  353. dispose(next,done);
  354. end;
  355. end.