test_c_parser.py 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248
  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. def test_int():
  140. c_src1 = 'int a = 1;'
  141. c_src2 = (
  142. 'int a = 1;' + '\n' +
  143. 'int b = 2;' + '\n'
  144. )
  145. c_src3 = 'int a = 2.345, b = 5.67;'
  146. c_src4 = 'int p = 6, q = 23.45;'
  147. c_src5 = "int x = '0', y = 'a';"
  148. c_src6 = "int r = true, s = false;"
  149. # cin.TypeKind.UCHAR
  150. c_src_type1 = (
  151. "signed char a = 1, b = 5.1;"
  152. )
  153. # cin.TypeKind.SHORT
  154. c_src_type2 = (
  155. "short a = 1, b = 5.1;"
  156. "signed short c = 1, d = 5.1;"
  157. "short int e = 1, f = 5.1;"
  158. "signed short int g = 1, h = 5.1;"
  159. )
  160. # cin.TypeKind.INT
  161. c_src_type3 = (
  162. "signed int a = 1, b = 5.1;"
  163. "int c = 1, d = 5.1;"
  164. )
  165. # cin.TypeKind.LONG
  166. c_src_type4 = (
  167. "long a = 1, b = 5.1;"
  168. "long int c = 1, d = 5.1;"
  169. )
  170. # cin.TypeKind.UCHAR
  171. c_src_type5 = "unsigned char a = 1, b = 5.1;"
  172. # cin.TypeKind.USHORT
  173. c_src_type6 = (
  174. "unsigned short a = 1, b = 5.1;"
  175. "unsigned short int c = 1, d = 5.1;"
  176. )
  177. # cin.TypeKind.UINT
  178. c_src_type7 = "unsigned int a = 1, b = 5.1;"
  179. # cin.TypeKind.ULONG
  180. c_src_type8 = (
  181. "unsigned long a = 1, b = 5.1;"
  182. "unsigned long int c = 1, d = 5.1;"
  183. )
  184. res1 = SymPyExpression(c_src1, 'c').return_expr()
  185. res2 = SymPyExpression(c_src2, 'c').return_expr()
  186. res3 = SymPyExpression(c_src3, 'c').return_expr()
  187. res4 = SymPyExpression(c_src4, 'c').return_expr()
  188. res5 = SymPyExpression(c_src5, 'c').return_expr()
  189. res6 = SymPyExpression(c_src6, 'c').return_expr()
  190. res_type1 = SymPyExpression(c_src_type1, 'c').return_expr()
  191. res_type2 = SymPyExpression(c_src_type2, 'c').return_expr()
  192. res_type3 = SymPyExpression(c_src_type3, 'c').return_expr()
  193. res_type4 = SymPyExpression(c_src_type4, 'c').return_expr()
  194. res_type5 = SymPyExpression(c_src_type5, 'c').return_expr()
  195. res_type6 = SymPyExpression(c_src_type6, 'c').return_expr()
  196. res_type7 = SymPyExpression(c_src_type7, 'c').return_expr()
  197. res_type8 = SymPyExpression(c_src_type8, 'c').return_expr()
  198. assert res1[0] == Declaration(
  199. Variable(
  200. Symbol('a'),
  201. type=IntBaseType(String('intc')),
  202. value=Integer(1)
  203. )
  204. )
  205. assert res2[0] == Declaration(
  206. Variable(
  207. Symbol('a'),
  208. type=IntBaseType(String('intc')),
  209. value=Integer(1)
  210. )
  211. )
  212. assert res2[1] == Declaration(
  213. Variable(
  214. Symbol('b'),
  215. type=IntBaseType(String('intc')),
  216. value=Integer(2)
  217. )
  218. )
  219. assert res3[0] == Declaration(
  220. Variable(
  221. Symbol('a'),
  222. type=IntBaseType(String('intc')),
  223. value=Integer(2)
  224. )
  225. )
  226. assert res3[1] == Declaration(
  227. Variable(
  228. Symbol('b'),
  229. type=IntBaseType(String('intc')),
  230. value=Integer(5)
  231. )
  232. )
  233. assert res4[0] == Declaration(
  234. Variable(
  235. Symbol('p'),
  236. type=IntBaseType(String('intc')),
  237. value=Integer(6)
  238. )
  239. )
  240. assert res4[1] == Declaration(
  241. Variable(
  242. Symbol('q'),
  243. type=IntBaseType(String('intc')),
  244. value=Integer(23)
  245. )
  246. )
  247. assert res5[0] == Declaration(
  248. Variable(
  249. Symbol('x'),
  250. type=IntBaseType(String('intc')),
  251. value=Integer(48)
  252. )
  253. )
  254. assert res5[1] == Declaration(
  255. Variable(
  256. Symbol('y'),
  257. type=IntBaseType(String('intc')),
  258. value=Integer(97)
  259. )
  260. )
  261. assert res6[0] == Declaration(
  262. Variable(
  263. Symbol('r'),
  264. type=IntBaseType(String('intc')),
  265. value=Integer(1)
  266. )
  267. )
  268. assert res6[1] == Declaration(
  269. Variable(
  270. Symbol('s'),
  271. type=IntBaseType(String('intc')),
  272. value=Integer(0)
  273. )
  274. )
  275. assert res_type1[0] == Declaration(
  276. Variable(
  277. Symbol('a'),
  278. type=SignedIntType(
  279. String('int8'),
  280. nbits=Integer(8)
  281. ),
  282. value=Integer(1)
  283. )
  284. )
  285. assert res_type1[1] == Declaration(
  286. Variable(
  287. Symbol('b'),
  288. type=SignedIntType(
  289. String('int8'),
  290. nbits=Integer(8)
  291. ),
  292. value=Integer(5)
  293. )
  294. )
  295. assert res_type2[0] == Declaration(
  296. Variable(
  297. Symbol('a'),
  298. type=SignedIntType(
  299. String('int16'),
  300. nbits=Integer(16)
  301. ),
  302. value=Integer(1)
  303. )
  304. )
  305. assert res_type2[1] == Declaration(
  306. Variable(
  307. Symbol('b'),
  308. type=SignedIntType(
  309. String('int16'),
  310. nbits=Integer(16)
  311. ),
  312. value=Integer(5)
  313. )
  314. )
  315. assert res_type2[2] == Declaration(
  316. Variable(Symbol('c'),
  317. type=SignedIntType(
  318. String('int16'),
  319. nbits=Integer(16)
  320. ),
  321. value=Integer(1)
  322. )
  323. )
  324. assert res_type2[3] == Declaration(
  325. Variable(
  326. Symbol('d'),
  327. type=SignedIntType(
  328. String('int16'),
  329. nbits=Integer(16)
  330. ),
  331. value=Integer(5)
  332. )
  333. )
  334. assert res_type2[4] == Declaration(
  335. Variable(
  336. Symbol('e'),
  337. type=SignedIntType(
  338. String('int16'),
  339. nbits=Integer(16)
  340. ),
  341. value=Integer(1)
  342. )
  343. )
  344. assert res_type2[5] == Declaration(
  345. Variable(
  346. Symbol('f'),
  347. type=SignedIntType(
  348. String('int16'),
  349. nbits=Integer(16)
  350. ),
  351. value=Integer(5)
  352. )
  353. )
  354. assert res_type2[6] == Declaration(
  355. Variable(
  356. Symbol('g'),
  357. type=SignedIntType(
  358. String('int16'),
  359. nbits=Integer(16)
  360. ),
  361. value=Integer(1)
  362. )
  363. )
  364. assert res_type2[7] == Declaration(
  365. Variable(
  366. Symbol('h'),
  367. type=SignedIntType(
  368. String('int16'),
  369. nbits=Integer(16)
  370. ),
  371. value=Integer(5)
  372. )
  373. )
  374. assert res_type3[0] == Declaration(
  375. Variable(
  376. Symbol('a'),
  377. type=IntBaseType(String('intc')),
  378. value=Integer(1)
  379. )
  380. )
  381. assert res_type3[1] == Declaration(
  382. Variable(
  383. Symbol('b'),
  384. type=IntBaseType(String('intc')),
  385. value=Integer(5)
  386. )
  387. )
  388. assert res_type3[2] == Declaration(
  389. Variable(
  390. Symbol('c'),
  391. type=IntBaseType(String('intc')),
  392. value=Integer(1)
  393. )
  394. )
  395. assert res_type3[3] == Declaration(
  396. Variable(
  397. Symbol('d'),
  398. type=IntBaseType(String('intc')),
  399. value=Integer(5)
  400. )
  401. )
  402. assert res_type4[0] == Declaration(
  403. Variable(
  404. Symbol('a'),
  405. type=SignedIntType(
  406. String('int64'),
  407. nbits=Integer(64)
  408. ),
  409. value=Integer(1)
  410. )
  411. )
  412. assert res_type4[1] == Declaration(
  413. Variable(
  414. Symbol('b'),
  415. type=SignedIntType(
  416. String('int64'),
  417. nbits=Integer(64)
  418. ),
  419. value=Integer(5)
  420. )
  421. )
  422. assert res_type4[2] == Declaration(
  423. Variable(
  424. Symbol('c'),
  425. type=SignedIntType(
  426. String('int64'),
  427. nbits=Integer(64)
  428. ),
  429. value=Integer(1)
  430. )
  431. )
  432. assert res_type4[3] == Declaration(
  433. Variable(
  434. Symbol('d'),
  435. type=SignedIntType(
  436. String('int64'),
  437. nbits=Integer(64)
  438. ),
  439. value=Integer(5)
  440. )
  441. )
  442. assert res_type5[0] == Declaration(
  443. Variable(
  444. Symbol('a'),
  445. type=UnsignedIntType(
  446. String('uint8'),
  447. nbits=Integer(8)
  448. ),
  449. value=Integer(1)
  450. )
  451. )
  452. assert res_type5[1] == Declaration(
  453. Variable(
  454. Symbol('b'),
  455. type=UnsignedIntType(
  456. String('uint8'),
  457. nbits=Integer(8)
  458. ),
  459. value=Integer(5)
  460. )
  461. )
  462. assert res_type6[0] == Declaration(
  463. Variable(
  464. Symbol('a'),
  465. type=UnsignedIntType(
  466. String('uint16'),
  467. nbits=Integer(16)
  468. ),
  469. value=Integer(1)
  470. )
  471. )
  472. assert res_type6[1] == Declaration(
  473. Variable(
  474. Symbol('b'),
  475. type=UnsignedIntType(
  476. String('uint16'),
  477. nbits=Integer(16)
  478. ),
  479. value=Integer(5)
  480. )
  481. )
  482. assert res_type6[2] == Declaration(
  483. Variable(
  484. Symbol('c'),
  485. type=UnsignedIntType(
  486. String('uint16'),
  487. nbits=Integer(16)
  488. ),
  489. value=Integer(1)
  490. )
  491. )
  492. assert res_type6[3] == Declaration(
  493. Variable(
  494. Symbol('d'),
  495. type=UnsignedIntType(
  496. String('uint16'),
  497. nbits=Integer(16)
  498. ),
  499. value=Integer(5)
  500. )
  501. )
  502. assert res_type7[0] == Declaration(
  503. Variable(
  504. Symbol('a'),
  505. type=UnsignedIntType(
  506. String('uint32'),
  507. nbits=Integer(32)
  508. ),
  509. value=Integer(1)
  510. )
  511. )
  512. assert res_type7[1] == Declaration(
  513. Variable(
  514. Symbol('b'),
  515. type=UnsignedIntType(
  516. String('uint32'),
  517. nbits=Integer(32)
  518. ),
  519. value=Integer(5)
  520. )
  521. )
  522. assert res_type8[0] == Declaration(
  523. Variable(
  524. Symbol('a'),
  525. type=UnsignedIntType(
  526. String('uint64'),
  527. nbits=Integer(64)
  528. ),
  529. value=Integer(1)
  530. )
  531. )
  532. assert res_type8[1] == Declaration(
  533. Variable(
  534. Symbol('b'),
  535. type=UnsignedIntType(
  536. String('uint64'),
  537. nbits=Integer(64)
  538. ),
  539. value=Integer(5)
  540. )
  541. )
  542. assert res_type8[2] == Declaration(
  543. Variable(
  544. Symbol('c'),
  545. type=UnsignedIntType(
  546. String('uint64'),
  547. nbits=Integer(64)
  548. ),
  549. value=Integer(1)
  550. )
  551. )
  552. assert res_type8[3] == Declaration(
  553. Variable(
  554. Symbol('d'),
  555. type=UnsignedIntType(
  556. String('uint64'),
  557. nbits=Integer(64)
  558. ),
  559. value=Integer(5)
  560. )
  561. )
  562. def test_float():
  563. c_src1 = 'float a = 1.0;'
  564. c_src2 = (
  565. 'float a = 1.25;' + '\n' +
  566. 'float b = 2.39;' + '\n'
  567. )
  568. c_src3 = 'float x = 1, y = 2;'
  569. c_src4 = 'float p = 5, e = 7.89;'
  570. c_src5 = 'float r = true, s = false;'
  571. # cin.TypeKind.FLOAT
  572. c_src_type1 = 'float x = 1, y = 2.5;'
  573. # cin.TypeKind.DOUBLE
  574. c_src_type2 = 'double x = 1, y = 2.5;'
  575. # cin.TypeKind.LONGDOUBLE
  576. c_src_type3 = 'long double x = 1, y = 2.5;'
  577. res1 = SymPyExpression(c_src1, 'c').return_expr()
  578. res2 = SymPyExpression(c_src2, 'c').return_expr()
  579. res3 = SymPyExpression(c_src3, 'c').return_expr()
  580. res4 = SymPyExpression(c_src4, 'c').return_expr()
  581. res5 = SymPyExpression(c_src5, 'c').return_expr()
  582. res_type1 = SymPyExpression(c_src_type1, 'c').return_expr()
  583. res_type2 = SymPyExpression(c_src_type2, 'c').return_expr()
  584. res_type3 = SymPyExpression(c_src_type3, 'c').return_expr()
  585. assert res1[0] == Declaration(
  586. Variable(
  587. Symbol('a'),
  588. type=FloatType(
  589. String('float32'),
  590. nbits=Integer(32),
  591. nmant=Integer(23),
  592. nexp=Integer(8)
  593. ),
  594. value=Float('1.0', precision=53)
  595. )
  596. )
  597. assert res2[0] == Declaration(
  598. Variable(
  599. Symbol('a'),
  600. type=FloatType(
  601. String('float32'),
  602. nbits=Integer(32),
  603. nmant=Integer(23),
  604. nexp=Integer(8)
  605. ),
  606. value=Float('1.25', precision=53)
  607. )
  608. )
  609. assert res2[1] == Declaration(
  610. Variable(
  611. Symbol('b'),
  612. type=FloatType(
  613. String('float32'),
  614. nbits=Integer(32),
  615. nmant=Integer(23),
  616. nexp=Integer(8)
  617. ),
  618. value=Float('2.3900000000000001', precision=53)
  619. )
  620. )
  621. assert res3[0] == Declaration(
  622. Variable(
  623. Symbol('x'),
  624. type=FloatType(
  625. String('float32'),
  626. nbits=Integer(32),
  627. nmant=Integer(23),
  628. nexp=Integer(8)
  629. ),
  630. value=Float('1.0', precision=53)
  631. )
  632. )
  633. assert res3[1] == Declaration(
  634. Variable(
  635. Symbol('y'),
  636. type=FloatType(
  637. String('float32'),
  638. nbits=Integer(32),
  639. nmant=Integer(23),
  640. nexp=Integer(8)
  641. ),
  642. value=Float('2.0', precision=53)
  643. )
  644. )
  645. assert res4[0] == Declaration(
  646. Variable(
  647. Symbol('p'),
  648. type=FloatType(
  649. String('float32'),
  650. nbits=Integer(32),
  651. nmant=Integer(23),
  652. nexp=Integer(8)
  653. ),
  654. value=Float('5.0', precision=53)
  655. )
  656. )
  657. assert res4[1] == Declaration(
  658. Variable(
  659. Symbol('e'),
  660. type=FloatType(
  661. String('float32'),
  662. nbits=Integer(32),
  663. nmant=Integer(23),
  664. nexp=Integer(8)
  665. ),
  666. value=Float('7.89', precision=53)
  667. )
  668. )
  669. assert res5[0] == Declaration(
  670. Variable(
  671. Symbol('r'),
  672. type=FloatType(
  673. String('float32'),
  674. nbits=Integer(32),
  675. nmant=Integer(23),
  676. nexp=Integer(8)
  677. ),
  678. value=Float('1.0', precision=53)
  679. )
  680. )
  681. assert res5[1] == Declaration(
  682. Variable(
  683. Symbol('s'),
  684. type=FloatType(
  685. String('float32'),
  686. nbits=Integer(32),
  687. nmant=Integer(23),
  688. nexp=Integer(8)
  689. ),
  690. value=Float('0.0', precision=53)
  691. )
  692. )
  693. assert res_type1[0] == Declaration(
  694. Variable(
  695. Symbol('x'),
  696. type=FloatType(
  697. String('float32'),
  698. nbits=Integer(32),
  699. nmant=Integer(23),
  700. nexp=Integer(8)
  701. ),
  702. value=Float('1.0', precision=53)
  703. )
  704. )
  705. assert res_type1[1] == Declaration(
  706. Variable(
  707. Symbol('y'),
  708. type=FloatType(
  709. String('float32'),
  710. nbits=Integer(32),
  711. nmant=Integer(23),
  712. nexp=Integer(8)
  713. ),
  714. value=Float('2.5', precision=53)
  715. )
  716. )
  717. assert res_type2[0] == Declaration(
  718. Variable(
  719. Symbol('x'),
  720. type=FloatType(
  721. String('float64'),
  722. nbits=Integer(64),
  723. nmant=Integer(52),
  724. nexp=Integer(11)
  725. ),
  726. value=Float('1.0', precision=53)
  727. )
  728. )
  729. assert res_type2[1] == Declaration(
  730. Variable(
  731. Symbol('y'),
  732. type=FloatType(
  733. String('float64'),
  734. nbits=Integer(64),
  735. nmant=Integer(52),
  736. nexp=Integer(11)
  737. ),
  738. value=Float('2.5', precision=53)
  739. )
  740. )
  741. assert res_type3[0] == Declaration(
  742. Variable(
  743. Symbol('x'),
  744. type=FloatType(
  745. String('float80'),
  746. nbits=Integer(80),
  747. nmant=Integer(63),
  748. nexp=Integer(15)
  749. ),
  750. value=Float('1.0', precision=53)
  751. )
  752. )
  753. assert res_type3[1] == Declaration(
  754. Variable(
  755. Symbol('y'),
  756. type=FloatType(
  757. String('float80'),
  758. nbits=Integer(80),
  759. nmant=Integer(63),
  760. nexp=Integer(15)
  761. ),
  762. value=Float('2.5', precision=53)
  763. )
  764. )
  765. def test_bool():
  766. c_src1 = (
  767. 'bool a = true, b = false;'
  768. )
  769. c_src2 = (
  770. 'bool a = 1, b = 0;'
  771. )
  772. c_src3 = (
  773. 'bool a = 10, b = 20;'
  774. )
  775. c_src4 = (
  776. 'bool a = 19.1, b = 9.0, c = 0.0;'
  777. )
  778. res1 = SymPyExpression(c_src1, 'c').return_expr()
  779. res2 = SymPyExpression(c_src2, 'c').return_expr()
  780. res3 = SymPyExpression(c_src3, 'c').return_expr()
  781. res4 = SymPyExpression(c_src4, 'c').return_expr()
  782. assert res1[0] == Declaration(
  783. Variable(Symbol('a'),
  784. type=Type(String('bool')),
  785. value=true
  786. )
  787. )
  788. assert res1[1] == Declaration(
  789. Variable(Symbol('b'),
  790. type=Type(String('bool')),
  791. value=false
  792. )
  793. )
  794. assert res2[0] == Declaration(
  795. Variable(Symbol('a'),
  796. type=Type(String('bool')),
  797. value=true)
  798. )
  799. assert res2[1] == Declaration(
  800. Variable(Symbol('b'),
  801. type=Type(String('bool')),
  802. value=false
  803. )
  804. )
  805. assert res3[0] == Declaration(
  806. Variable(Symbol('a'),
  807. type=Type(String('bool')),
  808. value=true
  809. )
  810. )
  811. assert res3[1] == Declaration(
  812. Variable(Symbol('b'),
  813. type=Type(String('bool')),
  814. value=true
  815. )
  816. )
  817. assert res4[0] == Declaration(
  818. Variable(Symbol('a'),
  819. type=Type(String('bool')),
  820. value=true)
  821. )
  822. assert res4[1] == Declaration(
  823. Variable(Symbol('b'),
  824. type=Type(String('bool')),
  825. value=true
  826. )
  827. )
  828. assert res4[2] == Declaration(
  829. Variable(Symbol('c'),
  830. type=Type(String('bool')),
  831. value=false
  832. )
  833. )
  834. @XFAIL # this is expected to fail because of a bug in the C parser.
  835. def test_function():
  836. c_src1 = (
  837. 'void fun1()' + '\n' +
  838. '{' + '\n' +
  839. 'int a;' + '\n' +
  840. '}'
  841. )
  842. c_src2 = (
  843. 'int fun2()' + '\n' +
  844. '{'+ '\n' +
  845. 'int a;' + '\n' +
  846. 'return a;' + '\n' +
  847. '}'
  848. )
  849. c_src3 = (
  850. 'float fun3()' + '\n' +
  851. '{' + '\n' +
  852. 'float b;' + '\n' +
  853. 'return b;' + '\n' +
  854. '}'
  855. )
  856. c_src4 = (
  857. 'float fun4()' + '\n' +
  858. '{}'
  859. )
  860. res1 = SymPyExpression(c_src1, 'c').return_expr()
  861. res2 = SymPyExpression(c_src2, 'c').return_expr()
  862. res3 = SymPyExpression(c_src3, 'c').return_expr()
  863. res4 = SymPyExpression(c_src4, 'c').return_expr()
  864. assert res1[0] == FunctionDefinition(
  865. NoneToken(),
  866. name=String('fun1'),
  867. parameters=(),
  868. body=CodeBlock(
  869. Declaration(
  870. Variable(
  871. Symbol('a'),
  872. type=IntBaseType(String('intc'))
  873. )
  874. )
  875. )
  876. )
  877. assert res2[0] == FunctionDefinition(
  878. IntBaseType(String('intc')),
  879. name=String('fun2'),
  880. parameters=(),
  881. body=CodeBlock(
  882. Declaration(
  883. Variable(
  884. Symbol('a'),
  885. type=IntBaseType(String('intc'))
  886. )
  887. ),
  888. Return('a')
  889. )
  890. )
  891. assert res3[0] == FunctionDefinition(
  892. FloatType(
  893. String('float32'),
  894. nbits=Integer(32),
  895. nmant=Integer(23),
  896. nexp=Integer(8)
  897. ),
  898. name=String('fun3'),
  899. parameters=(),
  900. body=CodeBlock(
  901. Declaration(
  902. Variable(
  903. Symbol('b'),
  904. type=FloatType(
  905. String('float32'),
  906. nbits=Integer(32),
  907. nmant=Integer(23),
  908. nexp=Integer(8)
  909. )
  910. )
  911. ),
  912. Return('b')
  913. )
  914. )
  915. assert res4[0] == FunctionPrototype(
  916. FloatType(
  917. String('float32'),
  918. nbits=Integer(32),
  919. nmant=Integer(23),
  920. nexp=Integer(8)
  921. ),
  922. name=String('fun4'),
  923. parameters=()
  924. )
  925. @XFAIL # this is expected to fail because of a bug in the C parser.
  926. def test_parameters():
  927. c_src1 = (
  928. 'void fun1( int a)' + '\n' +
  929. '{' + '\n' +
  930. 'int i;' + '\n' +
  931. '}'
  932. )
  933. c_src2 = (
  934. 'int fun2(float x, float y)' + '\n' +
  935. '{'+ '\n' +
  936. 'int a;' + '\n' +
  937. 'return a;' + '\n' +
  938. '}'
  939. )
  940. c_src3 = (
  941. 'float fun3(int p, float q, int r)' + '\n' +
  942. '{' + '\n' +
  943. 'float b;' + '\n' +
  944. 'return b;' + '\n' +
  945. '}'
  946. )
  947. res1 = SymPyExpression(c_src1, 'c').return_expr()
  948. res2 = SymPyExpression(c_src2, 'c').return_expr()
  949. res3 = SymPyExpression(c_src3, 'c').return_expr()
  950. assert res1[0] == FunctionDefinition(
  951. NoneToken(),
  952. name=String('fun1'),
  953. parameters=(
  954. Variable(
  955. Symbol('a'),
  956. type=IntBaseType(String('intc'))
  957. ),
  958. ),
  959. body=CodeBlock(
  960. Declaration(
  961. Variable(
  962. Symbol('i'),
  963. type=IntBaseType(String('intc'))
  964. )
  965. )
  966. )
  967. )
  968. assert res2[0] == FunctionDefinition(
  969. IntBaseType(String('intc')),
  970. name=String('fun2'),
  971. parameters=(
  972. Variable(
  973. Symbol('x'),
  974. type=FloatType(
  975. String('float32'),
  976. nbits=Integer(32),
  977. nmant=Integer(23),
  978. nexp=Integer(8)
  979. )
  980. ),
  981. Variable(
  982. Symbol('y'),
  983. type=FloatType(
  984. String('float32'),
  985. nbits=Integer(32),
  986. nmant=Integer(23),
  987. nexp=Integer(8)
  988. )
  989. )
  990. ),
  991. body=CodeBlock(
  992. Declaration(
  993. Variable(
  994. Symbol('a'),
  995. type=IntBaseType(String('intc'))
  996. )
  997. ),
  998. Return('a')
  999. )
  1000. )
  1001. assert res3[0] == FunctionDefinition(
  1002. FloatType(
  1003. String('float32'),
  1004. nbits=Integer(32),
  1005. nmant=Integer(23),
  1006. nexp=Integer(8)
  1007. ),
  1008. name=String('fun3'),
  1009. parameters=(
  1010. Variable(
  1011. Symbol('p'),
  1012. type=IntBaseType(String('intc'))
  1013. ),
  1014. Variable(
  1015. Symbol('q'),
  1016. type=FloatType(
  1017. String('float32'),
  1018. nbits=Integer(32),
  1019. nmant=Integer(23),
  1020. nexp=Integer(8)
  1021. )
  1022. ),
  1023. Variable(
  1024. Symbol('r'),
  1025. type=IntBaseType(String('intc'))
  1026. )
  1027. ),
  1028. body=CodeBlock(
  1029. Declaration(
  1030. Variable(
  1031. Symbol('b'),
  1032. type=FloatType(
  1033. String('float32'),
  1034. nbits=Integer(32),
  1035. nmant=Integer(23),
  1036. nexp=Integer(8)
  1037. )
  1038. )
  1039. ),
  1040. Return('b')
  1041. )
  1042. )
  1043. @XFAIL # this is expected to fail because of a bug in the C parser.
  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. Mod(
  2334. Symbol('a'),
  2335. Symbol('mod')
  2336. ),
  2337. Mul(
  2338. Integer(-1),
  2339. Mod(
  2340. Symbol('b'),
  2341. Symbol('mod')
  2342. )
  2343. )
  2344. ),
  2345. Mod(
  2346. Add(
  2347. Symbol('a'),
  2348. Symbol('b')
  2349. ),
  2350. Symbol('mod')
  2351. )
  2352. ),
  2353. Symbol('mod')
  2354. )
  2355. )
  2356. )
  2357. )
  2358. assert res20[0] == FunctionDefinition(
  2359. NoneToken(),
  2360. name=String('func'),
  2361. parameters=(),
  2362. body=CodeBlock(
  2363. Declaration(
  2364. Variable(Symbol('a'),
  2365. type=Type(String('bool'))
  2366. )
  2367. ),
  2368. Declaration(
  2369. Variable(Symbol('b'),
  2370. type=Type(String('bool'))
  2371. )
  2372. ),
  2373. Assignment(
  2374. Variable(Symbol('a')),
  2375. false
  2376. ),
  2377. Assignment(
  2378. Variable(Symbol('b')),
  2379. true
  2380. )
  2381. )
  2382. )
  2383. assert res21[0] == FunctionDefinition(
  2384. NoneToken(),
  2385. name=String('func'),
  2386. parameters=(),
  2387. body=CodeBlock(
  2388. Declaration(
  2389. Variable(Symbol('a'),
  2390. type=Type(String('bool'))
  2391. )
  2392. ),
  2393. Declaration(
  2394. Variable(Symbol('b'),
  2395. type=Type(String('bool'))
  2396. )
  2397. ),
  2398. Declaration(
  2399. Variable(Symbol('c'),
  2400. type=Type(String('bool'))
  2401. )
  2402. ),
  2403. Declaration(
  2404. Variable(Symbol('d'),
  2405. type=Type(String('bool'))
  2406. )
  2407. ),
  2408. Assignment(
  2409. Variable(Symbol('a')),
  2410. false
  2411. ),
  2412. Assignment(
  2413. Variable(Symbol('b')),
  2414. true
  2415. ),
  2416. Assignment(
  2417. Variable(Symbol('c')),
  2418. false
  2419. ),
  2420. Assignment(
  2421. Variable(Symbol('d')),
  2422. false
  2423. )
  2424. )
  2425. )
  2426. assert res22[0] == FunctionDefinition(
  2427. NoneToken(),
  2428. name=String('func'),
  2429. parameters=(),
  2430. body=CodeBlock(
  2431. Declaration(
  2432. Variable(Symbol('a'),
  2433. type=IntBaseType(String('intc')),
  2434. value=Integer(1)
  2435. )
  2436. ),
  2437. Declaration(
  2438. Variable(Symbol('b'),
  2439. type=IntBaseType(String('intc')),
  2440. value=Integer(2)
  2441. )
  2442. ),
  2443. Declaration(
  2444. Variable(Symbol('c1'),
  2445. type=Type(String('bool'))
  2446. )
  2447. ),
  2448. Declaration(
  2449. Variable(Symbol('c2'),
  2450. type=Type(String('bool'))
  2451. )
  2452. ),
  2453. Declaration(
  2454. Variable(Symbol('c3'),
  2455. type=Type(String('bool'))
  2456. )
  2457. ),
  2458. Declaration(
  2459. Variable(Symbol('c4'),
  2460. type=Type(String('bool'))
  2461. )
  2462. ),
  2463. Declaration(
  2464. Variable(Symbol('c5'),
  2465. type=Type(String('bool'))
  2466. )
  2467. ),
  2468. Declaration(
  2469. Variable(Symbol('c6'),
  2470. type=Type(String('bool'))
  2471. )
  2472. ),
  2473. Declaration(
  2474. Variable(Symbol('c7'),
  2475. type=Type(String('bool'))
  2476. )
  2477. ),
  2478. Declaration(
  2479. Variable(Symbol('c8'),
  2480. type=Type(String('bool'))
  2481. )
  2482. ),
  2483. Assignment(
  2484. Variable(Symbol('c1')),
  2485. Equality(
  2486. Symbol('a'),
  2487. Integer(1)
  2488. )
  2489. ),
  2490. Assignment(
  2491. Variable(Symbol('c2')),
  2492. Equality(
  2493. Symbol('b'),
  2494. Integer(2)
  2495. )
  2496. ),
  2497. Assignment(
  2498. Variable(Symbol('c3')),
  2499. Unequality(
  2500. Integer(1),
  2501. Symbol('a')
  2502. )
  2503. ),
  2504. Assignment(
  2505. Variable(Symbol('c4')),
  2506. Unequality(
  2507. Integer(1),
  2508. Symbol('b')
  2509. )
  2510. ),
  2511. Assignment(
  2512. Variable(Symbol('c5')),
  2513. StrictLessThan(
  2514. Symbol('a'),
  2515. Integer(0)
  2516. )
  2517. ),
  2518. Assignment(
  2519. Variable(Symbol('c6')),
  2520. LessThan(
  2521. Symbol('b'),
  2522. Integer(10)
  2523. )
  2524. ),
  2525. Assignment(
  2526. Variable(Symbol('c7')),
  2527. StrictGreaterThan(
  2528. Symbol('a'),
  2529. Integer(0)
  2530. )
  2531. ),
  2532. Assignment(
  2533. Variable(Symbol('c8')),
  2534. GreaterThan(
  2535. Symbol('b'),
  2536. Integer(11)
  2537. )
  2538. )
  2539. )
  2540. )
  2541. assert res23[0] == FunctionDefinition(
  2542. NoneToken(),
  2543. name=String('func'),
  2544. parameters=(),
  2545. body=CodeBlock(
  2546. Declaration(
  2547. Variable(Symbol('a'),
  2548. type=IntBaseType(String('intc')),
  2549. value=Integer(3)
  2550. )
  2551. ),
  2552. Declaration(
  2553. Variable(Symbol('b'),
  2554. type=IntBaseType(String('intc')),
  2555. value=Integer(4)
  2556. )
  2557. ),
  2558. Declaration(
  2559. Variable(Symbol('c1'),
  2560. type=Type(String('bool'))
  2561. )
  2562. ),
  2563. Declaration(
  2564. Variable(Symbol('c2'),
  2565. type=Type(String('bool'))
  2566. )
  2567. ),
  2568. Declaration(
  2569. Variable(Symbol('c3'),
  2570. type=Type(String('bool'))
  2571. )
  2572. ),
  2573. Declaration(
  2574. Variable(Symbol('c4'),
  2575. type=Type(String('bool'))
  2576. )
  2577. ),
  2578. Declaration(
  2579. Variable(Symbol('c5'),
  2580. type=Type(String('bool'))
  2581. )
  2582. ),
  2583. Declaration(
  2584. Variable(Symbol('c6'),
  2585. type=Type(String('bool'))
  2586. )
  2587. ),
  2588. Assignment(
  2589. Variable(Symbol('c1')),
  2590. Equality(
  2591. Symbol('a'),
  2592. Symbol('b')
  2593. )
  2594. ),
  2595. Assignment(
  2596. Variable(Symbol('c2')),
  2597. Unequality(
  2598. Symbol('a'),
  2599. Symbol('b')
  2600. )
  2601. ),
  2602. Assignment(
  2603. Variable(Symbol('c3')),
  2604. StrictLessThan(
  2605. Symbol('a'),
  2606. Symbol('b')
  2607. )
  2608. ),
  2609. Assignment(
  2610. Variable(Symbol('c4')),
  2611. LessThan(
  2612. Symbol('a'),
  2613. Symbol('b')
  2614. )
  2615. ),
  2616. Assignment(
  2617. Variable(Symbol('c5')),
  2618. StrictGreaterThan(
  2619. Symbol('a'),
  2620. Symbol('b')
  2621. )
  2622. ),
  2623. Assignment(
  2624. Variable(Symbol('c6')),
  2625. GreaterThan(
  2626. Symbol('a'),
  2627. Symbol('b')
  2628. )
  2629. )
  2630. )
  2631. )
  2632. assert res24[0] == FunctionDefinition(
  2633. NoneToken(),
  2634. name=String('func'),
  2635. parameters=(),
  2636. body=CodeBlock(
  2637. Declaration(
  2638. Variable(Symbol('a'),
  2639. type=FloatType(
  2640. String('float32'),
  2641. nbits=Integer(32),
  2642. nmant=Integer(23),
  2643. nexp=Integer(8)
  2644. )
  2645. )
  2646. ),
  2647. Declaration(
  2648. Variable(Symbol('c1'),
  2649. type=Type(String('bool'))
  2650. )
  2651. ),
  2652. Declaration(
  2653. Variable(Symbol('c2'),
  2654. type=Type(String('bool'))
  2655. )
  2656. ),
  2657. Declaration(
  2658. Variable(Symbol('c3'),
  2659. type=Type(String('bool'))
  2660. )
  2661. ),
  2662. Declaration(
  2663. Variable(Symbol('c4'),
  2664. type=Type(String('bool'))
  2665. )
  2666. ),
  2667. Assignment(
  2668. Variable(Symbol('c1')),
  2669. Equality(
  2670. Symbol('a'),
  2671. Float('1.25', precision=53)
  2672. )
  2673. ),
  2674. Assignment(
  2675. Variable(Symbol('c3')),
  2676. Unequality(
  2677. Float('1.2', precision=53),
  2678. Symbol('a')
  2679. )
  2680. )
  2681. )
  2682. )
  2683. assert res25[0] == FunctionDefinition(
  2684. NoneToken(),
  2685. name=String('func'),
  2686. parameters=(),
  2687. body=CodeBlock(
  2688. Declaration(
  2689. Variable(Symbol('a'),
  2690. type=FloatType(
  2691. String('float32'),
  2692. nbits=Integer(32),
  2693. nmant=Integer(23),
  2694. nexp=Integer(8)
  2695. ),
  2696. value=Float('1.25', precision=53)
  2697. )
  2698. ),
  2699. Declaration(
  2700. Variable(Symbol('b'),
  2701. type=FloatType(
  2702. String('float32'),
  2703. nbits=Integer(32),
  2704. nmant=Integer(23),
  2705. nexp=Integer(8)
  2706. ),
  2707. value=Float('2.5', precision=53)
  2708. )
  2709. ),
  2710. Declaration(
  2711. Variable(Symbol('c1'),
  2712. type=Type(String('bool'))
  2713. )
  2714. ),
  2715. Declaration(
  2716. Variable(Symbol('c2'),
  2717. type=Type(String('bool')
  2718. )
  2719. )
  2720. ),
  2721. Declaration(
  2722. Variable(Symbol('c3'),
  2723. type=Type(String('bool'))
  2724. )
  2725. ),
  2726. Declaration(
  2727. Variable(Symbol('c4'),
  2728. type=Type(String('bool'))
  2729. )
  2730. ),
  2731. Declaration(
  2732. Variable(Symbol('c5'),
  2733. type=Type(String('bool'))
  2734. )
  2735. ),
  2736. Declaration(
  2737. Variable(Symbol('c6'),
  2738. type=Type(String('bool'))
  2739. )
  2740. ),
  2741. Assignment(
  2742. Variable(Symbol('c1')),
  2743. Equality(
  2744. Symbol('a'),
  2745. Symbol('b')
  2746. )
  2747. ),
  2748. Assignment(
  2749. Variable(Symbol('c2')),
  2750. Unequality(
  2751. Symbol('a'),
  2752. Symbol('b')
  2753. )
  2754. ),
  2755. Assignment(
  2756. Variable(Symbol('c3')),
  2757. StrictLessThan(
  2758. Symbol('a'),
  2759. Symbol('b')
  2760. )
  2761. ),
  2762. Assignment(
  2763. Variable(Symbol('c4')),
  2764. LessThan(
  2765. Symbol('a'),
  2766. Symbol('b')
  2767. )
  2768. ),
  2769. Assignment(
  2770. Variable(Symbol('c5')),
  2771. StrictGreaterThan(
  2772. Symbol('a'),
  2773. Symbol('b')
  2774. )
  2775. ),
  2776. Assignment(
  2777. Variable(Symbol('c6')),
  2778. GreaterThan(
  2779. Symbol('a'),
  2780. Symbol('b')
  2781. )
  2782. )
  2783. )
  2784. )
  2785. assert res26[0] == FunctionDefinition(
  2786. NoneToken(),
  2787. name=String('func'),
  2788. parameters=(), body=CodeBlock(
  2789. Declaration(
  2790. Variable(Symbol('c1'),
  2791. type=Type(String('bool'))
  2792. )
  2793. ),
  2794. Declaration(
  2795. Variable(Symbol('c2'),
  2796. type=Type(String('bool'))
  2797. )
  2798. ),
  2799. Declaration(
  2800. Variable(Symbol('c3'),
  2801. type=Type(String('bool'))
  2802. )
  2803. ),
  2804. Declaration(
  2805. Variable(Symbol('c4'),
  2806. type=Type(String('bool'))
  2807. )
  2808. ),
  2809. Declaration(
  2810. Variable(Symbol('c5'),
  2811. type=Type(String('bool'))
  2812. )
  2813. ),
  2814. Declaration(
  2815. Variable(Symbol('c6'),
  2816. type=Type(String('bool'))
  2817. )
  2818. ),
  2819. Assignment(
  2820. Variable(Symbol('c1')),
  2821. true
  2822. ),
  2823. Assignment(
  2824. Variable(Symbol('c2')),
  2825. false
  2826. ),
  2827. Assignment(
  2828. Variable(Symbol('c3')),
  2829. true
  2830. ),
  2831. Assignment(
  2832. Variable(Symbol('c4')),
  2833. false
  2834. ),
  2835. Assignment(
  2836. Variable(Symbol('c5')),
  2837. true
  2838. ),
  2839. Assignment(
  2840. Variable(Symbol('c6')),
  2841. false
  2842. )
  2843. )
  2844. )
  2845. assert res27[0] == FunctionDefinition(
  2846. NoneToken(),
  2847. name=String('func'),
  2848. parameters=(),
  2849. body=CodeBlock(
  2850. Declaration(
  2851. Variable(Symbol('c1'),
  2852. type=Type(String('bool'))
  2853. )
  2854. ),
  2855. Declaration(
  2856. Variable(Symbol('c2'),
  2857. type=Type(String('bool'))
  2858. )
  2859. ),
  2860. Declaration(
  2861. Variable(Symbol('c3'),
  2862. type=Type(String('bool'))
  2863. )
  2864. ),
  2865. Declaration(
  2866. Variable(Symbol('c4'),
  2867. type=Type(String('bool'))
  2868. )
  2869. ),
  2870. Declaration(
  2871. Variable(Symbol('c5'),
  2872. type=Type(String('bool'))
  2873. )
  2874. ),
  2875. Declaration(
  2876. Variable(Symbol('c6'),
  2877. type=Type(String('bool'))
  2878. )
  2879. ),
  2880. Assignment(
  2881. Variable(Symbol('c1')),
  2882. true
  2883. ),
  2884. Assignment(
  2885. Variable(Symbol('c2')),
  2886. false
  2887. ),
  2888. Assignment(
  2889. Variable(Symbol('c3')),
  2890. false
  2891. ),
  2892. Assignment(
  2893. Variable(Symbol('c4')),
  2894. true
  2895. ),
  2896. Assignment(
  2897. Variable(Symbol('c5')),
  2898. true
  2899. ),
  2900. Assignment(
  2901. Variable(Symbol('c6')),
  2902. false)
  2903. )
  2904. )
  2905. assert res28[0] == FunctionDefinition(
  2906. NoneToken(),
  2907. name=String('func'),
  2908. parameters=(),
  2909. body=CodeBlock(
  2910. Declaration(
  2911. Variable(Symbol('a'),
  2912. type=Type(String('bool'))
  2913. )
  2914. ),
  2915. Declaration(
  2916. Variable(Symbol('c1'),
  2917. type=Type(String('bool'))
  2918. )
  2919. ),
  2920. Declaration(
  2921. Variable(Symbol('c2'),
  2922. type=Type(String('bool'))
  2923. )
  2924. ),
  2925. Declaration(
  2926. Variable(Symbol('c3'),
  2927. type=Type(String('bool'))
  2928. )
  2929. ),
  2930. Declaration(
  2931. Variable(Symbol('c4'),
  2932. type=Type(String('bool'))
  2933. )
  2934. ),
  2935. Assignment(
  2936. Variable(Symbol('c1')),
  2937. Symbol('a')
  2938. ),
  2939. Assignment(
  2940. Variable(Symbol('c2')),
  2941. false
  2942. ),
  2943. Assignment(
  2944. Variable(Symbol('c3')),
  2945. true
  2946. ),
  2947. Assignment(
  2948. Variable(Symbol('c4')),
  2949. Symbol('a')
  2950. )
  2951. )
  2952. )
  2953. assert res29[0] == FunctionDefinition(
  2954. NoneToken(),
  2955. name=String('func'),
  2956. parameters=(),
  2957. body=CodeBlock(
  2958. Declaration(
  2959. Variable(Symbol('a'),
  2960. type=IntBaseType(String('intc'))
  2961. )
  2962. ),
  2963. Declaration(
  2964. Variable(Symbol('c1'),
  2965. type=Type(String('bool'))
  2966. )
  2967. ),
  2968. Declaration(
  2969. Variable(Symbol('c2'),
  2970. type=Type(String('bool'))
  2971. )
  2972. ),
  2973. Declaration(
  2974. Variable(Symbol('c3'),
  2975. type=Type(String('bool'))
  2976. )
  2977. ),
  2978. Declaration(
  2979. Variable(Symbol('c4'),
  2980. type=Type(String('bool'))
  2981. )
  2982. ),
  2983. Assignment(
  2984. Variable(Symbol('c1')),
  2985. Symbol('a')
  2986. ),
  2987. Assignment(
  2988. Variable(Symbol('c2')),
  2989. false
  2990. ),
  2991. Assignment(
  2992. Variable(Symbol('c3')),
  2993. true
  2994. ),
  2995. Assignment(
  2996. Variable(Symbol('c4')),
  2997. Symbol('a')
  2998. )
  2999. )
  3000. )
  3001. assert res30[0] == FunctionDefinition(
  3002. NoneToken(),
  3003. name=String('func'),
  3004. parameters=(),
  3005. body=CodeBlock(
  3006. Declaration(
  3007. Variable(Symbol('a'),
  3008. type=IntBaseType(String('intc'))
  3009. )
  3010. ),
  3011. Declaration(
  3012. Variable(Symbol('b'),
  3013. type=IntBaseType(String('intc'))
  3014. )
  3015. ),
  3016. Declaration(
  3017. Variable(Symbol('c'),
  3018. type=Type(String('bool'))
  3019. )
  3020. ),
  3021. Declaration(
  3022. Variable(Symbol('d'),
  3023. type=Type(String('bool'))
  3024. )
  3025. ),
  3026. Declaration(
  3027. Variable(Symbol('c1'),
  3028. type=Type(String('bool'))
  3029. )
  3030. ),
  3031. Declaration(
  3032. Variable(Symbol('c2'),
  3033. type=Type(String('bool'))
  3034. )
  3035. ),
  3036. Declaration(
  3037. Variable(Symbol('c3'),
  3038. type=Type(String('bool'))
  3039. )
  3040. ),
  3041. Declaration(
  3042. Variable(Symbol('c4'),
  3043. type=Type(String('bool'))
  3044. )
  3045. ),
  3046. Declaration(
  3047. Variable(Symbol('c5'),
  3048. type=Type(String('bool'))
  3049. )
  3050. ),
  3051. Declaration(
  3052. Variable(Symbol('c6'),
  3053. type=Type(String('bool'))
  3054. )
  3055. ),
  3056. Assignment(
  3057. Variable(Symbol('c1')),
  3058. And(
  3059. Symbol('a'),
  3060. Symbol('b')
  3061. )
  3062. ),
  3063. Assignment(
  3064. Variable(Symbol('c2')),
  3065. And(
  3066. Symbol('a'),
  3067. Symbol('c')
  3068. )
  3069. ),
  3070. Assignment(
  3071. Variable(Symbol('c3')),
  3072. And(
  3073. Symbol('c'),
  3074. Symbol('d')
  3075. )
  3076. ),
  3077. Assignment(
  3078. Variable(Symbol('c4')),
  3079. Or(
  3080. Symbol('a'),
  3081. Symbol('b')
  3082. )
  3083. ),
  3084. Assignment(
  3085. Variable(Symbol('c5')),
  3086. Or(
  3087. Symbol('a'),
  3088. Symbol('c')
  3089. )
  3090. ),
  3091. Assignment(
  3092. Variable(Symbol('c6')),
  3093. Or(
  3094. Symbol('c'),
  3095. Symbol('d')
  3096. )
  3097. )
  3098. )
  3099. )
  3100. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise1, 'c'))
  3101. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise2, 'c'))
  3102. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise3, 'c'))
  3103. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise4, 'c'))
  3104. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise5, 'c'))
  3105. @XFAIL
  3106. def test_var_decl():
  3107. c_src1 = (
  3108. 'int b = 100;' + '\n' +
  3109. 'int a = b;' + '\n'
  3110. )
  3111. c_src2 = (
  3112. 'int a = 1;' + '\n' +
  3113. 'int b = a + 1;' + '\n'
  3114. )
  3115. c_src3 = (
  3116. 'float a = 10.0 + 2.5;' + '\n' +
  3117. 'float b = a * 20.0;' + '\n'
  3118. )
  3119. c_src4 = (
  3120. 'int a = 1 + 100 - 3 * 6;' + '\n'
  3121. )
  3122. c_src5 = (
  3123. 'int a = (((1 + 100) * 12) - 3) * (6 - 10);' + '\n'
  3124. )
  3125. c_src6 = (
  3126. 'int b = 2;' + '\n' +
  3127. 'int c = 3;' + '\n' +
  3128. 'int a = b + c * 4;' + '\n'
  3129. )
  3130. c_src7 = (
  3131. 'int b = 1;' + '\n' +
  3132. 'int c = b + 2;' + '\n' +
  3133. 'int a = 10 * b * b * c;' + '\n'
  3134. )
  3135. c_src8 = (
  3136. 'void func()'+
  3137. '{' + '\n' +
  3138. 'int a = 1;' + '\n' +
  3139. 'int b = 2;' + '\n' +
  3140. 'int temp = a;' + '\n' +
  3141. 'a = b;' + '\n' +
  3142. 'b = temp;' + '\n' +
  3143. '}'
  3144. )
  3145. c_src9 = (
  3146. 'int a = 1;' + '\n' +
  3147. 'int b = 2;' + '\n' +
  3148. 'int c = a;' + '\n' +
  3149. 'int d = a + b + c;' + '\n' +
  3150. 'int e = a*a*a + 3*a*a*b + 3*a*b*b + b*b*b;' + '\n'
  3151. 'int f = (a + b + c) * (a + b - c);' + '\n' +
  3152. 'int g = (a + b + c + d)*(a + b + c + d)*(a * (b - c));'
  3153. + '\n'
  3154. )
  3155. c_src10 = (
  3156. 'float a = 10.0;' + '\n' +
  3157. 'float b = 2.5;' + '\n' +
  3158. 'float c = a*a + 2*a*b + b*b;' + '\n'
  3159. )
  3160. c_src11 = (
  3161. 'float a = 10.0 / 2.5;' + '\n'
  3162. )
  3163. c_src12 = (
  3164. 'int a = 100 / 4;' + '\n'
  3165. )
  3166. c_src13 = (
  3167. 'int a = 20 - 100 / 4 * 5 + 10;' + '\n'
  3168. )
  3169. c_src14 = (
  3170. 'int a = (20 - 100) / 4 * (5 + 10);' + '\n'
  3171. )
  3172. c_src15 = (
  3173. 'int a = 4;' + '\n' +
  3174. 'int b = 2;' + '\n' +
  3175. 'float c = b/a;' + '\n'
  3176. )
  3177. c_src16 = (
  3178. 'int a = 2;' + '\n' +
  3179. 'int d = 5;' + '\n' +
  3180. 'int n = 10;' + '\n' +
  3181. 'int s = (a/2)*(2*a + (n-1)*d);' + '\n'
  3182. )
  3183. c_src17 = (
  3184. 'int a = 1 % 2;' + '\n'
  3185. )
  3186. c_src18 = (
  3187. 'int a = 2;' + '\n' +
  3188. 'int b = a % 3;' + '\n'
  3189. )
  3190. c_src19 = (
  3191. 'int a = 100;' + '\n' +
  3192. 'int b = 3;' + '\n' +
  3193. 'int c = a % b;' + '\n'
  3194. )
  3195. c_src20 = (
  3196. 'int a = 100;' + '\n' +
  3197. 'int b = 3;' + '\n' +
  3198. 'int mod = 1000000007;' + '\n' +
  3199. 'int c = (a + b * (100/a)) % mod;' + '\n'
  3200. )
  3201. c_src21 = (
  3202. 'int a = 100;' + '\n' +
  3203. 'int b = 3;' + '\n' +
  3204. 'int mod = 1000000007;' + '\n' +
  3205. 'int c = ((a % mod + b % mod) % mod *' \
  3206. '(a % mod - b % mod) % mod) % mod;' + '\n'
  3207. )
  3208. c_src22 = (
  3209. 'bool a = 1 == 2, b = 1 != 2;'
  3210. )
  3211. c_src23 = (
  3212. 'bool a = 1 < 2, b = 1 <= 2, c = 1 > 2, d = 1 >= 2;'
  3213. )
  3214. c_src24 = (
  3215. 'int a = 1, b = 2;' + '\n' +
  3216. 'bool c1 = a == 1;' + '\n' +
  3217. 'bool c2 = b == 2;' + '\n' +
  3218. 'bool c3 = 1 != a;' + '\n' +
  3219. 'bool c4 = 1 != b;' + '\n' +
  3220. 'bool c5 = a < 0;' + '\n' +
  3221. 'bool c6 = b <= 10;' + '\n' +
  3222. 'bool c7 = a > 0;' + '\n' +
  3223. 'bool c8 = b >= 11;'
  3224. )
  3225. c_src25 = (
  3226. 'int a = 3, b = 4;' + '\n' +
  3227. 'bool c1 = a == b;' + '\n' +
  3228. 'bool c2 = a != b;' + '\n' +
  3229. 'bool c3 = a < b;' + '\n' +
  3230. 'bool c4 = a <= b;' + '\n' +
  3231. 'bool c5 = a > b;' + '\n' +
  3232. 'bool c6 = a >= b;'
  3233. )
  3234. c_src26 = (
  3235. 'float a = 1.25, b = 2.5;' + '\n' +
  3236. 'bool c1 = a == 1.25;' + '\n' +
  3237. 'bool c2 = b == 2.54;' + '\n' +
  3238. 'bool c3 = 1.2 != a;' + '\n' +
  3239. 'bool c4 = 1.5 != b;'
  3240. )
  3241. c_src27 = (
  3242. 'float a = 1.25, b = 2.5;' + '\n' +
  3243. 'bool c1 = a == b;' + '\n' +
  3244. 'bool c2 = a != b;' + '\n' +
  3245. 'bool c3 = a < b;' + '\n' +
  3246. 'bool c4 = a <= b;' + '\n' +
  3247. 'bool c5 = a > b;' + '\n' +
  3248. 'bool c6 = a >= b;'
  3249. )
  3250. c_src28 = (
  3251. 'bool c1 = true == true;' + '\n' +
  3252. 'bool c2 = true == false;' + '\n' +
  3253. 'bool c3 = false == false;' + '\n' +
  3254. 'bool c4 = true != true;' + '\n' +
  3255. 'bool c5 = true != false;' + '\n' +
  3256. 'bool c6 = false != false;'
  3257. )
  3258. c_src29 = (
  3259. 'bool c1 = true && true;' + '\n' +
  3260. 'bool c2 = true && false;' + '\n' +
  3261. 'bool c3 = false && false;' + '\n' +
  3262. 'bool c4 = true || true;' + '\n' +
  3263. 'bool c5 = true || false;' + '\n' +
  3264. 'bool c6 = false || false;'
  3265. )
  3266. c_src30 = (
  3267. 'bool a = false;' + '\n' +
  3268. 'bool c1 = a && true;' + '\n' +
  3269. 'bool c2 = false && a;' + '\n' +
  3270. 'bool c3 = true || a;' + '\n' +
  3271. 'bool c4 = a || false;'
  3272. )
  3273. c_src31 = (
  3274. 'int a = 1;' + '\n' +
  3275. 'bool c1 = a && 1;' + '\n' +
  3276. 'bool c2 = a && 0;' + '\n' +
  3277. 'bool c3 = a || 1;' + '\n' +
  3278. 'bool c4 = 0 || a;'
  3279. )
  3280. c_src32 = (
  3281. 'int a = 1, b = 0;' + '\n' +
  3282. 'bool c = false, d = true;'+ '\n' +
  3283. 'bool c1 = a && b;' + '\n' +
  3284. 'bool c2 = a && c;' + '\n' +
  3285. 'bool c3 = c && d;' + '\n' +
  3286. 'bool c4 = a || b;' + '\n' +
  3287. 'bool c5 = a || c;' + '\n' +
  3288. 'bool c6 = c || d;'
  3289. )
  3290. c_src_raise1 = (
  3291. "char a = 'b';"
  3292. )
  3293. c_src_raise2 = (
  3294. 'int a[] = {10, 20};'
  3295. )
  3296. res1 = SymPyExpression(c_src1, 'c').return_expr()
  3297. res2 = SymPyExpression(c_src2, 'c').return_expr()
  3298. res3 = SymPyExpression(c_src3, 'c').return_expr()
  3299. res4 = SymPyExpression(c_src4, 'c').return_expr()
  3300. res5 = SymPyExpression(c_src5, 'c').return_expr()
  3301. res6 = SymPyExpression(c_src6, 'c').return_expr()
  3302. res7 = SymPyExpression(c_src7, 'c').return_expr()
  3303. res8 = SymPyExpression(c_src8, 'c').return_expr()
  3304. res9 = SymPyExpression(c_src9, 'c').return_expr()
  3305. res10 = SymPyExpression(c_src10, 'c').return_expr()
  3306. res11 = SymPyExpression(c_src11, 'c').return_expr()
  3307. res12 = SymPyExpression(c_src12, 'c').return_expr()
  3308. res13 = SymPyExpression(c_src13, 'c').return_expr()
  3309. res14 = SymPyExpression(c_src14, 'c').return_expr()
  3310. res15 = SymPyExpression(c_src15, 'c').return_expr()
  3311. res16 = SymPyExpression(c_src16, 'c').return_expr()
  3312. res17 = SymPyExpression(c_src17, 'c').return_expr()
  3313. res18 = SymPyExpression(c_src18, 'c').return_expr()
  3314. res19 = SymPyExpression(c_src19, 'c').return_expr()
  3315. res20 = SymPyExpression(c_src20, 'c').return_expr()
  3316. res21 = SymPyExpression(c_src21, 'c').return_expr()
  3317. res22 = SymPyExpression(c_src22, 'c').return_expr()
  3318. res23 = SymPyExpression(c_src23, 'c').return_expr()
  3319. res24 = SymPyExpression(c_src24, 'c').return_expr()
  3320. res25 = SymPyExpression(c_src25, 'c').return_expr()
  3321. res26 = SymPyExpression(c_src26, 'c').return_expr()
  3322. res27 = SymPyExpression(c_src27, 'c').return_expr()
  3323. res28 = SymPyExpression(c_src28, 'c').return_expr()
  3324. res29 = SymPyExpression(c_src29, 'c').return_expr()
  3325. res30 = SymPyExpression(c_src30, 'c').return_expr()
  3326. res31 = SymPyExpression(c_src31, 'c').return_expr()
  3327. res32 = SymPyExpression(c_src32, 'c').return_expr()
  3328. assert res1[0] == Declaration(
  3329. Variable(Symbol('b'),
  3330. type=IntBaseType(String('intc')),
  3331. value=Integer(100)
  3332. )
  3333. )
  3334. assert res1[1] == Declaration(
  3335. Variable(Symbol('a'),
  3336. type=IntBaseType(String('intc')),
  3337. value=Symbol('b')
  3338. )
  3339. )
  3340. assert res2[0] == Declaration(
  3341. Variable(Symbol('a'),
  3342. type=IntBaseType(String('intc')),
  3343. value=Integer(1)
  3344. )
  3345. )
  3346. assert res2[1] == Declaration(Variable(Symbol('b'),
  3347. type=IntBaseType(String('intc')),
  3348. value=Add(
  3349. Symbol('a'),
  3350. Integer(1)
  3351. )
  3352. )
  3353. )
  3354. assert res3[0] == Declaration(
  3355. Variable(Symbol('a'),
  3356. type=FloatType(
  3357. String('float32'),
  3358. nbits=Integer(32),
  3359. nmant=Integer(23),
  3360. nexp=Integer(8)
  3361. ),
  3362. value=Float('12.5', precision=53)
  3363. )
  3364. )
  3365. assert res3[1] == Declaration(
  3366. Variable(Symbol('b'),
  3367. type=FloatType(
  3368. String('float32'),
  3369. nbits=Integer(32),
  3370. nmant=Integer(23),
  3371. nexp=Integer(8)
  3372. ),
  3373. value=Mul(
  3374. Float('20.0', precision=53),
  3375. Symbol('a')
  3376. )
  3377. )
  3378. )
  3379. assert res4[0] == Declaration(
  3380. Variable(Symbol('a'),
  3381. type=IntBaseType(String('intc')),
  3382. value=Integer(83)
  3383. )
  3384. )
  3385. assert res5[0] == Declaration(
  3386. Variable(Symbol('a'),
  3387. type=IntBaseType(String('intc')),
  3388. value=Integer(-4836)
  3389. )
  3390. )
  3391. assert res6[0] == Declaration(
  3392. Variable(Symbol('b'),
  3393. type=IntBaseType(String('intc')),
  3394. value=Integer(2)
  3395. )
  3396. )
  3397. assert res6[1] == Declaration(
  3398. Variable(Symbol('c'),
  3399. type=IntBaseType(String('intc')),
  3400. value=Integer(3)
  3401. )
  3402. )
  3403. assert res6[2] == Declaration(
  3404. Variable(Symbol('a'),
  3405. type=IntBaseType(String('intc')),
  3406. value=Add(
  3407. Symbol('b'),
  3408. Mul(
  3409. Integer(4),
  3410. Symbol('c')
  3411. )
  3412. )
  3413. )
  3414. )
  3415. assert res7[0] == Declaration(
  3416. Variable(Symbol('b'),
  3417. type=IntBaseType(String('intc')),
  3418. value=Integer(1)
  3419. )
  3420. )
  3421. assert res7[1] == Declaration(
  3422. Variable(Symbol('c'),
  3423. type=IntBaseType(String('intc')),
  3424. value=Add(
  3425. Symbol('b'),
  3426. Integer(2)
  3427. )
  3428. )
  3429. )
  3430. assert res7[2] == Declaration(
  3431. Variable(Symbol('a'),
  3432. type=IntBaseType(String('intc')),
  3433. value=Mul(
  3434. Integer(10),
  3435. Pow(
  3436. Symbol('b'),
  3437. Integer(2)
  3438. ),
  3439. Symbol('c')
  3440. )
  3441. )
  3442. )
  3443. assert res8[0] == FunctionDefinition(
  3444. NoneToken(),
  3445. name=String('func'),
  3446. parameters=(),
  3447. body=CodeBlock(
  3448. Declaration(
  3449. Variable(Symbol('a'),
  3450. type=IntBaseType(String('intc')),
  3451. value=Integer(1)
  3452. )
  3453. ),
  3454. Declaration(
  3455. Variable(Symbol('b'),
  3456. type=IntBaseType(String('intc')),
  3457. value=Integer(2)
  3458. )
  3459. ),
  3460. Declaration(
  3461. Variable(Symbol('temp'),
  3462. type=IntBaseType(String('intc')),
  3463. value=Symbol('a')
  3464. )
  3465. ),
  3466. Assignment(
  3467. Variable(Symbol('a')),
  3468. Symbol('b')
  3469. ),
  3470. Assignment(
  3471. Variable(Symbol('b')),
  3472. Symbol('temp')
  3473. )
  3474. )
  3475. )
  3476. assert res9[0] == Declaration(
  3477. Variable(Symbol('a'),
  3478. type=IntBaseType(String('intc')),
  3479. value=Integer(1)
  3480. )
  3481. )
  3482. assert res9[1] == Declaration(
  3483. Variable(Symbol('b'),
  3484. type=IntBaseType(String('intc')),
  3485. value=Integer(2)
  3486. )
  3487. )
  3488. assert res9[2] == Declaration(
  3489. Variable(Symbol('c'),
  3490. type=IntBaseType(String('intc')),
  3491. value=Symbol('a')
  3492. )
  3493. )
  3494. assert res9[3] == Declaration(
  3495. Variable(Symbol('d'),
  3496. type=IntBaseType(String('intc')),
  3497. value=Add(
  3498. Symbol('a'),
  3499. Symbol('b'),
  3500. Symbol('c')
  3501. )
  3502. )
  3503. )
  3504. assert res9[4] == Declaration(
  3505. Variable(Symbol('e'),
  3506. type=IntBaseType(String('intc')),
  3507. value=Add(
  3508. Pow(
  3509. Symbol('a'),
  3510. Integer(3)
  3511. ),
  3512. Mul(
  3513. Integer(3),
  3514. Pow(
  3515. Symbol('a'),
  3516. Integer(2)
  3517. ),
  3518. Symbol('b')
  3519. ),
  3520. Mul(
  3521. Integer(3),
  3522. Symbol('a'),
  3523. Pow(
  3524. Symbol('b'),
  3525. Integer(2)
  3526. )
  3527. ),
  3528. Pow(
  3529. Symbol('b'),
  3530. Integer(3)
  3531. )
  3532. )
  3533. )
  3534. )
  3535. assert res9[5] == Declaration(
  3536. Variable(Symbol('f'),
  3537. type=IntBaseType(String('intc')),
  3538. value=Mul(
  3539. Add(
  3540. Symbol('a'),
  3541. Symbol('b'),
  3542. Mul(
  3543. Integer(-1),
  3544. Symbol('c')
  3545. )
  3546. ),
  3547. Add(
  3548. Symbol('a'),
  3549. Symbol('b'),
  3550. Symbol('c')
  3551. )
  3552. )
  3553. )
  3554. )
  3555. assert res9[6] == Declaration(
  3556. Variable(Symbol('g'),
  3557. type=IntBaseType(String('intc')),
  3558. value=Mul(
  3559. Symbol('a'),
  3560. Add(
  3561. Symbol('b'),
  3562. Mul(
  3563. Integer(-1),
  3564. Symbol('c')
  3565. )
  3566. ),
  3567. Pow(
  3568. Add(
  3569. Symbol('a'),
  3570. Symbol('b'),
  3571. Symbol('c'),
  3572. Symbol('d')
  3573. ),
  3574. Integer(2)
  3575. )
  3576. )
  3577. )
  3578. )
  3579. assert res10[0] == Declaration(
  3580. Variable(Symbol('a'),
  3581. type=FloatType(
  3582. String('float32'),
  3583. nbits=Integer(32),
  3584. nmant=Integer(23),
  3585. nexp=Integer(8)
  3586. ),
  3587. value=Float('10.0', precision=53)
  3588. )
  3589. )
  3590. assert res10[1] == Declaration(
  3591. Variable(Symbol('b'),
  3592. type=FloatType(
  3593. String('float32'),
  3594. nbits=Integer(32),
  3595. nmant=Integer(23),
  3596. nexp=Integer(8)
  3597. ),
  3598. value=Float('2.5', precision=53)
  3599. )
  3600. )
  3601. assert res10[2] == Declaration(
  3602. Variable(Symbol('c'),
  3603. type=FloatType(
  3604. String('float32'),
  3605. nbits=Integer(32),
  3606. nmant=Integer(23),
  3607. nexp=Integer(8)
  3608. ),
  3609. value=Add(
  3610. Pow(
  3611. Symbol('a'),
  3612. Integer(2)
  3613. ),
  3614. Mul(
  3615. Integer(2),
  3616. Symbol('a'),
  3617. Symbol('b')
  3618. ),
  3619. Pow(
  3620. Symbol('b'),
  3621. Integer(2)
  3622. )
  3623. )
  3624. )
  3625. )
  3626. assert res11[0] == Declaration(
  3627. Variable(Symbol('a'),
  3628. type=FloatType(
  3629. String('float32'),
  3630. nbits=Integer(32),
  3631. nmant=Integer(23),
  3632. nexp=Integer(8)
  3633. ),
  3634. value=Float('4.0', precision=53)
  3635. )
  3636. )
  3637. assert res12[0] == Declaration(
  3638. Variable(Symbol('a'),
  3639. type=IntBaseType(String('intc')),
  3640. value=Integer(25)
  3641. )
  3642. )
  3643. assert res13[0] == Declaration(
  3644. Variable(Symbol('a'),
  3645. type=IntBaseType(String('intc')),
  3646. value=Integer(-95)
  3647. )
  3648. )
  3649. assert res14[0] == Declaration(
  3650. Variable(Symbol('a'),
  3651. type=IntBaseType(String('intc')),
  3652. value=Integer(-300)
  3653. )
  3654. )
  3655. assert res15[0] == Declaration(
  3656. Variable(Symbol('a'),
  3657. type=IntBaseType(String('intc')),
  3658. value=Integer(4)
  3659. )
  3660. )
  3661. assert res15[1] == Declaration(
  3662. Variable(Symbol('b'),
  3663. type=IntBaseType(String('intc')),
  3664. value=Integer(2)
  3665. )
  3666. )
  3667. assert res15[2] == Declaration(
  3668. Variable(Symbol('c'),
  3669. type=FloatType(
  3670. String('float32'),
  3671. nbits=Integer(32),
  3672. nmant=Integer(23),
  3673. nexp=Integer(8)
  3674. ),
  3675. value=Mul(
  3676. Pow(
  3677. Symbol('a'),
  3678. Integer(-1)
  3679. ),
  3680. Symbol('b')
  3681. )
  3682. )
  3683. )
  3684. assert res16[0] == Declaration(
  3685. Variable(Symbol('a'),
  3686. type=IntBaseType(String('intc')),
  3687. value=Integer(2)
  3688. )
  3689. )
  3690. assert res16[1] == Declaration(
  3691. Variable(Symbol('d'),
  3692. type=IntBaseType(String('intc')),
  3693. value=Integer(5)
  3694. )
  3695. )
  3696. assert res16[2] == Declaration(
  3697. Variable(Symbol('n'),
  3698. type=IntBaseType(String('intc')),
  3699. value=Integer(10)
  3700. )
  3701. )
  3702. assert res16[3] == Declaration(
  3703. Variable(Symbol('s'),
  3704. type=IntBaseType(String('intc')),
  3705. value=Mul(
  3706. Rational(1, 2),
  3707. Symbol('a'),
  3708. Add(
  3709. Mul(
  3710. Integer(2),
  3711. Symbol('a')
  3712. ),
  3713. Mul(
  3714. Symbol('d'),
  3715. Add(
  3716. Symbol('n'),
  3717. Integer(-1)
  3718. )
  3719. )
  3720. )
  3721. )
  3722. )
  3723. )
  3724. assert res17[0] == Declaration(
  3725. Variable(Symbol('a'),
  3726. type=IntBaseType(String('intc')),
  3727. value=Integer(1)
  3728. )
  3729. )
  3730. assert res18[0] == Declaration(
  3731. Variable(Symbol('a'),
  3732. type=IntBaseType(String('intc')),
  3733. value=Integer(2)
  3734. )
  3735. )
  3736. assert res18[1] == Declaration(
  3737. Variable(Symbol('b'),
  3738. type=IntBaseType(String('intc')),
  3739. value=Mod(
  3740. Symbol('a'),
  3741. Integer(3)
  3742. )
  3743. )
  3744. )
  3745. assert res19[0] == Declaration(
  3746. Variable(Symbol('a'),
  3747. type=IntBaseType(String('intc')),
  3748. value=Integer(100)
  3749. )
  3750. )
  3751. assert res19[1] == Declaration(
  3752. Variable(Symbol('b'),
  3753. type=IntBaseType(String('intc')),
  3754. value=Integer(3)
  3755. )
  3756. )
  3757. assert res19[2] == Declaration(
  3758. Variable(Symbol('c'),
  3759. type=IntBaseType(String('intc')),
  3760. value=Mod(
  3761. Symbol('a'),
  3762. Symbol('b')
  3763. )
  3764. )
  3765. )
  3766. assert res20[0] == Declaration(
  3767. Variable(Symbol('a'),
  3768. type=IntBaseType(String('intc')),
  3769. value=Integer(100)
  3770. )
  3771. )
  3772. assert res20[1] == Declaration(
  3773. Variable(Symbol('b'),
  3774. type=IntBaseType(String('intc')),
  3775. value=Integer(3)
  3776. )
  3777. )
  3778. assert res20[2] == Declaration(
  3779. Variable(Symbol('mod'),
  3780. type=IntBaseType(String('intc')),
  3781. value=Integer(1000000007)
  3782. )
  3783. )
  3784. assert res20[3] == Declaration(
  3785. Variable(Symbol('c'),
  3786. type=IntBaseType(String('intc')),
  3787. value=Mod(
  3788. Add(
  3789. Symbol('a'),
  3790. Mul(
  3791. Integer(100),
  3792. Pow(
  3793. Symbol('a'),
  3794. Integer(-1)
  3795. ),
  3796. Symbol('b')
  3797. )
  3798. ),
  3799. Symbol('mod')
  3800. )
  3801. )
  3802. )
  3803. assert res21[0] == Declaration(
  3804. Variable(Symbol('a'),
  3805. type=IntBaseType(String('intc')),
  3806. value=Integer(100)
  3807. )
  3808. )
  3809. assert res21[1] == Declaration(
  3810. Variable(Symbol('b'),
  3811. type=IntBaseType(String('intc')),
  3812. value=Integer(3)
  3813. )
  3814. )
  3815. assert res21[2] == Declaration(
  3816. Variable(Symbol('mod'),
  3817. type=IntBaseType(String('intc')),
  3818. value=Integer(1000000007)
  3819. )
  3820. )
  3821. assert res21[3] == Declaration(
  3822. Variable(Symbol('c'),
  3823. type=IntBaseType(String('intc')),
  3824. value=Mod(
  3825. Mul(
  3826. Add(
  3827. Symbol('a'),
  3828. Mul(
  3829. Integer(-1),
  3830. Symbol('b')
  3831. )
  3832. ),
  3833. Add(
  3834. Symbol('a'),
  3835. Symbol('b')
  3836. )
  3837. ),
  3838. Symbol('mod')
  3839. )
  3840. )
  3841. )
  3842. assert res22[0] == Declaration(
  3843. Variable(Symbol('a'),
  3844. type=Type(String('bool')),
  3845. value=false
  3846. )
  3847. )
  3848. assert res22[1] == Declaration(
  3849. Variable(Symbol('b'),
  3850. type=Type(String('bool')),
  3851. value=true
  3852. )
  3853. )
  3854. assert res23[0] == Declaration(
  3855. Variable(Symbol('a'),
  3856. type=Type(String('bool')),
  3857. value=true
  3858. )
  3859. )
  3860. assert res23[1] == Declaration(
  3861. Variable(Symbol('b'),
  3862. type=Type(String('bool')),
  3863. value=true
  3864. )
  3865. )
  3866. assert res23[2] == Declaration(
  3867. Variable(Symbol('c'),
  3868. type=Type(String('bool')),
  3869. value=false
  3870. )
  3871. )
  3872. assert res23[3] == Declaration(
  3873. Variable(Symbol('d'),
  3874. type=Type(String('bool')),
  3875. value=false
  3876. )
  3877. )
  3878. assert res24[0] == Declaration(
  3879. Variable(Symbol('a'),
  3880. type=IntBaseType(String('intc')),
  3881. value=Integer(1)
  3882. )
  3883. )
  3884. assert res24[1] == Declaration(
  3885. Variable(Symbol('b'),
  3886. type=IntBaseType(String('intc')),
  3887. value=Integer(2)
  3888. )
  3889. )
  3890. assert res24[2] == Declaration(
  3891. Variable(Symbol('c1'),
  3892. type=Type(String('bool')),
  3893. value=Equality(
  3894. Symbol('a'),
  3895. Integer(1)
  3896. )
  3897. )
  3898. )
  3899. assert res24[3] == Declaration(
  3900. Variable(Symbol('c2'),
  3901. type=Type(String('bool')),
  3902. value=Equality(
  3903. Symbol('b'),
  3904. Integer(2)
  3905. )
  3906. )
  3907. )
  3908. assert res24[4] == Declaration(
  3909. Variable(Symbol('c3'),
  3910. type=Type(String('bool')),
  3911. value=Unequality(
  3912. Integer(1),
  3913. Symbol('a')
  3914. )
  3915. )
  3916. )
  3917. assert res24[5] == Declaration(
  3918. Variable(Symbol('c4'),
  3919. type=Type(String('bool')),
  3920. value=Unequality(
  3921. Integer(1),
  3922. Symbol('b')
  3923. )
  3924. )
  3925. )
  3926. assert res24[6] == Declaration(
  3927. Variable(Symbol('c5'),
  3928. type=Type(String('bool')),
  3929. value=StrictLessThan(Symbol('a'),
  3930. Integer(0)
  3931. )
  3932. )
  3933. )
  3934. assert res24[7] == Declaration(
  3935. Variable(Symbol('c6'),
  3936. type=Type(String('bool')),
  3937. value=LessThan(
  3938. Symbol('b'),
  3939. Integer(10)
  3940. )
  3941. )
  3942. )
  3943. assert res24[8] == Declaration(
  3944. Variable(Symbol('c7'),
  3945. type=Type(String('bool')),
  3946. value=StrictGreaterThan(
  3947. Symbol('a'),
  3948. Integer(0)
  3949. )
  3950. )
  3951. )
  3952. assert res24[9] == Declaration(
  3953. Variable(Symbol('c8'),
  3954. type=Type(String('bool')),
  3955. value=GreaterThan(
  3956. Symbol('b'),
  3957. Integer(11)
  3958. )
  3959. )
  3960. )
  3961. assert res25[0] == Declaration(
  3962. Variable(Symbol('a'),
  3963. type=IntBaseType(String('intc')),
  3964. value=Integer(3)
  3965. )
  3966. )
  3967. assert res25[1] == Declaration(
  3968. Variable(Symbol('b'),
  3969. type=IntBaseType(String('intc')),
  3970. value=Integer(4)
  3971. )
  3972. )
  3973. assert res25[2] == Declaration(Variable(Symbol('c1'),
  3974. type=Type(String('bool')),
  3975. value=Equality(
  3976. Symbol('a'),
  3977. Symbol('b')
  3978. )
  3979. )
  3980. )
  3981. assert res25[3] == Declaration(
  3982. Variable(Symbol('c2'),
  3983. type=Type(String('bool')),
  3984. value=Unequality(
  3985. Symbol('a'),
  3986. Symbol('b')
  3987. )
  3988. )
  3989. )
  3990. assert res25[4] == Declaration(
  3991. Variable(Symbol('c3'),
  3992. type=Type(String('bool')),
  3993. value=StrictLessThan(
  3994. Symbol('a'),
  3995. Symbol('b')
  3996. )
  3997. )
  3998. )
  3999. assert res25[5] == Declaration(
  4000. Variable(Symbol('c4'),
  4001. type=Type(String('bool')),
  4002. value=LessThan(
  4003. Symbol('a'),
  4004. Symbol('b')
  4005. )
  4006. )
  4007. )
  4008. assert res25[6] == Declaration(
  4009. Variable(Symbol('c5'),
  4010. type=Type(String('bool')),
  4011. value=StrictGreaterThan(
  4012. Symbol('a'),
  4013. Symbol('b')
  4014. )
  4015. )
  4016. )
  4017. assert res25[7] == Declaration(
  4018. Variable(Symbol('c6'),
  4019. type=Type(String('bool')),
  4020. value=GreaterThan(
  4021. Symbol('a'),
  4022. Symbol('b')
  4023. )
  4024. )
  4025. )
  4026. assert res26[0] == Declaration(
  4027. Variable(Symbol('a'),
  4028. type=FloatType(
  4029. String('float32'),
  4030. nbits=Integer(32),
  4031. nmant=Integer(23),
  4032. nexp=Integer(8)
  4033. ),
  4034. value=Float('1.25', precision=53)
  4035. )
  4036. )
  4037. assert res26[1] == Declaration(
  4038. Variable(Symbol('b'),
  4039. type=FloatType(
  4040. String('float32'),
  4041. nbits=Integer(32),
  4042. nmant=Integer(23),
  4043. nexp=Integer(8)
  4044. ),
  4045. value=Float('2.5', precision=53)
  4046. )
  4047. )
  4048. assert res26[2] == Declaration(
  4049. Variable(Symbol('c1'),
  4050. type=Type(String('bool')),
  4051. value=Equality(
  4052. Symbol('a'),
  4053. Float('1.25', precision=53)
  4054. )
  4055. )
  4056. )
  4057. assert res26[3] == Declaration(
  4058. Variable(Symbol('c2'),
  4059. type=Type(String('bool')),
  4060. value=Equality(
  4061. Symbol('b'),
  4062. Float('2.54', precision=53)
  4063. )
  4064. )
  4065. )
  4066. assert res26[4] == Declaration(
  4067. Variable(Symbol('c3'),
  4068. type=Type(String('bool')),
  4069. value=Unequality(
  4070. Float('1.2', precision=53),
  4071. Symbol('a')
  4072. )
  4073. )
  4074. )
  4075. assert res26[5] == Declaration(
  4076. Variable(Symbol('c4'),
  4077. type=Type(String('bool')),
  4078. value=Unequality(
  4079. Float('1.5', precision=53),
  4080. Symbol('b')
  4081. )
  4082. )
  4083. )
  4084. assert res27[0] == Declaration(
  4085. Variable(Symbol('a'),
  4086. type=FloatType(
  4087. String('float32'),
  4088. nbits=Integer(32),
  4089. nmant=Integer(23),
  4090. nexp=Integer(8)
  4091. ),
  4092. value=Float('1.25', precision=53)
  4093. )
  4094. )
  4095. assert res27[1] == Declaration(
  4096. Variable(Symbol('b'),
  4097. type=FloatType(
  4098. String('float32'),
  4099. nbits=Integer(32),
  4100. nmant=Integer(23),
  4101. nexp=Integer(8)
  4102. ),
  4103. value=Float('2.5', precision=53)
  4104. )
  4105. )
  4106. assert res27[2] == Declaration(
  4107. Variable(Symbol('c1'),
  4108. type=Type(String('bool')),
  4109. value=Equality(
  4110. Symbol('a'),
  4111. Symbol('b')
  4112. )
  4113. )
  4114. )
  4115. assert res27[3] == Declaration(
  4116. Variable(Symbol('c2'),
  4117. type=Type(String('bool')),
  4118. value=Unequality(
  4119. Symbol('a'),
  4120. Symbol('b')
  4121. )
  4122. )
  4123. )
  4124. assert res27[4] == Declaration(
  4125. Variable(Symbol('c3'),
  4126. type=Type(String('bool')),
  4127. value=StrictLessThan(
  4128. Symbol('a'),
  4129. Symbol('b')
  4130. )
  4131. )
  4132. )
  4133. assert res27[5] == Declaration(
  4134. Variable(Symbol('c4'),
  4135. type=Type(String('bool')),
  4136. value=LessThan(
  4137. Symbol('a'),
  4138. Symbol('b')
  4139. )
  4140. )
  4141. )
  4142. assert res27[6] == Declaration(
  4143. Variable(Symbol('c5'),
  4144. type=Type(String('bool')),
  4145. value=StrictGreaterThan(
  4146. Symbol('a'),
  4147. Symbol('b')
  4148. )
  4149. )
  4150. )
  4151. assert res27[7] == Declaration(
  4152. Variable(Symbol('c6'),
  4153. type=Type(String('bool')),
  4154. value=GreaterThan(
  4155. Symbol('a'),
  4156. Symbol('b')
  4157. )
  4158. )
  4159. )
  4160. assert res28[0] == Declaration(
  4161. Variable(Symbol('c1'),
  4162. type=Type(String('bool')),
  4163. value=true
  4164. )
  4165. )
  4166. assert res28[1] == Declaration(
  4167. Variable(Symbol('c2'),
  4168. type=Type(String('bool')),
  4169. value=false
  4170. )
  4171. )
  4172. assert res28[2] == Declaration(
  4173. Variable(Symbol('c3'),
  4174. type=Type(String('bool')),
  4175. value=true
  4176. )
  4177. )
  4178. assert res28[3] == Declaration(
  4179. Variable(Symbol('c4'),
  4180. type=Type(String('bool')),
  4181. value=false
  4182. )
  4183. )
  4184. assert res28[4] == Declaration(
  4185. Variable(Symbol('c5'),
  4186. type=Type(String('bool')),
  4187. value=true
  4188. )
  4189. )
  4190. assert res28[5] == Declaration(
  4191. Variable(Symbol('c6'),
  4192. type=Type(String('bool')),
  4193. value=false
  4194. )
  4195. )
  4196. assert res29[0] == Declaration(
  4197. Variable(Symbol('c1'),
  4198. type=Type(String('bool')),
  4199. value=true
  4200. )
  4201. )
  4202. assert res29[1] == Declaration(
  4203. Variable(Symbol('c2'),
  4204. type=Type(String('bool')),
  4205. value=false
  4206. )
  4207. )
  4208. assert res29[2] == Declaration(
  4209. Variable(Symbol('c3'),
  4210. type=Type(String('bool')),
  4211. value=false
  4212. )
  4213. )
  4214. assert res29[3] == Declaration(
  4215. Variable(Symbol('c4'),
  4216. type=Type(String('bool')),
  4217. value=true
  4218. )
  4219. )
  4220. assert res29[4] == Declaration(
  4221. Variable(Symbol('c5'),
  4222. type=Type(String('bool')),
  4223. value=true
  4224. )
  4225. )
  4226. assert res29[5] == Declaration(
  4227. Variable(Symbol('c6'),
  4228. type=Type(String('bool')),
  4229. value=false
  4230. )
  4231. )
  4232. assert res30[0] == Declaration(
  4233. Variable(Symbol('a'),
  4234. type=Type(String('bool')),
  4235. value=false
  4236. )
  4237. )
  4238. assert res30[1] == Declaration(
  4239. Variable(Symbol('c1'),
  4240. type=Type(String('bool')),
  4241. value=Symbol('a')
  4242. )
  4243. )
  4244. assert res30[2] == Declaration(
  4245. Variable(Symbol('c2'),
  4246. type=Type(String('bool')),
  4247. value=false
  4248. )
  4249. )
  4250. assert res30[3] == Declaration(
  4251. Variable(Symbol('c3'),
  4252. type=Type(String('bool')),
  4253. value=true
  4254. )
  4255. )
  4256. assert res30[4] == Declaration(
  4257. Variable(Symbol('c4'),
  4258. type=Type(String('bool')),
  4259. value=Symbol('a')
  4260. )
  4261. )
  4262. assert res31[0] == Declaration(
  4263. Variable(Symbol('a'),
  4264. type=IntBaseType(String('intc')),
  4265. value=Integer(1)
  4266. )
  4267. )
  4268. assert res31[1] == Declaration(
  4269. Variable(Symbol('c1'),
  4270. type=Type(String('bool')),
  4271. value=Symbol('a')
  4272. )
  4273. )
  4274. assert res31[2] == Declaration(
  4275. Variable(Symbol('c2'),
  4276. type=Type(String('bool')),
  4277. value=false
  4278. )
  4279. )
  4280. assert res31[3] == Declaration(
  4281. Variable(Symbol('c3'),
  4282. type=Type(String('bool')),
  4283. value=true
  4284. )
  4285. )
  4286. assert res31[4] == Declaration(
  4287. Variable(Symbol('c4'),
  4288. type=Type(String('bool')),
  4289. value=Symbol('a')
  4290. )
  4291. )
  4292. assert res32[0] == Declaration(
  4293. Variable(Symbol('a'),
  4294. type=IntBaseType(String('intc')),
  4295. value=Integer(1)
  4296. )
  4297. )
  4298. assert res32[1] == Declaration(
  4299. Variable(Symbol('b'),
  4300. type=IntBaseType(String('intc')),
  4301. value=Integer(0)
  4302. )
  4303. )
  4304. assert res32[2] == Declaration(
  4305. Variable(Symbol('c'),
  4306. type=Type(String('bool')),
  4307. value=false
  4308. )
  4309. )
  4310. assert res32[3] == Declaration(
  4311. Variable(Symbol('d'),
  4312. type=Type(String('bool')),
  4313. value=true
  4314. )
  4315. )
  4316. assert res32[4] == Declaration(
  4317. Variable(Symbol('c1'),
  4318. type=Type(String('bool')),
  4319. value=And(
  4320. Symbol('a'),
  4321. Symbol('b')
  4322. )
  4323. )
  4324. )
  4325. assert res32[5] == Declaration(
  4326. Variable(Symbol('c2'),
  4327. type=Type(String('bool')),
  4328. value=And(
  4329. Symbol('a'),
  4330. Symbol('c')
  4331. )
  4332. )
  4333. )
  4334. assert res32[6] == Declaration(
  4335. Variable(Symbol('c3'),
  4336. type=Type(String('bool')),
  4337. value=And(
  4338. Symbol('c'),
  4339. Symbol('d')
  4340. )
  4341. )
  4342. )
  4343. assert res32[7] == Declaration(
  4344. Variable(Symbol('c4'),
  4345. type=Type(String('bool')),
  4346. value=Or(
  4347. Symbol('a'),
  4348. Symbol('b')
  4349. )
  4350. )
  4351. )
  4352. assert res32[8] == Declaration(
  4353. Variable(Symbol('c5'),
  4354. type=Type(String('bool')),
  4355. value=Or(
  4356. Symbol('a'),
  4357. Symbol('c')
  4358. )
  4359. )
  4360. )
  4361. assert res32[9] == Declaration(
  4362. Variable(Symbol('c6'),
  4363. type=Type(String('bool')),
  4364. value=Or(
  4365. Symbol('c'),
  4366. Symbol('d')
  4367. )
  4368. )
  4369. )
  4370. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise1, 'c'))
  4371. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise2, 'c'))
  4372. def test_paren_expr():
  4373. c_src1 = (
  4374. 'int a = (1);'
  4375. 'int b = (1 + 2 * 3);'
  4376. )
  4377. c_src2 = (
  4378. 'int a = 1, b = 2, c = 3;'
  4379. 'int d = (a);'
  4380. 'int e = (a + 1);'
  4381. 'int f = (a + b * c - d / e);'
  4382. )
  4383. res1 = SymPyExpression(c_src1, 'c').return_expr()
  4384. res2 = SymPyExpression(c_src2, 'c').return_expr()
  4385. assert res1[0] == Declaration(
  4386. Variable(Symbol('a'),
  4387. type=IntBaseType(String('intc')),
  4388. value=Integer(1)
  4389. )
  4390. )
  4391. assert res1[1] == Declaration(
  4392. Variable(Symbol('b'),
  4393. type=IntBaseType(String('intc')),
  4394. value=Integer(7)
  4395. )
  4396. )
  4397. assert res2[0] == Declaration(
  4398. Variable(Symbol('a'),
  4399. type=IntBaseType(String('intc')),
  4400. value=Integer(1)
  4401. )
  4402. )
  4403. assert res2[1] == Declaration(
  4404. Variable(Symbol('b'),
  4405. type=IntBaseType(String('intc')),
  4406. value=Integer(2)
  4407. )
  4408. )
  4409. assert res2[2] == Declaration(
  4410. Variable(Symbol('c'),
  4411. type=IntBaseType(String('intc')),
  4412. value=Integer(3)
  4413. )
  4414. )
  4415. assert res2[3] == Declaration(
  4416. Variable(Symbol('d'),
  4417. type=IntBaseType(String('intc')),
  4418. value=Symbol('a')
  4419. )
  4420. )
  4421. assert res2[4] == Declaration(
  4422. Variable(Symbol('e'),
  4423. type=IntBaseType(String('intc')),
  4424. value=Add(
  4425. Symbol('a'),
  4426. Integer(1)
  4427. )
  4428. )
  4429. )
  4430. assert res2[5] == Declaration(
  4431. Variable(Symbol('f'),
  4432. type=IntBaseType(String('intc')),
  4433. value=Add(
  4434. Symbol('a'),
  4435. Mul(
  4436. Symbol('b'),
  4437. Symbol('c')
  4438. ),
  4439. Mul(
  4440. Integer(-1),
  4441. Symbol('d'),
  4442. Pow(
  4443. Symbol('e'),
  4444. Integer(-1)
  4445. )
  4446. )
  4447. )
  4448. )
  4449. )
  4450. def test_unary_operators():
  4451. c_src1 = (
  4452. 'void func()'+
  4453. '{' + '\n' +
  4454. 'int a = 10;' + '\n' +
  4455. 'int b = 20;' + '\n' +
  4456. '++a;' + '\n' +
  4457. '--b;' + '\n' +
  4458. 'a++;' + '\n' +
  4459. 'b--;' + '\n' +
  4460. '}'
  4461. )
  4462. c_src2 = (
  4463. 'void func()'+
  4464. '{' + '\n' +
  4465. 'int a = 10;' + '\n' +
  4466. 'int b = -100;' + '\n' +
  4467. 'int c = +19;' + '\n' +
  4468. 'int d = ++a;' + '\n' +
  4469. 'int e = --b;' + '\n' +
  4470. 'int f = a++;' + '\n' +
  4471. 'int g = b--;' + '\n' +
  4472. 'bool h = !false;' + '\n' +
  4473. 'bool i = !d;' + '\n' +
  4474. 'bool j = !0;' + '\n' +
  4475. 'bool k = !10.0;' + '\n' +
  4476. '}'
  4477. )
  4478. c_src_raise1 = (
  4479. 'void func()'+
  4480. '{' + '\n' +
  4481. 'int a = 10;' + '\n' +
  4482. 'int b = ~a;' + '\n' +
  4483. '}'
  4484. )
  4485. c_src_raise2 = (
  4486. 'void func()'+
  4487. '{' + '\n' +
  4488. 'int a = 10;' + '\n' +
  4489. 'int b = *&a;' + '\n' +
  4490. '}'
  4491. )
  4492. res1 = SymPyExpression(c_src1, 'c').return_expr()
  4493. res2 = SymPyExpression(c_src2, 'c').return_expr()
  4494. assert res1[0] == FunctionDefinition(
  4495. NoneToken(),
  4496. name=String('func'),
  4497. parameters=(),
  4498. body=CodeBlock(
  4499. Declaration(
  4500. Variable(Symbol('a'),
  4501. type=IntBaseType(String('intc')),
  4502. value=Integer(10)
  4503. )
  4504. ),
  4505. Declaration(
  4506. Variable(Symbol('b'),
  4507. type=IntBaseType(String('intc')),
  4508. value=Integer(20)
  4509. )
  4510. ),
  4511. PreIncrement(Symbol('a')),
  4512. PreDecrement(Symbol('b')),
  4513. PostIncrement(Symbol('a')),
  4514. PostDecrement(Symbol('b'))
  4515. )
  4516. )
  4517. assert res2[0] == FunctionDefinition(
  4518. NoneToken(),
  4519. name=String('func'),
  4520. parameters=(),
  4521. body=CodeBlock(
  4522. Declaration(
  4523. Variable(Symbol('a'),
  4524. type=IntBaseType(String('intc')),
  4525. value=Integer(10)
  4526. )
  4527. ),
  4528. Declaration(
  4529. Variable(Symbol('b'),
  4530. type=IntBaseType(String('intc')),
  4531. value=Integer(-100)
  4532. )
  4533. ),
  4534. Declaration(
  4535. Variable(Symbol('c'),
  4536. type=IntBaseType(String('intc')),
  4537. value=Integer(19)
  4538. )
  4539. ),
  4540. Declaration(
  4541. Variable(Symbol('d'),
  4542. type=IntBaseType(String('intc')),
  4543. value=PreIncrement(Symbol('a'))
  4544. )
  4545. ),
  4546. Declaration(
  4547. Variable(Symbol('e'),
  4548. type=IntBaseType(String('intc')),
  4549. value=PreDecrement(Symbol('b'))
  4550. )
  4551. ),
  4552. Declaration(
  4553. Variable(Symbol('f'),
  4554. type=IntBaseType(String('intc')),
  4555. value=PostIncrement(Symbol('a'))
  4556. )
  4557. ),
  4558. Declaration(
  4559. Variable(Symbol('g'),
  4560. type=IntBaseType(String('intc')),
  4561. value=PostDecrement(Symbol('b'))
  4562. )
  4563. ),
  4564. Declaration(
  4565. Variable(Symbol('h'),
  4566. type=Type(String('bool')),
  4567. value=true
  4568. )
  4569. ),
  4570. Declaration(
  4571. Variable(Symbol('i'),
  4572. type=Type(String('bool')),
  4573. value=Not(Symbol('d'))
  4574. )
  4575. ),
  4576. Declaration(
  4577. Variable(Symbol('j'),
  4578. type=Type(String('bool')),
  4579. value=true
  4580. )
  4581. ),
  4582. Declaration(
  4583. Variable(Symbol('k'),
  4584. type=Type(String('bool')),
  4585. value=false
  4586. )
  4587. )
  4588. )
  4589. )
  4590. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise1, 'c'))
  4591. raises(NotImplementedError, lambda: SymPyExpression(c_src_raise2, 'c'))
  4592. def test_compound_assignment_operator():
  4593. c_src = (
  4594. 'void func()'+
  4595. '{' + '\n' +
  4596. 'int a = 100;' + '\n' +
  4597. 'a += 10;' + '\n' +
  4598. 'a -= 10;' + '\n' +
  4599. 'a *= 10;' + '\n' +
  4600. 'a /= 10;' + '\n' +
  4601. 'a %= 10;' + '\n' +
  4602. '}'
  4603. )
  4604. res = SymPyExpression(c_src, 'c').return_expr()
  4605. assert res[0] == FunctionDefinition(
  4606. NoneToken(),
  4607. name=String('func'),
  4608. parameters=(),
  4609. body=CodeBlock(
  4610. Declaration(
  4611. Variable(
  4612. Symbol('a'),
  4613. type=IntBaseType(String('intc')),
  4614. value=Integer(100)
  4615. )
  4616. ),
  4617. AddAugmentedAssignment(
  4618. Variable(Symbol('a')),
  4619. Integer(10)
  4620. ),
  4621. SubAugmentedAssignment(
  4622. Variable(Symbol('a')),
  4623. Integer(10)
  4624. ),
  4625. MulAugmentedAssignment(
  4626. Variable(Symbol('a')),
  4627. Integer(10)
  4628. ),
  4629. DivAugmentedAssignment(
  4630. Variable(Symbol('a')),
  4631. Integer(10)
  4632. ),
  4633. ModAugmentedAssignment(
  4634. Variable(Symbol('a')),
  4635. Integer(10)
  4636. )
  4637. )
  4638. )
  4639. @XFAIL # this is expected to fail because of a bug in the C parser.
  4640. def test_while_stmt():
  4641. c_src1 = (
  4642. 'void func()'+
  4643. '{' + '\n' +
  4644. 'int i = 0;' + '\n' +
  4645. 'while(i < 10)' + '\n' +
  4646. '{' + '\n' +
  4647. 'i++;' + '\n' +
  4648. '}'
  4649. '}'
  4650. )
  4651. c_src2 = (
  4652. 'void func()'+
  4653. '{' + '\n' +
  4654. 'int i = 0;' + '\n' +
  4655. 'while(i < 10)' + '\n' +
  4656. 'i++;' + '\n' +
  4657. '}'
  4658. )
  4659. c_src3 = (
  4660. 'void func()'+
  4661. '{' + '\n' +
  4662. 'int i = 10;' + '\n' +
  4663. 'int cnt = 0;' + '\n' +
  4664. 'while(i > 0)' + '\n' +
  4665. '{' + '\n' +
  4666. 'i--;' + '\n' +
  4667. 'cnt++;' + '\n' +
  4668. '}' + '\n' +
  4669. '}'
  4670. )
  4671. c_src4 = (
  4672. 'int digit_sum(int n)'+
  4673. '{' + '\n' +
  4674. 'int sum = 0;' + '\n' +
  4675. 'while(n > 0)' + '\n' +
  4676. '{' + '\n' +
  4677. 'sum += (n % 10);' + '\n' +
  4678. 'n /= 10;' + '\n' +
  4679. '}' + '\n' +
  4680. 'return sum;' + '\n' +
  4681. '}'
  4682. )
  4683. c_src5 = (
  4684. 'void func()'+
  4685. '{' + '\n' +
  4686. 'while(1);' + '\n' +
  4687. '}'
  4688. )
  4689. res1 = SymPyExpression(c_src1, 'c').return_expr()
  4690. res2 = SymPyExpression(c_src2, 'c').return_expr()
  4691. res3 = SymPyExpression(c_src3, 'c').return_expr()
  4692. res4 = SymPyExpression(c_src4, 'c').return_expr()
  4693. res5 = SymPyExpression(c_src5, 'c').return_expr()
  4694. assert res1[0] == FunctionDefinition(
  4695. NoneToken(),
  4696. name=String('func'),
  4697. parameters=(),
  4698. body=CodeBlock(
  4699. Declaration(
  4700. Variable(Symbol('i'),
  4701. type=IntBaseType(String('intc')),
  4702. value=Integer(0)
  4703. )
  4704. ),
  4705. While(
  4706. StrictLessThan(
  4707. Symbol('i'),
  4708. Integer(10)
  4709. ),
  4710. body=CodeBlock(
  4711. PostIncrement(
  4712. Symbol('i')
  4713. )
  4714. )
  4715. )
  4716. )
  4717. )
  4718. assert res2[0] == res1[0]
  4719. assert res3[0] == FunctionDefinition(
  4720. NoneToken(),
  4721. name=String('func'),
  4722. parameters=(),
  4723. body=CodeBlock(
  4724. Declaration(
  4725. Variable(
  4726. Symbol('i'),
  4727. type=IntBaseType(String('intc')),
  4728. value=Integer(10)
  4729. )
  4730. ),
  4731. Declaration(
  4732. Variable(
  4733. Symbol('cnt'),
  4734. type=IntBaseType(String('intc')),
  4735. value=Integer(0)
  4736. )
  4737. ),
  4738. While(
  4739. StrictGreaterThan(
  4740. Symbol('i'),
  4741. Integer(0)
  4742. ),
  4743. body=CodeBlock(
  4744. PostDecrement(
  4745. Symbol('i')
  4746. ),
  4747. PostIncrement(
  4748. Symbol('cnt')
  4749. )
  4750. )
  4751. )
  4752. )
  4753. )
  4754. assert res4[0] == FunctionDefinition(
  4755. IntBaseType(String('intc')),
  4756. name=String('digit_sum'),
  4757. parameters=(
  4758. Variable(
  4759. Symbol('n'),
  4760. type=IntBaseType(String('intc'))
  4761. ),
  4762. ),
  4763. body=CodeBlock(
  4764. Declaration(
  4765. Variable(
  4766. Symbol('sum'),
  4767. type=IntBaseType(String('intc')),
  4768. value=Integer(0)
  4769. )
  4770. ),
  4771. While(
  4772. StrictGreaterThan(
  4773. Symbol('n'),
  4774. Integer(0)
  4775. ),
  4776. body=CodeBlock(
  4777. AddAugmentedAssignment(
  4778. Variable(
  4779. Symbol('sum')
  4780. ),
  4781. Mod(
  4782. Symbol('n'),
  4783. Integer(10)
  4784. )
  4785. ),
  4786. DivAugmentedAssignment(
  4787. Variable(
  4788. Symbol('n')
  4789. ),
  4790. Integer(10)
  4791. )
  4792. )
  4793. ),
  4794. Return('sum')
  4795. )
  4796. )
  4797. assert res5[0] == FunctionDefinition(
  4798. NoneToken(),
  4799. name=String('func'),
  4800. parameters=(),
  4801. body=CodeBlock(
  4802. While(
  4803. Integer(1),
  4804. body=CodeBlock(
  4805. NoneToken()
  4806. )
  4807. )
  4808. )
  4809. )
  4810. else:
  4811. def test_raise():
  4812. from sympy.parsing.c.c_parser import CCodeConverter
  4813. raises(ImportError, lambda: CCodeConverter())
  4814. raises(ImportError, lambda: SymPyExpression(' ', mode = 'c'))