h2pas.y 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112
  1. %{
  2. program h2pas;
  3. (*
  4. $Id: h2pas.y,v 1.10 2005/02/20 11:09:41 florian Exp $
  5. Copyright (c) 1998-2000 by Florian Klaempfl
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************)
  18. uses
  19. SysUtils,classes,
  20. options,scan,converu,lexlib,yacclib;
  21. type
  22. YYSTYPE = presobject;
  23. const
  24. SHORT_STR = 'smallint';
  25. USHORT_STR = 'word';
  26. INT_STR = 'longint';
  27. UINT_STR = 'dword';
  28. CHAR_STR = 'char';
  29. UCHAR_STR = 'byte'; { should we use byte or char for 'unsigned char' ?? }
  30. INT64_STR = 'int64';
  31. QWORD_STR = 'qword';
  32. REAL_STR = 'double';
  33. WCHAR_STR = 'widechar';
  34. {ctypes strings}
  35. const
  36. cint8_STR = 'cint8';
  37. cuint8_STR = 'cuint8';
  38. cchar_STR = 'cchar';
  39. cschar_STR = 'cschar';
  40. cuchar_STR = 'cuchar';
  41. cint16_STR = 'cint16';
  42. cuint16_STR = 'cuint16';
  43. cshort_STR = 'cshort';
  44. csshort_STR = 'csshort';
  45. cushort_STR = 'cushort';
  46. cint32_STR = 'cint32';
  47. cuint32_STR = 'cuint32';
  48. cint_STR = 'cint';
  49. csint_STR = 'csint';
  50. cuint_STR = 'cuint';
  51. csigned_STR = 'csigned';
  52. cunsigned_STR = 'cunsigned';
  53. cint64_STR = 'cint64';
  54. cuint64_STR = 'cuint64';
  55. clonglong_STR = 'clonglong';
  56. cslonglong_STR = 'cslonglong';
  57. culonglong_STR = 'culonglong';
  58. cbool_STR = 'cbool';
  59. clong_STR = 'clong';
  60. cslong_STR = 'cslong';
  61. culong_STR = 'culong';
  62. cfloat_STR = 'cfloat';
  63. cdouble_STR = 'cdouble';
  64. clongdouble_STR = 'clongdouble';
  65. const
  66. MAX_CTYPESARRAY = 25;
  67. CTypesArray : array [0..MAX_CTYPESARRAY] of string =
  68. (cint8_STR, cuint8_STR,
  69. cchar_STR, cschar_STR, cuchar_STR,
  70. cint16_STR, cuint16_STR,
  71. cshort_STR, csshort_STR, cushort_STR,
  72. csigned_STR, cunsigned_STR,
  73. cint32_STR, cuint32_STR, cint_STR,
  74. csint_STR, cuint_STR,
  75. cint64_STR, cuint64_STR,
  76. clonglong_STR, cslonglong_STR, culonglong_STR,
  77. cbool_STR,
  78. clong_STR, cslong_STR, culong_STR);
  79. var
  80. hp,ph : presobject;
  81. implemfile : text; (* file for implementation headers extern procs *)
  82. IsExtern : boolean;
  83. NeedEllipsisOverload : boolean;
  84. must_write_packed_field : boolean;
  85. tempfile : text;
  86. No_pop : boolean;
  87. s,TN,PN : String;
  88. pointerprefix: boolean;
  89. freedynlibproc,
  90. loaddynlibproc : tstringlist;
  91. (* $ define yydebug
  92. compile with -dYYDEBUG to get debugging info *)
  93. const
  94. (* number of a?b:c construction in one define *)
  95. if_nb : longint = 0;
  96. is_packed : boolean = false;
  97. is_procvar : boolean = false;
  98. var space_array : array [0..255] of byte;
  99. space_index : byte;
  100. { Used when PPointers is used - pointer type definitions }
  101. PTypeList : TStringList;
  102. procedure shift(space_number : byte);
  103. var
  104. i : byte;
  105. begin
  106. space_array[space_index]:=space_number;
  107. inc(space_index);
  108. for i:=1 to space_number do
  109. aktspace:=aktspace+' ';
  110. end;
  111. procedure popshift;
  112. begin
  113. dec(space_index);
  114. if space_index<0 then
  115. internalerror(20);
  116. delete(aktspace,1,space_array[space_index]);
  117. end;
  118. function str(i : longint) : string;
  119. var
  120. s : string;
  121. begin
  122. system.str(i,s);
  123. str:=s;
  124. end;
  125. function hexstr(i : cardinal) : string;
  126. const
  127. HexTbl : array[0..15] of char='0123456789ABCDEF';
  128. var
  129. str : string;
  130. begin
  131. str:='';
  132. while i<>0 do
  133. begin
  134. str:=hextbl[i and $F]+str;
  135. i:=i shr 4;
  136. end;
  137. if str='' then str:='0';
  138. hexstr:='$'+str;
  139. end;
  140. function uppercase(s : string) : string;
  141. var
  142. i : byte;
  143. begin
  144. for i:=1 to length(s) do
  145. s[i]:=UpCase(s[i]);
  146. uppercase:=s;
  147. end;
  148. procedure write_type_specifier(var outfile:text; p : presobject);forward;
  149. procedure write_p_a_def(var outfile:text; p,simple_type : presobject);forward;
  150. procedure write_ifexpr(var outfile:text; p : presobject);forward;
  151. procedure write_funexpr(var outfile:text; p : presobject);forward;
  152. procedure yymsg(const msg : string);
  153. begin
  154. writeln('line ',line_no,': ',msg);
  155. end;
  156. { This converts pascal reserved words to
  157. the correct syntax.
  158. }
  159. function FixId(const s:string):string;
  160. const
  161. maxtokens = 14;
  162. reservedid: array[1..maxtokens] of string[14] =
  163. (
  164. 'CLASS',
  165. 'DISPOSE',
  166. 'FUNCTION',
  167. 'FALSE',
  168. 'LABEL',
  169. 'NEW',
  170. 'PROPERTY',
  171. 'PROCEDURE',
  172. 'RECORD',
  173. 'REPEAT',
  174. 'STRING',
  175. 'TYPE',
  176. 'TRUE',
  177. 'UNTIL'
  178. );
  179. var
  180. b : boolean;
  181. up : string;
  182. i: integer;
  183. begin
  184. if s='' then
  185. begin
  186. FixId:='';
  187. exit;
  188. end;
  189. b:=false;
  190. up:=Uppercase(s);
  191. for i:=1 to maxtokens do
  192. begin
  193. if up=reservedid[i] then
  194. begin
  195. b:=true;
  196. break;
  197. end;
  198. end;
  199. if b then
  200. FixId:='_'+s
  201. else
  202. FixId:=s;
  203. end;
  204. function TypeName(const s:string):string;
  205. var
  206. i : longint;
  207. begin
  208. i:=1;
  209. if RemoveUnderScore and (length(s)>1) and (s[1]='_') then
  210. i:=2;
  211. if PrependTypes then
  212. TypeName:='T'+Copy(s,i,255)
  213. else
  214. TypeName:=Copy(s,i,255);
  215. end;
  216. function IsACType(const s : String) : Boolean;
  217. var i : Integer;
  218. begin
  219. IsACType := True;
  220. for i := 0 to MAX_CTYPESARRAY do
  221. begin
  222. if s = CTypesArray[i] then
  223. begin
  224. Exit;
  225. end;
  226. end;
  227. IsACType := False;
  228. end;
  229. function PointerName(const s:string):string;
  230. var
  231. i : longint;
  232. begin
  233. if UseCTypesUnit then
  234. begin
  235. if IsACType(s) then
  236. begin
  237. PointerName := 'p'+s;
  238. exit;
  239. end;
  240. end;
  241. i:=1;
  242. if RemoveUnderScore and (length(s)>1) and (s[1]='_') then
  243. i:=2;
  244. if UsePPointers then
  245. begin
  246. PointerName:='P'+Copy(s,i,255);
  247. PTypeList.Add(PointerName);
  248. end
  249. else
  250. PointerName:=Copy(s,i,255);
  251. if PointerPrefix then
  252. PTypeList.Add('P'+s);
  253. end;
  254. procedure write_packed_fields_info(var outfile:text; p : presobject; ph : string);
  255. var
  256. hp1,hp2,hp3 : presobject;
  257. is_sized : boolean;
  258. line : string;
  259. flag_index : longint;
  260. name : pchar;
  261. ps : byte;
  262. begin
  263. { write out the tempfile created }
  264. close(tempfile);
  265. reset(tempfile);
  266. is_sized:=false;
  267. flag_index:=0;
  268. writeln(outfile);
  269. writeln(outfile,aktspace,'const');
  270. shift(3);
  271. while not eof(tempfile) do
  272. begin
  273. readln(tempfile,line);
  274. ps:=pos('&',line);
  275. if ps>0 then
  276. line:=copy(line,1,ps-1)+ph+'_'+copy(line,ps+1,255);
  277. writeln(outfile,aktspace,line);
  278. end;
  279. writeln(outfile);
  280. close(tempfile);
  281. rewrite(tempfile);
  282. popshift;
  283. (* walk through all members *)
  284. hp1 := p^.p1;
  285. while assigned(hp1) do
  286. begin
  287. (* hp2 is t_memberdec *)
  288. hp2:=hp1^.p1;
  289. (* hp3 is t_declist *)
  290. hp3:=hp2^.p2;
  291. while assigned(hp3) do
  292. begin
  293. if assigned(hp3^.p1^.p3) and
  294. (hp3^.p1^.p3^.typ = t_size_specifier) then
  295. begin
  296. is_sized:=true;
  297. name:=hp3^.p1^.p2^.p;
  298. { get function in interface }
  299. write(outfile,aktspace,'function ',name);
  300. write(outfile,'(var a : ',ph,') : ');
  301. shift(2);
  302. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  303. writeln(outfile,';');
  304. popshift;
  305. { get function in implementation }
  306. write(implemfile,aktspace,'function ',name);
  307. write(implemfile,'(var a : ',ph,') : ');
  308. if not compactmode then
  309. shift(2);
  310. write_p_a_def(implemfile,hp3^.p1^.p1,hp2^.p1);
  311. writeln(implemfile,';');
  312. writeln(implemfile,aktspace,'begin');
  313. shift(3);
  314. write(implemfile,aktspace,name,':=(a.flag',flag_index);
  315. writeln(implemfile,' and bm_',ph,'_',name,') shr bp_',ph,'_',name,';');
  316. popshift;
  317. writeln(implemfile,aktspace,'end;');
  318. if not compactmode then
  319. popshift;
  320. writeln(implemfile,'');
  321. { set function in interface }
  322. write(outfile,aktspace,'procedure set_',name);
  323. write(outfile,'(var a : ',ph,'; __',name,' : ');
  324. shift(2);
  325. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  326. writeln(outfile,');');
  327. popshift;
  328. { set function in implementation }
  329. write(implemfile,aktspace,'procedure set_',name);
  330. write(implemfile,'(var a : ',ph,'; __',name,' : ');
  331. if not compactmode then
  332. shift(2);
  333. write_p_a_def(implemfile,hp3^.p1^.p1,hp2^.p1);
  334. writeln(implemfile,');');
  335. writeln(implemfile,aktspace,'begin');
  336. shift(3);
  337. write(implemfile,aktspace,'a.flag',flag_index,':=');
  338. write(implemfile,'a.flag',flag_index,' or ');
  339. writeln(implemfile,'((__',name,' shl bp_',ph,'_',name,') and bm_',ph,'_',name,');');
  340. popshift;
  341. writeln(implemfile,aktspace,'end;');
  342. if not compactmode then
  343. popshift;
  344. writeln(implemfile,'');
  345. end
  346. else if is_sized then
  347. begin
  348. is_sized:=false;
  349. inc(flag_index);
  350. end;
  351. hp3:=hp3^.next;
  352. end;
  353. hp1:=hp1^.next;
  354. end;
  355. must_write_packed_field:=false;
  356. block_type:=bt_no;
  357. end;
  358. procedure write_expr(var outfile:text; p : presobject);
  359. begin
  360. if assigned(p) then
  361. begin
  362. case p^.typ of
  363. t_id,
  364. t_ifexpr :
  365. write(outfile,FixId(p^.p));
  366. t_funexprlist :
  367. write_funexpr(outfile,p);
  368. t_exprlist :
  369. begin
  370. if assigned(p^.p1) then
  371. write_expr(outfile,p^.p1);
  372. if assigned(p^.next) then
  373. begin
  374. write(', ');
  375. write_expr(outfile,p^.next);
  376. end;
  377. end;
  378. t_preop : begin
  379. write(outfile,p^.p,'(');
  380. write_expr(outfile,p^.p1);
  381. write(outfile,')');
  382. flush(outfile);
  383. end;
  384. t_typespec : begin
  385. write_type_specifier(outfile,p^.p1);
  386. write(outfile,'(');
  387. write_expr(outfile,p^.p2);
  388. write(outfile,')');
  389. flush(outfile);
  390. end;
  391. t_bop : begin
  392. if p^.p1^.typ<>t_id then
  393. write(outfile,'(');
  394. write_expr(outfile,p^.p1);
  395. if p^.p1^.typ<>t_id then
  396. write(outfile,')');
  397. write(outfile,p^.p);
  398. if p^.p2^.typ<>t_id then
  399. write(outfile,'(');
  400. write_expr(outfile,p^.p2);
  401. if p^.p2^.typ<>t_id then
  402. write(outfile,')');
  403. flush(outfile);
  404. end;
  405. t_arrayop :
  406. begin
  407. write_expr(outfile,p^.p1);
  408. write(outfile,p^.p,'[');
  409. write_expr(outfile,p^.p2);
  410. write(outfile,']');
  411. flush(outfile);
  412. end;
  413. t_callop :
  414. begin
  415. write_expr(outfile,p^.p1);
  416. write(outfile,p^.p,'(');
  417. write_expr(outfile,p^.p2);
  418. write(outfile,')');
  419. flush(outfile);
  420. end;
  421. else
  422. begin
  423. writeln(ord(p^.typ));
  424. internalerror(2);
  425. end;
  426. end;
  427. end;
  428. end;
  429. procedure write_ifexpr(var outfile:text; p : presobject);
  430. begin
  431. flush(outfile);
  432. write(outfile,'if ');
  433. write_expr(outfile,p^.p1);
  434. writeln(outfile,' then');
  435. write(outfile,aktspace,' ');
  436. write(outfile,p^.p);
  437. write(outfile,':=');
  438. write_expr(outfile,p^.p2);
  439. writeln(outfile);
  440. writeln(outfile,aktspace,'else');
  441. write(outfile,aktspace,' ');
  442. write(outfile,p^.p);
  443. write(outfile,':=');
  444. write_expr(outfile,p^.p3);
  445. writeln(outfile,';');
  446. write(outfile,aktspace);
  447. flush(outfile);
  448. end;
  449. procedure write_all_ifexpr(var outfile:text; p : presobject);
  450. begin
  451. if assigned(p) then
  452. begin
  453. case p^.typ of
  454. t_id :;
  455. t_preop :
  456. write_all_ifexpr(outfile,p^.p1);
  457. t_callop,
  458. t_arrayop,
  459. t_bop :
  460. begin
  461. write_all_ifexpr(outfile,p^.p1);
  462. write_all_ifexpr(outfile,p^.p2);
  463. end;
  464. t_ifexpr :
  465. begin
  466. write_all_ifexpr(outfile,p^.p1);
  467. write_all_ifexpr(outfile,p^.p2);
  468. write_all_ifexpr(outfile,p^.p3);
  469. write_ifexpr(outfile,p);
  470. end;
  471. t_typespec :
  472. write_all_ifexpr(outfile,p^.p2);
  473. t_funexprlist,
  474. t_exprlist :
  475. begin
  476. if assigned(p^.p1) then
  477. write_all_ifexpr(outfile,p^.p1);
  478. if assigned(p^.next) then
  479. write_all_ifexpr(outfile,p^.next);
  480. end
  481. else
  482. internalerror(6);
  483. end;
  484. end;
  485. end;
  486. procedure write_funexpr(var outfile:text; p : presobject);
  487. var
  488. i : longint;
  489. begin
  490. if assigned(p) then
  491. begin
  492. case p^.typ of
  493. t_ifexpr :
  494. write(outfile,p^.p);
  495. t_exprlist :
  496. begin
  497. write_expr(outfile,p^.p1);
  498. if assigned(p^.next) then
  499. begin
  500. write(outfile,',');
  501. write_funexpr(outfile,p^.next);
  502. end
  503. end;
  504. t_funcname :
  505. begin
  506. if not compactmode then
  507. shift(2);
  508. if if_nb>0 then
  509. begin
  510. writeln(outfile,aktspace,'var');
  511. write(outfile,aktspace,' ');
  512. for i:=1 to if_nb do
  513. begin
  514. write(outfile,'if_local',i);
  515. if i<if_nb then
  516. write(outfile,', ')
  517. else
  518. writeln(outfile,' : longint;');
  519. end;
  520. writeln(outfile,aktspace,'(* result types are not known *)');
  521. if_nb:=0;
  522. end;
  523. writeln(outfile,aktspace,'begin');
  524. shift(3);
  525. write(outfile,aktspace);
  526. write_all_ifexpr(outfile,p^.p2);
  527. write_expr(outfile,p^.p1);
  528. write(outfile,':=');
  529. write_funexpr(outfile,p^.p2);
  530. writeln(outfile,';');
  531. popshift;
  532. writeln(outfile,aktspace,'end;');
  533. if not compactmode then
  534. popshift;
  535. flush(outfile);
  536. end;
  537. t_funexprlist :
  538. begin
  539. if assigned(p^.p3) then
  540. begin
  541. write_type_specifier(outfile,p^.p3);
  542. write(outfile,'(');
  543. end;
  544. if assigned(p^.p1) then
  545. write_funexpr(outfile,p^.p1);
  546. if assigned(p^.p2) then
  547. begin
  548. write(outfile,'(');
  549. write_funexpr(outfile,p^.p2);
  550. write(outfile,')');
  551. end;
  552. if assigned(p^.p3) then
  553. write(outfile,')');
  554. end
  555. else internalerror(5);
  556. end;
  557. end;
  558. end;
  559. function ellipsisarg : presobject;
  560. begin
  561. ellipsisarg:=new(presobject,init_two(t_arg,nil,nil));
  562. end;
  563. const
  564. (* if in args *dname is replaced by pdname *)
  565. in_args : boolean = false;
  566. typedef_level : longint = 0;
  567. (* writes an argument list, where p is t_arglist *)
  568. procedure write_args(var outfile:text; p : presobject);
  569. var
  570. len,para : longint;
  571. old_in_args : boolean;
  572. varpara : boolean;
  573. lastp : presobject;
  574. hs : string;
  575. begin
  576. NeedEllipsisOverload:=false;
  577. para:=1;
  578. len:=0;
  579. lastp:=nil;
  580. old_in_args:=in_args;
  581. in_args:=true;
  582. write(outfile,'(');
  583. shift(2);
  584. (* walk through all arguments *)
  585. (* p must be of type t_arglist *)
  586. while assigned(p) do
  587. begin
  588. if p^.typ<>t_arglist then
  589. internalerror(10);
  590. (* is ellipsis ? *)
  591. if not assigned(p^.p1^.p1) and
  592. not assigned(p^.p1^.next) then
  593. begin
  594. write(outfile,'args:array of const');
  595. (* if variable number of args we must allways pop *)
  596. no_pop:=false;
  597. (* Needs 2 declarations, also one without args, becuase
  598. in C you can omit the second parameter. Default parameter
  599. doesn't help as that isn't possible with array of const *)
  600. NeedEllipsisOverload:=true;
  601. (* Remove this para *)
  602. if assigned(lastp) then
  603. lastp^.next:=nil;
  604. dispose(p,done);
  605. (* leave the loop as p isnot valid anymore *)
  606. break;
  607. end
  608. (* we need to correct this in the pp file after *)
  609. else
  610. begin
  611. (* generate a call by reference parameter ? *)
  612. // varpara:=usevarparas and
  613. // assigned(p^.p1^.p2^.p1) and
  614. // (p^.p1^.p2^.p1^.typ in [t_addrdef,t_pointerdef]) and
  615. // assigned(p^.p1^.p2^.p1^.p1) and
  616. // (p^.p1^.p2^.p1^.p1^.typ<>t_procdef);
  617. varpara:=usevarparas and
  618. assigned(p^.p1^.p1) and
  619. (p^.p1^.p1^.typ in [t_addrdef,t_pointerdef]) and
  620. assigned(p^.p1^.p1^.p1) and
  621. (p^.p1^.p1^.p1^.typ<>t_procdef);
  622. (* do not do it for char pointer !! *)
  623. (* para : pchar; and var para : char; are *)
  624. (* completely different in pascal *)
  625. (* here we exclude all typename containing char *)
  626. (* is this a good method ?? *)
  627. if varpara and
  628. (p^.p1^.p1^.typ=t_pointerdef) and
  629. (p^.p1^.p1^.p1^.typ=t_id) and
  630. (pos('CHAR',uppercase(p^.p1^.p1^.p1^.str))<>0) then
  631. varpara:=false;
  632. if varpara then
  633. begin
  634. write(outfile,'var ');
  635. inc(len,4);
  636. end;
  637. (* write new parameter name *)
  638. if assigned(p^.p1^.p2^.p2) then
  639. begin
  640. hs:=FixId(p^.p1^.p2^.p2^.p);
  641. write(outfile,hs);
  642. inc(len,length(hs));
  643. end
  644. else
  645. begin
  646. If removeUnderscore then
  647. begin
  648. Write (outfile,'para',para);
  649. inc(Len,5);
  650. end
  651. else
  652. begin
  653. write(outfile,'_para',para);
  654. inc(Len,6);
  655. end;
  656. end;
  657. write(outfile,':');
  658. if varpara then
  659. begin
  660. write_p_a_def(outfile,p^.p1^.p2^.p1,p^.p1^.p1^.p1);
  661. end
  662. else
  663. write_p_a_def(outfile,p^.p1^.p2^.p1,p^.p1^.p1);
  664. end;
  665. lastp:=p;
  666. p:=p^.next;
  667. if assigned(p) then
  668. begin
  669. write(outfile,'; ');
  670. { if len>40 then : too complicated to compute }
  671. if (para mod 5) = 0 then
  672. begin
  673. writeln(outfile);
  674. write(outfile,aktspace);
  675. end;
  676. end;
  677. inc(para);
  678. end;
  679. write(outfile,')');
  680. flush(outfile);
  681. in_args:=old_in_args;
  682. popshift;
  683. end;
  684. procedure write_p_a_def(var outfile:text; p,simple_type : presobject);
  685. var
  686. i : longint;
  687. error : integer;
  688. pointerwritten,
  689. constant : boolean;
  690. begin
  691. if not(assigned(p)) then
  692. begin
  693. write_type_specifier(outfile,simple_type);
  694. exit;
  695. end;
  696. case p^.typ of
  697. t_pointerdef : begin
  698. (* procedure variable ? *)
  699. if assigned(p^.p1) and (p^.p1^.typ=t_procdef) then
  700. begin
  701. is_procvar:=true;
  702. (* distinguish between procedure and function *)
  703. if (simple_type^.typ=t_void) and (p^.p1^.p1=nil) then
  704. begin
  705. write(outfile,'procedure ');
  706. shift(10);
  707. (* write arguments *)
  708. if assigned(p^.p1^.p2) then
  709. write_args(outfile,p^.p1^.p2);
  710. flush(outfile);
  711. popshift;
  712. end
  713. else
  714. begin
  715. write(outfile,'function ');
  716. shift(9);
  717. (* write arguments *)
  718. if assigned(p^.p1^.p2) then
  719. write_args(outfile,p^.p1^.p2);
  720. write(outfile,':');
  721. flush(outfile);
  722. write_p_a_def(outfile,p^.p1^.p1,simple_type);
  723. popshift;
  724. end
  725. end
  726. else
  727. begin
  728. (* generate "pointer" ? *)
  729. if (simple_type^.typ=t_void) and (p^.p1=nil) then
  730. begin
  731. write(outfile,'pointer');
  732. flush(outfile);
  733. end
  734. else
  735. begin
  736. pointerwritten:=false;
  737. if (p^.p1=nil) and UsePPointers then
  738. begin
  739. if (simple_type^.typ=t_id) then
  740. begin
  741. write(outfile,PointerName(simple_type^.p));
  742. pointerwritten:=true;
  743. end
  744. { structure }
  745. else if (simple_type^.typ in [t_uniondef,t_structdef]) and
  746. (simple_type^.p1=nil) and (simple_type^.p2^.typ=t_id) then
  747. begin
  748. write(outfile,PointerName(simple_type^.p2^.p));
  749. pointerwritten:=true;
  750. end;
  751. end;
  752. if not pointerwritten then
  753. begin
  754. if in_args then
  755. begin
  756. write(outfile,'P');
  757. pointerprefix:=true;
  758. end
  759. else
  760. write(outfile,'^');
  761. write_p_a_def(outfile,p^.p1,simple_type);
  762. pointerprefix:=false;
  763. end;
  764. end;
  765. end;
  766. end;
  767. t_arraydef : begin
  768. constant:=false;
  769. if assigned(p^.p2) then
  770. begin
  771. if p^.p2^.typ=t_id then
  772. begin
  773. val(p^.p2^.str,i,error);
  774. if error=0 then
  775. begin
  776. dec(i);
  777. constant:=true;
  778. end;
  779. end;
  780. if not constant then
  781. begin
  782. write(outfile,'array[0..(');
  783. write_expr(outfile,p^.p2);
  784. write(outfile,')-1] of ');
  785. end
  786. else
  787. begin
  788. write(outfile,'array[0..',i,'] of ');
  789. end;
  790. end
  791. else
  792. begin
  793. (* open array *)
  794. write(outfile,'array of ');
  795. end;
  796. flush(outfile);
  797. write_p_a_def(outfile,p^.p1,simple_type);
  798. end;
  799. else internalerror(1);
  800. end;
  801. end;
  802. procedure write_type_specifier(var outfile:text; p : presobject);
  803. var
  804. hp1,hp2,hp3,lastexpr : presobject;
  805. i,l,w : longint;
  806. error : integer;
  807. current_power,
  808. mask : cardinal;
  809. flag_index : longint;
  810. current_level : byte;
  811. pointerwritten,
  812. is_sized : boolean;
  813. begin
  814. case p^.typ of
  815. t_id :
  816. begin
  817. if pointerprefix then
  818. if UseCtypesUnit then
  819. begin
  820. if not IsACType(p^.p) then
  821. begin
  822. PTypeList.Add('P'+p^.str);
  823. end;
  824. end
  825. else
  826. PTypeList.Add('P'+p^.str);
  827. if p^.intname then
  828. write(outfile,p^.p)
  829. else
  830. write(outfile,TypeName(p^.p));
  831. end;
  832. { what can we do with void defs ? }
  833. t_void :
  834. write(outfile,'void');
  835. t_pointerdef :
  836. begin
  837. pointerwritten:=false;
  838. if (p^.p1^.typ=t_void) then
  839. begin
  840. write(outfile,'pointer');
  841. pointerwritten:=true;
  842. end
  843. else
  844. if UsePPointers then
  845. begin
  846. if (p^.p1^.typ=t_id) then
  847. begin
  848. write(outfile,PointerName(p^.p1^.p));
  849. pointerwritten:=true;
  850. end
  851. { structure }
  852. else if (p^.p1^.typ in [t_uniondef,t_structdef]) and
  853. (p^.p1^.p1=nil) and (p^.p1^.p2^.typ=t_id) then
  854. begin
  855. write(outfile,PointerName(p^.p1^.p2^.p));
  856. pointerwritten:=true;
  857. end;
  858. end;
  859. if not pointerwritten then
  860. begin
  861. if in_args then
  862. begin
  863. if UseCTypesUnit and (IsACType(p^.p1^.p)=False) then
  864. write(outfile,'P')
  865. else
  866. write(outfile,'p');
  867. pointerprefix:=true;
  868. end
  869. else
  870. begin
  871. if UseCTypesUnit and (IsACType(p^.p1^.p)=False) then
  872. write(outfile,'^')
  873. else
  874. write(outfile,'p');
  875. end;
  876. write_type_specifier(outfile,p^.p1);
  877. pointerprefix:=false;
  878. end;
  879. end;
  880. t_enumdef :
  881. begin
  882. if (typedef_level>1) and (p^.p1=nil) and
  883. (p^.p2^.typ=t_id) then
  884. begin
  885. if pointerprefix then
  886. if UseCTypesUnit and (IsACType( p^.p2^.p )=False) then
  887. PTypeList.Add('P'+p^.p2^.str);
  888. write(outfile,p^.p2^.p);
  889. end
  890. else
  891. if not EnumToConst then
  892. begin
  893. write(outfile,'(');
  894. hp1:=p^.p1;
  895. w:=length(aktspace);
  896. while assigned(hp1) do
  897. begin
  898. write(outfile,hp1^.p1^.p);
  899. if assigned(hp1^.p2) then
  900. begin
  901. write(outfile,' := ');
  902. write_expr(outfile,hp1^.p2);
  903. w:=w+6;(* strlen(hp1^.p); *)
  904. end;
  905. w:=w+length(hp1^.p1^.str);
  906. hp1:=hp1^.next;
  907. if assigned(hp1) then
  908. write(outfile,',');
  909. if w>40 then
  910. begin
  911. writeln(outfile);
  912. write(outfile,aktspace);
  913. w:=length(aktspace);
  914. end;
  915. flush(outfile);
  916. end;
  917. write(outfile,')');
  918. flush(outfile);
  919. end
  920. else
  921. begin
  922. Writeln (outfile,' Longint;');
  923. hp1:=p^.p1;
  924. l:=0;
  925. lastexpr:=nil;
  926. Writeln (outfile,copy(aktspace,1,length(aktspace)-2),'Const');
  927. while assigned(hp1) do
  928. begin
  929. write (outfile,aktspace,hp1^.p1^.p,' = ');
  930. if assigned(hp1^.p2) then
  931. begin
  932. write_expr(outfile,hp1^.p2);
  933. writeln(outfile,';');
  934. lastexpr:=hp1^.p2;
  935. if lastexpr^.typ=t_id then
  936. begin
  937. val(lastexpr^.str,l,error);
  938. if error=0 then
  939. begin
  940. inc(l);
  941. lastexpr:=nil;
  942. end
  943. else
  944. l:=1;
  945. end
  946. else
  947. l:=1;
  948. end
  949. else
  950. begin
  951. if assigned(lastexpr) then
  952. begin
  953. write(outfile,'(');
  954. write_expr(outfile,lastexpr);
  955. writeln(outfile,')+',l,';');
  956. end
  957. else
  958. writeln (outfile,l,';');
  959. inc(l);
  960. end;
  961. hp1:=hp1^.next;
  962. flush(outfile);
  963. end;
  964. block_type:=bt_const;
  965. end;
  966. end;
  967. t_structdef :
  968. begin
  969. inc(typedef_level);
  970. flag_index:=-1;
  971. is_sized:=false;
  972. current_level:=0;
  973. if ((in_args) or (typedef_level>1)) and
  974. (p^.p1=nil) and (p^.p2^.typ=t_id) then
  975. begin
  976. if pointerprefix then
  977. if UseCTypesUnit and (IsACType(p^.p2^.str)=false) then
  978. PTypeList.Add('P'+p^.p2^.str);
  979. write(outfile,TypeName(p^.p2^.p));
  980. end
  981. else
  982. begin
  983. if packrecords then
  984. writeln(outfile,'packed record')
  985. else
  986. writeln(outfile,'record');
  987. shift(3);
  988. hp1:=p^.p1;
  989. (* walk through all members *)
  990. while assigned(hp1) do
  991. begin
  992. (* hp2 is t_memberdec *)
  993. hp2:=hp1^.p1;
  994. (* hp3 is t_declist *)
  995. hp3:=hp2^.p2;
  996. while assigned(hp3) do
  997. begin
  998. if not assigned(hp3^.p1^.p3) or
  999. (hp3^.p1^.p3^.typ <> t_size_specifier) then
  1000. begin
  1001. if is_sized then
  1002. begin
  1003. if current_level <= 16 then
  1004. writeln(outfile,'word;')
  1005. else if current_level <= 32 then
  1006. writeln(outfile,'longint;')
  1007. else
  1008. internalerror(11);
  1009. is_sized:=false;
  1010. end;
  1011. write(outfile,aktspace,FixId(hp3^.p1^.p2^.p));
  1012. write(outfile,' : ');
  1013. shift(2);
  1014. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  1015. popshift;
  1016. end;
  1017. { size specifier or default value ? }
  1018. if assigned(hp3^.p1^.p3) then
  1019. begin
  1020. { we could use mask to implement this }
  1021. { because we need to respect the positions }
  1022. if hp3^.p1^.p3^.typ = t_size_specifier then
  1023. begin
  1024. if not is_sized then
  1025. begin
  1026. current_power:=1;
  1027. current_level:=0;
  1028. inc(flag_index);
  1029. write(outfile,aktspace,'flag',flag_index,' : ');
  1030. end;
  1031. must_write_packed_field:=true;
  1032. is_sized:=true;
  1033. { can it be something else than a constant ? }
  1034. { it can be a macro !! }
  1035. if hp3^.p1^.p3^.p1^.typ=t_id then
  1036. begin
  1037. val(hp3^.p1^.p3^.p1^.str,l,error);
  1038. if error=0 then
  1039. begin
  1040. mask:=0;
  1041. for i:=1 to l do
  1042. begin
  1043. inc(mask,current_power);
  1044. current_power:=current_power*2;
  1045. end;
  1046. write(tempfile,'bm_&',hp3^.p1^.p2^.p);
  1047. writeln(tempfile,' = ',hexstr(mask),';');
  1048. write(tempfile,'bp_&',hp3^.p1^.p2^.p);
  1049. writeln(tempfile,' = ',current_level,';');
  1050. current_level:=current_level + l;
  1051. { go to next flag if 31 }
  1052. if current_level = 32 then
  1053. begin
  1054. write(outfile,'longint');
  1055. is_sized:=false;
  1056. end;
  1057. end;
  1058. end;
  1059. end
  1060. else if hp3^.p1^.p3^.typ = t_default_value then
  1061. begin
  1062. write(outfile,'{=');
  1063. write_expr(outfile,hp3^.p1^.p3^.p1);
  1064. write(outfile,' ignored}');
  1065. end;
  1066. end;
  1067. if not is_sized then
  1068. begin
  1069. if is_procvar then
  1070. begin
  1071. if not no_pop then
  1072. begin
  1073. write(outfile,';cdecl');
  1074. no_pop:=true;
  1075. end;
  1076. is_procvar:=false;
  1077. end;
  1078. writeln(outfile,';');
  1079. end;
  1080. hp3:=hp3^.next;
  1081. end;
  1082. hp1:=hp1^.next;
  1083. end;
  1084. if is_sized then
  1085. begin
  1086. if current_level <= 16 then
  1087. writeln(outfile,'word;')
  1088. else if current_level <= 32 then
  1089. writeln(outfile,'longint;')
  1090. else
  1091. internalerror(11);
  1092. is_sized:=false;
  1093. end;
  1094. popshift;
  1095. write(outfile,aktspace,'end');
  1096. flush(outfile);
  1097. end;
  1098. dec(typedef_level);
  1099. end;
  1100. t_uniondef :
  1101. begin
  1102. inc(typedef_level);
  1103. if (typedef_level>1) and (p^.p1=nil) and
  1104. (p^.p2^.typ=t_id) then
  1105. begin
  1106. write(outfile,p^.p2^.p);
  1107. end
  1108. else
  1109. begin
  1110. inc(typedef_level);
  1111. if packrecords then
  1112. writeln(outfile,'packed record')
  1113. else
  1114. writeln(outfile,'record');
  1115. shift(2);
  1116. writeln(outfile,aktspace,'case longint of');
  1117. shift(3);
  1118. l:=0;
  1119. hp1:=p^.p1;
  1120. (* walk through all members *)
  1121. while assigned(hp1) do
  1122. begin
  1123. (* hp2 is t_memberdec *)
  1124. hp2:=hp1^.p1;
  1125. (* hp3 is t_declist *)
  1126. hp3:=hp2^.p2;
  1127. while assigned(hp3) do
  1128. begin
  1129. write(outfile,aktspace,l,' : ( ');
  1130. write(outfile,FixId(hp3^.p1^.p2^.p),' : ');
  1131. shift(2);
  1132. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  1133. popshift;
  1134. writeln(outfile,' );');
  1135. hp3:=hp3^.next;
  1136. inc(l);
  1137. end;
  1138. hp1:=hp1^.next;
  1139. end;
  1140. popshift;
  1141. write(outfile,aktspace,'end');
  1142. popshift;
  1143. flush(outfile);
  1144. dec(typedef_level);
  1145. end;
  1146. dec(typedef_level);
  1147. end;
  1148. else
  1149. internalerror(3);
  1150. end;
  1151. end;
  1152. procedure write_def_params(var outfile:text; p : presobject);
  1153. var
  1154. hp1 : presobject;
  1155. begin
  1156. case p^.typ of
  1157. t_enumdef : begin
  1158. hp1:=p^.p1;
  1159. while assigned(hp1) do
  1160. begin
  1161. write(outfile,FixId(hp1^.p1^.p));
  1162. hp1:=hp1^.next;
  1163. if assigned(hp1) then
  1164. write(outfile,',')
  1165. else
  1166. write(outfile);
  1167. flush(outfile);
  1168. end;
  1169. flush(outfile);
  1170. end;
  1171. else internalerror(4);
  1172. end;
  1173. end;
  1174. procedure write_statement_block(var outfile:text; p : presobject);
  1175. begin
  1176. writeln(outfile,aktspace,'begin');
  1177. while assigned(p) do
  1178. begin
  1179. shift(2);
  1180. if assigned(p^.p1) then
  1181. begin
  1182. case p^.p1^.typ of
  1183. t_whilenode:
  1184. begin
  1185. write(outfile,aktspace,'while ');
  1186. write_expr(outfile,p^.p1^.p1);
  1187. writeln(outfile,' do');
  1188. shift(2);
  1189. write_statement_block(outfile,p^.p1^.p2);
  1190. popshift;
  1191. end;
  1192. else
  1193. begin
  1194. write(outfile,aktspace);
  1195. write_expr(outfile,p^.p1);
  1196. writeln(outfile,';');
  1197. end;
  1198. end;
  1199. end;
  1200. p:=p^.next;
  1201. popshift;
  1202. end;
  1203. writeln(outfile,aktspace,'end;');
  1204. end;
  1205. %}
  1206. %token _WHILE _FOR _DO _GOTO _CONTINUE _BREAK
  1207. %token TYPEDEF DEFINE
  1208. %token COLON SEMICOLON COMMA
  1209. %token LKLAMMER RKLAMMER LECKKLAMMER RECKKLAMMER
  1210. %token LGKLAMMER RGKLAMMER
  1211. %token STRUCT UNION ENUM
  1212. %token ID NUMBER CSTRING
  1213. %token SHORT UNSIGNED LONG INT REAL _CHAR
  1214. %token VOID _CONST
  1215. %token _FAR _HUGE _NEAR
  1216. %token NEW_LINE SPACE_DEFINE
  1217. %token EXTERN STDCALL CDECL CALLBACK PASCAL WINAPI APIENTRY WINGDIAPI SYS_TRAP
  1218. %token _PACKED
  1219. %token ELLIPSIS
  1220. %right _ASSIGN
  1221. %right R_AND
  1222. %left EQUAL UNEQUAL GT LT GTE LTE
  1223. %left QUESTIONMARK COLON
  1224. %left _OR
  1225. %left _AND
  1226. %left _PLUS MINUS
  1227. %left _SHR _SHL
  1228. %left STAR _SLASH
  1229. %right _NOT
  1230. %right LKLAMMER
  1231. %right PSTAR
  1232. %right P_AND
  1233. %right LECKKLAMMER
  1234. %left POINT DEREF
  1235. %left COMMA
  1236. %left STICK
  1237. %token SIGNED
  1238. %%
  1239. file : declaration_list
  1240. ;
  1241. maybe_space :
  1242. SPACE_DEFINE
  1243. {
  1244. $$:=nil;
  1245. } |
  1246. {
  1247. $$:=nil;
  1248. }
  1249. ;
  1250. error_info : {
  1251. writeln(outfile,'(* error ');
  1252. writeln(outfile,yyline);
  1253. };
  1254. declaration_list : declaration_list declaration
  1255. { if yydebug then writeln('declaration reduced at line ',line_no);
  1256. if yydebug then writeln(outfile,'(* declaration reduced *)');
  1257. }
  1258. | declaration_list define_dec
  1259. { if yydebug then writeln('define declaration reduced at line ',line_no);
  1260. if yydebug then writeln(outfile,'(* define declaration reduced *)');
  1261. }
  1262. | declaration
  1263. { if yydebug then writeln('declaration reduced at line ',line_no);
  1264. }
  1265. | define_dec
  1266. { if yydebug then writeln('define declaration reduced at line ',line_no);
  1267. }
  1268. ;
  1269. dec_specifier :
  1270. EXTERN { $$:=new(presobject,init_id('extern')); }
  1271. |{ $$:=new(presobject,init_id('intern')); }
  1272. ;
  1273. dec_modifier :
  1274. STDCALL { $$:=new(presobject,init_id('no_pop')); }
  1275. | CDECL { $$:=new(presobject,init_id('cdecl')); }
  1276. | CALLBACK { $$:=new(presobject,init_id('no_pop')); }
  1277. | PASCAL { $$:=new(presobject,init_id('no_pop')); }
  1278. | WINAPI { $$:=new(presobject,init_id('no_pop')); }
  1279. | APIENTRY { $$:=new(presobject,init_id('no_pop')); }
  1280. | WINGDIAPI { $$:=new(presobject,init_id('no_pop')); }
  1281. | { $$:=nil }
  1282. ;
  1283. systrap_specifier:
  1284. SYS_TRAP LKLAMMER dname RKLAMMER { $$:=$3; }
  1285. | { $$:=nil; }
  1286. ;
  1287. statement :
  1288. expr SEMICOLON { $$:=$1; } |
  1289. _WHILE LKLAMMER expr RKLAMMER statement_list { $$:=new(presobject,init_two(t_whilenode,$3,$5)); }
  1290. ;
  1291. statement_list : statement statement_list
  1292. {
  1293. $$:=new(presobject,init_one(t_statement_list,$1));
  1294. $$^.next:=$2;
  1295. } |
  1296. statement
  1297. {
  1298. $$:=new(presobject,init_one(t_statement_list,$1));
  1299. } |
  1300. SEMICOLON
  1301. {
  1302. $$:=new(presobject,init_one(t_statement_list,nil));
  1303. } |
  1304. {
  1305. $$:=new(presobject,init_one(t_statement_list,nil));
  1306. }
  1307. ;
  1308. statement_block :
  1309. LGKLAMMER statement_list RGKLAMMER { $$:=$2; }
  1310. ;
  1311. declaration :
  1312. dec_specifier type_specifier dec_modifier declarator_list statement_block
  1313. {
  1314. IsExtern:=false;
  1315. (* by default we must pop the args pushed on stack *)
  1316. no_pop:=false;
  1317. if (assigned($4)and assigned($4^.p1)and assigned($4^.p1^.p1))
  1318. and ($4^.p1^.p1^.typ=t_procdef) then
  1319. begin
  1320. repeat
  1321. If UseLib then
  1322. IsExtern:=true
  1323. else
  1324. IsExtern:=assigned($1)and($1^.str='extern');
  1325. no_pop:=assigned($3) and ($3^.str='no_pop');
  1326. if (block_type<>bt_func) and not(createdynlib) then
  1327. begin
  1328. writeln(outfile);
  1329. block_type:=bt_func;
  1330. end;
  1331. (* dyn. procedures must be put into a var block *)
  1332. if createdynlib then
  1333. begin
  1334. if (block_type<>bt_var) then
  1335. begin
  1336. if not(compactmode) then
  1337. writeln(outfile);
  1338. writeln(outfile,aktspace,'var');
  1339. block_type:=bt_var;
  1340. end;
  1341. shift(2);
  1342. end;
  1343. if not CompactMode then
  1344. begin
  1345. write(outfile,aktspace);
  1346. if not IsExtern then
  1347. write(implemfile,aktspace);
  1348. end;
  1349. (* distinguish between procedure and function *)
  1350. if assigned($2) then
  1351. if ($2^.typ=t_void) and ($4^.p1^.p1^.p1=nil) then
  1352. begin
  1353. if createdynlib then
  1354. begin
  1355. write(outfile,$4^.p1^.p2^.p,' : procedure');
  1356. end
  1357. else
  1358. begin
  1359. shift(10);
  1360. write(outfile,'procedure ',$4^.p1^.p2^.p);
  1361. end;
  1362. if assigned($4^.p1^.p1^.p2) then
  1363. write_args(outfile,$4^.p1^.p1^.p2);
  1364. if createdynlib then
  1365. begin
  1366. loaddynlibproc.add('pointer('+$4^.p1^.p2^.p+'):=GetProcAddress(hlib,'''+$4^.p1^.p2^.p+''');');
  1367. freedynlibproc.add($4^.p1^.p2^.p+':=nil;');
  1368. end
  1369. else if not IsExtern then
  1370. begin
  1371. write(implemfile,'procedure ',$4^.p1^.p2^.p);
  1372. if assigned($4^.p1^.p1^.p2) then
  1373. write_args(implemfile,$4^.p1^.p1^.p2);
  1374. end;
  1375. end
  1376. else
  1377. begin
  1378. if createdynlib then
  1379. begin
  1380. write(outfile,$4^.p1^.p2^.p,' : function');
  1381. end
  1382. else
  1383. begin
  1384. shift(9);
  1385. write(outfile,'function ',$4^.p1^.p2^.p);
  1386. end;
  1387. if assigned($4^.p1^.p1^.p2) then
  1388. write_args(outfile,$4^.p1^.p1^.p2);
  1389. write(outfile,':');
  1390. write_p_a_def(outfile,$4^.p1^.p1^.p1,$2);
  1391. if createdynlib then
  1392. begin
  1393. loaddynlibproc.add('pointer('+$4^.p1^.p2^.p+'):=GetProcAddress(hlib,'''+$4^.p1^.p2^.p+''');');
  1394. freedynlibproc.add($4^.p1^.p2^.p+':=nil;');
  1395. end
  1396. else if not IsExtern then
  1397. begin
  1398. write(implemfile,'function ',$4^.p1^.p2^.p);
  1399. if assigned($4^.p1^.p1^.p2) then
  1400. write_args(implemfile,$4^.p1^.p1^.p2);
  1401. write(implemfile,':');
  1402. write_p_a_def(implemfile,$4^.p1^.p1^.p1,$2);
  1403. end;
  1404. end;
  1405. (* No CDECL in interface for Uselib *)
  1406. if IsExtern and (not no_pop) then
  1407. write(outfile,';cdecl');
  1408. popshift;
  1409. if createdynlib then
  1410. begin
  1411. writeln(outfile,';');
  1412. end
  1413. else if UseLib then
  1414. begin
  1415. if IsExtern then
  1416. begin
  1417. write (outfile,';external');
  1418. If UseName then
  1419. Write(outfile,' External_library name ''',$4^.p1^.p2^.p,'''');
  1420. end;
  1421. writeln(outfile,';');
  1422. end
  1423. else
  1424. begin
  1425. writeln(outfile,';');
  1426. if not IsExtern then
  1427. begin
  1428. writeln(implemfile,';');
  1429. shift(2);
  1430. if $5^.typ=t_statement_list then
  1431. write_statement_block(implemfile,$5);
  1432. popshift;
  1433. end;
  1434. end;
  1435. IsExtern:=false;
  1436. if not(compactmode) and not(createdynlib) then
  1437. writeln(outfile);
  1438. until not NeedEllipsisOverload;
  1439. end
  1440. else (* $4^.p1^.p1^.typ=t_procdef *)
  1441. if assigned($4)and assigned($4^.p1) then
  1442. begin
  1443. shift(2);
  1444. if block_type<>bt_var then
  1445. begin
  1446. if not(compactmode) then
  1447. writeln(outfile);
  1448. writeln(outfile,aktspace,'var');
  1449. end;
  1450. block_type:=bt_var;
  1451. shift(3);
  1452. IsExtern:=assigned($1)and($1^.str='extern');
  1453. (* walk through all declarations *)
  1454. hp:=$4;
  1455. while assigned(hp) and assigned(hp^.p1) do
  1456. begin
  1457. (* write new var name *)
  1458. if assigned(hp^.p1^.p2) and assigned(hp^.p1^.p2^.p) then
  1459. write(outfile,aktspace,hp^.p1^.p2^.p);
  1460. write(outfile,' : ');
  1461. shift(2);
  1462. (* write its type *)
  1463. write_p_a_def(outfile,hp^.p1^.p1,$2);
  1464. if assigned(hp^.p1^.p2)and assigned(hp^.p1^.p2^.p)then
  1465. begin
  1466. if isExtern then
  1467. write(outfile,';cvar;external')
  1468. else
  1469. write(outfile,';cvar;public');
  1470. end;
  1471. writeln(outfile,';');
  1472. popshift;
  1473. hp:=hp^.p2;
  1474. end;
  1475. popshift;
  1476. popshift;
  1477. end;
  1478. if assigned($1) then
  1479. dispose($1,done);
  1480. if assigned($2) then
  1481. dispose($2,done);
  1482. if assigned($3) then
  1483. dispose($3,done);
  1484. if assigned($4) then
  1485. dispose($4,done);
  1486. if assigned($5) then
  1487. dispose($5,done);
  1488. }
  1489. | dec_specifier type_specifier dec_modifier declarator_list systrap_specifier SEMICOLON
  1490. {
  1491. IsExtern:=false;
  1492. (* by default we must pop the args pushed on stack *)
  1493. no_pop:=false;
  1494. if (assigned($4)and assigned($4^.p1)and assigned($4^.p1^.p1))
  1495. and ($4^.p1^.p1^.typ=t_procdef) then
  1496. begin
  1497. repeat
  1498. If UseLib then
  1499. IsExtern:=true
  1500. else
  1501. IsExtern:=assigned($1)and($1^.str='extern');
  1502. no_pop:=assigned($3) and ($3^.str='no_pop');
  1503. if (block_type<>bt_func) and not(createdynlib) then
  1504. begin
  1505. writeln(outfile);
  1506. block_type:=bt_func;
  1507. end;
  1508. (* dyn. procedures must be put into a var block *)
  1509. if createdynlib then
  1510. begin
  1511. if (block_type<>bt_var) then
  1512. begin
  1513. if not(compactmode) then
  1514. writeln(outfile);
  1515. writeln(outfile,aktspace,'var');
  1516. block_type:=bt_var;
  1517. end;
  1518. shift(2);
  1519. end;
  1520. if not CompactMode then
  1521. begin
  1522. write(outfile,aktspace);
  1523. if not IsExtern then
  1524. write(implemfile,aktspace);
  1525. end;
  1526. (* distinguish between procedure and function *)
  1527. if assigned($2) then
  1528. if ($2^.typ=t_void) and ($4^.p1^.p1^.p1=nil) then
  1529. begin
  1530. if createdynlib then
  1531. begin
  1532. write(outfile,$4^.p1^.p2^.p,' : procedure');
  1533. end
  1534. else
  1535. begin
  1536. shift(10);
  1537. write(outfile,'procedure ',$4^.p1^.p2^.p);
  1538. end;
  1539. if assigned($4^.p1^.p1^.p2) then
  1540. write_args(outfile,$4^.p1^.p1^.p2);
  1541. if createdynlib then
  1542. begin
  1543. loaddynlibproc.add('pointer('+$4^.p1^.p2^.p+'):=GetProcAddress(hlib,'''+$4^.p1^.p2^.p+''');');
  1544. freedynlibproc.add($4^.p1^.p2^.p+':=nil;');
  1545. end
  1546. else if not IsExtern then
  1547. begin
  1548. write(implemfile,'procedure ',$4^.p1^.p2^.p);
  1549. if assigned($4^.p1^.p1^.p2) then
  1550. write_args(implemfile,$4^.p1^.p1^.p2);
  1551. end;
  1552. end
  1553. else
  1554. begin
  1555. if createdynlib then
  1556. begin
  1557. write(outfile,$4^.p1^.p2^.p,' : function');
  1558. end
  1559. else
  1560. begin
  1561. shift(9);
  1562. write(outfile,'function ',$4^.p1^.p2^.p);
  1563. end;
  1564. if assigned($4^.p1^.p1^.p2) then
  1565. write_args(outfile,$4^.p1^.p1^.p2);
  1566. write(outfile,':');
  1567. write_p_a_def(outfile,$4^.p1^.p1^.p1,$2);
  1568. if createdynlib then
  1569. begin
  1570. loaddynlibproc.add('pointer('+$4^.p1^.p2^.p+'):=GetProcAddress(hlib,'''+$4^.p1^.p2^.p+''');');
  1571. freedynlibproc.add($4^.p1^.p2^.p+':=nil;');
  1572. end
  1573. else if not IsExtern then
  1574. begin
  1575. write(implemfile,'function ',$4^.p1^.p2^.p);
  1576. if assigned($4^.p1^.p1^.p2) then
  1577. write_args(implemfile,$4^.p1^.p1^.p2);
  1578. write(implemfile,':');
  1579. write_p_a_def(implemfile,$4^.p1^.p1^.p1,$2);
  1580. end;
  1581. end;
  1582. if assigned($5) then
  1583. write(outfile,';systrap ',$5^.p);
  1584. (* No CDECL in interface for Uselib *)
  1585. if IsExtern and (not no_pop) then
  1586. write(outfile,';cdecl');
  1587. popshift;
  1588. if createdynlib then
  1589. begin
  1590. writeln(outfile,';');
  1591. end
  1592. else if UseLib then
  1593. begin
  1594. if IsExtern then
  1595. begin
  1596. write (outfile,';external');
  1597. If UseName then
  1598. Write(outfile,' External_library name ''',$4^.p1^.p2^.p,'''');
  1599. end;
  1600. writeln(outfile,';');
  1601. end
  1602. else
  1603. begin
  1604. writeln(outfile,';');
  1605. if not IsExtern then
  1606. begin
  1607. writeln(implemfile,';');
  1608. writeln(implemfile,aktspace,'begin');
  1609. writeln(implemfile,aktspace,' { You must implement this function }');
  1610. writeln(implemfile,aktspace,'end;');
  1611. end;
  1612. end;
  1613. IsExtern:=false;
  1614. if not(compactmode) and not(createdynlib) then
  1615. writeln(outfile);
  1616. until not NeedEllipsisOverload;
  1617. end
  1618. else (* $4^.p1^.p1^.typ=t_procdef *)
  1619. if assigned($4)and assigned($4^.p1) then
  1620. begin
  1621. shift(2);
  1622. if block_type<>bt_var then
  1623. begin
  1624. if not(compactmode) then
  1625. writeln(outfile);
  1626. writeln(outfile,aktspace,'var');
  1627. end;
  1628. block_type:=bt_var;
  1629. shift(3);
  1630. IsExtern:=assigned($1)and($1^.str='extern');
  1631. (* walk through all declarations *)
  1632. hp:=$4;
  1633. while assigned(hp) and assigned(hp^.p1) do
  1634. begin
  1635. (* write new var name *)
  1636. if assigned(hp^.p1^.p2) and assigned(hp^.p1^.p2^.p) then
  1637. write(outfile,aktspace,hp^.p1^.p2^.p);
  1638. write(outfile,' : ');
  1639. shift(2);
  1640. (* write its type *)
  1641. write_p_a_def(outfile,hp^.p1^.p1,$2);
  1642. if assigned(hp^.p1^.p2)and assigned(hp^.p1^.p2^.p)then
  1643. begin
  1644. if isExtern then
  1645. write(outfile,';cvar;external')
  1646. else
  1647. write(outfile,';cvar;public');
  1648. end;
  1649. writeln(outfile,';');
  1650. popshift;
  1651. hp:=hp^.p2;
  1652. end;
  1653. popshift;
  1654. popshift;
  1655. end;
  1656. if assigned($1)then dispose($1,done);
  1657. if assigned($2)then dispose($2,done);
  1658. if assigned($4)then dispose($4,done);
  1659. } |
  1660. special_type_specifier SEMICOLON
  1661. {
  1662. if block_type<>bt_type then
  1663. begin
  1664. if not(compactmode) then
  1665. writeln(outfile);
  1666. writeln(outfile,aktspace,'type');
  1667. block_type:=bt_type;
  1668. end;
  1669. shift(3);
  1670. if ( yyv[yysp-1]^.p2 <> nil ) then
  1671. begin
  1672. (* write new type name *)
  1673. TN:=TypeName($1^.p2^.p);
  1674. PN:=PointerName($1^.p2^.p);
  1675. (* define a Pointer type also for structs *)
  1676. if UsePPointers and (Uppercase(tn)<>Uppercase(pn)) and
  1677. assigned($1) and ($1^.typ in [t_uniondef,t_structdef]) then
  1678. writeln(outfile,aktspace,PN,' = ^',TN,';');
  1679. write(outfile,aktspace,TN,' = ');
  1680. shift(2);
  1681. hp:=$1;
  1682. write_type_specifier(outfile,hp);
  1683. popshift;
  1684. (* enum_to_const can make a switch to const *)
  1685. if block_type=bt_type then
  1686. writeln(outfile,';');
  1687. writeln(outfile);
  1688. flush(outfile);
  1689. popshift;
  1690. if must_write_packed_field then
  1691. write_packed_fields_info(outfile,hp,TN);
  1692. if assigned(hp) then
  1693. dispose(hp,done)
  1694. end
  1695. else
  1696. begin
  1697. TN:=TypeName(yyv[yysp-1]^.str);
  1698. PN:=PointerName(yyv[yysp-1]^.str);
  1699. if UsePPointers then writeln(outfile,aktspace,PN,' = ^',TN,';');
  1700. if PackRecords then
  1701. writeln(outfile, aktspace, TN, ' = packed record')
  1702. else
  1703. writeln(outfile, aktspace, TN, ' = record');
  1704. writeln(outfile, aktspace, ' {undefined structure}');
  1705. writeln(outfile, aktspace, ' end;');
  1706. writeln(outfile);
  1707. popshift;
  1708. end;
  1709. } |
  1710. TYPEDEF STRUCT dname dname SEMICOLON
  1711. {
  1712. (* TYPEDEF STRUCT dname dname SEMICOLON *)
  1713. if block_type<>bt_type then
  1714. begin
  1715. if not(compactmode) then
  1716. writeln(outfile);
  1717. writeln(outfile,aktspace,'type');
  1718. block_type:=bt_type;
  1719. end;
  1720. PN:=TypeName($3^.p);
  1721. TN:=TypeName($4^.p);
  1722. if Uppercase(tn)<>Uppercase(pn) then
  1723. begin
  1724. shift(3);
  1725. writeln(outfile,aktspace,PN,' = ',TN,';');
  1726. popshift;
  1727. end;
  1728. if assigned($3) then
  1729. dispose($3,done);
  1730. if assigned($4) then
  1731. dispose($4,done);
  1732. } |
  1733. TYPEDEF type_specifier LKLAMMER dec_modifier declarator RKLAMMER maybe_space LKLAMMER argument_declaration_list RKLAMMER SEMICOLON
  1734. {
  1735. (* TYPEDEF type_specifier LKLAMMER dec_modifier declarator RKLAMMER maybe_space LKLAMMER argument_declaration_list RKLAMMER SEMICOLON *)
  1736. if block_type<>bt_type then
  1737. begin
  1738. if not(compactmode) then
  1739. writeln(outfile);
  1740. writeln(outfile,aktspace,'type');
  1741. block_type:=bt_type;
  1742. end;
  1743. no_pop:=assigned($4) and ($4^.str='no_pop');
  1744. shift(3);
  1745. (* walk through all declarations *)
  1746. hp:=$5;
  1747. if assigned(hp) then
  1748. begin
  1749. hp:=$5;
  1750. while assigned(hp^.p1) do
  1751. hp:=hp^.p1;
  1752. hp^.p1:=new(presobject,init_two(t_procdef,nil,$9));
  1753. hp:=$5;
  1754. if assigned(hp^.p1) and assigned(hp^.p1^.p1) then
  1755. begin
  1756. writeln(outfile);
  1757. (* write new type name *)
  1758. write(outfile,aktspace,TypeName(hp^.p2^.p),' = ');
  1759. shift(2);
  1760. write_p_a_def(outfile,hp^.p1,$2);
  1761. popshift;
  1762. (* if no_pop it is normal fpc calling convention *)
  1763. if is_procvar and
  1764. (not no_pop) then
  1765. write(outfile,';cdecl');
  1766. writeln(outfile,';');
  1767. flush(outfile);
  1768. end;
  1769. end;
  1770. popshift;
  1771. if assigned($2)then
  1772. dispose($2,done);
  1773. if assigned($4)then
  1774. dispose($4,done);
  1775. if assigned($5)then (* disposes also $9 *)
  1776. dispose($5,done);
  1777. } |
  1778. TYPEDEF type_specifier dec_modifier declarator_list SEMICOLON
  1779. {
  1780. (* TYPEDEF type_specifier dec_modifier declarator_list SEMICOLON *)
  1781. if block_type<>bt_type then
  1782. begin
  1783. if not(compactmode) then
  1784. writeln(outfile);
  1785. writeln(outfile,aktspace,'type');
  1786. block_type:=bt_type;
  1787. end;
  1788. no_pop:=assigned($3) and ($3^.str='no_pop');
  1789. shift(3);
  1790. (* Get the name to write the type definition for, try
  1791. to use the tag name first *)
  1792. if assigned($2^.p2) then
  1793. begin
  1794. ph:=$2^.p2;
  1795. end
  1796. else
  1797. begin
  1798. if not assigned($4^.p1^.p2) then
  1799. internalerror(4444);
  1800. ph:=$4^.p1^.p2;
  1801. end;
  1802. (* write type definition *)
  1803. is_procvar:=false;
  1804. writeln(outfile);
  1805. TN:=TypeName(ph^.p);
  1806. PN:=PointerName(ph^.p);
  1807. if UsePPointers and (Uppercase(tn)<>Uppercase(pn)) and
  1808. assigned($2) and ($2^.typ<>t_procdef) then
  1809. writeln(outfile,aktspace,PN,' = ^',TN,';');
  1810. (* write new type name *)
  1811. write(outfile,aktspace,TN,' = ');
  1812. shift(2);
  1813. write_type_specifier(outfile,$2);
  1814. popshift;
  1815. (* if no_pop it is normal fpc calling convention *)
  1816. if is_procvar and
  1817. (not no_pop) then
  1818. write(outfile,';cdecl');
  1819. writeln(outfile,';');
  1820. flush(outfile);
  1821. (* write alias names, ph points to the name already used *)
  1822. hp:=$4;
  1823. while assigned(hp) do
  1824. begin
  1825. if (hp<>ph) and assigned(hp^.p1^.p2) then
  1826. begin
  1827. PN:=TypeName(ph^.p);
  1828. TN:=TypeName(hp^.p1^.p2^.p);
  1829. if Uppercase(TN)<>Uppercase(PN) then
  1830. begin
  1831. write(outfile,aktspace,TN,' = ');
  1832. write_p_a_def(outfile,hp^.p1^.p1,ph);
  1833. writeln(outfile,';');
  1834. PN:=PointerName(hp^.p1^.p2^.p);
  1835. if UsePPointers and (Uppercase(tn)<>Uppercase(pn)) and
  1836. assigned($2) and ($2^.typ<>t_procdef) then
  1837. writeln(outfile,aktspace,PN,' = ^',TN,';');
  1838. end;
  1839. end;
  1840. hp:=hp^.next;
  1841. end;
  1842. popshift;
  1843. if must_write_packed_field then
  1844. if assigned(ph) then
  1845. write_packed_fields_info(outfile,$2,ph^.str)
  1846. else if assigned($2^.p2) then
  1847. write_packed_fields_info(outfile,$2,$2^.p2^.str);
  1848. if assigned($2)then
  1849. dispose($2,done);
  1850. if assigned($3)then
  1851. dispose($3,done);
  1852. if assigned($4)then
  1853. dispose($4,done);
  1854. } |
  1855. TYPEDEF dname SEMICOLON
  1856. {
  1857. if block_type<>bt_type then
  1858. begin
  1859. if not(compactmode) then
  1860. writeln(outfile);
  1861. writeln(outfile,aktspace,'type');
  1862. block_type:=bt_type;
  1863. end;
  1864. shift(3);
  1865. (* write as pointer *)
  1866. writeln(outfile);
  1867. writeln(outfile,'(* generic typedef *)');
  1868. writeln(outfile,aktspace,$2^.p,' = pointer;');
  1869. flush(outfile);
  1870. popshift;
  1871. if assigned($2) then
  1872. dispose($2,done);
  1873. }
  1874. | error error_info SEMICOLON
  1875. { writeln(outfile,'in declaration at line ',line_no,' *)');
  1876. aktspace:='';
  1877. in_space_define:=0;
  1878. in_define:=false;
  1879. arglevel:=0;
  1880. if_nb:=0;
  1881. aktspace:=' ';
  1882. space_index:=1;
  1883. yyerrok;}
  1884. ;
  1885. define_dec :
  1886. DEFINE dname LKLAMMER enum_list RKLAMMER para_def_expr NEW_LINE
  1887. {
  1888. (* DEFINE dname LKLAMMER enum_list RKLAMMER para_def_expr NEW_LINE *)
  1889. if not stripinfo then
  1890. begin
  1891. writeln (outfile,aktspace,'{ was #define dname(params) para_def_expr }');
  1892. writeln (implemfile,aktspace,'{ was #define dname(params) para_def_expr }');
  1893. if assigned($4) then
  1894. begin
  1895. writeln (outfile,aktspace,'{ argument types are unknown }');
  1896. writeln (implemfile,aktspace,'{ argument types are unknown }');
  1897. end;
  1898. if not assigned($6^.p3) then
  1899. begin
  1900. writeln(outfile,aktspace,'{ return type might be wrong } ');
  1901. writeln(implemfile,aktspace,'{ return type might be wrong } ');
  1902. end;
  1903. end;
  1904. block_type:=bt_func;
  1905. write(outfile,aktspace,'function ',$2^.p);
  1906. write(implemfile,aktspace,'function ',$2^.p);
  1907. if assigned($4) then
  1908. begin
  1909. write(outfile,'(');
  1910. write(implemfile,'(');
  1911. ph:=new(presobject,init_one(t_enumdef,$4));
  1912. write_def_params(outfile,ph);
  1913. write_def_params(implemfile,ph);
  1914. if assigned(ph) then dispose(ph,done);
  1915. ph:=nil;
  1916. (* types are unknown *)
  1917. write(outfile,' : longint)');
  1918. write(implemfile,' : longint)');
  1919. end;
  1920. if not assigned($6^.p3) then
  1921. begin
  1922. writeln(outfile,' : longint;',aktspace,commentstr);
  1923. writeln(implemfile,' : longint;');
  1924. flush(outfile);
  1925. end
  1926. else
  1927. begin
  1928. write(outfile,' : ');
  1929. write_type_specifier(outfile,$6^.p3);
  1930. writeln(outfile,';',aktspace,commentstr);
  1931. flush(outfile);
  1932. write(implemfile,' : ');
  1933. write_type_specifier(implemfile,$6^.p3);
  1934. writeln(implemfile,';');
  1935. end;
  1936. writeln(outfile);
  1937. flush(outfile);
  1938. hp:=new(presobject,init_two(t_funcname,$2,$6));
  1939. write_funexpr(implemfile,hp);
  1940. writeln(implemfile);
  1941. flush(implemfile);
  1942. if assigned(hp)then dispose(hp,done);
  1943. }|
  1944. DEFINE dname SPACE_DEFINE NEW_LINE
  1945. {
  1946. (* DEFINE dname SPACE_DEFINE NEW_LINE *)
  1947. writeln(outfile,'{$define ',$2^.p,'}',aktspace,commentstr);
  1948. flush(outfile);
  1949. if assigned($2)then
  1950. dispose($2,done);
  1951. }|
  1952. DEFINE dname NEW_LINE
  1953. {
  1954. writeln(outfile,'{$define ',$2^.p,'}',aktspace,commentstr);
  1955. flush(outfile);
  1956. if assigned($2)then
  1957. dispose($2,done);
  1958. } |
  1959. DEFINE dname SPACE_DEFINE def_expr NEW_LINE
  1960. {
  1961. (* DEFINE dname SPACE_DEFINE def_expr NEW_LINE *)
  1962. if ($4^.typ=t_exprlist) and
  1963. $4^.p1^.is_const and
  1964. not assigned($4^.next) then
  1965. begin
  1966. if block_type<>bt_const then
  1967. begin
  1968. writeln(outfile);
  1969. writeln(outfile,aktspace,'const');
  1970. end;
  1971. block_type:=bt_const;
  1972. shift(3);
  1973. write(outfile,aktspace,$2^.p);
  1974. write(outfile,' = ');
  1975. flush(outfile);
  1976. write_expr(outfile,$4^.p1);
  1977. writeln(outfile,';',aktspace,commentstr);
  1978. popshift;
  1979. if assigned($2) then
  1980. dispose($2,done);
  1981. if assigned($4) then
  1982. dispose($4,done);
  1983. end
  1984. else
  1985. begin
  1986. if not stripinfo then
  1987. begin
  1988. writeln (outfile,aktspace,'{ was #define dname def_expr }');
  1989. writeln (implemfile,aktspace,'{ was #define dname def_expr }');
  1990. end;
  1991. block_type:=bt_func;
  1992. write(outfile,aktspace,'function ',$2^.p);
  1993. write(implemfile,aktspace,'function ',$2^.p);
  1994. shift(2);
  1995. if not assigned($4^.p3) then
  1996. begin
  1997. writeln(outfile,' : longint;');
  1998. writeln(outfile,aktspace,' { return type might be wrong }');
  1999. flush(outfile);
  2000. writeln(implemfile,' : longint;');
  2001. writeln(implemfile,aktspace,' { return type might be wrong }');
  2002. end
  2003. else
  2004. begin
  2005. write(outfile,' : ');
  2006. write_type_specifier(outfile,$4^.p3);
  2007. writeln(outfile,';',aktspace,commentstr);
  2008. flush(outfile);
  2009. write(implemfile,' : ');
  2010. write_type_specifier(implemfile,$4^.p3);
  2011. writeln(implemfile,';');
  2012. end;
  2013. writeln(outfile);
  2014. flush(outfile);
  2015. hp:=new(presobject,init_two(t_funcname,$2,$4));
  2016. write_funexpr(implemfile,hp);
  2017. popshift;
  2018. dispose(hp,done);
  2019. writeln(implemfile);
  2020. flush(implemfile);
  2021. end;
  2022. }
  2023. | error error_info NEW_LINE
  2024. { writeln(outfile,'in define line ',line_no,' *)');
  2025. aktspace:='';
  2026. in_space_define:=0;
  2027. in_define:=false;
  2028. arglevel:=0;
  2029. if_nb:=0;
  2030. aktspace:=' ';
  2031. space_index:=1;
  2032. yyerrok;}
  2033. ;
  2034. closed_list : LGKLAMMER member_list RGKLAMMER
  2035. {$$:=$2;} |
  2036. error error_info RGKLAMMER
  2037. { writeln(outfile,' in member_list *)');
  2038. yyerrok;
  2039. $$:=nil;
  2040. }
  2041. ;
  2042. closed_enum_list : LGKLAMMER enum_list RGKLAMMER
  2043. {$$:=$2;} |
  2044. error error_info RGKLAMMER
  2045. { writeln(outfile,' in enum_list *)');
  2046. yyerrok;
  2047. $$:=nil;
  2048. }
  2049. ;
  2050. special_type_specifier :
  2051. STRUCT dname closed_list _PACKED
  2052. {
  2053. if (not is_packed) and (not packrecords) then
  2054. writeln(outfile,'{$PACKRECORDS 1}');
  2055. is_packed:=true;
  2056. $$:=new(presobject,init_two(t_structdef,$3,$2));
  2057. } |
  2058. STRUCT dname closed_list
  2059. {
  2060. if (is_packed) and (not packrecords) then
  2061. writeln(outfile,'{$PACKRECORDS 4}');
  2062. is_packed:=false;
  2063. $$:=new(presobject,init_two(t_structdef,$3,$2));
  2064. } |
  2065. UNION dname closed_list _PACKED
  2066. {
  2067. if (not is_packed) and (not packrecords) then
  2068. writeln(outfile,'{$PACKRECORDS 1}');
  2069. is_packed:=true;
  2070. $$:=new(presobject,init_two(t_uniondef,$3,$2));
  2071. } |
  2072. UNION dname closed_list
  2073. {
  2074. $$:=new(presobject,init_two(t_uniondef,$3,$2));
  2075. } |
  2076. UNION dname
  2077. {
  2078. $$:=$2;
  2079. } |
  2080. STRUCT dname
  2081. {
  2082. $$:=$2;
  2083. } |
  2084. ENUM dname closed_enum_list
  2085. {
  2086. $$:=new(presobject,init_two(t_enumdef,$3,$2));
  2087. } |
  2088. ENUM dname
  2089. {
  2090. $$:=$2;
  2091. };
  2092. type_specifier :
  2093. _CONST type_specifier
  2094. {
  2095. if not stripinfo then
  2096. writeln(outfile,'(* Const before type ignored *)');
  2097. $$:=$2;
  2098. } |
  2099. UNION closed_list _PACKED
  2100. {
  2101. if (not is_packed) and (not packrecords)then
  2102. writeln(outfile,'{$PACKRECORDS 1}');
  2103. is_packed:=true;
  2104. $$:=new(presobject,init_one(t_uniondef,$2));
  2105. } |
  2106. UNION closed_list
  2107. {
  2108. $$:=new(presobject,init_one(t_uniondef,$2));
  2109. } |
  2110. STRUCT closed_list _PACKED
  2111. {
  2112. if (not is_packed) and (not packrecords) then
  2113. writeln(outfile,'{$PACKRECORDS 1}');
  2114. is_packed:=true;
  2115. $$:=new(presobject,init_one(t_structdef,$2));
  2116. } |
  2117. STRUCT closed_list
  2118. {
  2119. if (is_packed) and (not packrecords) then
  2120. writeln(outfile,'{$PACKRECORDS 4}');
  2121. is_packed:=false;
  2122. $$:=new(presobject,init_one(t_structdef,$2));
  2123. } |
  2124. ENUM closed_enum_list
  2125. {
  2126. $$:=new(presobject,init_one(t_enumdef,$2));
  2127. } |
  2128. special_type_specifier
  2129. {
  2130. $$:=$1;
  2131. } |
  2132. simple_type_name { $$:=$1; }
  2133. ;
  2134. member_list : member_declaration member_list
  2135. {
  2136. $$:=new(presobject,init_one(t_memberdeclist,$1));
  2137. $$^.next:=$2;
  2138. } |
  2139. member_declaration
  2140. {
  2141. $$:=new(presobject,init_one(t_memberdeclist,$1));
  2142. }
  2143. ;
  2144. member_declaration :
  2145. type_specifier declarator_list SEMICOLON
  2146. {
  2147. $$:=new(presobject,init_two(t_memberdec,$1,$2));
  2148. }
  2149. ;
  2150. dname : ID { (*dname*)
  2151. $$:=new(presobject,init_id(act_token));
  2152. }
  2153. ;
  2154. special_type_name :
  2155. SIGNED special_type_name
  2156. {
  2157. hp:=$2;
  2158. $$:=hp;
  2159. if assigned(hp) then
  2160. begin
  2161. s:=strpas(hp^.p);
  2162. if UseCTypesUnit then
  2163. begin
  2164. if s=cint_STR then
  2165. s:=csint_STR
  2166. else if s=cshort_STR then
  2167. s:=csshort_STR
  2168. else if s=cchar_STR then
  2169. s:=cschar_STR
  2170. else if s=clong_STR then
  2171. s:=cslong_STR
  2172. else if s=clonglong_STR then
  2173. s:=cslonglong_STR
  2174. else
  2175. s:='';
  2176. end
  2177. else
  2178. begin
  2179. if s=UINT_STR then
  2180. s:=INT_STR
  2181. else if s=USHORT_STR then
  2182. s:=SHORT_STR
  2183. else if s=UCHAR_STR then
  2184. s:=CHAR_STR
  2185. else if s=QWORD_STR then
  2186. s:=INT64_STR
  2187. else
  2188. s:='';
  2189. end;
  2190. if s<>'' then
  2191. hp^.setstr(s);
  2192. end;
  2193. } |
  2194. UNSIGNED special_type_name
  2195. {
  2196. hp:=$2;
  2197. $$:=hp;
  2198. if assigned(hp) then
  2199. begin
  2200. s:=strpas(hp^.p);
  2201. if UseCTypesUnit then
  2202. begin
  2203. if s=cint_STR then
  2204. s:=cuint_STR
  2205. else if s=cshort_STR then
  2206. s:=cushort_STR
  2207. else if s=cchar_STR then
  2208. s:=cuchar_STR
  2209. else if s=clong_STR then
  2210. s:=culong_STR
  2211. else if s=clonglong_STR then
  2212. s:=culonglong_STR
  2213. else
  2214. s:='';
  2215. end
  2216. else
  2217. begin
  2218. if s=INT_STR then
  2219. s:=UINT_STR
  2220. else if s=SHORT_STR then
  2221. s:=USHORT_STR
  2222. else if s=CHAR_STR then
  2223. s:=UCHAR_STR
  2224. else if s=INT64_STR then
  2225. s:=QWORD_STR
  2226. else
  2227. s:='';
  2228. end;
  2229. if s<>'' then
  2230. hp^.setstr(s);
  2231. end;
  2232. } |
  2233. INT
  2234. {
  2235. if UseCTypesUnit then
  2236. $$:=new(presobject,init_id(cint_STR))
  2237. else
  2238. $$:=new(presobject,init_intid(INT_STR));
  2239. } |
  2240. LONG
  2241. {
  2242. if UseCTypesUnit then
  2243. $$:=new(presobject,init_id(clong_STR))
  2244. else
  2245. $$:=new(presobject,init_intid(INT_STR));
  2246. } |
  2247. LONG INT
  2248. {
  2249. if UseCTypesUnit then
  2250. $$:=new(presobject,init_id(clong_STR))
  2251. else
  2252. $$:=new(presobject,init_intid(INT_STR));
  2253. } |
  2254. LONG LONG
  2255. {
  2256. if UseCTypesUnit then
  2257. $$:=new(presobject,init_id(clonglong_STR))
  2258. else
  2259. $$:=new(presobject,init_intid(INT64_STR));
  2260. } |
  2261. LONG LONG INT
  2262. {
  2263. if UseCTypesUnit then
  2264. $$:=new(presobject,init_id(clonglong_STR))
  2265. else
  2266. $$:=new(presobject,init_intid(INT64_STR));
  2267. } |
  2268. SHORT
  2269. {
  2270. if UseCTypesUnit then
  2271. $$:=new(presobject,init_id(cshort_STR))
  2272. else
  2273. $$:=new(presobject,init_intid(SHORT_STR));
  2274. } |
  2275. SHORT INT
  2276. {
  2277. if UseCTypesUnit then
  2278. $$:=new(presobject,init_id(csint_STR))
  2279. else
  2280. $$:=new(presobject,init_intid(SHORT_STR));
  2281. } |
  2282. REAL
  2283. {
  2284. $$:=new(presobject,init_intid(REAL_STR));
  2285. } |
  2286. VOID
  2287. {
  2288. $$:=new(presobject,init_no(t_void));
  2289. } |
  2290. _CHAR
  2291. {
  2292. if UseCTypesUnit then
  2293. $$:=new(presobject,init_id(cchar_STR))
  2294. else
  2295. $$:=new(presobject,init_intid(CHAR_STR));
  2296. } |
  2297. UNSIGNED
  2298. {
  2299. if UseCTypesUnit then
  2300. $$:=new(presobject,init_id(cunsigned_STR))
  2301. else
  2302. $$:=new(presobject,init_intid(UINT_STR));
  2303. }
  2304. ;
  2305. simple_type_name :
  2306. special_type_name
  2307. {
  2308. $$:=$1;
  2309. }
  2310. |
  2311. dname
  2312. {
  2313. $$:=$1;
  2314. tn:=$$^.str;
  2315. if removeunderscore and
  2316. (length(tn)>1) and (tn[1]='_') then
  2317. $$^.setstr(Copy(tn,2,length(tn)-1));
  2318. }
  2319. ;
  2320. declarator_list :
  2321. declarator_list COMMA declarator
  2322. {
  2323. $$:=$1;
  2324. hp:=$1;
  2325. while assigned(hp^.next) do
  2326. hp:=hp^.next;
  2327. hp^.next:=new(presobject,init_one(t_declist,$3));
  2328. }|
  2329. error error_info COMMA declarator_list
  2330. {
  2331. writeln(outfile,' in declarator_list *)');
  2332. $$:=$4;
  2333. yyerrok;
  2334. }|
  2335. error error_info
  2336. {
  2337. writeln(outfile,' in declarator_list *)');
  2338. yyerrok;
  2339. }|
  2340. declarator
  2341. {
  2342. $$:=new(presobject,init_one(t_declist,$1));
  2343. }
  2344. ;
  2345. argument_declaration : type_specifier declarator
  2346. {
  2347. $$:=new(presobject,init_two(t_arg,$1,$2));
  2348. } |
  2349. type_specifier STAR declarator
  2350. {
  2351. (* type_specifier STAR declarator *)
  2352. hp:=new(presobject,init_one(t_pointerdef,$1));
  2353. $$:=new(presobject,init_two(t_arg,hp,$3));
  2354. } |
  2355. type_specifier abstract_declarator
  2356. {
  2357. $$:=new(presobject,init_two(t_arg,$1,$2));
  2358. }
  2359. ;
  2360. argument_declaration_list : argument_declaration
  2361. {
  2362. $$:=new(presobject,init_two(t_arglist,$1,nil));
  2363. } |
  2364. argument_declaration COMMA argument_declaration_list
  2365. {
  2366. $$:=new(presobject,init_two(t_arglist,$1,nil));
  2367. $$^.next:=$3;
  2368. } |
  2369. ELLIPSIS
  2370. {
  2371. $$:=new(presobject,init_two(t_arglist,ellipsisarg,nil));
  2372. } |
  2373. {
  2374. $$:=nil;
  2375. }
  2376. ;
  2377. size_overrider :
  2378. _FAR
  2379. { $$:=new(presobject,init_id('far'));}
  2380. | _NEAR
  2381. { $$:=new(presobject,init_id('near'));}
  2382. | _HUGE
  2383. { $$:=new(presobject,init_id('huge'));}
  2384. ;
  2385. declarator :
  2386. _CONST declarator
  2387. {
  2388. if not stripinfo then
  2389. writeln(outfile,'(* Const before declarator ignored *)');
  2390. $$:=$2;
  2391. } |
  2392. size_overrider STAR declarator
  2393. {
  2394. if not stripinfo then
  2395. writeln(outfile,aktspace,'(* ',$1^.p,' ignored *)');
  2396. dispose($1,done);
  2397. hp:=$3;
  2398. $$:=hp;
  2399. while assigned(hp^.p1) do
  2400. hp:=hp^.p1;
  2401. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  2402. } |
  2403. STAR declarator
  2404. {
  2405. (* %prec PSTAR this was wrong!! *)
  2406. hp:=$2;
  2407. $$:=hp;
  2408. while assigned(hp^.p1) do
  2409. hp:=hp^.p1;
  2410. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  2411. } |
  2412. _AND declarator %prec P_AND
  2413. {
  2414. hp:=$2;
  2415. $$:=hp;
  2416. while assigned(hp^.p1) do
  2417. hp:=hp^.p1;
  2418. hp^.p1:=new(presobject,init_one(t_addrdef,nil));
  2419. } |
  2420. dname COLON expr
  2421. {
  2422. (* size specifier supported *)
  2423. hp:=new(presobject,init_one(t_size_specifier,$3));
  2424. $$:=new(presobject,init_three(t_dec,nil,$1,hp));
  2425. }|
  2426. dname ASSIGN expr
  2427. {
  2428. if not stripinfo then
  2429. writeln(outfile,'(* Warning : default value for ',$1^.p,' ignored *)');
  2430. hp:=new(presobject,init_one(t_default_value,$3));
  2431. $$:=new(presobject,init_three(t_dec,nil,$1,hp));
  2432. }|
  2433. dname
  2434. {
  2435. $$:=new(presobject,init_two(t_dec,nil,$1));
  2436. }|
  2437. declarator LKLAMMER argument_declaration_list RKLAMMER
  2438. {
  2439. hp:=$1;
  2440. $$:=hp;
  2441. while assigned(hp^.p1) do
  2442. hp:=hp^.p1;
  2443. hp^.p1:=new(presobject,init_two(t_procdef,nil,$3));
  2444. } |
  2445. declarator no_arg
  2446. {
  2447. hp:=$1;
  2448. $$:=hp;
  2449. while assigned(hp^.p1) do
  2450. hp:=hp^.p1;
  2451. hp^.p1:=new(presobject,init_two(t_procdef,nil,nil));
  2452. } |
  2453. declarator LECKKLAMMER expr RECKKLAMMER
  2454. {
  2455. hp:=$1;
  2456. $$:=hp;
  2457. while assigned(hp^.p1) do
  2458. hp:=hp^.p1;
  2459. hp^.p1:=new(presobject,init_two(t_arraydef,nil,$3));
  2460. } |
  2461. declarator LECKKLAMMER RECKKLAMMER
  2462. {
  2463. (* this is translated into a pointer *)
  2464. hp:=$1;
  2465. $$:=hp;
  2466. while assigned(hp^.p1) do
  2467. hp:=hp^.p1;
  2468. hp^.p1:=new(presobject,init_two(t_arraydef,nil,nil));
  2469. } |
  2470. LKLAMMER declarator RKLAMMER
  2471. {
  2472. $$:=$2;
  2473. }
  2474. ;
  2475. no_arg : LKLAMMER RKLAMMER |
  2476. LKLAMMER VOID RKLAMMER;
  2477. abstract_declarator :
  2478. _CONST abstract_declarator
  2479. {
  2480. if not stripinfo then
  2481. writeln(outfile,'(* Const before abstract_declarator ignored *)');
  2482. $$:=$2;
  2483. } |
  2484. size_overrider STAR abstract_declarator
  2485. {
  2486. if not stripinfo then
  2487. writeln(outfile,aktspace,'(* ',$1^.p,' ignored *)');
  2488. dispose($1,done);
  2489. hp:=$3;
  2490. $$:=hp;
  2491. while assigned(hp^.p1) do
  2492. hp:=hp^.p1;
  2493. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  2494. } |
  2495. STAR abstract_declarator %prec PSTAR
  2496. {
  2497. hp:=$2;
  2498. $$:=hp;
  2499. while assigned(hp^.p1) do
  2500. hp:=hp^.p1;
  2501. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  2502. } |
  2503. abstract_declarator LKLAMMER argument_declaration_list RKLAMMER
  2504. {
  2505. hp:=$1;
  2506. $$:=hp;
  2507. while assigned(hp^.p1) do
  2508. hp:=hp^.p1;
  2509. hp^.p1:=new(presobject,init_two(t_procdef,nil,$3));
  2510. } |
  2511. abstract_declarator no_arg
  2512. {
  2513. hp:=$1;
  2514. $$:=hp;
  2515. while assigned(hp^.p1) do
  2516. hp:=hp^.p1;
  2517. hp^.p1:=new(presobject,init_two(t_procdef,nil,nil));
  2518. } |
  2519. abstract_declarator LECKKLAMMER expr RECKKLAMMER
  2520. {
  2521. hp:=$1;
  2522. $$:=hp;
  2523. while assigned(hp^.p1) do
  2524. hp:=hp^.p1;
  2525. hp^.p1:=new(presobject,init_two(t_arraydef,nil,$3));
  2526. } |
  2527. declarator LECKKLAMMER RECKKLAMMER
  2528. {
  2529. (* this is translated into a pointer *)
  2530. hp:=$1;
  2531. $$:=hp;
  2532. while assigned(hp^.p1) do
  2533. hp:=hp^.p1;
  2534. hp^.p1:=new(presobject,init_two(t_arraydef,nil,nil));
  2535. } |
  2536. LKLAMMER abstract_declarator RKLAMMER
  2537. {
  2538. $$:=$2;
  2539. } |
  2540. {
  2541. $$:=new(presobject,init_two(t_dec,nil,nil));
  2542. }
  2543. ;
  2544. expr : shift_expr
  2545. { $$:=$1; }
  2546. ;
  2547. shift_expr :
  2548. expr _ASSIGN expr
  2549. { $$:=new(presobject,init_bop(':=',$1,$3)); }
  2550. | expr EQUAL expr
  2551. { $$:=new(presobject,init_bop('=',$1,$3));}
  2552. | expr UNEQUAL expr
  2553. { $$:=new(presobject,init_bop('<>',$1,$3));}
  2554. | expr GT expr
  2555. { $$:=new(presobject,init_bop('>',$1,$3));}
  2556. | expr GTE expr
  2557. { $$:=new(presobject,init_bop('>=',$1,$3));}
  2558. | expr LT expr
  2559. { $$:=new(presobject,init_bop('<',$1,$3));}
  2560. | expr LTE expr
  2561. { $$:=new(presobject,init_bop('<=',$1,$3));}
  2562. | expr _PLUS expr
  2563. { $$:=new(presobject,init_bop('+',$1,$3));}
  2564. | expr MINUS expr
  2565. { $$:=new(presobject,init_bop('-',$1,$3));}
  2566. | expr STAR expr
  2567. { $$:=new(presobject,init_bop('*',$1,$3));}
  2568. | expr _SLASH expr
  2569. { $$:=new(presobject,init_bop('/',$1,$3));}
  2570. | expr _OR expr
  2571. { $$:=new(presobject,init_bop(' or ',$1,$3));}
  2572. | expr _AND expr
  2573. { $$:=new(presobject,init_bop(' and ',$1,$3));}
  2574. | expr _NOT expr
  2575. { $$:=new(presobject,init_bop(' not ',$1,$3));}
  2576. | expr _SHL expr
  2577. { $$:=new(presobject,init_bop(' shl ',$1,$3));}
  2578. | expr _SHR expr
  2579. { $$:=new(presobject,init_bop(' shr ',$1,$3));}
  2580. | expr QUESTIONMARK colon_expr
  2581. {
  2582. $3^.p1:=$1;
  2583. $$:=$3;
  2584. inc(if_nb);
  2585. $$^.p:=strpnew('if_local'+str(if_nb));
  2586. } |
  2587. unary_expr {$$:=$1;}
  2588. ;
  2589. colon_expr : expr COLON expr
  2590. { (* if A then B else C *)
  2591. $$:=new(presobject,init_three(t_ifexpr,nil,$1,$3));}
  2592. ;
  2593. maybe_empty_unary_expr :
  2594. unary_expr
  2595. { $$:=$1; }
  2596. |
  2597. { $$:=nil;}
  2598. ;
  2599. unary_expr:
  2600. dname
  2601. {
  2602. $$:=$1;
  2603. } |
  2604. special_type_name
  2605. {
  2606. $$:=$1;
  2607. } |
  2608. CSTRING
  2609. {
  2610. (* remove L prefix for widestrings *)
  2611. s:=act_token;
  2612. if Win32headers and (s[1]='L') then
  2613. delete(s,1,1);
  2614. $$:=new(presobject,init_id(''''+copy(s,2,length(s)-2)+''''));
  2615. } |
  2616. NUMBER
  2617. {
  2618. $$:=new(presobject,init_id(act_token));
  2619. } |
  2620. unary_expr POINT expr
  2621. {
  2622. $$:=new(presobject,init_bop('.',$1,$3));
  2623. } |
  2624. unary_expr DEREF expr
  2625. {
  2626. $$:=new(presobject,init_bop('^.',$1,$3));
  2627. } |
  2628. MINUS unary_expr
  2629. {
  2630. $$:=new(presobject,init_preop('-',$2));
  2631. }|
  2632. _AND unary_expr %prec R_AND
  2633. {
  2634. $$:=new(presobject,init_preop('@',$2));
  2635. }|
  2636. _NOT unary_expr
  2637. {
  2638. $$:=new(presobject,init_preop(' not ',$2));
  2639. } |
  2640. LKLAMMER dname RKLAMMER maybe_empty_unary_expr
  2641. {
  2642. if assigned($4) then
  2643. $$:=new(presobject,init_two(t_typespec,$2,$4))
  2644. else
  2645. $$:=$2;
  2646. } |
  2647. LKLAMMER type_specifier RKLAMMER unary_expr
  2648. {
  2649. $$:=new(presobject,init_two(t_typespec,$2,$4));
  2650. } |
  2651. LKLAMMER type_specifier STAR RKLAMMER unary_expr
  2652. {
  2653. hp:=new(presobject,init_one(t_pointerdef,$2));
  2654. $$:=new(presobject,init_two(t_typespec,hp,$5));
  2655. } |
  2656. LKLAMMER type_specifier size_overrider STAR RKLAMMER unary_expr
  2657. {
  2658. if not stripinfo then
  2659. writeln(outfile,aktspace,'(* ',$3^.p,' ignored *)');
  2660. dispose($3,done);
  2661. write_type_specifier(outfile,$2);
  2662. writeln(outfile,' ignored *)');
  2663. hp:=new(presobject,init_one(t_pointerdef,$2));
  2664. $$:=new(presobject,init_two(t_typespec,hp,$6));
  2665. } |
  2666. dname LKLAMMER exprlist RKLAMMER
  2667. {
  2668. hp:=new(presobject,init_one(t_exprlist,$1));
  2669. $$:=new(presobject,init_three(t_funexprlist,hp,$3,nil));
  2670. } |
  2671. LKLAMMER shift_expr RKLAMMER
  2672. {
  2673. $$:=$2;
  2674. } |
  2675. LKLAMMER STAR unary_expr RKLAMMER maybe_space LKLAMMER exprlist RKLAMMER
  2676. {
  2677. $$:=new(presobject,init_two(t_callop,$3,$7));
  2678. } |
  2679. dname LECKKLAMMER exprlist RECKKLAMMER
  2680. {
  2681. $$:=new(presobject,init_two(t_arrayop,$1,$3));
  2682. }
  2683. ;
  2684. enum_list :
  2685. enum_element COMMA enum_list
  2686. { (*enum_element COMMA enum_list *)
  2687. $$:=$1;
  2688. $$^.next:=$3;
  2689. } |
  2690. enum_element {
  2691. $$:=$1;
  2692. } |
  2693. {(* empty enum list *)
  2694. $$:=nil;};
  2695. enum_element :
  2696. dname _ASSIGN expr
  2697. { begin (*enum_element: dname _ASSIGN expr *)
  2698. $$:=new(presobject,init_two(t_enumlist,$1,$3));
  2699. end;
  2700. } |
  2701. dname
  2702. {
  2703. begin (*enum_element: dname*)
  2704. $$:=new(presobject,init_two(t_enumlist,$1,nil));
  2705. end;
  2706. };
  2707. def_expr :
  2708. unary_expr
  2709. {
  2710. if $1^.typ=t_funexprlist then
  2711. $$:=$1
  2712. else
  2713. $$:=new(presobject,init_two(t_exprlist,$1,nil));
  2714. (* if here is a type specifier
  2715. we know the return type *)
  2716. if ($1^.typ=t_typespec) then
  2717. $$^.p3:=$1^.p1^.get_copy;
  2718. }
  2719. ;
  2720. para_def_expr :
  2721. SPACE_DEFINE def_expr
  2722. {
  2723. $$:=$2;
  2724. } |
  2725. maybe_space LKLAMMER def_expr RKLAMMER
  2726. {
  2727. $$:=$3
  2728. }
  2729. ;
  2730. exprlist : exprelem COMMA exprlist
  2731. { (*exprlist COMMA expr*)
  2732. $$:=$1;
  2733. $1^.next:=$3;
  2734. } |
  2735. exprelem
  2736. {
  2737. $$:=$1;
  2738. } |
  2739. { (* empty expression list *)
  2740. $$:=nil; };
  2741. exprelem :
  2742. expr
  2743. {
  2744. $$:=new(presobject,init_one(t_exprlist,$1));
  2745. };
  2746. %%
  2747. function yylex : Integer;
  2748. begin
  2749. yylex:=scan.yylex;
  2750. line_no:=yylineno;
  2751. end;
  2752. procedure WriteFileHeader(var headerfile: Text);
  2753. var
  2754. i: integer;
  2755. originalstr: string;
  2756. begin
  2757. { write unit header }
  2758. if not includefile then
  2759. begin
  2760. if createdynlib then
  2761. writeln(headerfile,'{$mode objfpc}');
  2762. writeln(headerfile,'unit ',unitname,';');
  2763. writeln(headerfile,'interface');
  2764. writeln(headerfile);
  2765. if UseCTypesUnit then
  2766. begin
  2767. writeln(headerfile,'uses');
  2768. writeln(headerfile,' ctypes;');
  2769. writeln(headerfile);
  2770. end;
  2771. writeln(headerfile,'{');
  2772. writeln(headerfile,' Automatically converted by H2Pas ',version,' from ',inputfilename);
  2773. writeln(headerfile,' The following command line parameters were used:');
  2774. for i:=1 to paramcount do
  2775. writeln(headerfile,' ',paramstr(i));
  2776. writeln(headerfile,'}');
  2777. writeln(headerfile);
  2778. end;
  2779. if UseName then
  2780. begin
  2781. writeln(headerfile,aktspace,'const');
  2782. writeln(headerfile,aktspace,' External_library=''',libfilename,'''; {Setup as you need}');
  2783. writeln(headerfile);
  2784. end;
  2785. if UsePPointers then
  2786. begin
  2787. Writeln(headerfile,aktspace,'{ Pointers to basic pascal types, inserted by h2pas conversion program.}');
  2788. Writeln(headerfile,aktspace,'Type');
  2789. Writeln(headerfile,aktspace,' PLongint = ^Longint;');
  2790. Writeln(headerfile,aktspace,' PSmallInt = ^SmallInt;');
  2791. Writeln(headerfile,aktspace,' PByte = ^Byte;');
  2792. Writeln(headerfile,aktspace,' PWord = ^Word;');
  2793. Writeln(headerfile,aktspace,' PDWord = ^DWord;');
  2794. Writeln(headerfile,aktspace,' PDouble = ^Double;');
  2795. Writeln(headerfile);
  2796. end;
  2797. if PTypeList.count <> 0 then
  2798. Writeln(headerfile,aktspace,'Type');
  2799. for i:=0 to (PTypeList.Count-1) do
  2800. begin
  2801. originalstr:=copy(PTypelist[i],2,length(PTypeList[i]));
  2802. Writeln(headerfile,aktspace,PTypeList[i],' = ^',originalstr,';');
  2803. end;
  2804. if not packrecords then
  2805. begin
  2806. writeln(headerfile,'{$IFDEF FPC}');
  2807. writeln(headerfile,'{$PACKRECORDS C}');
  2808. writeln(headerfile,'{$ENDIF}');
  2809. end;
  2810. writeln(headerfile);
  2811. end;
  2812. var
  2813. SS : string;
  2814. i : longint;
  2815. headerfile: Text;
  2816. finaloutfile: Text;
  2817. begin
  2818. pointerprefix:=false;
  2819. { Initialize }
  2820. PTypeList:=TStringList.Create;
  2821. PTypeList.Sorted := true;
  2822. PTypeList.Duplicates := dupIgnore;
  2823. freedynlibproc:=TStringList.Create;
  2824. loaddynlibproc:=TStringList.Create;
  2825. yydebug:=true;
  2826. aktspace:='';
  2827. block_type:=bt_no;
  2828. IsExtern:=false;
  2829. { Read commandline options }
  2830. ProcessOptions;
  2831. if not CompactMode then
  2832. aktspace:=' ';
  2833. { open input and output files }
  2834. assign(yyinput, inputfilename);
  2835. {$I-}
  2836. reset(yyinput);
  2837. {$I+}
  2838. if ioresult<>0 then
  2839. begin
  2840. writeln('file ',inputfilename,' not found!');
  2841. halt(1);
  2842. end;
  2843. { This is the intermediate output file }
  2844. assign(outfile, 'ext3.tmp');
  2845. {$I-}
  2846. rewrite(outfile);
  2847. {$I+}
  2848. if ioresult<>0 then
  2849. begin
  2850. writeln('file ext3.tmp could not be created!');
  2851. halt(1);
  2852. end;
  2853. writeln(outfile);
  2854. { Open tempfiles }
  2855. { This is where the implementation section of the unit shall be stored }
  2856. Assign(implemfile,'ext.tmp');
  2857. rewrite(implemfile);
  2858. Assign(tempfile,'ext2.tmp');
  2859. rewrite(tempfile);
  2860. { Parse! }
  2861. yyparse;
  2862. { Write implementation if needed }
  2863. if not(includefile) then
  2864. begin
  2865. writeln(outfile);
  2866. writeln(outfile,'implementation');
  2867. writeln(outfile);
  2868. end;
  2869. { here we have a problem if a line is longer than 255 chars !! }
  2870. reset(implemfile);
  2871. while not eof(implemfile) do
  2872. begin
  2873. readln(implemfile,SS);
  2874. writeln(outfile,SS);
  2875. end;
  2876. if createdynlib then
  2877. begin
  2878. writeln(outfile,' uses');
  2879. writeln(outfile,' SysUtils,');
  2880. writeln(outfile,'{$ifdef Win32}');
  2881. writeln(outfile,' Windows;');
  2882. writeln(outfile,'{$else}');
  2883. writeln(outfile,' DLLFuncs;');
  2884. writeln(outfile,'{$endif win32}');
  2885. writeln(outfile);
  2886. writeln(outfile,' var');
  2887. writeln(outfile,' hlib : thandle;');
  2888. writeln(outfile);
  2889. writeln(outfile);
  2890. writeln(outfile,' procedure Free',unitname,';');
  2891. writeln(outfile,' begin');
  2892. writeln(outfile,' FreeLibrary(hlib);');
  2893. for i:=0 to (freedynlibproc.Count-1) do
  2894. Writeln(outfile,' ',freedynlibproc[i]);
  2895. writeln(outfile,' end;');
  2896. writeln(outfile);
  2897. writeln(outfile);
  2898. writeln(outfile,' procedure Load',unitname,'(lib : pchar);');
  2899. writeln(outfile,' begin');
  2900. writeln(outfile,' Free',unitname,';');
  2901. writeln(outfile,' hlib:=LoadLibrary(lib);');
  2902. writeln(outfile,' if hlib=0 then');
  2903. writeln(outfile,' raise Exception.Create(format(''Could not load library: %s'',[lib]));');
  2904. writeln(outfile);
  2905. for i:=0 to (loaddynlibproc.Count-1) do
  2906. Writeln(outfile,' ',loaddynlibproc[i]);
  2907. writeln(outfile,' end;');
  2908. writeln(outfile);
  2909. writeln(outfile);
  2910. writeln(outfile,'initialization');
  2911. writeln(outfile,' Load',unitname,'(''',unitname,''');');
  2912. writeln(outfile,'finalization');
  2913. writeln(outfile,' Free',unitname,';');
  2914. end;
  2915. { write end of file }
  2916. writeln(outfile);
  2917. if not(includefile) then
  2918. writeln(outfile,'end.');
  2919. { close and erase tempfiles }
  2920. close(implemfile);
  2921. erase(implemfile);
  2922. close(tempfile);
  2923. erase(tempfile);
  2924. flush(outfile);
  2925. {**** generate full file ****}
  2926. assign(headerfile, 'ext4.tmp');
  2927. {$I-}
  2928. rewrite(headerfile);
  2929. {$I+}
  2930. if ioresult<>0 then
  2931. begin
  2932. writeln('file ext4.tmp could not be created!');
  2933. halt(1);
  2934. end;
  2935. WriteFileHeader(HeaderFile);
  2936. { Final output filename }
  2937. assign(finaloutfile, outputfilename);
  2938. {$I-}
  2939. rewrite(finaloutfile);
  2940. {$I+}
  2941. if ioresult<>0 then
  2942. begin
  2943. writeln('file ',outputfilename,' could not be created!');
  2944. halt(1);
  2945. end;
  2946. writeln(finaloutfile);
  2947. { Read unit header file }
  2948. reset(headerfile);
  2949. while not eof(headerfile) do
  2950. begin
  2951. readln(headerfile,SS);
  2952. writeln(finaloutfile,SS);
  2953. end;
  2954. { Read interface and implementation file }
  2955. reset(outfile);
  2956. while not eof(outfile) do
  2957. begin
  2958. readln(outfile,SS);
  2959. writeln(finaloutfile,SS);
  2960. end;
  2961. close(HeaderFile);
  2962. close(outfile);
  2963. close(finaloutfile);
  2964. erase(outfile);
  2965. erase(headerfile);
  2966. PTypeList.Free;
  2967. freedynlibproc.free;
  2968. loaddynlibproc.free;
  2969. end.