test_c_parser.py 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241
  1. from sympy.parsing.sym_expr import SymPyExpression
  2. from sympy.testing.pytest import raises, XFAIL
  3. from sympy.external import import_module
  4. cin = import_module('clang.cindex', import_kwargs = {'fromlist': ['cindex']})
  5. if cin:
  6. from sympy.codegen.ast import (Variable, String, Return,
  7. FunctionDefinition, Integer, Float, Declaration, CodeBlock,
  8. FunctionPrototype, FunctionCall, NoneToken, Assignment, Type,
  9. IntBaseType, SignedIntType, UnsignedIntType, FloatType,
  10. AddAugmentedAssignment, SubAugmentedAssignment,
  11. MulAugmentedAssignment, DivAugmentedAssignment,
  12. ModAugmentedAssignment, While)
  13. from sympy.codegen.cnodes import (PreDecrement, PostDecrement,
  14. PreIncrement, PostIncrement)
  15. from sympy.core import (Add, Mul, Mod, Pow, Rational,
  16. StrictLessThan, LessThan, StrictGreaterThan, GreaterThan,
  17. Equality, Unequality)
  18. from sympy.logic.boolalg import And, Not, Or
  19. from sympy.core.symbol import Symbol
  20. from sympy.logic.boolalg import (false, true)
  21. import os
  22. def test_variable():
  23. c_src1 = (
  24. 'int a;' + '\n' +
  25. 'int b;' + '\n'
  26. )
  27. c_src2 = (
  28. 'float a;' + '\n'
  29. + 'float b;' + '\n'
  30. )
  31. c_src3 = (
  32. 'int a;' + '\n' +
  33. 'float b;' + '\n' +
  34. 'int c;'
  35. )
  36. c_src4 = (
  37. 'int x = 1, y = 6.78;' + '\n' +
  38. 'float p = 2, q = 9.67;'
  39. )
  40. res1 = SymPyExpression(c_src1, 'c').return_expr()
  41. res2 = SymPyExpression(c_src2, 'c').return_expr()
  42. res3 = SymPyExpression(c_src3, 'c').return_expr()
  43. res4 = SymPyExpression(c_src4, 'c').return_expr()
  44. assert res1[0] == Declaration(
  45. Variable(
  46. Symbol('a'),
  47. type=IntBaseType(String('intc'))
  48. )
  49. )
  50. assert res1[1] == Declaration(
  51. Variable(
  52. Symbol('b'),
  53. type=IntBaseType(String('intc'))
  54. )
  55. )
  56. assert res2[0] == Declaration(
  57. Variable(
  58. Symbol('a'),
  59. type=FloatType(
  60. String('float32'),
  61. nbits=Integer(32),
  62. nmant=Integer(23),
  63. nexp=Integer(8)
  64. )
  65. )
  66. )
  67. assert res2[1] == Declaration(
  68. Variable(
  69. Symbol('b'),
  70. type=FloatType(
  71. String('float32'),
  72. nbits=Integer(32),
  73. nmant=Integer(23),
  74. nexp=Integer(8)
  75. )
  76. )
  77. )
  78. assert res3[0] == Declaration(
  79. Variable(
  80. Symbol('a'),
  81. type=IntBaseType(String('intc'))
  82. )
  83. )
  84. assert res3[1] == Declaration(
  85. Variable(
  86. Symbol('b'),
  87. type=FloatType(
  88. String('float32'),
  89. nbits=Integer(32),
  90. nmant=Integer(23),
  91. nexp=Integer(8)
  92. )
  93. )
  94. )
  95. assert res3[2] == Declaration(
  96. Variable(
  97. Symbol('c'),
  98. type=IntBaseType(String('intc'))
  99. )
  100. )
  101. assert res4[0] == Declaration(
  102. Variable(
  103. Symbol('x'),
  104. type=IntBaseType(String('intc')),
  105. value=Integer(1)
  106. )
  107. )
  108. assert res4[1] == Declaration(
  109. Variable(
  110. Symbol('y'),
  111. type=IntBaseType(String('intc')),
  112. value=Integer(6)
  113. )
  114. )
  115. assert res4[2] == Declaration(
  116. Variable(
  117. Symbol('p'),
  118. type=FloatType(
  119. String('float32'),
  120. nbits=Integer(32),
  121. nmant=Integer(23),
  122. nexp=Integer(8)
  123. ),
  124. value=Float('2.0', precision=53)
  125. )
  126. )
  127. assert res4[3] == Declaration(
  128. Variable(
  129. Symbol('q'),
  130. type=FloatType(
  131. String('float32'),
  132. nbits=Integer(32),
  133. nmant=Integer(23),
  134. nexp=Integer(8)
  135. ),
  136. value=Float('9.67', precision=53)
  137. )
  138. )
  139. @XFAIL
  140. def test_int():
  141. c_src1 = 'int a = 1;'
  142. c_src2 = (
  143. 'int a = 1;' + '\n' +
  144. 'int b = 2;' + '\n'
  145. )
  146. c_src3 = 'int a = 2.345, b = 5.67;'
  147. c_src4 = 'int p = 6, q = 23.45;'
  148. c_src5 = "int x = '0', y = 'a';"
  149. c_src6 = "int r = true, s = false;"
  150. # cin.TypeKind.UCHAR
  151. c_src_type1 = (
  152. "signed char a = 1, b = 5.1;"
  153. )
  154. # cin.TypeKind.SHORT
  155. c_src_type2 = (
  156. "short a = 1, b = 5.1;"
  157. "signed short c = 1, d = 5.1;"
  158. "short int e = 1, f = 5.1;"
  159. "signed short int g = 1, h = 5.1;"
  160. )
  161. # cin.TypeKind.INT
  162. c_src_type3 = (
  163. "signed int a = 1, b = 5.1;"
  164. "int c = 1, d = 5.1;"
  165. )
  166. # cin.TypeKind.LONG
  167. c_src_type4 = (
  168. "long a = 1, b = 5.1;"
  169. "long int c = 1, d = 5.1;"
  170. )
  171. # cin.TypeKind.UCHAR
  172. c_src_type5 = "unsigned char a = 1, b = 5.1;"
  173. # cin.TypeKind.USHORT
  174. c_src_type6 = (
  175. "unsigned short a = 1, b = 5.1;"
  176. "unsigned short int c = 1, d = 5.1;"
  177. )
  178. # cin.TypeKind.UINT
  179. c_src_type7 = "unsigned int a = 1, b = 5.1;"
  180. # cin.TypeKind.ULONG
  181. c_src_type8 = (
  182. "unsigned long a = 1, b = 5.1;"
  183. "unsigned long int c = 1, d = 5.1;"
  184. )
  185. res1 = SymPyExpression(c_src1, 'c').return_expr()
  186. res2 = SymPyExpression(c_src2, 'c').return_expr()
  187. res3 = SymPyExpression(c_src3, 'c').return_expr()
  188. res4 = SymPyExpression(c_src4, 'c').return_expr()
  189. res5 = SymPyExpression(c_src5, 'c').return_expr()
  190. res6 = SymPyExpression(c_src6, 'c').return_expr()
  191. res_type1 = SymPyExpression(c_src_type1, 'c').return_expr()
  192. res_type2 = SymPyExpression(c_src_type2, 'c').return_expr()
  193. res_type3 = SymPyExpression(c_src_type3, 'c').return_expr()
  194. res_type4 = SymPyExpression(c_src_type4, 'c').return_expr()
  195. res_type5 = SymPyExpression(c_src_type5, 'c').return_expr()
  196. res_type6 = SymPyExpression(c_src_type6, 'c').return_expr()
  197. res_type7 = SymPyExpression(c_src_type7, 'c').return_expr()
  198. res_type8 = SymPyExpression(c_src_type8, 'c').return_expr()
  199. assert res1[0] == Declaration(
  200. Variable(
  201. Symbol('a'),
  202. type=IntBaseType(String('intc')),
  203. value=Integer(1)
  204. )
  205. )
  206. assert res2[0] == Declaration(
  207. Variable(
  208. Symbol('a'),
  209. type=IntBaseType(String('intc')),
  210. value=Integer(1)
  211. )
  212. )
  213. assert res2[1] == Declaration(
  214. Variable(
  215. Symbol('b'),
  216. type=IntBaseType(String('intc')),
  217. value=Integer(2)
  218. )
  219. )
  220. assert res3[0] == Declaration(
  221. Variable(
  222. Symbol('a'),
  223. type=IntBaseType(String('intc')),
  224. value=Integer(2)
  225. )
  226. )
  227. assert res3[1] == Declaration(
  228. Variable(
  229. Symbol('b'),
  230. type=IntBaseType(String('intc')),
  231. value=Integer(5)
  232. )
  233. )
  234. assert res4[0] == Declaration(
  235. Variable(
  236. Symbol('p'),
  237. type=IntBaseType(String('intc')),
  238. value=Integer(6)
  239. )
  240. )
  241. assert res4[1] == Declaration(
  242. Variable(
  243. Symbol('q'),
  244. type=IntBaseType(String('intc')),
  245. value=Integer(23)
  246. )
  247. )
  248. assert res5[0] == Declaration(
  249. Variable(
  250. Symbol('x'),
  251. type=IntBaseType(String('intc')),
  252. value=Integer(48)
  253. )
  254. )
  255. assert res5[1] == Declaration(
  256. Variable(
  257. Symbol('y'),
  258. type=IntBaseType(String('intc')),
  259. value=Integer(97)
  260. )
  261. )
  262. assert res6[0] == Declaration(
  263. Variable(
  264. Symbol('r'),
  265. type=IntBaseType(String('intc')),
  266. value=Integer(1)
  267. )
  268. )
  269. assert res6[1] == Declaration(
  270. Variable(
  271. Symbol('s'),
  272. type=IntBaseType(String('intc')),
  273. value=Integer(0)
  274. )
  275. )
  276. assert res_type1[0] == Declaration(
  277. Variable(
  278. Symbol('a'),
  279. type=SignedIntType(
  280. String('int8'),
  281. nbits=Integer(8)
  282. ),
  283. value=Integer(1)
  284. )
  285. )
  286. assert res_type1[1] == Declaration(
  287. Variable(
  288. Symbol('b'),
  289. type=SignedIntType(
  290. String('int8'),
  291. nbits=Integer(8)
  292. ),
  293. value=Integer(5)
  294. )
  295. )
  296. assert res_type2[0] == Declaration(
  297. Variable(
  298. Symbol('a'),
  299. type=SignedIntType(
  300. String('int16'),
  301. nbits=Integer(16)
  302. ),
  303. value=Integer(1)
  304. )
  305. )
  306. assert res_type2[1] == Declaration(
  307. Variable(
  308. Symbol('b'),
  309. type=SignedIntType(
  310. String('int16'),
  311. nbits=Integer(16)
  312. ),
  313. value=Integer(5)
  314. )
  315. )
  316. assert res_type2[2] == Declaration(
  317. Variable(Symbol('c'),
  318. type=SignedIntType(
  319. String('int16'),
  320. nbits=Integer(16)
  321. ),
  322. value=Integer(1)
  323. )
  324. )
  325. assert res_type2[3] == Declaration(
  326. Variable(
  327. Symbol('d'),
  328. type=SignedIntType(
  329. String('int16'),
  330. nbits=Integer(16)
  331. ),
  332. value=Integer(5)
  333. )
  334. )
  335. assert res_type2[4] == Declaration(
  336. Variable(
  337. Symbol('e'),
  338. type=SignedIntType(
  339. String('int16'),
  340. nbits=Integer(16)
  341. ),
  342. value=Integer(1)
  343. )
  344. )
  345. assert res_type2[5] == Declaration(
  346. Variable(
  347. Symbol('f'),
  348. type=SignedIntType(
  349. String('int16'),
  350. nbits=Integer(16)
  351. ),
  352. value=Integer(5)
  353. )
  354. )
  355. assert res_type2[6] == Declaration(
  356. Variable(
  357. Symbol('g'),
  358. type=SignedIntType(
  359. String('int16'),
  360. nbits=Integer(16)
  361. ),
  362. value=Integer(1)
  363. )
  364. )
  365. assert res_type2[7] == Declaration(
  366. Variable(
  367. Symbol('h'),
  368. type=SignedIntType(
  369. String('int16'),
  370. nbits=Integer(16)
  371. ),
  372. value=Integer(5)
  373. )
  374. )
  375. assert res_type3[0] == Declaration(
  376. Variable(
  377. Symbol('a'),
  378. type=IntBaseType(String('intc')),
  379. value=Integer(1)
  380. )
  381. )
  382. assert res_type3[1] == Declaration(
  383. Variable(
  384. Symbol('b'),
  385. type=IntBaseType(String('intc')),
  386. value=Integer(5)
  387. )
  388. )
  389. assert res_type3[2] == Declaration(
  390. Variable(
  391. Symbol('c'),
  392. type=IntBaseType(String('intc')),
  393. value=Integer(1)
  394. )
  395. )
  396. assert res_type3[3] == Declaration(
  397. Variable(
  398. Symbol('d'),
  399. type=IntBaseType(String('intc')),
  400. value=Integer(5)
  401. )
  402. )
  403. assert res_type4[0] == Declaration(
  404. Variable(
  405. Symbol('a'),
  406. type=SignedIntType(
  407. String('int64'),
  408. nbits=Integer(64)
  409. ),
  410. value=Integer(1)
  411. )
  412. )
  413. assert res_type4[1] == Declaration(
  414. Variable(
  415. Symbol('b'),
  416. type=SignedIntType(
  417. String('int64'),
  418. nbits=Integer(64)
  419. ),
  420. value=Integer(5)
  421. )
  422. )
  423. assert res_type4[2] == Declaration(
  424. Variable(
  425. Symbol('c'),
  426. type=SignedIntType(
  427. String('int64'),
  428. nbits=Integer(64)
  429. ),
  430. value=Integer(1)
  431. )
  432. )
  433. assert res_type4[3] == Declaration(
  434. Variable(
  435. Symbol('d'),
  436. type=SignedIntType(
  437. String('int64'),
  438. nbits=Integer(64)
  439. ),
  440. value=Integer(5)
  441. )
  442. )
  443. assert res_type5[0] == Declaration(
  444. Variable(
  445. Symbol('a'),
  446. type=UnsignedIntType(
  447. String('uint8'),
  448. nbits=Integer(8)
  449. ),
  450. value=Integer(1)
  451. )
  452. )
  453. assert res_type5[1] == Declaration(
  454. Variable(
  455. Symbol('b'),
  456. type=UnsignedIntType(
  457. String('uint8'),
  458. nbits=Integer(8)
  459. ),
  460. value=Integer(5)
  461. )
  462. )
  463. assert res_type6[0] == Declaration(
  464. Variable(
  465. Symbol('a'),
  466. type=UnsignedIntType(
  467. String('uint16'),
  468. nbits=Integer(16)
  469. ),
  470. value=Integer(1)
  471. )
  472. )
  473. assert res_type6[1] == Declaration(
  474. Variable(
  475. Symbol('b'),
  476. type=UnsignedIntType(
  477. String('uint16'),
  478. nbits=Integer(16)
  479. ),
  480. value=Integer(5)
  481. )
  482. )
  483. assert res_type6[2] == Declaration(
  484. Variable(
  485. Symbol('c'),
  486. type=UnsignedIntType(
  487. String('uint16'),
  488. nbits=Integer(16)
  489. ),
  490. value=Integer(1)
  491. )
  492. )
  493. assert res_type6[3] == Declaration(
  494. Variable(
  495. Symbol('d'),
  496. type=UnsignedIntType(
  497. String('uint16'),
  498. nbits=Integer(16)
  499. ),
  500. value=Integer(5)
  501. )
  502. )
  503. assert res_type7[0] == Declaration(
  504. Variable(
  505. Symbol('a'),
  506. type=UnsignedIntType(
  507. String('uint32'),
  508. nbits=Integer(32)
  509. ),
  510. value=Integer(1)
  511. )
  512. )
  513. assert res_type7[1] == Declaration(
  514. Variable(
  515. Symbol('b'),
  516. type=UnsignedIntType(
  517. String('uint32'),
  518. nbits=Integer(32)
  519. ),
  520. value=Integer(5)
  521. )
  522. )
  523. assert res_type8[0] == Declaration(
  524. Variable(
  525. Symbol('a'),
  526. type=UnsignedIntType(
  527. String('uint64'),
  528. nbits=Integer(64)
  529. ),
  530. value=Integer(1)
  531. )
  532. )
  533. assert res_type8[1] == Declaration(
  534. Variable(
  535. Symbol('b'),
  536. type=UnsignedIntType(
  537. String('uint64'),
  538. nbits=Integer(64)
  539. ),
  540. value=Integer(5)
  541. )
  542. )
  543. assert res_type8[2] == Declaration(
  544. Variable(
  545. Symbol('c'),
  546. type=UnsignedIntType(
  547. String('uint64'),
  548. nbits=Integer(64)
  549. ),
  550. value=Integer(1)
  551. )
  552. )
  553. assert res_type8[3] == Declaration(
  554. Variable(
  555. Symbol('d'),
  556. type=UnsignedIntType(
  557. String('uint64'),
  558. nbits=Integer(64)
  559. ),
  560. value=Integer(5)
  561. )
  562. )
  563. @XFAIL
  564. def test_float():
  565. c_src1 = 'float a = 1.0;'
  566. c_src2 = (
  567. 'float a = 1.25;' + '\n' +
  568. 'float b = 2.39;' + '\n'
  569. )
  570. c_src3 = 'float x = 1, y = 2;'
  571. c_src4 = 'float p = 5, e = 7.89;'
  572. c_src5 = 'float r = true, s = false;'
  573. # cin.TypeKind.FLOAT
  574. c_src_type1 = 'float x = 1, y = 2.5;'
  575. # cin.TypeKind.DOUBLE
  576. c_src_type2 = 'double x = 1, y = 2.5;'
  577. # cin.TypeKind.LONGDOUBLE
  578. c_src_type3 = 'long double x = 1, y = 2.5;'
  579. res1 = SymPyExpression(c_src1, 'c').return_expr()
  580. res2 = SymPyExpression(c_src2, 'c').return_expr()
  581. res3 = SymPyExpression(c_src3, 'c').return_expr()
  582. res4 = SymPyExpression(c_src4, 'c').return_expr()
  583. res5 = SymPyExpression(c_src5, 'c').return_expr()
  584. res_type1 = SymPyExpression(c_src_type1, 'c').return_expr()
  585. res_type2 = SymPyExpression(c_src_type2, 'c').return_expr()
  586. res_type3 = SymPyExpression(c_src_type3, 'c').return_expr()
  587. assert res1[0] == Declaration(
  588. Variable(
  589. Symbol('a'),
  590. type=FloatType(
  591. String('float32'),
  592. nbits=Integer(32),
  593. nmant=Integer(23),
  594. nexp=Integer(8)
  595. ),
  596. value=Float('1.0', precision=53)
  597. )
  598. )
  599. assert res2[0] == Declaration(
  600. Variable(
  601. Symbol('a'),
  602. type=FloatType(
  603. String('float32'),
  604. nbits=Integer(32),
  605. nmant=Integer(23),
  606. nexp=Integer(8)
  607. ),
  608. value=Float('1.25', precision=53)
  609. )
  610. )
  611. assert res2[1] == Declaration(
  612. Variable(
  613. Symbol('b'),
  614. type=FloatType(
  615. String('float32'),
  616. nbits=Integer(32),
  617. nmant=Integer(23),
  618. nexp=Integer(8)
  619. ),
  620. value=Float('2.3900000000000001', precision=53)
  621. )
  622. )
  623. assert res3[0] == Declaration(
  624. Variable(
  625. Symbol('x'),
  626. type=FloatType(
  627. String('float32'),
  628. nbits=Integer(32),
  629. nmant=Integer(23),
  630. nexp=Integer(8)
  631. ),
  632. value=Float('1.0', precision=53)
  633. )
  634. )
  635. assert res3[1] == Declaration(
  636. Variable(
  637. Symbol('y'),
  638. type=FloatType(
  639. String('float32'),
  640. nbits=Integer(32),
  641. nmant=Integer(23),
  642. nexp=Integer(8)
  643. ),
  644. value=Float('2.0', precision=53)
  645. )
  646. )
  647. assert res4[0] == Declaration(
  648. Variable(
  649. Symbol('p'),
  650. type=FloatType(
  651. String('float32'),
  652. nbits=Integer(32),
  653. nmant=Integer(23),
  654. nexp=Integer(8)
  655. ),
  656. value=Float('5.0', precision=53)
  657. )
  658. )
  659. assert res4[1] == Declaration(
  660. Variable(
  661. Symbol('e'),
  662. type=FloatType(
  663. String('float32'),
  664. nbits=Integer(32),
  665. nmant=Integer(23),
  666. nexp=Integer(8)
  667. ),
  668. value=Float('7.89', precision=53)
  669. )
  670. )
  671. assert res5[0] == Declaration(
  672. Variable(
  673. Symbol('r'),
  674. type=FloatType(
  675. String('float32'),
  676. nbits=Integer(32),
  677. nmant=Integer(23),
  678. nexp=Integer(8)
  679. ),
  680. value=Float('1.0', precision=53)
  681. )
  682. )
  683. assert res5[1] == Declaration(
  684. Variable(
  685. Symbol('s'),
  686. type=FloatType(
  687. String('float32'),
  688. nbits=Integer(32),
  689. nmant=Integer(23),
  690. nexp=Integer(8)
  691. ),
  692. value=Float('0.0', precision=53)
  693. )
  694. )
  695. assert res_type1[0] == Declaration(
  696. Variable(
  697. Symbol('x'),
  698. type=FloatType(
  699. String('float32'),
  700. nbits=Integer(32),
  701. nmant=Integer(23),
  702. nexp=Integer(8)
  703. ),
  704. value=Float('1.0', precision=53)
  705. )
  706. )
  707. assert res_type1[1] == Declaration(
  708. Variable(
  709. Symbol('y'),
  710. type=FloatType(
  711. String('float32'),
  712. nbits=Integer(32),
  713. nmant=Integer(23),
  714. nexp=Integer(8)
  715. ),
  716. value=Float('2.5', precision=53)
  717. )
  718. )
  719. assert res_type2[0] == Declaration(
  720. Variable(
  721. Symbol('x'),
  722. type=FloatType(
  723. String('float64'),
  724. nbits=Integer(64),
  725. nmant=Integer(52),
  726. nexp=Integer(11)
  727. ),
  728. value=Float('1.0', precision=53)
  729. )
  730. )
  731. assert res_type2[1] == Declaration(
  732. Variable(
  733. Symbol('y'),
  734. type=FloatType(
  735. String('float64'),
  736. nbits=Integer(64),
  737. nmant=Integer(52),
  738. nexp=Integer(11)
  739. ),
  740. value=Float('2.5', precision=53)
  741. )
  742. )
  743. assert res_type3[0] == Declaration(
  744. Variable(
  745. Symbol('x'),
  746. type=FloatType(
  747. String('float80'),
  748. nbits=Integer(80),
  749. nmant=Integer(63),
  750. nexp=Integer(15)
  751. ),
  752. value=Float('1.0', precision=53)
  753. )
  754. )
  755. assert res_type3[1] == Declaration(
  756. Variable(
  757. Symbol('y'),
  758. type=FloatType(
  759. String('float80'),
  760. nbits=Integer(80),
  761. nmant=Integer(63),
  762. nexp=Integer(15)
  763. ),
  764. value=Float('2.5', precision=53)
  765. )
  766. )
  767. @XFAIL
  768. def test_bool():
  769. c_src1 = (
  770. 'bool a = true, b = false;'
  771. )
  772. c_src2 = (
  773. 'bool a = 1, b = 0;'
  774. )
  775. c_src3 = (
  776. 'bool a = 10, b = 20;'
  777. )
  778. c_src4 = (
  779. 'bool a = 19.1, b = 9.0, c = 0.0;'
  780. )
  781. res1 = SymPyExpression(c_src1, 'c').return_expr()
  782. res2 = SymPyExpression(c_src2, 'c').return_expr()
  783. res3 = SymPyExpression(c_src3, 'c').return_expr()
  784. res4 = SymPyExpression(c_src4, 'c').return_expr()
  785. assert res1[0] == Declaration(
  786. Variable(Symbol('a'),
  787. type=Type(String('bool')),
  788. value=true
  789. )
  790. )
  791. assert res1[1] == Declaration(
  792. Variable(Symbol('b'),
  793. type=Type(String('bool')),
  794. value=false
  795. )
  796. )
  797. assert res2[0] == Declaration(
  798. Variable(Symbol('a'),
  799. type=Type(String('bool')),
  800. value=true)
  801. )
  802. assert res2[1] == Declaration(
  803. Variable(Symbol('b'),
  804. type=Type(String('bool')),
  805. value=false
  806. )
  807. )
  808. assert res3[0] == Declaration(
  809. Variable(Symbol('a'),
  810. type=Type(String('bool')),
  811. value=true
  812. )
  813. )
  814. assert res3[1] == Declaration(
  815. Variable(Symbol('b'),
  816. type=Type(String('bool')),
  817. value=true
  818. )
  819. )
  820. assert res4[0] == Declaration(
  821. Variable(Symbol('a'),
  822. type=Type(String('bool')),
  823. value=true)
  824. )
  825. assert res4[1] == Declaration(
  826. Variable(Symbol('b'),
  827. type=Type(String('bool')),
  828. value=true
  829. )
  830. )
  831. assert res4[2] == Declaration(
  832. Variable(Symbol('c'),
  833. type=Type(String('bool')),
  834. value=false
  835. )
  836. )
  837. def test_function():
  838. c_src1 = (
  839. 'void fun1()' + '\n' +
  840. '{' + '\n' +
  841. 'int a;' + '\n' +
  842. '}'
  843. )
  844. c_src2 = (
  845. 'int fun2()' + '\n' +
  846. '{'+ '\n' +
  847. 'int a;' + '\n' +
  848. 'return a;' + '\n' +
  849. '}'
  850. )
  851. c_src3 = (
  852. 'float fun3()' + '\n' +
  853. '{' + '\n' +
  854. 'float b;' + '\n' +
  855. 'return b;' + '\n' +
  856. '}'
  857. )
  858. c_src4 = (
  859. 'float fun4()' + '\n' +
  860. '{}'
  861. )
  862. res1 = SymPyExpression(c_src1, 'c').return_expr()
  863. res2 = SymPyExpression(c_src2, 'c').return_expr()
  864. res3 = SymPyExpression(c_src3, 'c').return_expr()
  865. res4 = SymPyExpression(c_src4, 'c').return_expr()
  866. assert res1[0] == FunctionDefinition(
  867. NoneToken(),
  868. name=String('fun1'),
  869. parameters=(),
  870. body=CodeBlock(
  871. Declaration(
  872. Variable(
  873. Symbol('a'),
  874. type=IntBaseType(String('intc'))
  875. )
  876. )
  877. )
  878. )
  879. assert res2[0] == FunctionDefinition(
  880. IntBaseType(String('intc')),
  881. name=String('fun2'),
  882. parameters=(),
  883. body=CodeBlock(
  884. Declaration(
  885. Variable(
  886. Symbol('a'),
  887. type=IntBaseType(String('intc'))
  888. )
  889. ),
  890. Return('a')
  891. )
  892. )
  893. assert res3[0] == FunctionDefinition(
  894. FloatType(
  895. String('float32'),
  896. nbits=Integer(32),
  897. nmant=Integer(23),
  898. nexp=Integer(8)
  899. ),
  900. name=String('fun3'),
  901. parameters=(),
  902. body=CodeBlock(
  903. Declaration(
  904. Variable(
  905. Symbol('b'),
  906. type=FloatType(
  907. String('float32'),
  908. nbits=Integer(32),
  909. nmant=Integer(23),
  910. nexp=Integer(8)
  911. )
  912. )
  913. ),
  914. Return('b')
  915. )
  916. )
  917. assert res4[0] == FunctionPrototype(
  918. FloatType(
  919. String('float32'),
  920. nbits=Integer(32),
  921. nmant=Integer(23),
  922. nexp=Integer(8)
  923. ),
  924. name=String('fun4'),
  925. parameters=()
  926. )
  927. def test_parameters():
  928. c_src1 = (
  929. 'void fun1( int a)' + '\n' +
  930. '{' + '\n' +
  931. 'int i;' + '\n' +
  932. '}'
  933. )
  934. c_src2 = (
  935. 'int fun2(float x, float y)' + '\n' +
  936. '{'+ '\n' +
  937. 'int a;' + '\n' +
  938. 'return a;' + '\n' +
  939. '}'
  940. )
  941. c_src3 = (
  942. 'float fun3(int p, float q, int r)' + '\n' +
  943. '{' + '\n' +
  944. 'float b;' + '\n' +
  945. 'return b;' + '\n' +
  946. '}'
  947. )
  948. res1 = SymPyExpression(c_src1, 'c').return_expr()
  949. res2 = SymPyExpression(c_src2, 'c').return_expr()
  950. res3 = SymPyExpression(c_src3, 'c').return_expr()
  951. assert res1[0] == FunctionDefinition(
  952. NoneToken(),
  953. name=String('fun1'),
  954. parameters=(
  955. Variable(
  956. Symbol('a'),
  957. type=IntBaseType(String('intc'))
  958. ),
  959. ),
  960. body=CodeBlock(
  961. Declaration(
  962. Variable(
  963. Symbol('i'),
  964. type=IntBaseType(String('intc'))
  965. )
  966. )
  967. )
  968. )
  969. assert res2[0] == FunctionDefinition(
  970. IntBaseType(String('intc')),
  971. name=String('fun2'),
  972. parameters=(
  973. Variable(
  974. Symbol('x'),
  975. type=FloatType(
  976. String('float32'),
  977. nbits=Integer(32),
  978. nmant=Integer(23),
  979. nexp=Integer(8)
  980. )
  981. ),
  982. Variable(
  983. Symbol('y'),
  984. type=FloatType(
  985. String('float32'),
  986. nbits=Integer(32),
  987. nmant=Integer(23),
  988. nexp=Integer(8)
  989. )
  990. )
  991. ),
  992. body=CodeBlock(
  993. Declaration(
  994. Variable(
  995. Symbol('a'),
  996. type=IntBaseType(String('intc'))
  997. )
  998. ),
  999. Return('a')
  1000. )
  1001. )
  1002. assert res3[0] == FunctionDefinition(
  1003. FloatType(
  1004. String('float32'),
  1005. nbits=Integer(32),
  1006. nmant=Integer(23),
  1007. nexp=Integer(8)
  1008. ),
  1009. name=String('fun3'),
  1010. parameters=(
  1011. Variable(
  1012. Symbol('p'),
  1013. type=IntBaseType(String('intc'))
  1014. ),
  1015. Variable(
  1016. Symbol('q'),
  1017. type=FloatType(
  1018. String('float32'),
  1019. nbits=Integer(32),
  1020. nmant=Integer(23),
  1021. nexp=Integer(8)
  1022. )
  1023. ),
  1024. Variable(
  1025. Symbol('r'),
  1026. type=IntBaseType(String('intc'))
  1027. )
  1028. ),
  1029. body=CodeBlock(
  1030. Declaration(
  1031. Variable(
  1032. Symbol('b'),
  1033. type=FloatType(
  1034. String('float32'),
  1035. nbits=Integer(32),
  1036. nmant=Integer(23),
  1037. nexp=Integer(8)
  1038. )
  1039. )
  1040. ),
  1041. Return('b')
  1042. )
  1043. )
  1044. def test_function_call():
  1045. c_src1 = (
  1046. 'int fun1(int x)' + '\n' +
  1047. '{' + '\n' +
  1048. 'return x;' + '\n' +
  1049. '}' + '\n' +
  1050. 'void caller()' + '\n' +
  1051. '{' + '\n' +
  1052. 'int x = fun1(2);' + '\n' +
  1053. '}'
  1054. )
  1055. c_src2 = (
  1056. 'int fun2(int a, int b, int c)' + '\n' +
  1057. '{' + '\n' +
  1058. 'return a;' + '\n' +
  1059. '}' + '\n' +
  1060. 'void caller()' + '\n' +
  1061. '{' + '\n' +
  1062. 'int y = fun2(2, 3, 4);' + '\n' +
  1063. '}'
  1064. )
  1065. c_src3 = (
  1066. 'int fun3(int a, int b, int c)' + '\n' +
  1067. '{' + '\n' +
  1068. 'return b;' + '\n' +
  1069. '}' + '\n' +
  1070. 'void caller()' + '\n' +
  1071. '{' + '\n' +
  1072. 'int p;' + '\n' +
  1073. 'int q;' + '\n' +
  1074. 'int r;' + '\n' +
  1075. 'int z = fun3(p, q, r);' + '\n' +
  1076. '}'
  1077. )
  1078. c_src4 = (
  1079. 'int fun4(float a, float b, int c)' + '\n' +
  1080. '{' + '\n' +
  1081. 'return c;' + '\n' +
  1082. '}' + '\n' +
  1083. 'void caller()' + '\n' +
  1084. '{' + '\n' +
  1085. 'float x;' + '\n' +
  1086. 'float y;' + '\n' +
  1087. 'int z;' + '\n' +
  1088. 'int i = fun4(x, y, z)' + '\n' +
  1089. '}'
  1090. )
  1091. c_src5 = (
  1092. 'int fun()' + '\n' +
  1093. '{' + '\n' +
  1094. 'return 1;' + '\n' +
  1095. '}' + '\n' +
  1096. 'void caller()' + '\n' +
  1097. '{' + '\n' +
  1098. 'int a = fun()' + '\n' +
  1099. '}'
  1100. )
  1101. res1 = SymPyExpression(c_src1, 'c').return_expr()
  1102. res2 = SymPyExpression(c_src2, 'c').return_expr()
  1103. res3 = SymPyExpression(c_src3, 'c').return_expr()
  1104. res4 = SymPyExpression(c_src4, 'c').return_expr()
  1105. res5 = SymPyExpression(c_src5, 'c').return_expr()
  1106. assert res1[0] == FunctionDefinition(
  1107. IntBaseType(String('intc')),
  1108. name=String('fun1'),
  1109. parameters=(Variable(Symbol('x'),
  1110. type=IntBaseType(String('intc'))
  1111. ),
  1112. ),
  1113. body=CodeBlock(
  1114. Return('x')
  1115. )
  1116. )
  1117. assert res1[1] == FunctionDefinition(
  1118. NoneToken(),
  1119. name=String('caller'),
  1120. parameters=(),
  1121. body=CodeBlock(
  1122. Declaration(
  1123. Variable(Symbol('x'),
  1124. value=FunctionCall(String('fun1'),
  1125. function_args=(
  1126. Integer(2),
  1127. )
  1128. )
  1129. )
  1130. )
  1131. )
  1132. )
  1133. assert res2[0] == FunctionDefinition(
  1134. IntBaseType(String('intc')),
  1135. name=String('fun2'),
  1136. parameters=(Variable(Symbol('a'),
  1137. type=IntBaseType(String('intc'))
  1138. ),
  1139. Variable(Symbol('b'),
  1140. type=IntBaseType(String('intc'))
  1141. ),
  1142. Variable(Symbol('c'),
  1143. type=IntBaseType(String('intc'))
  1144. )
  1145. ),
  1146. body=CodeBlock(
  1147. Return('a')
  1148. )
  1149. )
  1150. assert res2[1] == FunctionDefinition(
  1151. NoneToken(),
  1152. name=String('caller'),
  1153. parameters=(),
  1154. body=CodeBlock(
  1155. Declaration(
  1156. Variable(Symbol('y'),
  1157. value=FunctionCall(
  1158. String('fun2'),
  1159. function_args=(
  1160. Integer(2),
  1161. Integer(3),
  1162. Integer(4)
  1163. )
  1164. )
  1165. )
  1166. )
  1167. )
  1168. )
  1169. assert res3[0] == FunctionDefinition(
  1170. IntBaseType(String('intc')),
  1171. name=String('fun3'),
  1172. parameters=(
  1173. Variable(Symbol('a'),
  1174. type=IntBaseType(String('intc'))
  1175. ),
  1176. Variable(Symbol('b'),
  1177. type=IntBaseType(String('intc'))
  1178. ),
  1179. Variable(Symbol('c'),
  1180. type=IntBaseType(String('intc'))
  1181. )
  1182. ),
  1183. body=CodeBlock(
  1184. Return('b')
  1185. )
  1186. )
  1187. assert res3[1] == FunctionDefinition(
  1188. NoneToken(),
  1189. name=String('caller'),
  1190. parameters=(),
  1191. body=CodeBlock(
  1192. Declaration(
  1193. Variable(Symbol('p'),
  1194. type=IntBaseType(String('intc'))
  1195. )
  1196. ),
  1197. Declaration(
  1198. Variable(Symbol('q'),
  1199. type=IntBaseType(String('intc'))
  1200. )
  1201. ),
  1202. Declaration(
  1203. Variable(Symbol('r'),
  1204. type=IntBaseType(String('intc'))
  1205. )
  1206. ),
  1207. Declaration(
  1208. Variable(Symbol('z'),
  1209. value=FunctionCall(
  1210. String('fun3'),
  1211. function_args=(
  1212. Symbol('p'),
  1213. Symbol('q'),
  1214. Symbol('r')
  1215. )
  1216. )
  1217. )
  1218. )
  1219. )
  1220. )
  1221. assert res4[0] == FunctionDefinition(
  1222. IntBaseType(String('intc')),
  1223. name=String('fun4'),
  1224. parameters=(Variable(Symbol('a'),
  1225. type=FloatType(
  1226. String('float32'),
  1227. nbits=Integer(32),
  1228. nmant=Integer(23),
  1229. nexp=Integer(8)
  1230. )
  1231. ),
  1232. Variable(Symbol('b'),
  1233. type=FloatType(
  1234. String('float32'),
  1235. nbits=Integer(32),
  1236. nmant=Integer(23),
  1237. nexp=Integer(8)
  1238. )
  1239. ),
  1240. Variable(Symbol('c'),
  1241. type=IntBaseType(String('intc'))
  1242. )
  1243. ),
  1244. body=CodeBlock(
  1245. Return('c')
  1246. )
  1247. )
  1248. assert res4[1] == FunctionDefinition(
  1249. NoneToken(),
  1250. name=String('caller'),
  1251. parameters=(),
  1252. body=CodeBlock(
  1253. Declaration(
  1254. Variable(Symbol('x'),
  1255. type=FloatType(
  1256. String('float32'),
  1257. nbits=Integer(32),
  1258. nmant=Integer(23),
  1259. nexp=Integer(8)
  1260. )
  1261. )
  1262. ),
  1263. Declaration(
  1264. Variable(Symbol('y'),
  1265. type=FloatType(
  1266. String('float32'),
  1267. nbits=Integer(32),
  1268. nmant=Integer(23),
  1269. nexp=Integer(8)
  1270. )
  1271. )
  1272. ),
  1273. Declaration(
  1274. Variable(Symbol('z'),
  1275. type=IntBaseType(String('intc'))
  1276. )
  1277. ),
  1278. Declaration(
  1279. Variable(Symbol('i'),
  1280. value=FunctionCall(String('fun4'),
  1281. function_args=(
  1282. Symbol('x'),
  1283. Symbol('y'),
  1284. Symbol('z')
  1285. )
  1286. )
  1287. )
  1288. )
  1289. )
  1290. )
  1291. assert res5[0] == FunctionDefinition(
  1292. IntBaseType(String('intc')),
  1293. name=String('fun'),
  1294. parameters=(),
  1295. body=CodeBlock(
  1296. Return('')
  1297. )
  1298. )
  1299. assert res5[1] == FunctionDefinition(
  1300. NoneToken(),
  1301. name=String('caller'),
  1302. parameters=(),
  1303. body=CodeBlock(
  1304. Declaration(
  1305. Variable(Symbol('a'),
  1306. value=FunctionCall(String('fun'),
  1307. function_args=()
  1308. )
  1309. )
  1310. )
  1311. )
  1312. )
  1313. def test_parse():
  1314. c_src1 = (
  1315. 'int a;' + '\n' +
  1316. 'int b;' + '\n'
  1317. )
  1318. c_src2 = (
  1319. 'void fun1()' + '\n' +
  1320. '{' + '\n' +
  1321. 'int a;' + '\n' +
  1322. '}'
  1323. )
  1324. f1 = open('..a.h', 'w')
  1325. f2 = open('..b.h', 'w')
  1326. f1.write(c_src1)
  1327. f2. write(c_src2)
  1328. f1.close()
  1329. f2.close()
  1330. res1 = SymPyExpression('..a.h', 'c').return_expr()
  1331. res2 = SymPyExpression('..b.h', 'c').return_expr()
  1332. os.remove('..a.h')
  1333. os.remove('..b.h')
  1334. assert res1[0] == Declaration(
  1335. Variable(
  1336. Symbol('a'),
  1337. type=IntBaseType(String('intc'))
  1338. )
  1339. )
  1340. assert res1[1] == Declaration(
  1341. Variable(
  1342. Symbol('b'),
  1343. type=IntBaseType(String('intc'))
  1344. )
  1345. )
  1346. assert res2[0] == FunctionDefinition(
  1347. NoneToken(),
  1348. name=String('fun1'),
  1349. parameters=(),
  1350. body=CodeBlock(
  1351. Declaration(
  1352. Variable(
  1353. Symbol('a'),
  1354. type=IntBaseType(String('intc'))
  1355. )
  1356. )
  1357. )
  1358. )
  1359. def test_binary_operators():
  1360. c_src1 = (
  1361. 'void func()'+
  1362. '{' + '\n' +
  1363. 'int a;' + '\n' +
  1364. 'a = 1;' + '\n' +
  1365. '}'
  1366. )
  1367. c_src2 = (
  1368. 'void func()'+
  1369. '{' + '\n' +
  1370. 'int a = 0;' + '\n' +
  1371. 'a = a + 1;' + '\n' +
  1372. 'a = 3*a - 10;' + '\n' +
  1373. '}'
  1374. )
  1375. c_src3 = (
  1376. 'void func()'+
  1377. '{' + '\n' +
  1378. 'int a = 10;' + '\n' +
  1379. 'a = 1 + a - 3 * 6;' + '\n' +
  1380. '}'
  1381. )
  1382. c_src4 = (
  1383. 'void func()'+
  1384. '{' + '\n' +
  1385. 'int a;' + '\n' +
  1386. 'int b;' + '\n' +
  1387. 'a = 100;' + '\n' +
  1388. 'b = a*a + a*a + a + 19*a + 1 + 24;' + '\n' +
  1389. '}'
  1390. )
  1391. c_src5 = (
  1392. 'void func()'+
  1393. '{' + '\n' +
  1394. 'int a;' + '\n' +
  1395. 'int b;' + '\n' +
  1396. 'int c;' + '\n' +
  1397. 'int d;' + '\n' +
  1398. 'a = 1;' + '\n' +
  1399. 'b = 2;' + '\n' +
  1400. 'c = b;' + '\n' +
  1401. 'd = ((a+b)*(a+c))*((c-d)*(a+c));' + '\n' +
  1402. '}'
  1403. )
  1404. c_src6 = (
  1405. 'void func()'+
  1406. '{' + '\n' +
  1407. 'int a;' + '\n' +
  1408. 'int b;' + '\n' +
  1409. 'int c;' + '\n' +
  1410. 'int d;' + '\n' +
  1411. 'a = 1;' + '\n' +
  1412. 'b = 2;' + '\n' +
  1413. 'c = 3;' + '\n' +
  1414. 'd = (a*a*a*a + 3*b*b + b + b + c*d);' + '\n' +
  1415. '}'
  1416. )
  1417. c_src7 = (
  1418. 'void func()'+
  1419. '{' + '\n' +
  1420. 'float a;' + '\n' +
  1421. 'a = 1.01;' + '\n' +
  1422. '}'
  1423. )
  1424. c_src8 = (
  1425. 'void func()'+
  1426. '{' + '\n' +
  1427. 'float a;' + '\n' +
  1428. 'a = 10.0 + 2.5;' + '\n' +
  1429. '}'
  1430. )
  1431. c_src9 = (
  1432. 'void func()'+
  1433. '{' + '\n' +
  1434. 'float a;' + '\n' +
  1435. 'a = 10.0 / 2.5;' + '\n' +
  1436. '}'
  1437. )
  1438. c_src10 = (
  1439. 'void func()'+
  1440. '{' + '\n' +
  1441. 'int a;' + '\n' +
  1442. 'a = 100 / 4;' + '\n' +
  1443. '}'
  1444. )
  1445. c_src11 = (
  1446. 'void func()'+
  1447. '{' + '\n' +
  1448. 'int a;' + '\n' +
  1449. 'a = 20 - 100 / 4 * 5 + 10;' + '\n' +
  1450. '}'
  1451. )
  1452. c_src12 = (
  1453. 'void func()'+
  1454. '{' + '\n' +
  1455. 'int a;' + '\n' +
  1456. 'a = (20 - 100) / 4 * (5 + 10);' + '\n' +
  1457. '}'
  1458. )
  1459. c_src13 = (
  1460. 'void func()'+
  1461. '{' + '\n' +
  1462. 'int a;' + '\n' +
  1463. 'int b;' + '\n' +
  1464. 'float c;' + '\n' +
  1465. 'c = b/a;' + '\n' +
  1466. '}'
  1467. )
  1468. c_src14 = (
  1469. 'void func()'+
  1470. '{' + '\n' +
  1471. 'int a = 2;' + '\n' +
  1472. 'int d = 5;' + '\n' +
  1473. 'int n = 10;' + '\n' +
  1474. 'int s;' + '\n' +
  1475. 's = (a/2)*(2*a + (n-1)*d);' + '\n' +
  1476. '}'
  1477. )
  1478. c_src15 = (
  1479. 'void func()'+
  1480. '{' + '\n' +
  1481. 'int a;' + '\n' +
  1482. 'a = 1 % 2;' + '\n' +
  1483. '}'
  1484. )
  1485. c_src16 = (
  1486. 'void func()'+
  1487. '{' + '\n' +
  1488. 'int a = 2;' + '\n' +
  1489. 'int b;' + '\n' +
  1490. 'b = a % 3;' + '\n' +
  1491. '}'
  1492. )
  1493. c_src17 = (
  1494. 'void func()'+
  1495. '{' + '\n' +
  1496. 'int a = 100;' + '\n' +
  1497. 'int b = 3;' + '\n' +
  1498. 'int c;' + '\n' +
  1499. 'c = a % b;' + '\n' +
  1500. '}'
  1501. )
  1502. c_src18 = (
  1503. 'void func()'+
  1504. '{' + '\n' +
  1505. 'int a = 100;' + '\n' +
  1506. 'int b = 3;' + '\n' +
  1507. 'int mod = 1000000007;' + '\n' +
  1508. 'int c;' + '\n' +
  1509. 'c = (a + b * (100/a)) % mod;' + '\n' +
  1510. '}'
  1511. )
  1512. c_src19 = (
  1513. 'void func()'+
  1514. '{' + '\n' +
  1515. 'int a = 100;' + '\n' +
  1516. 'int b = 3;' + '\n' +
  1517. 'int mod = 1000000007;' + '\n' +
  1518. 'int c;' + '\n' +
  1519. 'c = ((a % mod + b % mod) % mod *(' \
  1520. 'a % mod - b % mod) % mod) % mod;' + '\n' +
  1521. '}'
  1522. )
  1523. c_src20 = (
  1524. 'void func()'+
  1525. '{' + '\n' +
  1526. 'bool a' + '\n' +
  1527. 'bool b;' + '\n' +
  1528. 'a = 1 == 2;' + '\n' +
  1529. 'b = 1 != 2;' + '\n' +
  1530. '}'
  1531. )
  1532. c_src21 = (
  1533. 'void func()'+
  1534. '{' + '\n' +
  1535. 'bool a;' + '\n' +
  1536. 'bool b;' + '\n' +
  1537. 'bool c;' + '\n' +
  1538. 'bool d;' + '\n' +
  1539. 'a = 1 == 2;' + '\n' +
  1540. 'b = 1 <= 2;' + '\n' +
  1541. 'c = 1 > 2;' + '\n' +
  1542. 'd = 1 >= 2;' + '\n' +
  1543. '}'
  1544. )
  1545. c_src22 = (
  1546. 'void func()'+
  1547. '{' + '\n' +
  1548. 'int a = 1;' + '\n' +
  1549. 'int b = 2;' + '\n' +
  1550. 'bool c1;' + '\n' +
  1551. 'bool c2;' + '\n' +
  1552. 'bool c3;' + '\n' +
  1553. 'bool c4;' + '\n' +
  1554. 'bool c5;' + '\n' +
  1555. 'bool c6;' + '\n' +
  1556. 'bool c7;' + '\n' +
  1557. 'bool c8;' + '\n' +
  1558. 'c1 = a == 1;' + '\n' +
  1559. 'c2 = b == 2;' + '\n' +
  1560. 'c3 = 1 != a;' + '\n' +
  1561. 'c4 = 1 != b;' + '\n' +
  1562. 'c5 = a < 0;' + '\n' +
  1563. 'c6 = b <= 10;' + '\n' +
  1564. 'c7 = a > 0;' + '\n' +
  1565. 'c8 = b >= 11;' + '\n' +
  1566. '}'
  1567. )
  1568. c_src23 = (
  1569. 'void func()'+
  1570. '{' + '\n' +
  1571. 'int a = 3;' + '\n' +
  1572. 'int b = 4;' + '\n' +
  1573. 'bool c1;' + '\n' +
  1574. 'bool c2;' + '\n' +
  1575. 'bool c3;' + '\n' +
  1576. 'bool c4;' + '\n' +
  1577. 'bool c5;' + '\n' +
  1578. 'bool c6;' + '\n' +
  1579. 'c1 = a == b;' + '\n' +
  1580. 'c2 = a != b;' + '\n' +
  1581. 'c3 = a < b;' + '\n' +
  1582. 'c4 = a <= b;' + '\n' +
  1583. 'c5 = a > b;' + '\n' +
  1584. 'c6 = a >= b;' + '\n' +
  1585. '}'
  1586. )
  1587. c_src24 = (
  1588. 'void func()'+
  1589. '{' + '\n' +
  1590. 'float a = 1.25'
  1591. 'float b = 2.5;' + '\n' +
  1592. 'bool c1;' + '\n' +
  1593. 'bool c2;' + '\n' +
  1594. 'bool c3;' + '\n' +
  1595. 'bool c4;' + '\n' +
  1596. 'c1 = a == 1.25;' + '\n' +
  1597. 'c2 = b == 2.54;' + '\n' +
  1598. 'c3 = 1.2 != a;' + '\n' +
  1599. 'c4 = 1.5 != b;' + '\n' +
  1600. '}'
  1601. )
  1602. c_src25 = (
  1603. 'void func()'+
  1604. '{' + '\n' +
  1605. 'float a = 1.25' + '\n' +
  1606. 'float b = 2.5;' + '\n' +
  1607. 'bool c1;' + '\n' +
  1608. 'bool c2;' + '\n' +
  1609. 'bool c3;' + '\n' +
  1610. 'bool c4;' + '\n' +
  1611. 'bool c5;' + '\n' +
  1612. 'bool c6;' + '\n' +
  1613. 'c1 = a == b;' + '\n' +
  1614. 'c2 = a != b;' + '\n' +
  1615. 'c3 = a < b;' + '\n' +
  1616. 'c4 = a <= b;' + '\n' +
  1617. 'c5 = a > b;' + '\n' +
  1618. 'c6 = a >= b;' + '\n' +
  1619. '}'
  1620. )
  1621. c_src26 = (
  1622. 'void func()'+
  1623. '{' + '\n' +
  1624. 'bool c1;' + '\n' +
  1625. 'bool c2;' + '\n' +
  1626. 'bool c3;' + '\n' +
  1627. 'bool c4;' + '\n' +
  1628. 'bool c5;' + '\n' +
  1629. 'bool c6;' + '\n' +
  1630. 'c1 = true == true;' + '\n' +
  1631. 'c2 = true == false;' + '\n' +
  1632. 'c3 = false == false;' + '\n' +
  1633. 'c4 = true != true;' + '\n' +
  1634. 'c5 = true != false;' + '\n' +
  1635. 'c6 = false != false;' + '\n' +
  1636. '}'
  1637. )
  1638. c_src27 = (
  1639. 'void func()'+
  1640. '{' + '\n' +
  1641. 'bool c1;' + '\n' +
  1642. 'bool c2;' + '\n' +
  1643. 'bool c3;' + '\n' +
  1644. 'bool c4;' + '\n' +
  1645. 'bool c5;' + '\n' +
  1646. 'bool c6;' + '\n' +
  1647. 'c1 = true && true;' + '\n' +
  1648. 'c2 = true && false;' + '\n' +
  1649. 'c3 = false && false;' + '\n' +
  1650. 'c4 = true || true;' + '\n' +
  1651. 'c5 = true || false;' + '\n' +
  1652. 'c6 = false || false;' + '\n' +
  1653. '}'
  1654. )
  1655. c_src28 = (
  1656. 'void func()'+
  1657. '{' + '\n' +
  1658. 'bool a;' + '\n' +
  1659. 'bool c1;' + '\n' +
  1660. 'bool c2;' + '\n' +
  1661. 'bool c3;' + '\n' +
  1662. 'bool c4;' + '\n' +
  1663. 'c1 = a && true;' + '\n' +
  1664. 'c2 = false && a;' + '\n' +
  1665. 'c3 = true || a;' + '\n' +
  1666. 'c4 = a || false;' + '\n' +
  1667. '}'
  1668. )
  1669. c_src29 = (
  1670. 'void func()'+
  1671. '{' + '\n' +
  1672. 'int a;' + '\n' +
  1673. 'bool c1;' + '\n' +
  1674. 'bool c2;' + '\n' +
  1675. 'bool c3;' + '\n' +
  1676. 'bool c4;' + '\n' +
  1677. 'c1 = a && 1;' + '\n' +
  1678. 'c2 = a && 0;' + '\n' +
  1679. 'c3 = a || 1;' + '\n' +
  1680. 'c4 = 0 || a;' + '\n' +
  1681. '}'
  1682. )
  1683. c_src30 = (
  1684. 'void func()'+
  1685. '{' + '\n' +
  1686. 'int a;' + '\n' +
  1687. 'int b;' + '\n' +
  1688. 'bool c;'+ '\n' +
  1689. 'bool d;'+ '\n' +
  1690. 'bool c1;' + '\n' +
  1691. 'bool c2;' + '\n' +
  1692. 'bool c3;' + '\n' +
  1693. 'bool c4;' + '\n' +
  1694. 'bool c5;' + '\n' +
  1695. 'bool c6;' + '\n' +
  1696. 'c1 = a && b;' + '\n' +
  1697. 'c2 = a && c;' + '\n' +
  1698. 'c3 = c && d;' + '\n' +
  1699. 'c4 = a || b;' + '\n' +
  1700. 'c5 = a || c;' + '\n' +
  1701. 'c6 = c || d;' + '\n' +
  1702. '}'
  1703. )
  1704. c_src_raise1 = (
  1705. 'void func()'+
  1706. '{' + '\n' +
  1707. 'int a;' + '\n' +
  1708. 'a = -1;' + '\n' +
  1709. '}'
  1710. )
  1711. c_src_raise2 = (
  1712. 'void func()'+
  1713. '{' + '\n' +
  1714. 'int a;' + '\n' +
  1715. 'a = -+1;' + '\n' +
  1716. '}'
  1717. )
  1718. c_src_raise3 = (
  1719. 'void func()'+
  1720. '{' + '\n' +
  1721. 'int a;' + '\n' +
  1722. 'a = 2*-2;' + '\n' +
  1723. '}'
  1724. )
  1725. c_src_raise4 = (
  1726. 'void func()'+
  1727. '{' + '\n' +
  1728. 'int a;' + '\n' +
  1729. 'a = (int)2.0;' + '\n' +
  1730. '}'
  1731. )
  1732. c_src_raise5 = (
  1733. 'void func()'+
  1734. '{' + '\n' +
  1735. 'int a=100;' + '\n' +
  1736. 'a = (a==100)?(1):(0);' + '\n' +
  1737. '}'
  1738. )
  1739. res1 = SymPyExpression(c_src1, 'c').return_expr()
  1740. res2 = SymPyExpression(c_src2, 'c').return_expr()
  1741. res3 = SymPyExpression(c_src3, 'c').return_expr()
  1742. res4 = SymPyExpression(c_src4, 'c').return_expr()
  1743. res5 = SymPyExpression(c_src5, 'c').return_expr()
  1744. res6 = SymPyExpression(c_src6, 'c').return_expr()
  1745. res7 = SymPyExpression(c_src7, 'c').return_expr()
  1746. res8 = SymPyExpression(c_src8, 'c').return_expr()
  1747. res9 = SymPyExpression(c_src9, 'c').return_expr()
  1748. res10 = SymPyExpression(c_src10, 'c').return_expr()
  1749. res11 = SymPyExpression(c_src11, 'c').return_expr()
  1750. res12 = SymPyExpression(c_src12, 'c').return_expr()
  1751. res13 = SymPyExpression(c_src13, 'c').return_expr()
  1752. res14 = SymPyExpression(c_src14, 'c').return_expr()
  1753. res15 = SymPyExpression(c_src15, 'c').return_expr()
  1754. res16 = SymPyExpression(c_src16, 'c').return_expr()
  1755. res17 = SymPyExpression(c_src17, 'c').return_expr()
  1756. res18 = SymPyExpression(c_src18, 'c').return_expr()
  1757. res19 = SymPyExpression(c_src19, 'c').return_expr()
  1758. res20 = SymPyExpression(c_src20, 'c').return_expr()
  1759. res21 = SymPyExpression(c_src21, 'c').return_expr()
  1760. res22 = SymPyExpression(c_src22, 'c').return_expr()
  1761. res23 = SymPyExpression(c_src23, 'c').return_expr()
  1762. res24 = SymPyExpression(c_src24, 'c').return_expr()
  1763. res25 = SymPyExpression(c_src25, 'c').return_expr()
  1764. res26 = SymPyExpression(c_src26, 'c').return_expr()
  1765. res27 = SymPyExpression(c_src27, 'c').return_expr()
  1766. res28 = SymPyExpression(c_src28, 'c').return_expr()
  1767. res29 = SymPyExpression(c_src29, 'c').return_expr()
  1768. res30 = SymPyExpression(c_src30, 'c').return_expr()
  1769. assert res1[0] == FunctionDefinition(
  1770. NoneToken(),
  1771. name=String('func'),
  1772. parameters=(),
  1773. body=CodeBlock(
  1774. Declaration(
  1775. Variable(Symbol('a'),
  1776. type=IntBaseType(String('intc'))
  1777. )
  1778. ),
  1779. Assignment(Variable(Symbol('a')), Integer(1))
  1780. )
  1781. )
  1782. assert res2[0] == FunctionDefinition(
  1783. NoneToken(),
  1784. name=String('func'),
  1785. parameters=(),
  1786. body=CodeBlock(
  1787. Declaration(
  1788. Variable(Symbol('a'),
  1789. type=IntBaseType(String('intc')),
  1790. value=Integer(0))),
  1791. Assignment(
  1792. Variable(Symbol('a')),
  1793. Add(Symbol('a'),
  1794. Integer(1))
  1795. ),
  1796. Assignment(Variable(Symbol('a')),
  1797. Add(
  1798. Mul(
  1799. Integer(3),
  1800. Symbol('a')),
  1801. Integer(-10)
  1802. )
  1803. )
  1804. )
  1805. )
  1806. assert res3[0] == FunctionDefinition(
  1807. NoneToken(),
  1808. name=String('func'),
  1809. parameters=(),
  1810. body=CodeBlock(
  1811. Declaration(
  1812. Variable(Symbol('a'),
  1813. type=IntBaseType(String('intc')),
  1814. value=Integer(10)
  1815. )
  1816. ),
  1817. Assignment(
  1818. Variable(Symbol('a')),
  1819. Add(
  1820. Symbol('a'),
  1821. Integer(-17)
  1822. )
  1823. )
  1824. )
  1825. )
  1826. assert res4[0] == FunctionDefinition(
  1827. NoneToken(),
  1828. name=String('func'),
  1829. parameters=(),
  1830. body=CodeBlock(
  1831. Declaration(
  1832. Variable(Symbol('a'),
  1833. type=IntBaseType(String('intc'))
  1834. )
  1835. ),
  1836. Declaration(
  1837. Variable(Symbol('b'),
  1838. type=IntBaseType(String('intc'))
  1839. )
  1840. ),
  1841. Assignment(
  1842. Variable(Symbol('a')),
  1843. Integer(100)),
  1844. Assignment(
  1845. Variable(Symbol('b')),
  1846. Add(
  1847. Mul(
  1848. Integer(2),
  1849. Pow(
  1850. Symbol('a'),
  1851. Integer(2))
  1852. ),
  1853. Mul(
  1854. Integer(20),
  1855. Symbol('a')),
  1856. Integer(25)
  1857. )
  1858. )
  1859. )
  1860. )
  1861. assert res5[0] == FunctionDefinition(
  1862. NoneToken(),
  1863. name=String('func'),
  1864. parameters=(),
  1865. body=CodeBlock(
  1866. Declaration(
  1867. Variable(Symbol('a'),
  1868. type=IntBaseType(String('intc'))
  1869. )
  1870. ),
  1871. Declaration(
  1872. Variable(Symbol('b'),
  1873. type=IntBaseType(String('intc'))
  1874. )
  1875. ),
  1876. Declaration(
  1877. Variable(Symbol('c'),
  1878. type=IntBaseType(String('intc'))
  1879. )
  1880. ),
  1881. Declaration(
  1882. Variable(Symbol('d'),
  1883. type=IntBaseType(String('intc'))
  1884. )
  1885. ),
  1886. Assignment(
  1887. Variable(Symbol('a')),
  1888. Integer(1)),
  1889. Assignment(
  1890. Variable(Symbol('b')),
  1891. Integer(2)
  1892. ),
  1893. Assignment(
  1894. Variable(Symbol('c')),
  1895. Symbol('b')),
  1896. Assignment(
  1897. Variable(Symbol('d')),
  1898. Mul(
  1899. Add(
  1900. Symbol('a'),
  1901. Symbol('b')),
  1902. Pow(
  1903. Add(
  1904. Symbol('a'),
  1905. Symbol('c')
  1906. ),
  1907. Integer(2)
  1908. ),
  1909. Add(
  1910. Symbol('c'),
  1911. Mul(
  1912. Integer(-1),
  1913. Symbol('d')
  1914. )
  1915. )
  1916. )
  1917. )
  1918. )
  1919. )
  1920. assert res6[0] == FunctionDefinition(
  1921. NoneToken(),
  1922. name=String('func'),
  1923. parameters=(),
  1924. body=CodeBlock(
  1925. Declaration(
  1926. Variable(Symbol('a'),
  1927. type=IntBaseType(String('intc'))
  1928. )
  1929. ),
  1930. Declaration(
  1931. Variable(Symbol('b'),
  1932. type=IntBaseType(String('intc'))
  1933. )
  1934. ),
  1935. Declaration(
  1936. Variable(Symbol('c'),
  1937. type=IntBaseType(String('intc'))
  1938. )
  1939. ),
  1940. Declaration(
  1941. Variable(Symbol('d'),
  1942. type=IntBaseType(String('intc'))
  1943. )
  1944. ),
  1945. Assignment(
  1946. Variable(Symbol('a')),
  1947. Integer(1)
  1948. ),
  1949. Assignment(
  1950. Variable(Symbol('b')),
  1951. Integer(2)
  1952. ),
  1953. Assignment(
  1954. Variable(Symbol('c')),
  1955. Integer(3)
  1956. ),
  1957. Assignment(
  1958. Variable(Symbol('d')),
  1959. Add(
  1960. Pow(
  1961. Symbol('a'),
  1962. Integer(4)
  1963. ),
  1964. Mul(
  1965. Integer(3),
  1966. Pow(
  1967. Symbol('b'),
  1968. Integer(2)
  1969. )
  1970. ),
  1971. Mul(
  1972. Integer(2),
  1973. Symbol('b')
  1974. ),
  1975. Mul(
  1976. Symbol('c'),
  1977. Symbol('d')
  1978. )
  1979. )
  1980. )
  1981. )
  1982. )
  1983. assert res7[0] == FunctionDefinition(
  1984. NoneToken(),
  1985. name=String('func'),
  1986. parameters=(),
  1987. body=CodeBlock(
  1988. Declaration(
  1989. Variable(Symbol('a'),
  1990. type=FloatType(
  1991. String('float32'),
  1992. nbits=Integer(32),
  1993. nmant=Integer(23),
  1994. nexp=Integer(8)
  1995. )
  1996. )
  1997. ),
  1998. Assignment(
  1999. Variable(Symbol('a')),
  2000. Float('1.01', precision=53)
  2001. )
  2002. )
  2003. )
  2004. assert res8[0] == FunctionDefinition(
  2005. NoneToken(),
  2006. name=String('func'),
  2007. parameters=(),
  2008. body=CodeBlock(
  2009. Declaration(
  2010. Variable(Symbol('a'),
  2011. type=FloatType(
  2012. String('float32'),
  2013. nbits=Integer(32),
  2014. nmant=Integer(23),
  2015. nexp=Integer(8)
  2016. )
  2017. )
  2018. ),
  2019. Assignment(
  2020. Variable(Symbol('a')),
  2021. Float('12.5', precision=53)
  2022. )
  2023. )
  2024. )
  2025. assert res9[0] == FunctionDefinition(
  2026. NoneToken(),
  2027. name=String('func'),
  2028. parameters=(),
  2029. body=CodeBlock(
  2030. Declaration(
  2031. Variable(Symbol('a'),
  2032. type=FloatType(
  2033. String('float32'),
  2034. nbits=Integer(32),
  2035. nmant=Integer(23),
  2036. nexp=Integer(8)
  2037. )
  2038. )
  2039. ),
  2040. Assignment(
  2041. Variable(Symbol('a')),
  2042. Float('4.0', precision=53)
  2043. )
  2044. )
  2045. )
  2046. assert res10[0] == FunctionDefinition(
  2047. NoneToken(),
  2048. name=String('func'),
  2049. parameters=(),
  2050. body=CodeBlock(
  2051. Declaration(
  2052. Variable(Symbol('a'),
  2053. type=IntBaseType(String('intc'))
  2054. )
  2055. ),
  2056. Assignment(
  2057. Variable(Symbol('a')),
  2058. Integer(25)
  2059. )
  2060. )
  2061. )
  2062. assert res11[0] == FunctionDefinition(
  2063. NoneToken(),
  2064. name=String('func'),
  2065. parameters=(),
  2066. body=CodeBlock(
  2067. Declaration(
  2068. Variable(Symbol('a'),
  2069. type=IntBaseType(String('intc'))
  2070. )
  2071. ),
  2072. Assignment(
  2073. Variable(Symbol('a')),
  2074. Integer(-95)
  2075. )
  2076. )
  2077. )
  2078. assert res12[0] == FunctionDefinition(
  2079. NoneToken(),
  2080. name=String('func'),
  2081. parameters=(),
  2082. body=CodeBlock(
  2083. Declaration(
  2084. Variable(Symbol('a'),
  2085. type=IntBaseType(String('intc'))
  2086. )
  2087. ),
  2088. Assignment(
  2089. Variable(Symbol('a')),
  2090. Integer(-300)
  2091. )
  2092. )
  2093. )
  2094. assert res13[0] == FunctionDefinition(
  2095. NoneToken(),
  2096. name=String('func'),
  2097. parameters=(),
  2098. body=CodeBlock(
  2099. Declaration(
  2100. Variable(Symbol('a'),
  2101. type=IntBaseType(String('intc'))
  2102. )
  2103. ),
  2104. Declaration(
  2105. Variable(Symbol('b'),
  2106. type=IntBaseType(String('intc'))
  2107. )
  2108. ),
  2109. Declaration(
  2110. Variable(Symbol('c'),
  2111. type=FloatType(
  2112. String('float32'),
  2113. nbits=Integer(32),
  2114. nmant=Integer(23),
  2115. nexp=Integer(8)
  2116. )
  2117. )
  2118. ),
  2119. Assignment(
  2120. Variable(Symbol('c')),
  2121. Mul(
  2122. Pow(
  2123. Symbol('a'),
  2124. Integer(-1)
  2125. ),
  2126. Symbol('b')
  2127. )
  2128. )
  2129. )
  2130. )
  2131. assert res14[0] == FunctionDefinition(
  2132. NoneToken(),
  2133. name=String('func'),
  2134. parameters=(),
  2135. body=CodeBlock(
  2136. Declaration(
  2137. Variable(Symbol('a'),
  2138. type=IntBaseType(String('intc')),
  2139. value=Integer(2)
  2140. )
  2141. ),
  2142. Declaration(
  2143. Variable(Symbol('d'),
  2144. type=IntBaseType(String('intc')),
  2145. value=Integer(5)
  2146. )
  2147. ),
  2148. Declaration(
  2149. Variable(Symbol('n'),
  2150. type=IntBaseType(String('intc')),
  2151. value=Integer(10)
  2152. )
  2153. ),
  2154. Declaration(
  2155. Variable(Symbol('s'),
  2156. type=IntBaseType(String('intc'))
  2157. )
  2158. ),
  2159. Assignment(
  2160. Variable(Symbol('s')),
  2161. Mul(
  2162. Rational(1, 2),
  2163. Symbol('a'),
  2164. Add(
  2165. Mul(
  2166. Integer(2),
  2167. Symbol('a')
  2168. ),
  2169. Mul(
  2170. Symbol('d'),
  2171. Add(
  2172. Symbol('n'),
  2173. Integer(-1)
  2174. )
  2175. )
  2176. )
  2177. )
  2178. )
  2179. )
  2180. )
  2181. assert res15[0] == FunctionDefinition(
  2182. NoneToken(),
  2183. name=String('func'),
  2184. parameters=(),
  2185. body=CodeBlock(
  2186. Declaration(
  2187. Variable(Symbol('a'),
  2188. type=IntBaseType(String('intc'))
  2189. )
  2190. ),
  2191. Assignment(
  2192. Variable(Symbol('a')),
  2193. Integer(1)
  2194. )
  2195. )
  2196. )
  2197. assert res16[0] == FunctionDefinition(
  2198. NoneToken(),
  2199. name=String('func'),
  2200. parameters=(),
  2201. body=CodeBlock(
  2202. Declaration(
  2203. Variable(Symbol('a'),
  2204. type=IntBaseType(String('intc')),
  2205. value=Integer(2)
  2206. )
  2207. ),
  2208. Declaration(
  2209. Variable(Symbol('b'),
  2210. type=IntBaseType(String('intc'))
  2211. )
  2212. ),
  2213. Assignment(
  2214. Variable(Symbol('b')),
  2215. Mod(
  2216. Symbol('a'),
  2217. Integer(3)
  2218. )
  2219. )
  2220. )
  2221. )
  2222. assert res17[0] == FunctionDefinition(
  2223. NoneToken(),
  2224. name=String('func'),
  2225. parameters=(),
  2226. body=CodeBlock(
  2227. Declaration(
  2228. Variable(Symbol('a'),
  2229. type=IntBaseType(String('intc')),
  2230. value=Integer(100)
  2231. )
  2232. ),
  2233. Declaration(
  2234. Variable(Symbol('b'),
  2235. type=IntBaseType(String('intc')),
  2236. value=Integer(3)
  2237. )
  2238. ),
  2239. Declaration(
  2240. Variable(Symbol('c'),
  2241. type=IntBaseType(String('intc'))
  2242. )
  2243. ),
  2244. Assignment(
  2245. Variable(Symbol('c')),
  2246. Mod(
  2247. Symbol('a'),
  2248. Symbol('b')
  2249. )
  2250. )
  2251. )
  2252. )
  2253. assert res18[0] == FunctionDefinition(
  2254. NoneToken(),
  2255. name=String('func'),
  2256. parameters=(),
  2257. body=CodeBlock(
  2258. Declaration(
  2259. Variable(Symbol('a'),
  2260. type=IntBaseType(String('intc')),
  2261. value=Integer(100)
  2262. )
  2263. ),
  2264. Declaration(
  2265. Variable(Symbol('b'),
  2266. type=IntBaseType(String('intc')),
  2267. value=Integer(3)
  2268. )
  2269. ),
  2270. Declaration(
  2271. Variable(Symbol('mod'),
  2272. type=IntBaseType(String('intc')),
  2273. value=Integer(1000000007)
  2274. )
  2275. ),
  2276. Declaration(
  2277. Variable(Symbol('c'),
  2278. type=IntBaseType(String('intc'))
  2279. )
  2280. ),
  2281. Assignment(
  2282. Variable(Symbol('c')),
  2283. Mod(
  2284. Add(
  2285. Symbol('a'),
  2286. Mul(
  2287. Integer(100),
  2288. Pow(
  2289. Symbol('a'),
  2290. Integer(-1)
  2291. ),
  2292. Symbol('b')
  2293. )
  2294. ),
  2295. Symbol('mod')
  2296. )
  2297. )
  2298. )
  2299. )
  2300. assert res19[0] == FunctionDefinition(
  2301. NoneToken(),
  2302. name=String('func'),
  2303. parameters=(),
  2304. body=CodeBlock(
  2305. Declaration(
  2306. Variable(Symbol('a'),
  2307. type=IntBaseType(String('intc')),
  2308. value=Integer(100)
  2309. )
  2310. ),
  2311. Declaration(
  2312. Variable(Symbol('b'),
  2313. type=IntBaseType(String('intc')),
  2314. value=Integer(3)
  2315. )
  2316. ),
  2317. Declaration(
  2318. Variable(Symbol('mod'),
  2319. type=IntBaseType(String('intc')),
  2320. value=Integer(1000000007)
  2321. )
  2322. ),
  2323. Declaration(
  2324. Variable(Symbol('c'),
  2325. type=IntBaseType(String('intc'))
  2326. )
  2327. ),
  2328. Assignment(
  2329. Variable(Symbol('c')),
  2330. Mod(
  2331. Mul(
  2332. Add(
  2333. Symbol('a'),
  2334. Mul(Integer(-1),
  2335. Symbol('b')
  2336. )
  2337. ),
  2338. Add(
  2339. Symbol('a'),
  2340. Symbol('b')
  2341. )
  2342. ),
  2343. Symbol('mod')
  2344. )
  2345. )
  2346. )
  2347. )
  2348. assert res20[0] == FunctionDefinition(
  2349. NoneToken(),
  2350. name=String('func'),
  2351. parameters=(),
  2352. body=CodeBlock(
  2353. Declaration(
  2354. Variable(Symbol('a'),
  2355. type=Type(String('bool'))
  2356. )
  2357. ),
  2358. Declaration(
  2359. Variable(Symbol('b'),
  2360. type=Type(String('bool'))
  2361. )
  2362. ),
  2363. Assignment(
  2364. Variable(Symbol('a')),
  2365. false
  2366. ),
  2367. Assignment(
  2368. Variable(Symbol('b')),
  2369. true
  2370. )
  2371. )
  2372. )
  2373. assert res21[0] == FunctionDefinition(
  2374. NoneToken(),
  2375. name=String('func'),
  2376. parameters=(),
  2377. body=CodeBlock(
  2378. Declaration(
  2379. Variable(Symbol('a'),
  2380. type=Type(String('bool'))
  2381. )
  2382. ),
  2383. Declaration(
  2384. Variable(Symbol('b'),
  2385. type=Type(String('bool'))
  2386. )
  2387. ),
  2388. Declaration(
  2389. Variable(Symbol('c'),
  2390. type=Type(String('bool'))
  2391. )
  2392. ),
  2393. Declaration(
  2394. Variable(Symbol('d'),
  2395. type=Type(String('bool'))
  2396. )
  2397. ),
  2398. Assignment(
  2399. Variable(Symbol('a')),
  2400. false
  2401. ),
  2402. Assignment(
  2403. Variable(Symbol('b')),
  2404. true
  2405. ),
  2406. Assignment(
  2407. Variable(Symbol('c')),
  2408. false
  2409. ),
  2410. Assignment(
  2411. Variable(Symbol('d')),
  2412. false
  2413. )
  2414. )
  2415. )
  2416. assert res22[0] == FunctionDefinition(
  2417. NoneToken(),
  2418. name=String('func'),
  2419. parameters=(),
  2420. body=CodeBlock(
  2421. Declaration(
  2422. Variable(Symbol('a'),
  2423. type=IntBaseType(String('intc')),
  2424. value=Integer(1)
  2425. )
  2426. ),
  2427. Declaration(
  2428. Variable(Symbol('b'),
  2429. type=IntBaseType(String('intc')),
  2430. value=Integer(2)
  2431. )
  2432. ),
  2433. Declaration(
  2434. Variable(Symbol('c1'),
  2435. type=Type(String('bool'))
  2436. )
  2437. ),
  2438. Declaration(
  2439. Variable(Symbol('c2'),
  2440. type=Type(String('bool'))
  2441. )
  2442. ),
  2443. Declaration(
  2444. Variable(Symbol('c3'),
  2445. type=Type(String('bool'))
  2446. )
  2447. ),
  2448. Declaration(
  2449. Variable(Symbol('c4'),
  2450. type=Type(String('bool'))
  2451. )
  2452. ),
  2453. Declaration(
  2454. Variable(Symbol('c5'),
  2455. type=Type(String('bool'))
  2456. )
  2457. ),
  2458. Declaration(
  2459. Variable(Symbol('c6'),
  2460. type=Type(String('bool'))
  2461. )
  2462. ),
  2463. Declaration(
  2464. Variable(Symbol('c7'),
  2465. type=Type(String('bool'))
  2466. )
  2467. ),
  2468. Declaration(
  2469. Variable(Symbol('c8'),
  2470. type=Type(String('bool'))
  2471. )
  2472. ),
  2473. Assignment(
  2474. Variable(Symbol('c1')),
  2475. Equality(
  2476. Symbol('a'),
  2477. Integer(1)
  2478. )
  2479. ),
  2480. Assignment(
  2481. Variable(Symbol('c2')),
  2482. Equality(
  2483. Symbol('b'),
  2484. Integer(2)
  2485. )
  2486. ),
  2487. Assignment(
  2488. Variable(Symbol('c3')),
  2489. Unequality(
  2490. Integer(1),
  2491. Symbol('a')
  2492. )
  2493. ),
  2494. Assignment(
  2495. Variable(Symbol('c4')),
  2496. Unequality(
  2497. Integer(1),
  2498. Symbol('b')
  2499. )
  2500. ),
  2501. Assignment(
  2502. Variable(Symbol('c5')),
  2503. StrictLessThan(
  2504. Symbol('a'),
  2505. Integer(0)
  2506. )
  2507. ),
  2508. Assignment(
  2509. Variable(Symbol('c6')),
  2510. LessThan(
  2511. Symbol('b'),
  2512. Integer(10)
  2513. )
  2514. ),
  2515. Assignment(
  2516. Variable(Symbol('c7')),
  2517. StrictGreaterThan(
  2518. Symbol('a'),
  2519. Integer(0)
  2520. )
  2521. ),
  2522. Assignment(
  2523. Variable(Symbol('c8')),
  2524. GreaterThan(
  2525. Symbol('b'),
  2526. Integer(11)
  2527. )
  2528. )
  2529. )
  2530. )
  2531. assert res23[0] == FunctionDefinition(
  2532. NoneToken(),
  2533. name=String('func'),
  2534. parameters=(),
  2535. body=CodeBlock(
  2536. Declaration(
  2537. Variable(Symbol('a'),
  2538. type=IntBaseType(String('intc')),
  2539. value=Integer(3)
  2540. )
  2541. ),
  2542. Declaration(
  2543. Variable(Symbol('b'),
  2544. type=IntBaseType(String('intc')),
  2545. value=Integer(4)
  2546. )
  2547. ),
  2548. Declaration(
  2549. Variable(Symbol('c1'),
  2550. type=Type(String('bool'))
  2551. )
  2552. ),
  2553. Declaration(
  2554. Variable(Symbol('c2'),
  2555. type=Type(String('bool'))
  2556. )
  2557. ),
  2558. Declaration(
  2559. Variable(Symbol('c3'),
  2560. type=Type(String('bool'))
  2561. )
  2562. ),
  2563. Declaration(
  2564. Variable(Symbol('c4'),
  2565. type=Type(String('bool'))
  2566. )
  2567. ),
  2568. Declaration(
  2569. Variable(Symbol('c5'),
  2570. type=Type(String('bool'))
  2571. )
  2572. ),
  2573. Declaration(
  2574. Variable(Symbol('c6'),
  2575. type=Type(String('bool'))
  2576. )
  2577. ),
  2578. Assignment(
  2579. Variable(Symbol('c1')),
  2580. Equality(
  2581. Symbol('a'),
  2582. Symbol('b')
  2583. )
  2584. ),
  2585. Assignment(
  2586. Variable(Symbol('c2')),
  2587. Unequality(
  2588. Symbol('a'),
  2589. Symbol('b')
  2590. )
  2591. ),
  2592. Assignment(
  2593. Variable(Symbol('c3')),
  2594. StrictLessThan(
  2595. Symbol('a'),
  2596. Symbol('b')
  2597. )
  2598. ),
  2599. Assignment(
  2600. Variable(Symbol('c4')),
  2601. LessThan(
  2602. Symbol('a'),
  2603. Symbol('b')
  2604. )
  2605. ),
  2606. Assignment(
  2607. Variable(Symbol('c5')),
  2608. StrictGreaterThan(
  2609. Symbol('a'),
  2610. Symbol('b')
  2611. )
  2612. ),
  2613. Assignment(
  2614. Variable(Symbol('c6')),
  2615. GreaterThan(
  2616. Symbol('a'),
  2617. Symbol('b')
  2618. )
  2619. )
  2620. )
  2621. )
  2622. assert res24[0] == FunctionDefinition(
  2623. NoneToken(),
  2624. name=String('func'),
  2625. parameters=(),
  2626. body=CodeBlock(
  2627. Declaration(
  2628. Variable(Symbol('a'),
  2629. type=FloatType(
  2630. String('float32'),
  2631. nbits=Integer(32),
  2632. nmant=Integer(23),
  2633. nexp=Integer(8)
  2634. )
  2635. )
  2636. ),
  2637. Declaration(
  2638. Variable(Symbol('c1'),
  2639. type=Type(String('bool'))
  2640. )
  2641. ),
  2642. Declaration(
  2643. Variable(Symbol('c2'),
  2644. type=Type(String('bool'))
  2645. )
  2646. ),
  2647. Declaration(
  2648. Variable(Symbol('c3'),
  2649. type=Type(String('bool'))
  2650. )
  2651. ),
  2652. Declaration(
  2653. Variable(Symbol('c4'),
  2654. type=Type(String('bool'))
  2655. )
  2656. ),
  2657. Assignment(
  2658. Variable(Symbol('c1')),
  2659. Equality(
  2660. Symbol('a'),
  2661. Float('1.25', precision=53)
  2662. )
  2663. ),
  2664. Assignment(
  2665. Variable(Symbol('c3')),
  2666. Unequality(
  2667. Float('1.2', precision=53),
  2668. Symbol('a')
  2669. )
  2670. )
  2671. )
  2672. )
  2673. assert res25[0] == FunctionDefinition(
  2674. NoneToken(),
  2675. name=String('func'),
  2676. parameters=(),
  2677. body=CodeBlock(
  2678. Declaration(
  2679. Variable(Symbol('a'),
  2680. type=FloatType(
  2681. String('float32'),
  2682. nbits=Integer(32),
  2683. nmant=Integer(23),
  2684. nexp=Integer(8)
  2685. ),
  2686. value=Float('1.25', precision=53)
  2687. )
  2688. ),
  2689. Declaration(
  2690. Variable(Symbol('b'),
  2691. type=FloatType(
  2692. String('float32'),
  2693. nbits=Integer(32),
  2694. nmant=Integer(23),
  2695. nexp=Integer(8)
  2696. ),
  2697. value=Float('2.5', precision=53)
  2698. )
  2699. ),
  2700. Declaration(
  2701. Variable(Symbol('c1'),
  2702. type=Type(String('bool'))
  2703. )
  2704. ),
  2705. Declaration(
  2706. Variable(Symbol('c2'),
  2707. type=Type(String('bool')
  2708. )
  2709. )
  2710. ),
  2711. Declaration(
  2712. Variable(Symbol('c3'),
  2713. type=Type(String('bool'))
  2714. )
  2715. ),
  2716. Declaration(
  2717. Variable(Symbol('c4'),
  2718. type=Type(String('bool'))
  2719. )
  2720. ),
  2721. Declaration(
  2722. Variable(Symbol('c5'),
  2723. type=Type(String('bool'))
  2724. )
  2725. ),
  2726. Declaration(
  2727. Variable(Symbol('c6'),
  2728. type=Type(String('bool'))
  2729. )
  2730. ),
  2731. Assignment(
  2732. Variable(Symbol('c1')),
  2733. Equality(
  2734. Symbol('a'),
  2735. Symbol('b')
  2736. )
  2737. ),
  2738. Assignment(
  2739. Variable(Symbol('c2')),
  2740. Unequality(
  2741. Symbol('a'),
  2742. Symbol('b')
  2743. )
  2744. ),
  2745. Assignment(
  2746. Variable(Symbol('c3')),
  2747. StrictLessThan(
  2748. Symbol('a'),
  2749. Symbol('b')
  2750. )
  2751. ),
  2752. Assignment(
  2753. Variable(Symbol('c4')),
  2754. LessThan(
  2755. Symbol('a'),
  2756. Symbol('b')
  2757. )
  2758. ),
  2759. Assignment(
  2760. Variable(Symbol('c5')),
  2761. StrictGreaterThan(
  2762. Symbol('a'),
  2763. Symbol('b')
  2764. )
  2765. ),
  2766. Assignment(
  2767. Variable(Symbol('c6')),
  2768. GreaterThan(
  2769. Symbol('a'),
  2770. Symbol('b')
  2771. )
  2772. )
  2773. )
  2774. )
  2775. assert res26[0] == FunctionDefinition(
  2776. NoneToken(),
  2777. name=String('func'),
  2778. parameters=(), body=CodeBlock(
  2779. Declaration(
  2780. Variable(Symbol('c1'),
  2781. type=Type(String('bool'))
  2782. )
  2783. ),
  2784. Declaration(
  2785. Variable(Symbol('c2'),
  2786. type=Type(String('bool'))
  2787. )
  2788. ),
  2789. Declaration(
  2790. Variable(Symbol('c3'),
  2791. type=Type(String('bool'))
  2792. )
  2793. ),
  2794. Declaration(
  2795. Variable(Symbol('c4'),
  2796. type=Type(String('bool'))
  2797. )
  2798. ),
  2799. Declaration(
  2800. Variable(Symbol('c5'),
  2801. type=Type(String('bool'))
  2802. )
  2803. ),
  2804. Declaration(
  2805. Variable(Symbol('c6'),
  2806. type=Type(String('bool'))
  2807. )
  2808. ),
  2809. Assignment(
  2810. Variable(Symbol('c1')),
  2811. true
  2812. ),
  2813. Assignment(
  2814. Variable(Symbol('c2')),
  2815. false
  2816. ),
  2817. Assignment(
  2818. Variable(Symbol('c3')),
  2819. true
  2820. ),
  2821. Assignment(
  2822. Variable(Symbol('c4')),
  2823. false
  2824. ),
  2825. Assignment(
  2826. Variable(Symbol('c5')),
  2827. true
  2828. ),
  2829. Assignment(
  2830. Variable(Symbol('c6')),
  2831. false
  2832. )
  2833. )
  2834. )
  2835. assert res27[0] == FunctionDefinition(
  2836. NoneToken(),
  2837. name=String('func'),
  2838. parameters=(),
  2839. body=CodeBlock(
  2840. Declaration(
  2841. Variable(Symbol('c1'),
  2842. type=Type(String('bool'))
  2843. )
  2844. ),
  2845. Declaration(
  2846. Variable(Symbol('c2'),
  2847. type=Type(String('bool'))
  2848. )
  2849. ),
  2850. Declaration(
  2851. Variable(Symbol('c3'),
  2852. type=Type(String('bool'))
  2853. )
  2854. ),
  2855. Declaration(
  2856. Variable(Symbol('c4'),
  2857. type=Type(String('bool'))
  2858. )
  2859. ),
  2860. Declaration(
  2861. Variable(Symbol('c5'),
  2862. type=Type(String('bool'))
  2863. )
  2864. ),
  2865. Declaration(
  2866. Variable(Symbol('c6'),
  2867. type=Type(String('bool'))
  2868. )
  2869. ),
  2870. Assignment(
  2871. Variable(Symbol('c1')),
  2872. true
  2873. ),
  2874. Assignment(
  2875. Variable(Symbol('c2')),
  2876. false
  2877. ),
  2878. Assignment(
  2879. Variable(Symbol('c3')),
  2880. false
  2881. ),
  2882. Assignment(
  2883. Variable(Symbol('c4')),
  2884. true
  2885. ),
  2886. Assignment(
  2887. Variable(Symbol('c5')),
  2888. true
  2889. ),
  2890. Assignment(
  2891. Variable(Symbol('c6')),
  2892. false)
  2893. )
  2894. )
  2895. assert res28[0] == FunctionDefinition(
  2896. NoneToken(),
  2897. name=String('func'),
  2898. parameters=(),
  2899. body=CodeBlock(
  2900. Declaration(
  2901. Variable(Symbol('a'),
  2902. type=Type(String('bool'))
  2903. )
  2904. ),
  2905. Declaration(
  2906. Variable(Symbol('c1'),
  2907. type=Type(String('bool'))
  2908. )
  2909. ),
  2910. Declaration(
  2911. Variable(Symbol('c2'),
  2912. type=Type(String('bool'))
  2913. )
  2914. ),
  2915. Declaration(
  2916. Variable(Symbol('c3'),
  2917. type=Type(String('bool'))
  2918. )
  2919. ),
  2920. Declaration(
  2921. Variable(Symbol('c4'),
  2922. type=Type(String('bool'))
  2923. )
  2924. ),
  2925. Assignment(
  2926. Variable(Symbol('c1')),
  2927. Symbol('a')
  2928. ),
  2929. Assignment(
  2930. Variable(Symbol('c2')),
  2931. false
  2932. ),
  2933. Assignment(
  2934. Variable(Symbol('c3')),
  2935. true
  2936. ),
  2937. Assignment(
  2938. Variable(Symbol('c4')),
  2939. Symbol('a')
  2940. )
  2941. )
  2942. )
  2943. assert res29[0] == FunctionDefinition(
  2944. NoneToken(),
  2945. name=String('func'),
  2946. parameters=(),
  2947. body=CodeBlock(
  2948. Declaration(
  2949. Variable(Symbol('a'),
  2950. type=IntBaseType(String('intc'))
  2951. )
  2952. ),
  2953. Declaration(
  2954. Variable(Symbol('c1'),
  2955. type=Type(String('bool'))
  2956. )
  2957. ),
  2958. Declaration(
  2959. Variable(Symbol('c2'),
  2960. type=Type(String('bool'))
  2961. )
  2962. ),
  2963. Declaration(
  2964. Variable(Symbol('c3'),
  2965. type=Type(String('bool'))
  2966. )
  2967. ),
  2968. Declaration(
  2969. Variable(Symbol('c4'),
  2970. type=Type(String('bool'))
  2971. )
  2972. ),
  2973. Assignment(
  2974. Variable(Symbol('c1')),
  2975. Symbol('a')
  2976. ),
  2977. Assignment(
  2978. Variable(Symbol('c2')),
  2979. false
  2980. ),
  2981. Assignment(
  2982. Variable(Symbol('c3')),
  2983. true
  2984. ),
  2985. Assignment(
  2986. Variable(Symbol('c4')),
  2987. Symbol('a')
  2988. )
  2989. )
  2990. )
  2991. assert res30[0] == FunctionDefinition(
  2992. NoneToken(),
  2993. name=String('func'),
  2994. parameters=(),
  2995. body=CodeBlock(
  2996. Declaration(
  2997. Variable(Symbol('a'),
  2998. type=IntBaseType(String('intc'))
  2999. )
  3000. ),
  3001. Declaration(
  3002. Variable(Symbol('b'),
  3003. type=IntBaseType(String('intc'))
  3004. )
  3005. ),
  3006. Declaration(
  3007. Variable(Symbol('c'),
  3008. type=Type(String('bool'))
  3009. )
  3010. ),
  3011. Declaration(
  3012. Variable(Symbol('d'),
  3013. type=Type(String('bool'))
  3014. )
  3015. ),
  3016. Declaration(
  3017. Variable(Symbol('c1'),
  3018. type=Type(String('bool'))
  3019. )
  3020. ),
  3021. Declaration(
  3022. Variable(Symbol('c2'),
  3023. type=Type(String('bool'))
  3024. )
  3025. ),
  3026. Declaration(
  3027. Variable(Symbol('c3'),
  3028. type=Type(String('bool'))
  3029. )
  3030. ),
  3031. Declaration(
  3032. Variable(Symbol('c4'),
  3033. type=Type(String('bool'))
  3034. )
  3035. ),
  3036. Declaration(
  3037. Variable(Symbol('c5'),
  3038. type=Type(String('bool'))
  3039. )
  3040. ),
  3041. Declaration(
  3042. Variable(Symbol('c6'),
  3043. type=Type(String('bool'))
  3044. )
  3045. ),
  3046. Assignment(
  3047. Variable(Symbol('c1')),
  3048. And(
  3049. Symbol('a'),
  3050. Symbol('b')
  3051. )
  3052. ),
  3053. Assignment(
  3054. Variable(Symbol('c2')),
  3055. And(
  3056. Symbol('a'),
  3057. Symbol('c')
  3058. )
  3059. ),
  3060. Assignment(
  3061. Variable(Symbol('c3')),
  3062. And(
  3063. Symbol('c'),
  3064. Symbol('d')
  3065. )
  3066. ),
  3067. Assignment(
  3068. Variable(Symbol('c4')),
  3069. Or(
  3070. Symbol('a'),
  3071. Symbol('b')
  3072. )
  3073. ),
  3074. Assignment(
  3075. Variable(Symbol('c5')),
  3076. Or(
  3077. Symbol('a'),
  3078. Symbol('c')
  3079. )
  3080. ),
  3081. Assignment(
  3082. Variable(Symbol('c6')),
  3083. Or(
  3084. Symbol('c'),
  3085. Symbol('d')
  3086. )
  3087. )
  3088. )
  3089. )
  3090. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise1, 'c'))
  3091. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise2, 'c'))
  3092. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise3, 'c'))
  3093. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise4, 'c'))
  3094. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise5, 'c'))
  3095. @XFAIL
  3096. def test_var_decl():
  3097. c_src1 = (
  3098. 'int b = 100;' + '\n' +
  3099. 'int a = b;' + '\n'
  3100. )
  3101. c_src2 = (
  3102. 'int a = 1;' + '\n' +
  3103. 'int b = a + 1;' + '\n'
  3104. )
  3105. c_src3 = (
  3106. 'float a = 10.0 + 2.5;' + '\n' +
  3107. 'float b = a * 20.0;' + '\n'
  3108. )
  3109. c_src4 = (
  3110. 'int a = 1 + 100 - 3 * 6;' + '\n'
  3111. )
  3112. c_src5 = (
  3113. 'int a = (((1 + 100) * 12) - 3) * (6 - 10);' + '\n'
  3114. )
  3115. c_src6 = (
  3116. 'int b = 2;' + '\n' +
  3117. 'int c = 3;' + '\n' +
  3118. 'int a = b + c * 4;' + '\n'
  3119. )
  3120. c_src7 = (
  3121. 'int b = 1;' + '\n' +
  3122. 'int c = b + 2;' + '\n' +
  3123. 'int a = 10 * b * b * c;' + '\n'
  3124. )
  3125. c_src8 = (
  3126. 'void func()'+
  3127. '{' + '\n' +
  3128. 'int a = 1;' + '\n' +
  3129. 'int b = 2;' + '\n' +
  3130. 'int temp = a;' + '\n' +
  3131. 'a = b;' + '\n' +
  3132. 'b = temp;' + '\n' +
  3133. '}'
  3134. )
  3135. c_src9 = (
  3136. 'int a = 1;' + '\n' +
  3137. 'int b = 2;' + '\n' +
  3138. 'int c = a;' + '\n' +
  3139. 'int d = a + b + c;' + '\n' +
  3140. 'int e = a*a*a + 3*a*a*b + 3*a*b*b + b*b*b;' + '\n'
  3141. 'int f = (a + b + c) * (a + b - c);' + '\n' +
  3142. 'int g = (a + b + c + d)*(a + b + c + d)*(a * (b - c));'
  3143. + '\n'
  3144. )
  3145. c_src10 = (
  3146. 'float a = 10.0;' + '\n' +
  3147. 'float b = 2.5;' + '\n' +
  3148. 'float c = a*a + 2*a*b + b*b;' + '\n'
  3149. )
  3150. c_src11 = (
  3151. 'float a = 10.0 / 2.5;' + '\n'
  3152. )
  3153. c_src12 = (
  3154. 'int a = 100 / 4;' + '\n'
  3155. )
  3156. c_src13 = (
  3157. 'int a = 20 - 100 / 4 * 5 + 10;' + '\n'
  3158. )
  3159. c_src14 = (
  3160. 'int a = (20 - 100) / 4 * (5 + 10);' + '\n'
  3161. )
  3162. c_src15 = (
  3163. 'int a = 4;' + '\n' +
  3164. 'int b = 2;' + '\n' +
  3165. 'float c = b/a;' + '\n'
  3166. )
  3167. c_src16 = (
  3168. 'int a = 2;' + '\n' +
  3169. 'int d = 5;' + '\n' +
  3170. 'int n = 10;' + '\n' +
  3171. 'int s = (a/2)*(2*a + (n-1)*d);' + '\n'
  3172. )
  3173. c_src17 = (
  3174. 'int a = 1 % 2;' + '\n'
  3175. )
  3176. c_src18 = (
  3177. 'int a = 2;' + '\n' +
  3178. 'int b = a % 3;' + '\n'
  3179. )
  3180. c_src19 = (
  3181. 'int a = 100;' + '\n' +
  3182. 'int b = 3;' + '\n' +
  3183. 'int c = a % b;' + '\n'
  3184. )
  3185. c_src20 = (
  3186. 'int a = 100;' + '\n' +
  3187. 'int b = 3;' + '\n' +
  3188. 'int mod = 1000000007;' + '\n' +
  3189. 'int c = (a + b * (100/a)) % mod;' + '\n'
  3190. )
  3191. c_src21 = (
  3192. 'int a = 100;' + '\n' +
  3193. 'int b = 3;' + '\n' +
  3194. 'int mod = 1000000007;' + '\n' +
  3195. 'int c = ((a % mod + b % mod) % mod *(' \
  3196. 'a % mod - b % mod) % mod) % mod;' + '\n'
  3197. )
  3198. c_src22 = (
  3199. 'bool a = 1 == 2, b = 1 != 2;'
  3200. )
  3201. c_src23 = (
  3202. 'bool a = 1 < 2, b = 1 <= 2, c = 1 > 2, d = 1 >= 2;'
  3203. )
  3204. c_src24 = (
  3205. 'int a = 1, b = 2;' + '\n' +
  3206. 'bool c1 = a == 1;' + '\n' +
  3207. 'bool c2 = b == 2;' + '\n' +
  3208. 'bool c3 = 1 != a;' + '\n' +
  3209. 'bool c4 = 1 != b;' + '\n' +
  3210. 'bool c5 = a < 0;' + '\n' +
  3211. 'bool c6 = b <= 10;' + '\n' +
  3212. 'bool c7 = a > 0;' + '\n' +
  3213. 'bool c8 = b >= 11;'
  3214. )
  3215. c_src25 = (
  3216. 'int a = 3, b = 4;' + '\n' +
  3217. 'bool c1 = a == b;' + '\n' +
  3218. 'bool c2 = a != b;' + '\n' +
  3219. 'bool c3 = a < b;' + '\n' +
  3220. 'bool c4 = a <= b;' + '\n' +
  3221. 'bool c5 = a > b;' + '\n' +
  3222. 'bool c6 = a >= b;'
  3223. )
  3224. c_src26 = (
  3225. 'float a = 1.25, b = 2.5;' + '\n' +
  3226. 'bool c1 = a == 1.25;' + '\n' +
  3227. 'bool c2 = b == 2.54;' + '\n' +
  3228. 'bool c3 = 1.2 != a;' + '\n' +
  3229. 'bool c4 = 1.5 != b;'
  3230. )
  3231. c_src27 = (
  3232. 'float a = 1.25, b = 2.5;' + '\n' +
  3233. 'bool c1 = a == b;' + '\n' +
  3234. 'bool c2 = a != b;' + '\n' +
  3235. 'bool c3 = a < b;' + '\n' +
  3236. 'bool c4 = a <= b;' + '\n' +
  3237. 'bool c5 = a > b;' + '\n' +
  3238. 'bool c6 = a >= b;'
  3239. )
  3240. c_src28 = (
  3241. 'bool c1 = true == true;' + '\n' +
  3242. 'bool c2 = true == false;' + '\n' +
  3243. 'bool c3 = false == false;' + '\n' +
  3244. 'bool c4 = true != true;' + '\n' +
  3245. 'bool c5 = true != false;' + '\n' +
  3246. 'bool c6 = false != false;'
  3247. )
  3248. c_src29 = (
  3249. 'bool c1 = true && true;' + '\n' +
  3250. 'bool c2 = true && false;' + '\n' +
  3251. 'bool c3 = false && false;' + '\n' +
  3252. 'bool c4 = true || true;' + '\n' +
  3253. 'bool c5 = true || false;' + '\n' +
  3254. 'bool c6 = false || false;'
  3255. )
  3256. c_src30 = (
  3257. 'bool a = false;' + '\n' +
  3258. 'bool c1 = a && true;' + '\n' +
  3259. 'bool c2 = false && a;' + '\n' +
  3260. 'bool c3 = true || a;' + '\n' +
  3261. 'bool c4 = a || false;'
  3262. )
  3263. c_src31 = (
  3264. 'int a = 1;' + '\n' +
  3265. 'bool c1 = a && 1;' + '\n' +
  3266. 'bool c2 = a && 0;' + '\n' +
  3267. 'bool c3 = a || 1;' + '\n' +
  3268. 'bool c4 = 0 || a;'
  3269. )
  3270. c_src32 = (
  3271. 'int a = 1, b = 0;' + '\n' +
  3272. 'bool c = false, d = true;'+ '\n' +
  3273. 'bool c1 = a && b;' + '\n' +
  3274. 'bool c2 = a && c;' + '\n' +
  3275. 'bool c3 = c && d;' + '\n' +
  3276. 'bool c4 = a || b;' + '\n' +
  3277. 'bool c5 = a || c;' + '\n' +
  3278. 'bool c6 = c || d;'
  3279. )
  3280. c_src_raise1 = (
  3281. "char a = 'b';"
  3282. )
  3283. c_src_raise2 = (
  3284. 'int a[] = {10, 20};'
  3285. )
  3286. res1 = SymPyExpression(c_src1, 'c').return_expr()
  3287. res2 = SymPyExpression(c_src2, 'c').return_expr()
  3288. res3 = SymPyExpression(c_src3, 'c').return_expr()
  3289. res4 = SymPyExpression(c_src4, 'c').return_expr()
  3290. res5 = SymPyExpression(c_src5, 'c').return_expr()
  3291. res6 = SymPyExpression(c_src6, 'c').return_expr()
  3292. res7 = SymPyExpression(c_src7, 'c').return_expr()
  3293. res8 = SymPyExpression(c_src8, 'c').return_expr()
  3294. res9 = SymPyExpression(c_src9, 'c').return_expr()
  3295. res10 = SymPyExpression(c_src10, 'c').return_expr()
  3296. res11 = SymPyExpression(c_src11, 'c').return_expr()
  3297. res12 = SymPyExpression(c_src12, 'c').return_expr()
  3298. res13 = SymPyExpression(c_src13, 'c').return_expr()
  3299. res14 = SymPyExpression(c_src14, 'c').return_expr()
  3300. res15 = SymPyExpression(c_src15, 'c').return_expr()
  3301. res16 = SymPyExpression(c_src16, 'c').return_expr()
  3302. res17 = SymPyExpression(c_src17, 'c').return_expr()
  3303. res18 = SymPyExpression(c_src18, 'c').return_expr()
  3304. res19 = SymPyExpression(c_src19, 'c').return_expr()
  3305. res20 = SymPyExpression(c_src20, 'c').return_expr()
  3306. res21 = SymPyExpression(c_src21, 'c').return_expr()
  3307. res22 = SymPyExpression(c_src22, 'c').return_expr()
  3308. res23 = SymPyExpression(c_src23, 'c').return_expr()
  3309. res24 = SymPyExpression(c_src24, 'c').return_expr()
  3310. res25 = SymPyExpression(c_src25, 'c').return_expr()
  3311. res26 = SymPyExpression(c_src26, 'c').return_expr()
  3312. res27 = SymPyExpression(c_src27, 'c').return_expr()
  3313. res28 = SymPyExpression(c_src28, 'c').return_expr()
  3314. res29 = SymPyExpression(c_src29, 'c').return_expr()
  3315. res30 = SymPyExpression(c_src30, 'c').return_expr()
  3316. res31 = SymPyExpression(c_src31, 'c').return_expr()
  3317. res32 = SymPyExpression(c_src32, 'c').return_expr()
  3318. assert res1[0] == Declaration(
  3319. Variable(Symbol('b'),
  3320. type=IntBaseType(String('intc')),
  3321. value=Integer(100)
  3322. )
  3323. )
  3324. assert res1[1] == Declaration(
  3325. Variable(Symbol('a'),
  3326. type=IntBaseType(String('intc')),
  3327. value=Symbol('b')
  3328. )
  3329. )
  3330. assert res2[0] == Declaration(
  3331. Variable(Symbol('a'),
  3332. type=IntBaseType(String('intc')),
  3333. value=Integer(1)
  3334. )
  3335. )
  3336. assert res2[1] == Declaration(Variable(Symbol('b'),
  3337. type=IntBaseType(String('intc')),
  3338. value=Add(
  3339. Symbol('a'),
  3340. Integer(1)
  3341. )
  3342. )
  3343. )
  3344. assert res3[0] == Declaration(
  3345. Variable(Symbol('a'),
  3346. type=FloatType(
  3347. String('float32'),
  3348. nbits=Integer(32),
  3349. nmant=Integer(23),
  3350. nexp=Integer(8)
  3351. ),
  3352. value=Float('12.5', precision=53)
  3353. )
  3354. )
  3355. assert res3[1] == Declaration(
  3356. Variable(Symbol('b'),
  3357. type=FloatType(
  3358. String('float32'),
  3359. nbits=Integer(32),
  3360. nmant=Integer(23),
  3361. nexp=Integer(8)
  3362. ),
  3363. value=Mul(
  3364. Float('20.0', precision=53),
  3365. Symbol('a')
  3366. )
  3367. )
  3368. )
  3369. assert res4[0] == Declaration(
  3370. Variable(Symbol('a'),
  3371. type=IntBaseType(String('intc')),
  3372. value=Integer(83)
  3373. )
  3374. )
  3375. assert res5[0] == Declaration(
  3376. Variable(Symbol('a'),
  3377. type=IntBaseType(String('intc')),
  3378. value=Integer(-4836)
  3379. )
  3380. )
  3381. assert res6[0] == Declaration(
  3382. Variable(Symbol('b'),
  3383. type=IntBaseType(String('intc')),
  3384. value=Integer(2)
  3385. )
  3386. )
  3387. assert res6[1] == Declaration(
  3388. Variable(Symbol('c'),
  3389. type=IntBaseType(String('intc')),
  3390. value=Integer(3)
  3391. )
  3392. )
  3393. assert res6[2] == Declaration(
  3394. Variable(Symbol('a'),
  3395. type=IntBaseType(String('intc')),
  3396. value=Add(
  3397. Symbol('b'),
  3398. Mul(
  3399. Integer(4),
  3400. Symbol('c')
  3401. )
  3402. )
  3403. )
  3404. )
  3405. assert res7[0] == Declaration(
  3406. Variable(Symbol('b'),
  3407. type=IntBaseType(String('intc')),
  3408. value=Integer(1)
  3409. )
  3410. )
  3411. assert res7[1] == Declaration(
  3412. Variable(Symbol('c'),
  3413. type=IntBaseType(String('intc')),
  3414. value=Add(
  3415. Symbol('b'),
  3416. Integer(2)
  3417. )
  3418. )
  3419. )
  3420. assert res7[2] == Declaration(
  3421. Variable(Symbol('a'),
  3422. type=IntBaseType(String('intc')),
  3423. value=Mul(
  3424. Integer(10),
  3425. Pow(
  3426. Symbol('b'),
  3427. Integer(2)
  3428. ),
  3429. Symbol('c')
  3430. )
  3431. )
  3432. )
  3433. assert res8[0] == FunctionDefinition(
  3434. NoneToken(),
  3435. name=String('func'),
  3436. parameters=(),
  3437. body=CodeBlock(
  3438. Declaration(
  3439. Variable(Symbol('a'),
  3440. type=IntBaseType(String('intc')),
  3441. value=Integer(1)
  3442. )
  3443. ),
  3444. Declaration(
  3445. Variable(Symbol('b'),
  3446. type=IntBaseType(String('intc')),
  3447. value=Integer(2)
  3448. )
  3449. ),
  3450. Declaration(
  3451. Variable(Symbol('temp'),
  3452. type=IntBaseType(String('intc')),
  3453. value=Symbol('a')
  3454. )
  3455. ),
  3456. Assignment(
  3457. Variable(Symbol('a')),
  3458. Symbol('b')
  3459. ),
  3460. Assignment(
  3461. Variable(Symbol('b')),
  3462. Symbol('temp')
  3463. )
  3464. )
  3465. )
  3466. assert res9[0] == Declaration(
  3467. Variable(Symbol('a'),
  3468. type=IntBaseType(String('intc')),
  3469. value=Integer(1)
  3470. )
  3471. )
  3472. assert res9[1] == Declaration(
  3473. Variable(Symbol('b'),
  3474. type=IntBaseType(String('intc')),
  3475. value=Integer(2)
  3476. )
  3477. )
  3478. assert res9[2] == Declaration(
  3479. Variable(Symbol('c'),
  3480. type=IntBaseType(String('intc')),
  3481. value=Symbol('a')
  3482. )
  3483. )
  3484. assert res9[3] == Declaration(
  3485. Variable(Symbol('d'),
  3486. type=IntBaseType(String('intc')),
  3487. value=Add(
  3488. Symbol('a'),
  3489. Symbol('b'),
  3490. Symbol('c')
  3491. )
  3492. )
  3493. )
  3494. assert res9[4] == Declaration(
  3495. Variable(Symbol('e'),
  3496. type=IntBaseType(String('intc')),
  3497. value=Add(
  3498. Pow(
  3499. Symbol('a'),
  3500. Integer(3)
  3501. ),
  3502. Mul(
  3503. Integer(3),
  3504. Pow(
  3505. Symbol('a'),
  3506. Integer(2)
  3507. ),
  3508. Symbol('b')
  3509. ),
  3510. Mul(
  3511. Integer(3),
  3512. Symbol('a'),
  3513. Pow(
  3514. Symbol('b'),
  3515. Integer(2)
  3516. )
  3517. ),
  3518. Pow(
  3519. Symbol('b'),
  3520. Integer(3)
  3521. )
  3522. )
  3523. )
  3524. )
  3525. assert res9[5] == Declaration(
  3526. Variable(Symbol('f'),
  3527. type=IntBaseType(String('intc')),
  3528. value=Mul(
  3529. Add(
  3530. Symbol('a'),
  3531. Symbol('b'),
  3532. Mul(
  3533. Integer(-1),
  3534. Symbol('c')
  3535. )
  3536. ),
  3537. Add(
  3538. Symbol('a'),
  3539. Symbol('b'),
  3540. Symbol('c')
  3541. )
  3542. )
  3543. )
  3544. )
  3545. assert res9[6] == Declaration(
  3546. Variable(Symbol('g'),
  3547. type=IntBaseType(String('intc')),
  3548. value=Mul(
  3549. Symbol('a'),
  3550. Add(
  3551. Symbol('b'),
  3552. Mul(
  3553. Integer(-1),
  3554. Symbol('c')
  3555. )
  3556. ),
  3557. Pow(
  3558. Add(
  3559. Symbol('a'),
  3560. Symbol('b'),
  3561. Symbol('c'),
  3562. Symbol('d')
  3563. ),
  3564. Integer(2)
  3565. )
  3566. )
  3567. )
  3568. )
  3569. assert res10[0] == Declaration(
  3570. Variable(Symbol('a'),
  3571. type=FloatType(
  3572. String('float32'),
  3573. nbits=Integer(32),
  3574. nmant=Integer(23),
  3575. nexp=Integer(8)
  3576. ),
  3577. value=Float('10.0', precision=53)
  3578. )
  3579. )
  3580. assert res10[1] == Declaration(
  3581. Variable(Symbol('b'),
  3582. type=FloatType(
  3583. String('float32'),
  3584. nbits=Integer(32),
  3585. nmant=Integer(23),
  3586. nexp=Integer(8)
  3587. ),
  3588. value=Float('2.5', precision=53)
  3589. )
  3590. )
  3591. assert res10[2] == Declaration(
  3592. Variable(Symbol('c'),
  3593. type=FloatType(
  3594. String('float32'),
  3595. nbits=Integer(32),
  3596. nmant=Integer(23),
  3597. nexp=Integer(8)
  3598. ),
  3599. value=Add(
  3600. Pow(
  3601. Symbol('a'),
  3602. Integer(2)
  3603. ),
  3604. Mul(
  3605. Integer(2),
  3606. Symbol('a'),
  3607. Symbol('b')
  3608. ),
  3609. Pow(
  3610. Symbol('b'),
  3611. Integer(2)
  3612. )
  3613. )
  3614. )
  3615. )
  3616. assert res11[0] == Declaration(
  3617. Variable(Symbol('a'),
  3618. type=FloatType(
  3619. String('float32'),
  3620. nbits=Integer(32),
  3621. nmant=Integer(23),
  3622. nexp=Integer(8)
  3623. ),
  3624. value=Float('4.0', precision=53)
  3625. )
  3626. )
  3627. assert res12[0] == Declaration(
  3628. Variable(Symbol('a'),
  3629. type=IntBaseType(String('intc')),
  3630. value=Integer(25)
  3631. )
  3632. )
  3633. assert res13[0] == Declaration(
  3634. Variable(Symbol('a'),
  3635. type=IntBaseType(String('intc')),
  3636. value=Integer(-95)
  3637. )
  3638. )
  3639. assert res14[0] == Declaration(
  3640. Variable(Symbol('a'),
  3641. type=IntBaseType(String('intc')),
  3642. value=Integer(-300)
  3643. )
  3644. )
  3645. assert res15[0] == Declaration(
  3646. Variable(Symbol('a'),
  3647. type=IntBaseType(String('intc')),
  3648. value=Integer(4)
  3649. )
  3650. )
  3651. assert res15[1] == Declaration(
  3652. Variable(Symbol('b'),
  3653. type=IntBaseType(String('intc')),
  3654. value=Integer(2)
  3655. )
  3656. )
  3657. assert res15[2] == Declaration(
  3658. Variable(Symbol('c'),
  3659. type=FloatType(
  3660. String('float32'),
  3661. nbits=Integer(32),
  3662. nmant=Integer(23),
  3663. nexp=Integer(8)
  3664. ),
  3665. value=Mul(
  3666. Pow(
  3667. Symbol('a'),
  3668. Integer(-1)
  3669. ),
  3670. Symbol('b')
  3671. )
  3672. )
  3673. )
  3674. assert res16[0] == Declaration(
  3675. Variable(Symbol('a'),
  3676. type=IntBaseType(String('intc')),
  3677. value=Integer(2)
  3678. )
  3679. )
  3680. assert res16[1] == Declaration(
  3681. Variable(Symbol('d'),
  3682. type=IntBaseType(String('intc')),
  3683. value=Integer(5)
  3684. )
  3685. )
  3686. assert res16[2] == Declaration(
  3687. Variable(Symbol('n'),
  3688. type=IntBaseType(String('intc')),
  3689. value=Integer(10)
  3690. )
  3691. )
  3692. assert res16[3] == Declaration(
  3693. Variable(Symbol('s'),
  3694. type=IntBaseType(String('intc')),
  3695. value=Mul(
  3696. Rational(1, 2),
  3697. Symbol('a'),
  3698. Add(
  3699. Mul(
  3700. Integer(2),
  3701. Symbol('a')
  3702. ),
  3703. Mul(
  3704. Symbol('d'),
  3705. Add(
  3706. Symbol('n'),
  3707. Integer(-1)
  3708. )
  3709. )
  3710. )
  3711. )
  3712. )
  3713. )
  3714. assert res17[0] == Declaration(
  3715. Variable(Symbol('a'),
  3716. type=IntBaseType(String('intc')),
  3717. value=Integer(1)
  3718. )
  3719. )
  3720. assert res18[0] == Declaration(
  3721. Variable(Symbol('a'),
  3722. type=IntBaseType(String('intc')),
  3723. value=Integer(2)
  3724. )
  3725. )
  3726. assert res18[1] == Declaration(
  3727. Variable(Symbol('b'),
  3728. type=IntBaseType(String('intc')),
  3729. value=Mod(
  3730. Symbol('a'),
  3731. Integer(3)
  3732. )
  3733. )
  3734. )
  3735. assert res19[0] == Declaration(
  3736. Variable(Symbol('a'),
  3737. type=IntBaseType(String('intc')),
  3738. value=Integer(100)
  3739. )
  3740. )
  3741. assert res19[1] == Declaration(
  3742. Variable(Symbol('b'),
  3743. type=IntBaseType(String('intc')),
  3744. value=Integer(3)
  3745. )
  3746. )
  3747. assert res19[2] == Declaration(
  3748. Variable(Symbol('c'),
  3749. type=IntBaseType(String('intc')),
  3750. value=Mod(
  3751. Symbol('a'),
  3752. Symbol('b')
  3753. )
  3754. )
  3755. )
  3756. assert res20[0] == Declaration(
  3757. Variable(Symbol('a'),
  3758. type=IntBaseType(String('intc')),
  3759. value=Integer(100)
  3760. )
  3761. )
  3762. assert res20[1] == Declaration(
  3763. Variable(Symbol('b'),
  3764. type=IntBaseType(String('intc')),
  3765. value=Integer(3)
  3766. )
  3767. )
  3768. assert res20[2] == Declaration(
  3769. Variable(Symbol('mod'),
  3770. type=IntBaseType(String('intc')),
  3771. value=Integer(1000000007)
  3772. )
  3773. )
  3774. assert res20[3] == Declaration(
  3775. Variable(Symbol('c'),
  3776. type=IntBaseType(String('intc')),
  3777. value=Mod(
  3778. Add(
  3779. Symbol('a'),
  3780. Mul(
  3781. Integer(100),
  3782. Pow(
  3783. Symbol('a'),
  3784. Integer(-1)
  3785. ),
  3786. Symbol('b')
  3787. )
  3788. ),
  3789. Symbol('mod')
  3790. )
  3791. )
  3792. )
  3793. assert res21[0] == Declaration(
  3794. Variable(Symbol('a'),
  3795. type=IntBaseType(String('intc')),
  3796. value=Integer(100)
  3797. )
  3798. )
  3799. assert res21[1] == Declaration(
  3800. Variable(Symbol('b'),
  3801. type=IntBaseType(String('intc')),
  3802. value=Integer(3)
  3803. )
  3804. )
  3805. assert res21[2] == Declaration(
  3806. Variable(Symbol('mod'),
  3807. type=IntBaseType(String('intc')),
  3808. value=Integer(1000000007)
  3809. )
  3810. )
  3811. assert res21[3] == Declaration(
  3812. Variable(Symbol('c'),
  3813. type=IntBaseType(String('intc')),
  3814. value=Mod(
  3815. Mul(
  3816. Add(
  3817. Symbol('a'),
  3818. Mul(
  3819. Integer(-1),
  3820. Symbol('b')
  3821. )
  3822. ),
  3823. Add(
  3824. Symbol('a'),
  3825. Symbol('b')
  3826. )
  3827. ),
  3828. Symbol('mod')
  3829. )
  3830. )
  3831. )
  3832. assert res22[0] == Declaration(
  3833. Variable(Symbol('a'),
  3834. type=Type(String('bool')),
  3835. value=false
  3836. )
  3837. )
  3838. assert res22[1] == Declaration(
  3839. Variable(Symbol('b'),
  3840. type=Type(String('bool')),
  3841. value=true
  3842. )
  3843. )
  3844. assert res23[0] == Declaration(
  3845. Variable(Symbol('a'),
  3846. type=Type(String('bool')),
  3847. value=true
  3848. )
  3849. )
  3850. assert res23[1] == Declaration(
  3851. Variable(Symbol('b'),
  3852. type=Type(String('bool')),
  3853. value=true
  3854. )
  3855. )
  3856. assert res23[2] == Declaration(
  3857. Variable(Symbol('c'),
  3858. type=Type(String('bool')),
  3859. value=false
  3860. )
  3861. )
  3862. assert res23[3] == Declaration(
  3863. Variable(Symbol('d'),
  3864. type=Type(String('bool')),
  3865. value=false
  3866. )
  3867. )
  3868. assert res24[0] == Declaration(
  3869. Variable(Symbol('a'),
  3870. type=IntBaseType(String('intc')),
  3871. value=Integer(1)
  3872. )
  3873. )
  3874. assert res24[1] == Declaration(
  3875. Variable(Symbol('b'),
  3876. type=IntBaseType(String('intc')),
  3877. value=Integer(2)
  3878. )
  3879. )
  3880. assert res24[2] == Declaration(
  3881. Variable(Symbol('c1'),
  3882. type=Type(String('bool')),
  3883. value=Equality(
  3884. Symbol('a'),
  3885. Integer(1)
  3886. )
  3887. )
  3888. )
  3889. assert res24[3] == Declaration(
  3890. Variable(Symbol('c2'),
  3891. type=Type(String('bool')),
  3892. value=Equality(
  3893. Symbol('b'),
  3894. Integer(2)
  3895. )
  3896. )
  3897. )
  3898. assert res24[4] == Declaration(
  3899. Variable(Symbol('c3'),
  3900. type=Type(String('bool')),
  3901. value=Unequality(
  3902. Integer(1),
  3903. Symbol('a')
  3904. )
  3905. )
  3906. )
  3907. assert res24[5] == Declaration(
  3908. Variable(Symbol('c4'),
  3909. type=Type(String('bool')),
  3910. value=Unequality(
  3911. Integer(1),
  3912. Symbol('b')
  3913. )
  3914. )
  3915. )
  3916. assert res24[6] == Declaration(
  3917. Variable(Symbol('c5'),
  3918. type=Type(String('bool')),
  3919. value=StrictLessThan(Symbol('a'),
  3920. Integer(0)
  3921. )
  3922. )
  3923. )
  3924. assert res24[7] == Declaration(
  3925. Variable(Symbol('c6'),
  3926. type=Type(String('bool')),
  3927. value=LessThan(
  3928. Symbol('b'),
  3929. Integer(10)
  3930. )
  3931. )
  3932. )
  3933. assert res24[8] == Declaration(
  3934. Variable(Symbol('c7'),
  3935. type=Type(String('bool')),
  3936. value=StrictGreaterThan(
  3937. Symbol('a'),
  3938. Integer(0)
  3939. )
  3940. )
  3941. )
  3942. assert res24[9] == Declaration(
  3943. Variable(Symbol('c8'),
  3944. type=Type(String('bool')),
  3945. value=GreaterThan(
  3946. Symbol('b'),
  3947. Integer(11)
  3948. )
  3949. )
  3950. )
  3951. assert res25[0] == Declaration(
  3952. Variable(Symbol('a'),
  3953. type=IntBaseType(String('intc')),
  3954. value=Integer(3)
  3955. )
  3956. )
  3957. assert res25[1] == Declaration(
  3958. Variable(Symbol('b'),
  3959. type=IntBaseType(String('intc')),
  3960. value=Integer(4)
  3961. )
  3962. )
  3963. assert res25[2] == Declaration(Variable(Symbol('c1'),
  3964. type=Type(String('bool')),
  3965. value=Equality(
  3966. Symbol('a'),
  3967. Symbol('b')
  3968. )
  3969. )
  3970. )
  3971. assert res25[3] == Declaration(
  3972. Variable(Symbol('c2'),
  3973. type=Type(String('bool')),
  3974. value=Unequality(
  3975. Symbol('a'),
  3976. Symbol('b')
  3977. )
  3978. )
  3979. )
  3980. assert res25[4] == Declaration(
  3981. Variable(Symbol('c3'),
  3982. type=Type(String('bool')),
  3983. value=StrictLessThan(
  3984. Symbol('a'),
  3985. Symbol('b')
  3986. )
  3987. )
  3988. )
  3989. assert res25[5] == Declaration(
  3990. Variable(Symbol('c4'),
  3991. type=Type(String('bool')),
  3992. value=LessThan(
  3993. Symbol('a'),
  3994. Symbol('b')
  3995. )
  3996. )
  3997. )
  3998. assert res25[6] == Declaration(
  3999. Variable(Symbol('c5'),
  4000. type=Type(String('bool')),
  4001. value=StrictGreaterThan(
  4002. Symbol('a'),
  4003. Symbol('b')
  4004. )
  4005. )
  4006. )
  4007. assert res25[7] == Declaration(
  4008. Variable(Symbol('c6'),
  4009. type=Type(String('bool')),
  4010. value=GreaterThan(
  4011. Symbol('a'),
  4012. Symbol('b')
  4013. )
  4014. )
  4015. )
  4016. assert res26[0] == Declaration(
  4017. Variable(Symbol('a'),
  4018. type=FloatType(
  4019. String('float32'),
  4020. nbits=Integer(32),
  4021. nmant=Integer(23),
  4022. nexp=Integer(8)
  4023. ),
  4024. value=Float('1.25', precision=53)
  4025. )
  4026. )
  4027. assert res26[1] == Declaration(
  4028. Variable(Symbol('b'),
  4029. type=FloatType(
  4030. String('float32'),
  4031. nbits=Integer(32),
  4032. nmant=Integer(23),
  4033. nexp=Integer(8)
  4034. ),
  4035. value=Float('2.5', precision=53)
  4036. )
  4037. )
  4038. assert res26[2] == Declaration(
  4039. Variable(Symbol('c1'),
  4040. type=Type(String('bool')),
  4041. value=Equality(
  4042. Symbol('a'),
  4043. Float('1.25', precision=53)
  4044. )
  4045. )
  4046. )
  4047. assert res26[3] == Declaration(
  4048. Variable(Symbol('c2'),
  4049. type=Type(String('bool')),
  4050. value=Equality(
  4051. Symbol('b'),
  4052. Float('2.54', precision=53)
  4053. )
  4054. )
  4055. )
  4056. assert res26[4] == Declaration(
  4057. Variable(Symbol('c3'),
  4058. type=Type(String('bool')),
  4059. value=Unequality(
  4060. Float('1.2', precision=53),
  4061. Symbol('a')
  4062. )
  4063. )
  4064. )
  4065. assert res26[5] == Declaration(
  4066. Variable(Symbol('c4'),
  4067. type=Type(String('bool')),
  4068. value=Unequality(
  4069. Float('1.5', precision=53),
  4070. Symbol('b')
  4071. )
  4072. )
  4073. )
  4074. assert res27[0] == Declaration(
  4075. Variable(Symbol('a'),
  4076. type=FloatType(
  4077. String('float32'),
  4078. nbits=Integer(32),
  4079. nmant=Integer(23),
  4080. nexp=Integer(8)
  4081. ),
  4082. value=Float('1.25', precision=53)
  4083. )
  4084. )
  4085. assert res27[1] == Declaration(
  4086. Variable(Symbol('b'),
  4087. type=FloatType(
  4088. String('float32'),
  4089. nbits=Integer(32),
  4090. nmant=Integer(23),
  4091. nexp=Integer(8)
  4092. ),
  4093. value=Float('2.5', precision=53)
  4094. )
  4095. )
  4096. assert res27[2] == Declaration(
  4097. Variable(Symbol('c1'),
  4098. type=Type(String('bool')),
  4099. value=Equality(
  4100. Symbol('a'),
  4101. Symbol('b')
  4102. )
  4103. )
  4104. )
  4105. assert res27[3] == Declaration(
  4106. Variable(Symbol('c2'),
  4107. type=Type(String('bool')),
  4108. value=Unequality(
  4109. Symbol('a'),
  4110. Symbol('b')
  4111. )
  4112. )
  4113. )
  4114. assert res27[4] == Declaration(
  4115. Variable(Symbol('c3'),
  4116. type=Type(String('bool')),
  4117. value=StrictLessThan(
  4118. Symbol('a'),
  4119. Symbol('b')
  4120. )
  4121. )
  4122. )
  4123. assert res27[5] == Declaration(
  4124. Variable(Symbol('c4'),
  4125. type=Type(String('bool')),
  4126. value=LessThan(
  4127. Symbol('a'),
  4128. Symbol('b')
  4129. )
  4130. )
  4131. )
  4132. assert res27[6] == Declaration(
  4133. Variable(Symbol('c5'),
  4134. type=Type(String('bool')),
  4135. value=StrictGreaterThan(
  4136. Symbol('a'),
  4137. Symbol('b')
  4138. )
  4139. )
  4140. )
  4141. assert res27[7] == Declaration(
  4142. Variable(Symbol('c6'),
  4143. type=Type(String('bool')),
  4144. value=GreaterThan(
  4145. Symbol('a'),
  4146. Symbol('b')
  4147. )
  4148. )
  4149. )
  4150. assert res28[0] == Declaration(
  4151. Variable(Symbol('c1'),
  4152. type=Type(String('bool')),
  4153. value=true
  4154. )
  4155. )
  4156. assert res28[1] == Declaration(
  4157. Variable(Symbol('c2'),
  4158. type=Type(String('bool')),
  4159. value=false
  4160. )
  4161. )
  4162. assert res28[2] == Declaration(
  4163. Variable(Symbol('c3'),
  4164. type=Type(String('bool')),
  4165. value=true
  4166. )
  4167. )
  4168. assert res28[3] == Declaration(
  4169. Variable(Symbol('c4'),
  4170. type=Type(String('bool')),
  4171. value=false
  4172. )
  4173. )
  4174. assert res28[4] == Declaration(
  4175. Variable(Symbol('c5'),
  4176. type=Type(String('bool')),
  4177. value=true
  4178. )
  4179. )
  4180. assert res28[5] == Declaration(
  4181. Variable(Symbol('c6'),
  4182. type=Type(String('bool')),
  4183. value=false
  4184. )
  4185. )
  4186. assert res29[0] == Declaration(
  4187. Variable(Symbol('c1'),
  4188. type=Type(String('bool')),
  4189. value=true
  4190. )
  4191. )
  4192. assert res29[1] == Declaration(
  4193. Variable(Symbol('c2'),
  4194. type=Type(String('bool')),
  4195. value=false
  4196. )
  4197. )
  4198. assert res29[2] == Declaration(
  4199. Variable(Symbol('c3'),
  4200. type=Type(String('bool')),
  4201. value=false
  4202. )
  4203. )
  4204. assert res29[3] == Declaration(
  4205. Variable(Symbol('c4'),
  4206. type=Type(String('bool')),
  4207. value=true
  4208. )
  4209. )
  4210. assert res29[4] == Declaration(
  4211. Variable(Symbol('c5'),
  4212. type=Type(String('bool')),
  4213. value=true
  4214. )
  4215. )
  4216. assert res29[5] == Declaration(
  4217. Variable(Symbol('c6'),
  4218. type=Type(String('bool')),
  4219. value=false
  4220. )
  4221. )
  4222. assert res30[0] == Declaration(
  4223. Variable(Symbol('a'),
  4224. type=Type(String('bool')),
  4225. value=false
  4226. )
  4227. )
  4228. assert res30[1] == Declaration(
  4229. Variable(Symbol('c1'),
  4230. type=Type(String('bool')),
  4231. value=Symbol('a')
  4232. )
  4233. )
  4234. assert res30[2] == Declaration(
  4235. Variable(Symbol('c2'),
  4236. type=Type(String('bool')),
  4237. value=false
  4238. )
  4239. )
  4240. assert res30[3] == Declaration(
  4241. Variable(Symbol('c3'),
  4242. type=Type(String('bool')),
  4243. value=true
  4244. )
  4245. )
  4246. assert res30[4] == Declaration(
  4247. Variable(Symbol('c4'),
  4248. type=Type(String('bool')),
  4249. value=Symbol('a')
  4250. )
  4251. )
  4252. assert res31[0] == Declaration(
  4253. Variable(Symbol('a'),
  4254. type=IntBaseType(String('intc')),
  4255. value=Integer(1)
  4256. )
  4257. )
  4258. assert res31[1] == Declaration(
  4259. Variable(Symbol('c1'),
  4260. type=Type(String('bool')),
  4261. value=Symbol('a')
  4262. )
  4263. )
  4264. assert res31[2] == Declaration(
  4265. Variable(Symbol('c2'),
  4266. type=Type(String('bool')),
  4267. value=false
  4268. )
  4269. )
  4270. assert res31[3] == Declaration(
  4271. Variable(Symbol('c3'),
  4272. type=Type(String('bool')),
  4273. value=true
  4274. )
  4275. )
  4276. assert res31[4] == Declaration(
  4277. Variable(Symbol('c4'),
  4278. type=Type(String('bool')),
  4279. value=Symbol('a')
  4280. )
  4281. )
  4282. assert res32[0] == Declaration(
  4283. Variable(Symbol('a'),
  4284. type=IntBaseType(String('intc')),
  4285. value=Integer(1)
  4286. )
  4287. )
  4288. assert res32[1] == Declaration(
  4289. Variable(Symbol('b'),
  4290. type=IntBaseType(String('intc')),
  4291. value=Integer(0)
  4292. )
  4293. )
  4294. assert res32[2] == Declaration(
  4295. Variable(Symbol('c'),
  4296. type=Type(String('bool')),
  4297. value=false
  4298. )
  4299. )
  4300. assert res32[3] == Declaration(
  4301. Variable(Symbol('d'),
  4302. type=Type(String('bool')),
  4303. value=true
  4304. )
  4305. )
  4306. assert res32[4] == Declaration(
  4307. Variable(Symbol('c1'),
  4308. type=Type(String('bool')),
  4309. value=And(
  4310. Symbol('a'),
  4311. Symbol('b')
  4312. )
  4313. )
  4314. )
  4315. assert res32[5] == Declaration(
  4316. Variable(Symbol('c2'),
  4317. type=Type(String('bool')),
  4318. value=And(
  4319. Symbol('a'),
  4320. Symbol('c')
  4321. )
  4322. )
  4323. )
  4324. assert res32[6] == Declaration(
  4325. Variable(Symbol('c3'),
  4326. type=Type(String('bool')),
  4327. value=And(
  4328. Symbol('c'),
  4329. Symbol('d')
  4330. )
  4331. )
  4332. )
  4333. assert res32[7] == Declaration(
  4334. Variable(Symbol('c4'),
  4335. type=Type(String('bool')),
  4336. value=Or(
  4337. Symbol('a'),
  4338. Symbol('b')
  4339. )
  4340. )
  4341. )
  4342. assert res32[8] == Declaration(
  4343. Variable(Symbol('c5'),
  4344. type=Type(String('bool')),
  4345. value=Or(
  4346. Symbol('a'),
  4347. Symbol('c')
  4348. )
  4349. )
  4350. )
  4351. assert res32[9] == Declaration(
  4352. Variable(Symbol('c6'),
  4353. type=Type(String('bool')),
  4354. value=Or(
  4355. Symbol('c'),
  4356. Symbol('d')
  4357. )
  4358. )
  4359. )
  4360. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise1, 'c'))
  4361. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise2, 'c'))
  4362. def test_paren_expr():
  4363. c_src1 = (
  4364. 'int a = (1);'
  4365. 'int b = (1 + 2 * 3);'
  4366. )
  4367. c_src2 = (
  4368. 'int a = 1, b = 2, c = 3;'
  4369. 'int d = (a);'
  4370. 'int e = (a + 1);'
  4371. 'int f = (a + b * c - d / e);'
  4372. )
  4373. res1 = SymPyExpression(c_src1, 'c').return_expr()
  4374. res2 = SymPyExpression(c_src2, 'c').return_expr()
  4375. assert res1[0] == Declaration(
  4376. Variable(Symbol('a'),
  4377. type=IntBaseType(String('intc')),
  4378. value=Integer(1)
  4379. )
  4380. )
  4381. assert res1[1] == Declaration(
  4382. Variable(Symbol('b'),
  4383. type=IntBaseType(String('intc')),
  4384. value=Integer(7)
  4385. )
  4386. )
  4387. assert res2[0] == Declaration(
  4388. Variable(Symbol('a'),
  4389. type=IntBaseType(String('intc')),
  4390. value=Integer(1)
  4391. )
  4392. )
  4393. assert res2[1] == Declaration(
  4394. Variable(Symbol('b'),
  4395. type=IntBaseType(String('intc')),
  4396. value=Integer(2)
  4397. )
  4398. )
  4399. assert res2[2] == Declaration(
  4400. Variable(Symbol('c'),
  4401. type=IntBaseType(String('intc')),
  4402. value=Integer(3)
  4403. )
  4404. )
  4405. assert res2[3] == Declaration(
  4406. Variable(Symbol('d'),
  4407. type=IntBaseType(String('intc')),
  4408. value=Symbol('a')
  4409. )
  4410. )
  4411. assert res2[4] == Declaration(
  4412. Variable(Symbol('e'),
  4413. type=IntBaseType(String('intc')),
  4414. value=Add(
  4415. Symbol('a'),
  4416. Integer(1)
  4417. )
  4418. )
  4419. )
  4420. assert res2[5] == Declaration(
  4421. Variable(Symbol('f'),
  4422. type=IntBaseType(String('intc')),
  4423. value=Add(
  4424. Symbol('a'),
  4425. Mul(
  4426. Symbol('b'),
  4427. Symbol('c')
  4428. ),
  4429. Mul(
  4430. Integer(-1),
  4431. Symbol('d'),
  4432. Pow(
  4433. Symbol('e'),
  4434. Integer(-1)
  4435. )
  4436. )
  4437. )
  4438. )
  4439. )
  4440. def test_unary_operators():
  4441. c_src1 = (
  4442. 'void func()'+
  4443. '{' + '\n' +
  4444. 'int a = 10;' + '\n' +
  4445. 'int b = 20;' + '\n' +
  4446. '++a;' + '\n' +
  4447. '--b;' + '\n' +
  4448. 'a++;' + '\n' +
  4449. 'b--;' + '\n' +
  4450. '}'
  4451. )
  4452. c_src2 = (
  4453. 'void func()'+
  4454. '{' + '\n' +
  4455. 'int a = 10;' + '\n' +
  4456. 'int b = -100;' + '\n' +
  4457. 'int c = +19;' + '\n' +
  4458. 'int d = ++a;' + '\n' +
  4459. 'int e = --b;' + '\n' +
  4460. 'int f = a++;' + '\n' +
  4461. 'int g = b--;' + '\n' +
  4462. 'bool h = !false;' + '\n' +
  4463. 'bool i = !d;' + '\n' +
  4464. 'bool j = !0;' + '\n' +
  4465. 'bool k = !10.0;' + '\n' +
  4466. '}'
  4467. )
  4468. c_src_raise1 = (
  4469. 'void func()'+
  4470. '{' + '\n' +
  4471. 'int a = 10;' + '\n' +
  4472. 'int b = ~a;' + '\n' +
  4473. '}'
  4474. )
  4475. c_src_raise2 = (
  4476. 'void func()'+
  4477. '{' + '\n' +
  4478. 'int a = 10;' + '\n' +
  4479. 'int b = *&a;' + '\n' +
  4480. '}'
  4481. )
  4482. res1 = SymPyExpression(c_src1, 'c').return_expr()
  4483. res2 = SymPyExpression(c_src2, 'c').return_expr()
  4484. assert res1[0] == FunctionDefinition(
  4485. NoneToken(),
  4486. name=String('func'),
  4487. parameters=(),
  4488. body=CodeBlock(
  4489. Declaration(
  4490. Variable(Symbol('a'),
  4491. type=IntBaseType(String('intc')),
  4492. value=Integer(10)
  4493. )
  4494. ),
  4495. Declaration(
  4496. Variable(Symbol('b'),
  4497. type=IntBaseType(String('intc')),
  4498. value=Integer(20)
  4499. )
  4500. ),
  4501. PreIncrement(Symbol('a')),
  4502. PreDecrement(Symbol('b')),
  4503. PostIncrement(Symbol('a')),
  4504. PostDecrement(Symbol('b'))
  4505. )
  4506. )
  4507. assert res2[0] == FunctionDefinition(
  4508. NoneToken(),
  4509. name=String('func'),
  4510. parameters=(),
  4511. body=CodeBlock(
  4512. Declaration(
  4513. Variable(Symbol('a'),
  4514. type=IntBaseType(String('intc')),
  4515. value=Integer(10)
  4516. )
  4517. ),
  4518. Declaration(
  4519. Variable(Symbol('b'),
  4520. type=IntBaseType(String('intc')),
  4521. value=Integer(-100)
  4522. )
  4523. ),
  4524. Declaration(
  4525. Variable(Symbol('c'),
  4526. type=IntBaseType(String('intc')),
  4527. value=Integer(19)
  4528. )
  4529. ),
  4530. Declaration(
  4531. Variable(Symbol('d'),
  4532. type=IntBaseType(String('intc')),
  4533. value=PreIncrement(Symbol('a'))
  4534. )
  4535. ),
  4536. Declaration(
  4537. Variable(Symbol('e'),
  4538. type=IntBaseType(String('intc')),
  4539. value=PreDecrement(Symbol('b'))
  4540. )
  4541. ),
  4542. Declaration(
  4543. Variable(Symbol('f'),
  4544. type=IntBaseType(String('intc')),
  4545. value=PostIncrement(Symbol('a'))
  4546. )
  4547. ),
  4548. Declaration(
  4549. Variable(Symbol('g'),
  4550. type=IntBaseType(String('intc')),
  4551. value=PostDecrement(Symbol('b'))
  4552. )
  4553. ),
  4554. Declaration(
  4555. Variable(Symbol('h'),
  4556. type=Type(String('bool')),
  4557. value=true
  4558. )
  4559. ),
  4560. Declaration(
  4561. Variable(Symbol('i'),
  4562. type=Type(String('bool')),
  4563. value=Not(Symbol('d'))
  4564. )
  4565. ),
  4566. Declaration(
  4567. Variable(Symbol('j'),
  4568. type=Type(String('bool')),
  4569. value=true
  4570. )
  4571. ),
  4572. Declaration(
  4573. Variable(Symbol('k'),
  4574. type=Type(String('bool')),
  4575. value=false
  4576. )
  4577. )
  4578. )
  4579. )
  4580. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise1, 'c'))
  4581. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise2, 'c'))
  4582. def test_compound_assignment_operator():
  4583. c_src = (
  4584. 'void func()'+
  4585. '{' + '\n' +
  4586. 'int a = 100;' + '\n' +
  4587. 'a += 10;' + '\n' +
  4588. 'a -= 10;' + '\n' +
  4589. 'a *= 10;' + '\n' +
  4590. 'a /= 10;' + '\n' +
  4591. 'a %= 10;' + '\n' +
  4592. '}'
  4593. )
  4594. res = SymPyExpression(c_src, 'c').return_expr()
  4595. assert res[0] == FunctionDefinition(
  4596. NoneToken(),
  4597. name=String('func'),
  4598. parameters=(),
  4599. body=CodeBlock(
  4600. Declaration(
  4601. Variable(
  4602. Symbol('a'),
  4603. type=IntBaseType(String('intc')),
  4604. value=Integer(100)
  4605. )
  4606. ),
  4607. AddAugmentedAssignment(
  4608. Variable(Symbol('a')),
  4609. Integer(10)
  4610. ),
  4611. SubAugmentedAssignment(
  4612. Variable(Symbol('a')),
  4613. Integer(10)
  4614. ),
  4615. MulAugmentedAssignment(
  4616. Variable(Symbol('a')),
  4617. Integer(10)
  4618. ),
  4619. DivAugmentedAssignment(
  4620. Variable(Symbol('a')),
  4621. Integer(10)
  4622. ),
  4623. ModAugmentedAssignment(
  4624. Variable(Symbol('a')),
  4625. Integer(10)
  4626. )
  4627. )
  4628. )
  4629. def test_while_stmt():
  4630. c_src1 = (
  4631. 'void func()'+
  4632. '{' + '\n' +
  4633. 'int i = 0;' + '\n' +
  4634. 'while(i < 10)' + '\n' +
  4635. '{' + '\n' +
  4636. 'i++;' + '\n' +
  4637. '}'
  4638. '}'
  4639. )
  4640. c_src2 = (
  4641. 'void func()'+
  4642. '{' + '\n' +
  4643. 'int i = 0;' + '\n' +
  4644. 'while(i < 10)' + '\n' +
  4645. 'i++;' + '\n' +
  4646. '}'
  4647. )
  4648. c_src3 = (
  4649. 'void func()'+
  4650. '{' + '\n' +
  4651. 'int i = 10;' + '\n' +
  4652. 'int cnt = 0;' + '\n' +
  4653. 'while(i > 0)' + '\n' +
  4654. '{' + '\n' +
  4655. 'i--;' + '\n' +
  4656. 'cnt++;' + '\n' +
  4657. '}' + '\n' +
  4658. '}'
  4659. )
  4660. c_src4 = (
  4661. 'int digit_sum(int n)'+
  4662. '{' + '\n' +
  4663. 'int sum = 0;' + '\n' +
  4664. 'while(n > 0)' + '\n' +
  4665. '{' + '\n' +
  4666. 'sum += (n % 10);' + '\n' +
  4667. 'n /= 10;' + '\n' +
  4668. '}' + '\n' +
  4669. 'return sum;' + '\n' +
  4670. '}'
  4671. )
  4672. c_src5 = (
  4673. 'void func()'+
  4674. '{' + '\n' +
  4675. 'while(1);' + '\n' +
  4676. '}'
  4677. )
  4678. res1 = SymPyExpression(c_src1, 'c').return_expr()
  4679. res2 = SymPyExpression(c_src2, 'c').return_expr()
  4680. res3 = SymPyExpression(c_src3, 'c').return_expr()
  4681. res4 = SymPyExpression(c_src4, 'c').return_expr()
  4682. res5 = SymPyExpression(c_src5, 'c').return_expr()
  4683. assert res1[0] == FunctionDefinition(
  4684. NoneToken(),
  4685. name=String('func'),
  4686. parameters=(),
  4687. body=CodeBlock(
  4688. Declaration(
  4689. Variable(Symbol('i'),
  4690. type=IntBaseType(String('intc')),
  4691. value=Integer(0)
  4692. )
  4693. ),
  4694. While(
  4695. StrictLessThan(
  4696. Symbol('i'),
  4697. Integer(10)
  4698. ),
  4699. body=CodeBlock(
  4700. PostIncrement(
  4701. Symbol('i')
  4702. )
  4703. )
  4704. )
  4705. )
  4706. )
  4707. assert res2[0] == res1[0]
  4708. assert res3[0] == FunctionDefinition(
  4709. NoneToken(),
  4710. name=String('func'),
  4711. parameters=(),
  4712. body=CodeBlock(
  4713. Declaration(
  4714. Variable(
  4715. Symbol('i'),
  4716. type=IntBaseType(String('intc')),
  4717. value=Integer(10)
  4718. )
  4719. ),
  4720. Declaration(
  4721. Variable(
  4722. Symbol('cnt'),
  4723. type=IntBaseType(String('intc')),
  4724. value=Integer(0)
  4725. )
  4726. ),
  4727. While(
  4728. StrictGreaterThan(
  4729. Symbol('i'),
  4730. Integer(0)
  4731. ),
  4732. body=CodeBlock(
  4733. PostDecrement(
  4734. Symbol('i')
  4735. ),
  4736. PostIncrement(
  4737. Symbol('cnt')
  4738. )
  4739. )
  4740. )
  4741. )
  4742. )
  4743. assert res4[0] == FunctionDefinition(
  4744. IntBaseType(String('intc')),
  4745. name=String('digit_sum'),
  4746. parameters=(
  4747. Variable(
  4748. Symbol('n'),
  4749. type=IntBaseType(String('intc'))
  4750. ),
  4751. ),
  4752. body=CodeBlock(
  4753. Declaration(
  4754. Variable(
  4755. Symbol('sum'),
  4756. type=IntBaseType(String('intc')),
  4757. value=Integer(0)
  4758. )
  4759. ),
  4760. While(
  4761. StrictGreaterThan(
  4762. Symbol('n'),
  4763. Integer(0)
  4764. ),
  4765. body=CodeBlock(
  4766. AddAugmentedAssignment(
  4767. Variable(
  4768. Symbol('sum')
  4769. ),
  4770. Mod(
  4771. Symbol('n'),
  4772. Integer(10)
  4773. )
  4774. ),
  4775. DivAugmentedAssignment(
  4776. Variable(
  4777. Symbol('n')
  4778. ),
  4779. Integer(10)
  4780. )
  4781. )
  4782. ),
  4783. Return('sum')
  4784. )
  4785. )
  4786. assert res5[0] == FunctionDefinition(
  4787. NoneToken(),
  4788. name=String('func'),
  4789. parameters=(),
  4790. body=CodeBlock(
  4791. While(
  4792. Integer(1),
  4793. body=CodeBlock(
  4794. NoneToken()
  4795. )
  4796. )
  4797. )
  4798. )
  4799. else:
  4800. def test_raise():
  4801. from sympy.parsing.c.c_parser import CCodeConverter
  4802. raises(ImportError, lambda: CCodeConverter())
  4803. raises(ImportError, lambda: SymPyExpression(' ', mode = 'c'))