PikaParser.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876
  1. /*
  2. * This file is part of the PikaPython project.
  3. * http://github.com/pikastech/pikapython
  4. *
  5. * MIT License
  6. *
  7. * Copyright (c) 2021 lyon liang6516@outlook.com
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. */
  27. #include "PikaParser.h"
  28. #include "BaseObj.h"
  29. #include "PikaObj.h"
  30. #include "dataQueue.h"
  31. #include "dataQueueObj.h"
  32. #include "dataStack.h"
  33. #include "dataStrs.h"
  34. /* local head */
  35. char* AST_genAsm_top(AST* ast, Args* outBuffs);
  36. int32_t AST_deinit(AST* ast);
  37. uint8_t TokenStream_isContain(char* tokenStream,
  38. enum TokenType token_type,
  39. char* pyload);
  40. char* TokenStream_pop(Args* buffs_p, char** tokenStream);
  41. char* parser_lines2Target(Parser* self, char* sPyLines);
  42. /* Cursor preivilage */
  43. void _Cursor_init(struct Cursor* cs);
  44. void _Cursor_parse(struct Cursor* cs, char* stmt);
  45. void _Cursor_beforeIter(struct Cursor* cs);
  46. /* Cursor iter api */
  47. void Cursor_iterStart(struct Cursor* cs);
  48. void Cursor_iterEnd(struct Cursor* cs);
  49. void Cursor_deinit(struct Cursor* cs);
  50. /* Cursor high level api */
  51. char* Cursor_popToken(Args* buffs, char** pStmt, char* devide);
  52. pika_bool Cursor_isContain(char* stmt, TokenType type, char* pyload);
  53. char* Cursor_splitCollect(Args* buffs, char* stmt, char* devide, int index);
  54. char* pika_lines2Asm(Args* outBuffs, char* multiLine);
  55. uint16_t TokenStream_getSize(char* tokenStream) {
  56. if (strEqu("", tokenStream)) {
  57. return 0;
  58. }
  59. return strCountSign(tokenStream, 0x1F) + 1;
  60. }
  61. char* Cursor_popLastToken(Args* outBuffs, char** pStmt, char* str) {
  62. char* sStmts = *pStmt;
  63. uint8_t uDividerIndex = 0;
  64. Arg* aKeeped = arg_newStr("");
  65. Arg* aPoped = arg_newStr("");
  66. Cursor_forEach(cs, sStmts) {
  67. Cursor_iterStart(&cs);
  68. if (cs.bracket_deepth == 0) {
  69. if (strEqu(str, cs.token1.pyload)) {
  70. uDividerIndex = cs.iter_index;
  71. }
  72. }
  73. Cursor_iterEnd(&cs);
  74. }
  75. Cursor_deinit(&cs);
  76. Cursor_forEachExistPs(cs, sStmts) {
  77. Cursor_iterStart(&cs);
  78. if (cs.iter_index < uDividerIndex) {
  79. aPoped = arg_strAppend(aPoped, cs.token1.pyload);
  80. }
  81. if (cs.iter_index > uDividerIndex) {
  82. aKeeped = arg_strAppend(aKeeped, cs.token1.pyload);
  83. }
  84. Cursor_iterEnd(&cs);
  85. }
  86. Cursor_deinit(&cs);
  87. char* sKeeped = strsCopy(outBuffs, arg_getStr(aKeeped));
  88. char* sPoped = arg_getStr(aPoped);
  89. pika_platform_memcpy(sStmts, sPoped, strGetSize(sPoped) + 1);
  90. arg_deinit(aPoped);
  91. arg_deinit(aKeeped);
  92. return sKeeped;
  93. }
  94. char* Cursor_getCleanStmt(Args* outBuffs, char* cmd) {
  95. pika_assert(cmd != NULL);
  96. int32_t iSize = strGetSize(cmd);
  97. /* lexer may generate more chars than input */
  98. char* sOut = args_getBuff(outBuffs, iSize * 2);
  99. int32_t iOut = 0;
  100. Cursor_forEach(cs, cmd) {
  101. Cursor_iterStart(&cs);
  102. for (uint16_t k = 0; k < strGetSize(cs.token1.pyload); k++) {
  103. sOut[iOut] = cs.token1.pyload[k];
  104. iOut++;
  105. }
  106. Cursor_iterEnd(&cs);
  107. }
  108. Cursor_deinit(&cs);
  109. /* add \0 */
  110. sOut[iOut] = 0;
  111. return sOut;
  112. }
  113. static uint8_t Lexer_isError(char* line) {
  114. Args buffs = {0};
  115. uint8_t uRes = 0; /* not error */
  116. char* sTokenStream = Lexer_getTokenStream(&buffs, line);
  117. if (NULL == sTokenStream) {
  118. uRes = 1; /* lex error */
  119. goto __exit;
  120. }
  121. goto __exit;
  122. __exit:
  123. strsDeinit(&buffs);
  124. return uRes;
  125. }
  126. static char* Cursor_removeTokensBetween(Args* outBuffs,
  127. char* input,
  128. char* token_pyload1,
  129. char* token_pyload2) {
  130. Args buffs = {0};
  131. uint8_t uBlockDeepth = 0;
  132. char* sOutput = "";
  133. Cursor_forEach(cs, input) {
  134. Cursor_iterStart(&cs);
  135. if (strEqu(token_pyload1, cs.token1.pyload)) {
  136. if (uBlockDeepth == 0) {
  137. sOutput = strsAppend(&buffs, sOutput, cs.token1.pyload);
  138. }
  139. uBlockDeepth++;
  140. }
  141. if (strEqu(token_pyload2, cs.token1.pyload)) {
  142. uBlockDeepth--;
  143. }
  144. if (uBlockDeepth == 0) {
  145. sOutput = strsAppend(&buffs, sOutput, cs.token1.pyload);
  146. }
  147. Cursor_iterEnd(&cs);
  148. }
  149. Cursor_deinit(&cs);
  150. sOutput = strsCopy(outBuffs, sOutput);
  151. strsDeinit(&buffs);
  152. return sOutput;
  153. }
  154. char* _remove_sub_stmt(Args* outBuffs, char* sStmt) {
  155. Args buffs = {0};
  156. sStmt = strsCopy(&buffs, sStmt);
  157. sStmt = Cursor_removeTokensBetween(&buffs, sStmt, "(", ")");
  158. sStmt = Cursor_removeTokensBetween(&buffs, sStmt, "[", "]");
  159. sStmt = Cursor_removeTokensBetween(&buffs, sStmt, "{", "}");
  160. char* sRes = args_cacheStr(outBuffs, sStmt);
  161. strsDeinit(&buffs);
  162. return sRes;
  163. }
  164. static enum StmtType Lexer_matchStmtType(char* right) {
  165. Args buffs = {0};
  166. enum StmtType eStmtType = STMT_none;
  167. char* sTopStmt = _remove_sub_stmt(&buffs, right);
  168. pika_bool bOperator = pika_false;
  169. pika_bool bMethod = pika_false;
  170. pika_bool bString = pika_false;
  171. pika_bool bBytes = pika_false;
  172. pika_bool bNumber = pika_false;
  173. pika_bool bSymbol = pika_false;
  174. pika_bool bList = pika_false;
  175. pika_bool bSlice = pika_false;
  176. pika_bool bDict = pika_false;
  177. pika_bool bImport = pika_false;
  178. pika_bool bInhert = pika_false;
  179. pika_bool bChain = pika_false;
  180. Cursor_forEach(cs, sTopStmt) {
  181. Cursor_iterStart(&cs);
  182. /* collect type */
  183. if (strEqu(cs.token1.pyload, " import ")) {
  184. bImport = pika_true;
  185. goto __iter_continue;
  186. }
  187. if (strEqu(cs.token1.pyload, "@inh ")) {
  188. bInhert = pika_true;
  189. goto __iter_continue;
  190. }
  191. if (strEqu(cs.token2.pyload, "[")) {
  192. /* (symble | iteral | <]> | <)>) + <[> */
  193. if (TOKEN_symbol == cs.token1.type ||
  194. TOKEN_literal == cs.token1.type ||
  195. strEqu(cs.token1.pyload, "]") ||
  196. strEqu(cs.token1.pyload, ")")) {
  197. /* keep the last one of the chain or slice */
  198. bSlice = pika_true;
  199. bChain = pika_false;
  200. goto __iter_continue;
  201. }
  202. /* ( <,> | <=> ) + <[> */
  203. bList = pika_true;
  204. }
  205. if (strEqu(cs.token1.pyload, "[") && cs.iter_index == 1) {
  206. /* VOID + <[> */
  207. bList = pika_true;
  208. bMethod = pika_false;
  209. goto __iter_continue;
  210. }
  211. if (strEqu(cs.token1.pyload, "...")) {
  212. goto __iter_continue;
  213. }
  214. if (strEqu(cs.token1.pyload, "pass")) {
  215. goto __iter_continue;
  216. }
  217. if (strIsStartWith(cs.token1.pyload, ".")) {
  218. if (cs.iter_index != 1) {
  219. /* keep the last one of the chain or slice */
  220. bChain = pika_true;
  221. bSlice = pika_false;
  222. goto __iter_continue;
  223. }
  224. }
  225. if (strEqu(cs.token1.pyload, "{")) {
  226. bDict = pika_true;
  227. goto __iter_continue;
  228. }
  229. if (cs.token1.type == TOKEN_operator) {
  230. bOperator = pika_true;
  231. goto __iter_continue;
  232. }
  233. /* <(> */
  234. if (strEqu(cs.token1.pyload, "(")) {
  235. bMethod = pika_true;
  236. bSlice = pika_false;
  237. goto __iter_continue;
  238. }
  239. if (cs.token1.type == TOKEN_literal) {
  240. if (cs.token1.pyload[0] == '\'' || cs.token1.pyload[0] == '"') {
  241. bString = pika_true;
  242. goto __iter_continue;
  243. }
  244. if (cs.token1.pyload[1] == '\'' || cs.token1.pyload[1] == '"') {
  245. if (cs.token1.pyload[0] == 'b') {
  246. bBytes = pika_true;
  247. goto __iter_continue;
  248. }
  249. }
  250. bNumber = pika_true;
  251. goto __iter_continue;
  252. }
  253. if (cs.token1.type == TOKEN_symbol) {
  254. bSymbol = pika_true;
  255. goto __iter_continue;
  256. }
  257. __iter_continue:
  258. Cursor_iterEnd(&cs);
  259. }
  260. if (bInhert) {
  261. eStmtType = STMT_inhert;
  262. goto __exit;
  263. }
  264. if (bImport) {
  265. eStmtType = STMT_import;
  266. goto __exit;
  267. }
  268. if (bOperator) {
  269. eStmtType = STMT_operator;
  270. goto __exit;
  271. }
  272. if (bSlice) {
  273. eStmtType = STMT_slice;
  274. goto __exit;
  275. }
  276. if (bChain) {
  277. eStmtType = STMT_chain;
  278. goto __exit;
  279. }
  280. if (bList) {
  281. eStmtType = STMT_list;
  282. goto __exit;
  283. }
  284. if (bDict) {
  285. eStmtType = STMT_dict;
  286. goto __exit;
  287. }
  288. if (bMethod) {
  289. eStmtType = STMT_method;
  290. goto __exit;
  291. }
  292. if (bString) {
  293. /* support multi assign */
  294. if (Cursor_isContain(right, TOKEN_devider, ",")) {
  295. eStmtType = STMT_tuple;
  296. goto __exit;
  297. }
  298. eStmtType = STMT_string;
  299. goto __exit;
  300. }
  301. if (bBytes) {
  302. /* support multi assign */
  303. if (Cursor_isContain(right, TOKEN_devider, ",")) {
  304. eStmtType = STMT_tuple;
  305. goto __exit;
  306. }
  307. eStmtType = STMT_bytes;
  308. goto __exit;
  309. }
  310. if (bNumber) {
  311. /* support multi assign */
  312. if (Cursor_isContain(right, TOKEN_devider, ",")) {
  313. eStmtType = STMT_tuple;
  314. goto __exit;
  315. }
  316. eStmtType = STMT_number;
  317. goto __exit;
  318. }
  319. if (bSymbol) {
  320. /* support multi assign */
  321. if (Cursor_isContain(right, TOKEN_devider, ",")) {
  322. eStmtType = STMT_tuple;
  323. goto __exit;
  324. }
  325. eStmtType = STMT_reference;
  326. goto __exit;
  327. }
  328. __exit:
  329. Cursor_deinit(&cs);
  330. strsDeinit(&buffs);
  331. return eStmtType;
  332. }
  333. char* Lexer_printTokenStream(Args* outBuffs, char* tokenStream) {
  334. pika_assert(tokenStream);
  335. /* init */
  336. Args buffs = {0};
  337. char* sPrintOut = strsCopy(&buffs, "");
  338. /* process */
  339. uint16_t uTokenSize = TokenStream_getSize(tokenStream);
  340. for (uint16_t i = 0; i < uTokenSize; i++) {
  341. char* sToken = TokenStream_pop(&buffs, &tokenStream);
  342. if (sToken[0] == TOKEN_operator) {
  343. sPrintOut = strsAppend(&buffs, sPrintOut, "{opt}");
  344. sPrintOut = strsAppend(&buffs, sPrintOut, sToken + 1);
  345. }
  346. if (sToken[0] == TOKEN_devider) {
  347. sPrintOut = strsAppend(&buffs, sPrintOut, "{dvd}");
  348. sPrintOut = strsAppend(&buffs, sPrintOut, sToken + 1);
  349. }
  350. if (sToken[0] == TOKEN_symbol) {
  351. sPrintOut = strsAppend(&buffs, sPrintOut, "{sym}");
  352. sPrintOut = strsAppend(&buffs, sPrintOut, sToken + 1);
  353. }
  354. if (sToken[0] == TOKEN_literal) {
  355. sPrintOut = strsAppend(&buffs, sPrintOut, "{lit}");
  356. sPrintOut = strsAppend(&buffs, sPrintOut, sToken + 1);
  357. }
  358. }
  359. /* out put */
  360. sPrintOut = strsCopy(outBuffs, sPrintOut);
  361. strsDeinit(&buffs);
  362. return sPrintOut;
  363. }
  364. uint8_t Parser_checkIsDirect(char* sStr) {
  365. Args buffs = {0};
  366. uint8_t uRes = 0;
  367. pika_assert(NULL != sStr);
  368. char* sLeft = Cursor_splitCollect(&buffs, sStr, "=", 1);
  369. if (!strEqu(sLeft, sStr)) {
  370. uRes = 1;
  371. goto __exit;
  372. }
  373. __exit:
  374. strsDeinit(&buffs);
  375. return uRes;
  376. }
  377. Arg* Lexer_setToken(Arg* tokenStream_arg,
  378. enum TokenType token_type,
  379. char*
  380. operator) {
  381. Args buffs = {0};
  382. char sTokenTypeBuff[3] = {0};
  383. sTokenTypeBuff[0] = 0x1F;
  384. sTokenTypeBuff[1] = token_type;
  385. char* sTokenStream = arg_getStr(tokenStream_arg);
  386. sTokenStream = strsAppend(&buffs, sTokenStream, sTokenTypeBuff);
  387. sTokenStream = strsAppend(&buffs, sTokenStream, operator);
  388. Arg* aNewTokenStream = arg_newStr(sTokenStream);
  389. arg_deinit(tokenStream_arg);
  390. strsDeinit(&buffs);
  391. return aNewTokenStream;
  392. }
  393. Arg* Lexer_setSymbel(Arg* aTokenStream,
  394. char* stmt,
  395. int32_t i,
  396. int32_t* iSymbolStartIndex_p) {
  397. Args buffs = {0};
  398. char* sSymbolBuff = NULL;
  399. if (-1 == *iSymbolStartIndex_p) {
  400. /* no found symbol start index */
  401. goto __exit;
  402. }
  403. /* nothing to add symbel */
  404. if (i == *iSymbolStartIndex_p) {
  405. goto __exit;
  406. }
  407. sSymbolBuff = args_getBuff(&buffs, i - *iSymbolStartIndex_p);
  408. pika_platform_memcpy(sSymbolBuff, stmt + *iSymbolStartIndex_p,
  409. i - *iSymbolStartIndex_p);
  410. /* literal */
  411. if ((sSymbolBuff[0] == '\'') || (sSymbolBuff[0] == '"')) {
  412. /* "" or '' */
  413. aTokenStream = Lexer_setToken(aTokenStream, TOKEN_literal, sSymbolBuff);
  414. goto __exit;
  415. }
  416. if ((sSymbolBuff[0] >= '0') && (sSymbolBuff[0] <= '9')) {
  417. /* number */
  418. aTokenStream = Lexer_setToken(aTokenStream, TOKEN_literal, sSymbolBuff);
  419. goto __exit;
  420. }
  421. if ((sSymbolBuff[0] == 'b') &&
  422. ((sSymbolBuff[1] == '\'') || (sSymbolBuff[1] == '"'))) {
  423. /* b"" or b'' */
  424. aTokenStream = Lexer_setToken(aTokenStream, TOKEN_literal, sSymbolBuff);
  425. goto __exit;
  426. }
  427. /* symbol */
  428. aTokenStream = Lexer_setToken(aTokenStream, TOKEN_symbol, sSymbolBuff);
  429. goto __exit;
  430. __exit:
  431. *iSymbolStartIndex_p = -1;
  432. strsDeinit(&buffs);
  433. return aTokenStream;
  434. }
  435. /* tokenStream is devided by space */
  436. /* a token is [TOKENTYPE|(CONTENT)] */
  437. char* Lexer_getTokenStream(Args* outBuffs, char* sStmt) {
  438. /* init */
  439. Arg* aTokenStream = New_arg(NULL);
  440. aTokenStream = arg_setStr(aTokenStream, "", "");
  441. int32_t iSize = strGetSize(sStmt);
  442. uint8_t uBracketDeepth = 0;
  443. uint8_t cn2 = 0;
  444. uint8_t cn1 = 0;
  445. uint8_t c0 = 0;
  446. uint8_t c1 = 0;
  447. uint8_t c2 = 0;
  448. uint8_t c3 = 0;
  449. uint8_t c4 = 0;
  450. uint8_t c5 = 0;
  451. uint8_t c6 = 0;
  452. int32_t iSymbolStartIndex = -1;
  453. pika_bool bInString = 0;
  454. pika_bool bNumber = 0;
  455. char* sTokenStream;
  456. /* process */
  457. for (int32_t i = 0; i < iSize; i++) {
  458. /* update char */
  459. cn2 = 0;
  460. cn1 = 0;
  461. c0 = sStmt[i];
  462. c1 = 0;
  463. c2 = 0;
  464. c3 = 0;
  465. c4 = 0;
  466. c5 = 0;
  467. c6 = 0;
  468. if (i - 2 >= 0) {
  469. cn2 = sStmt[i - 2];
  470. }
  471. if (i - 1 >= 0) {
  472. cn1 = sStmt[i - 1];
  473. }
  474. if (i + 1 < iSize) {
  475. c1 = sStmt[i + 1];
  476. }
  477. if (i + 2 < iSize) {
  478. c2 = sStmt[i + 2];
  479. }
  480. if (i + 3 < iSize) {
  481. c3 = sStmt[i + 3];
  482. }
  483. if (i + 4 < iSize) {
  484. c4 = sStmt[i + 4];
  485. }
  486. if (i + 5 < iSize) {
  487. c5 = sStmt[i + 5];
  488. }
  489. if (i + 6 < iSize) {
  490. c6 = sStmt[i + 6];
  491. }
  492. if (-1 == iSymbolStartIndex) {
  493. bNumber = 0;
  494. if ((c0 >= '0') && (c0 <= '9')) {
  495. bNumber = 1;
  496. }
  497. iSymbolStartIndex = i;
  498. }
  499. /* solve string */
  500. if (0 == bInString) {
  501. if ('\'' == c0) {
  502. if ('\\' != cn1 || ('\\' == cn1 && '\\' == cn2)) {
  503. /* in ' */
  504. bInString = 1;
  505. continue;
  506. }
  507. }
  508. if ('"' == c0) {
  509. if ('\\' != cn1 || ('\\' == cn1 && '\\' == cn2)) {
  510. /* in "" */
  511. bInString = 2;
  512. continue;
  513. }
  514. }
  515. }
  516. if (1 == bInString) {
  517. if ('\'' == c0 && ('\\' != cn1 || ('\\' == cn1 && '\\' == cn2))) {
  518. bInString = 0;
  519. aTokenStream = Lexer_setSymbel(aTokenStream, sStmt, i + 1,
  520. &iSymbolStartIndex);
  521. }
  522. continue;
  523. }
  524. if (2 == bInString) {
  525. if ('"' == c0 && ('\\' != cn1 || ('\\' == cn1 && '\\' == cn2))) {
  526. bInString = 0;
  527. aTokenStream = Lexer_setSymbel(aTokenStream, sStmt, i + 1,
  528. &iSymbolStartIndex);
  529. }
  530. continue;
  531. }
  532. /* match annotation */
  533. if ('#' == c0) {
  534. break;
  535. }
  536. /* match devider*/
  537. if (('(' == c0) || (')' == c0) || (',' == c0) || ('[' == c0) ||
  538. (']' == c0) || (':' == c0) || ('{' == c0) || ('}' == c0) ||
  539. (';' == c0)) {
  540. aTokenStream =
  541. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  542. char sContent[2] = {0};
  543. sContent[0] = c0;
  544. aTokenStream =
  545. Lexer_setToken(aTokenStream, TOKEN_devider, sContent);
  546. if (c0 == '(') {
  547. uBracketDeepth++;
  548. }
  549. if (c0 == ')') {
  550. uBracketDeepth--;
  551. }
  552. continue;
  553. }
  554. /* match operator */
  555. if (('>' == c0) || ('<' == c0) || ('*' == c0) || ('/' == c0) ||
  556. ('+' == c0) || ('-' == c0) || ('!' == c0) || ('=' == c0) ||
  557. ('%' == c0) || ('&' == c0) || ('|' == c0) || ('^' == c0) ||
  558. ('~' == c0)) {
  559. if ('-' == c0 && bNumber) {
  560. if ((cn1 == 'e') || (cn1 == 'E')) {
  561. continue;
  562. }
  563. }
  564. if (('*' == c0) || ('/' == c0)) {
  565. /*
  566. =, **=, //
  567. */
  568. if ((c0 == c1) && ('=' == c2)) {
  569. char sContent[4] = {0};
  570. sContent[0] = c0;
  571. sContent[1] = c1;
  572. sContent[2] = '=';
  573. aTokenStream = Lexer_setSymbel(aTokenStream, sStmt, i,
  574. &iSymbolStartIndex);
  575. aTokenStream =
  576. Lexer_setToken(aTokenStream, TOKEN_operator, sContent);
  577. i = i + 2;
  578. continue;
  579. }
  580. }
  581. /*
  582. >>, <<, **, //
  583. */
  584. if (('>' == c0) || ('<' == c0) || ('*' == c0) || ('/' == c0)) {
  585. if (c0 == c1) {
  586. /* >>=, <<=, **=, //= */
  587. if ('=' == c2) {
  588. char sContent[4] = {0};
  589. sContent[0] = c0;
  590. sContent[1] = c1;
  591. sContent[2] = '=';
  592. aTokenStream = Lexer_setSymbel(aTokenStream, sStmt, i,
  593. &iSymbolStartIndex);
  594. aTokenStream = Lexer_setToken(aTokenStream,
  595. TOKEN_operator, sContent);
  596. i = i + 2;
  597. continue;
  598. }
  599. char content[3] = {0};
  600. content[0] = c0;
  601. content[1] = c1;
  602. aTokenStream = Lexer_setSymbel(aTokenStream, sStmt, i,
  603. &iSymbolStartIndex);
  604. aTokenStream =
  605. Lexer_setToken(aTokenStream, TOKEN_operator, content);
  606. i = i + 1;
  607. continue;
  608. }
  609. }
  610. /*
  611. >=, <=, *=, /=, +=, -=, !=, ==, %=, |=, ^=, &=
  612. */
  613. if (('>' == c0) || ('<' == c0) || ('*' == c0) || ('/' == c0) ||
  614. ('+' == c0) || ('-' == c0) || ('!' == c0) || ('=' == c0) ||
  615. ('%' == c0) || ('|' == c0) || ('&' == c0) || ('^' == c0)) {
  616. if ('=' == c1) {
  617. char content[3] = {0};
  618. content[0] = c0;
  619. content[1] = c1;
  620. aTokenStream = Lexer_setSymbel(aTokenStream, sStmt, i,
  621. &iSymbolStartIndex);
  622. aTokenStream =
  623. Lexer_setToken(aTokenStream, TOKEN_operator, content);
  624. i = i + 1;
  625. continue;
  626. }
  627. }
  628. /* single operator */
  629. /*
  630. +, -, *, ... /
  631. */
  632. char sContent[2] = {0};
  633. sContent[0] = c0;
  634. aTokenStream =
  635. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  636. aTokenStream =
  637. Lexer_setToken(aTokenStream, TOKEN_operator, sContent);
  638. continue;
  639. }
  640. // not the string operator
  641. if ((cn1 >= 'a' && cn1 <= 'z') || (cn1 >= 'A' && cn1 <= 'Z') ||
  642. (cn1 >= '0' && cn1 <= '9') || cn1 == '_' || cn1 == '.') {
  643. goto __after_match_string_operator;
  644. }
  645. /* not */
  646. if ('n' == c0) {
  647. if (('o' == c1) && ('t' == c2) && (' ' == c3)) {
  648. aTokenStream =
  649. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  650. aTokenStream =
  651. Lexer_setToken(aTokenStream, TOKEN_operator, " not ");
  652. i = i + 3;
  653. continue;
  654. }
  655. }
  656. /* and */
  657. if ('a' == c0) {
  658. if (('n' == c1) && ('d' == c2) && (' ' == c3)) {
  659. aTokenStream =
  660. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  661. aTokenStream =
  662. Lexer_setToken(aTokenStream, TOKEN_operator, " and ");
  663. i = i + 3;
  664. continue;
  665. }
  666. }
  667. /* or */
  668. if ('o' == c0) {
  669. if (('r' == c1) && (' ' == c2)) {
  670. aTokenStream =
  671. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  672. aTokenStream =
  673. Lexer_setToken(aTokenStream, TOKEN_operator, " or ");
  674. i = i + 2;
  675. continue;
  676. }
  677. }
  678. /* is */
  679. if ('i' == c0) {
  680. if (('s' == c1) && (' ' == c2)) {
  681. aTokenStream =
  682. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  683. aTokenStream =
  684. Lexer_setToken(aTokenStream, TOKEN_operator, " is ");
  685. i = i + 2;
  686. continue;
  687. }
  688. }
  689. /* in */
  690. if ('i' == c0) {
  691. if (('n' == c1) && (' ' == c2)) {
  692. aTokenStream =
  693. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  694. aTokenStream =
  695. Lexer_setToken(aTokenStream, TOKEN_operator, " in ");
  696. i = i + 2;
  697. continue;
  698. }
  699. }
  700. /* if */
  701. if ('i' == c0) {
  702. if (('f' == c1) && (' ' == c2)) {
  703. aTokenStream =
  704. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  705. aTokenStream =
  706. Lexer_setToken(aTokenStream, TOKEN_keyword, " if ");
  707. i = i + 2;
  708. continue;
  709. }
  710. }
  711. /* as */
  712. if ('a' == c0) {
  713. if (('s' == c1) && (' ' == c2)) {
  714. aTokenStream =
  715. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  716. aTokenStream =
  717. Lexer_setToken(aTokenStream, TOKEN_operator, " as ");
  718. i = i + 2;
  719. continue;
  720. }
  721. }
  722. /* for */
  723. if ('f' == c0) {
  724. if (('o' == c1) && ('r' == c2) && (' ' == c3)) {
  725. aTokenStream =
  726. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  727. aTokenStream =
  728. Lexer_setToken(aTokenStream, TOKEN_keyword, " for ");
  729. i = i + 3;
  730. continue;
  731. }
  732. }
  733. /* import */
  734. if ('i' == c0) {
  735. if (('m' == c1) && ('p' == c2) && ('o' == c3) && ('r' == c4) &&
  736. ('t' == c5) && (' ' == c6)) {
  737. aTokenStream =
  738. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  739. aTokenStream =
  740. Lexer_setToken(aTokenStream, TOKEN_operator, " import ");
  741. i = i + 5;
  742. continue;
  743. }
  744. }
  745. /* @inh */
  746. if ('@' == c0) {
  747. if (('i' == c1) && ('n' == c2) && ('h' == c3) && (' ' == c4)) {
  748. aTokenStream =
  749. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  750. aTokenStream =
  751. Lexer_setToken(aTokenStream, TOKEN_operator, "@inh ");
  752. i = i + 3;
  753. continue;
  754. }
  755. }
  756. __after_match_string_operator:
  757. /* skip spaces */
  758. if (' ' == c0) {
  759. /* not get symbal */
  760. if (i == iSymbolStartIndex) {
  761. iSymbolStartIndex = -1;
  762. } else {
  763. /* already get symbal */
  764. aTokenStream =
  765. Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
  766. }
  767. }
  768. if (i == iSize - 1) {
  769. /* last check symbel */
  770. // if('\n' == c0){
  771. // continue;
  772. // }
  773. aTokenStream =
  774. Lexer_setSymbel(aTokenStream, sStmt, iSize, &iSymbolStartIndex);
  775. }
  776. }
  777. /* output */
  778. sTokenStream = arg_getStr(aTokenStream);
  779. sTokenStream = strsCopy(outBuffs, sTokenStream);
  780. arg_deinit(aTokenStream);
  781. return sTokenStream;
  782. }
  783. char* TokenStream_pop(Args* buffs, char** sTokenStream_pp) {
  784. return strsPopToken(buffs, sTokenStream_pp, 0x1F);
  785. }
  786. enum TokenType Token_getType(char* sToken) {
  787. return (enum TokenType)sToken[0];
  788. }
  789. char* Token_getPyload(char* sToken) {
  790. return (char*)((uintptr_t)sToken + 1);
  791. }
  792. uint8_t TokenStream_count(char* sTokenStream,
  793. enum TokenType eTokenType,
  794. char* sPyload) {
  795. Args buffs = {0};
  796. char* sTokenStreamBuff = strsCopy(&buffs, sTokenStream);
  797. uint8_t uRes = 0;
  798. uint16_t uTokenSize = TokenStream_getSize(sTokenStream);
  799. for (int i = 0; i < uTokenSize; i++) {
  800. char* sToken = TokenStream_pop(&buffs, &sTokenStreamBuff);
  801. if (eTokenType == Token_getType(sToken)) {
  802. if (strEqu(Token_getPyload(sToken), sPyload)) {
  803. uRes++;
  804. }
  805. }
  806. }
  807. strsDeinit(&buffs);
  808. return uRes;
  809. }
  810. uint8_t TokenStream_isContain(char* sTokenStream,
  811. enum TokenType eTokenType,
  812. char* sPyload) {
  813. if (TokenStream_count(sTokenStream, eTokenType, sPyload) > 0) {
  814. return 1;
  815. }
  816. return 0;
  817. }
  818. static char* _solveEqualLevelOperator(Args* buffs,
  819. char* sOperator,
  820. char* sOp1,
  821. char* sOp2,
  822. char* sStmt) {
  823. if ((strEqu(sOperator, sOp1)) || (strEqu(sOperator, sOp2))) {
  824. Cursor_forEach(cs, sStmt) {
  825. Cursor_iterStart(&cs);
  826. if (strEqu(cs.token1.pyload, sOp1)) {
  827. sOperator = strsCopy(buffs, sOp1);
  828. }
  829. if (strEqu(cs.token1.pyload, sOp2)) {
  830. sOperator = strsCopy(buffs, sOp2);
  831. }
  832. Cursor_iterEnd(&cs);
  833. }
  834. Cursor_deinit(&cs);
  835. }
  836. return sOperator;
  837. }
  838. static const char operators[][9] = {
  839. "**", "~", "*", "/", "%", "//", "+", "-", ">>",
  840. "<<", "&", "^", "|", "<", "<=", ">", ">=", "!=",
  841. "==", " is ", " in ", "%=", "/=", "//=", "-=", "+=", "*=",
  842. "**=", "^=", " not ", " and ", " or ", " import "};
  843. char* Lexer_getOperator(Args* outBuffs, char* sStmt) {
  844. Args buffs = {0};
  845. char* sOperator = NULL;
  846. // use parse state foreach to get operator
  847. for (uint32_t i = 0; i < sizeof(operators) / 9; i++) {
  848. Cursor_forEach(cs, sStmt) {
  849. Cursor_iterStart(&cs);
  850. // get operator
  851. if (strEqu(cs.token1.pyload, (char*)operators[i])) {
  852. // solve the iuuse of "~-1"
  853. sOperator = strsCopy(&buffs, (char*)operators[i]);
  854. Cursor_iterEnd(&cs);
  855. break;
  856. }
  857. Cursor_iterEnd(&cs);
  858. };
  859. Cursor_deinit(&cs);
  860. }
  861. /* solve the iuuse of "~-1" */
  862. if (strEqu(sOperator, "-")) {
  863. Cursor_forEach(cs, sStmt) {
  864. Cursor_iterStart(&cs);
  865. if (strEqu(cs.token2.pyload, "-")) {
  866. if (cs.token1.type == TOKEN_operator) {
  867. sOperator = strsCopy(&buffs, cs.token1.pyload);
  868. Cursor_iterEnd(&cs);
  869. break;
  870. }
  871. }
  872. Cursor_iterEnd(&cs);
  873. };
  874. Cursor_deinit(&cs);
  875. }
  876. /* match the last operator in equal level */
  877. sOperator = _solveEqualLevelOperator(&buffs, sOperator, "+", "-", sStmt);
  878. sOperator = _solveEqualLevelOperator(&buffs, sOperator, "*", "/", sStmt);
  879. /* out put */
  880. if (NULL == sOperator) {
  881. return NULL;
  882. }
  883. sOperator = strsCopy(outBuffs, sOperator);
  884. strsDeinit(&buffs);
  885. return sOperator;
  886. }
  887. const char void_str[] = "";
  888. void LexToken_update(struct LexToken* lex_token) {
  889. lex_token->type = Token_getType(lex_token->tokenStream);
  890. if (lex_token->type == TOKEN_strEnd) {
  891. lex_token->pyload = (char*)void_str;
  892. } else {
  893. lex_token->pyload = Token_getPyload(lex_token->tokenStream);
  894. }
  895. }
  896. void Cursor_iterStart(struct Cursor* cs) {
  897. cs->iter_index++;
  898. cs->iter_buffs = New_strBuff();
  899. /* token1 is the last token */
  900. cs->token1.tokenStream =
  901. strsCopy(cs->iter_buffs, arg_getStr(cs->last_token));
  902. /* token2 is the next token */
  903. cs->token2.tokenStream = TokenStream_pop(cs->iter_buffs, &cs->tokenStream);
  904. /* store last token */
  905. arg_deinit(cs->last_token);
  906. cs->last_token = arg_newStr(cs->token2.tokenStream);
  907. LexToken_update(&cs->token1);
  908. LexToken_update(&cs->token2);
  909. if (strEqu(cs->token1.pyload, "(")) {
  910. cs->bracket_deepth++;
  911. }
  912. if (strEqu(cs->token1.pyload, ")")) {
  913. cs->bracket_deepth--;
  914. }
  915. if (strEqu(cs->token1.pyload, "[")) {
  916. cs->bracket_deepth++;
  917. }
  918. if (strEqu(cs->token1.pyload, "]")) {
  919. cs->bracket_deepth--;
  920. }
  921. if (strEqu(cs->token1.pyload, "{")) {
  922. cs->bracket_deepth++;
  923. }
  924. if (strEqu(cs->token1.pyload, "}")) {
  925. cs->bracket_deepth--;
  926. }
  927. }
  928. void LexToken_init(struct LexToken* lt) {
  929. lt->pyload = NULL;
  930. lt->tokenStream = NULL;
  931. lt->type = TOKEN_strEnd;
  932. }
  933. void _Cursor_init(struct Cursor* cs) {
  934. cs->tokenStream = NULL;
  935. cs->length = 0;
  936. cs->iter_index = 0;
  937. cs->bracket_deepth = 0;
  938. cs->last_token = NULL;
  939. cs->iter_buffs = NULL;
  940. cs->buffs_p = New_strBuff();
  941. cs->result = PIKA_RES_OK;
  942. LexToken_init(&cs->token1);
  943. LexToken_init(&cs->token2);
  944. }
  945. void Cursor_iterEnd(struct Cursor* cs) {
  946. args_deinit(cs->iter_buffs);
  947. }
  948. void Cursor_deinit(struct Cursor* cs) {
  949. if (NULL != cs->last_token) {
  950. arg_deinit(cs->last_token);
  951. }
  952. args_deinit(cs->buffs_p);
  953. }
  954. void _Cursor_parse(struct Cursor* cs, char* stmt) {
  955. if (NULL == stmt) {
  956. cs->result = PIKA_RES_ERR_SYNTAX_ERROR;
  957. return;
  958. }
  959. cs->tokenStream = Lexer_getTokenStream(cs->buffs_p, stmt);
  960. if (NULL == cs->tokenStream) {
  961. cs->result = PIKA_RES_ERR_SYNTAX_ERROR;
  962. return;
  963. }
  964. cs->length = TokenStream_getSize(cs->tokenStream);
  965. }
  966. void _Cursor_beforeIter(struct Cursor* cs) {
  967. /* clear first token */
  968. if (cs->result != PIKA_RES_OK) {
  969. return;
  970. }
  971. TokenStream_pop(cs->buffs_p, &cs->tokenStream);
  972. cs->last_token = arg_newStr(TokenStream_pop(cs->buffs_p, &cs->tokenStream));
  973. }
  974. uint8_t Token_isBranketStart(LexToken* token) {
  975. if (token->type != TOKEN_devider) {
  976. return pika_false;
  977. }
  978. if (strEqu(token->pyload, "(") || strEqu(token->pyload, "[") ||
  979. strEqu(token->pyload, "{")) {
  980. return pika_true;
  981. }
  982. return pika_false;
  983. }
  984. uint8_t Token_isBranketEnd(LexToken* token) {
  985. if (token->type != TOKEN_devider) {
  986. return pika_false;
  987. }
  988. if (strEqu(token->pyload, ")") || strEqu(token->pyload, "]") ||
  989. strEqu(token->pyload, "}")) {
  990. return pika_true;
  991. }
  992. return pika_false;
  993. }
  994. uint8_t Token_isBranket(LexToken* token) {
  995. return Token_isBranketStart(token) || Token_isBranketEnd(token);
  996. }
  997. uint8_t _Cursor_count(char* sStmt,
  998. TokenType eType,
  999. char* sPyload,
  1000. pika_bool bSkipbracket) {
  1001. /* fast return */
  1002. if (!strstr(sStmt, sPyload)) {
  1003. return pika_false;
  1004. }
  1005. Args buffs = {0};
  1006. uint8_t uRes = 0;
  1007. Cursor_forEach(cs, sStmt) {
  1008. Cursor_iterStart(&cs);
  1009. if (cs.token1.type == eType && (strEqu(cs.token1.pyload, sPyload))) {
  1010. if (bSkipbracket) {
  1011. uint8_t branket_deepth_check = 0;
  1012. if (Token_isBranketStart(&cs.token1)) {
  1013. branket_deepth_check = 1;
  1014. }
  1015. if (cs.bracket_deepth > branket_deepth_check) {
  1016. /* skip bracket */
  1017. Cursor_iterEnd(&cs);
  1018. continue;
  1019. }
  1020. }
  1021. uRes++;
  1022. }
  1023. Cursor_iterEnd(&cs);
  1024. };
  1025. Cursor_deinit(&cs);
  1026. strsDeinit(&buffs);
  1027. return uRes;
  1028. }
  1029. uint8_t Cursor_count(char* sStmt, TokenType eType, char* sPyload) {
  1030. return _Cursor_count(sStmt, eType, sPyload, pika_false);
  1031. }
  1032. pika_bool Cursor_isContain(char* sStmt, TokenType eType, char* sPyload) {
  1033. if (Cursor_count(sStmt, eType, sPyload) > 0) {
  1034. return pika_true;
  1035. }
  1036. return pika_false;
  1037. }
  1038. char* Cursor_popToken(Args* buffs, char** sStmt_p, char* sDevide) {
  1039. Arg* aOutitem = arg_newStr("");
  1040. Arg* aTokenStreamAfter = arg_newStr("");
  1041. pika_bool bFindDevide = pika_false;
  1042. Cursor_forEach(cs, *sStmt_p) {
  1043. Cursor_iterStart(&cs);
  1044. if (!bFindDevide) {
  1045. if ((cs.bracket_deepth == 0 && strEqu(cs.token1.pyload, sDevide)) ||
  1046. cs.iter_index == cs.length) {
  1047. bFindDevide = pika_true;
  1048. Cursor_iterEnd(&cs);
  1049. continue;
  1050. }
  1051. }
  1052. if (!bFindDevide) {
  1053. aOutitem = arg_strAppend(aOutitem, cs.token1.pyload);
  1054. } else {
  1055. aTokenStreamAfter =
  1056. arg_strAppend(aTokenStreamAfter, cs.token1.pyload);
  1057. }
  1058. Cursor_iterEnd(&cs);
  1059. }
  1060. Cursor_deinit(&cs);
  1061. /* cache out item */
  1062. char* sOutItem = strsCopy(buffs, arg_getStr(aOutitem));
  1063. arg_deinit(aOutitem);
  1064. /* cache tokenStream after */
  1065. char* sTokenAfter = strsCopy(buffs, arg_getStr(aTokenStreamAfter));
  1066. arg_deinit(aTokenStreamAfter);
  1067. /* update tokenStream */
  1068. *sStmt_p = sTokenAfter;
  1069. return sOutItem;
  1070. }
  1071. char* Cursor_splitCollect(Args* buffs, char* sStmt, char* sDevide, int index) {
  1072. Arg* aOut = arg_newStr("");
  1073. int iExpectBracket = 0;
  1074. if (sDevide[0] == '(' || sDevide[0] == '[' || sDevide[0] == '{') {
  1075. iExpectBracket = 1;
  1076. }
  1077. int i = 0;
  1078. Cursor_forEach(cs, sStmt) {
  1079. Cursor_iterStart(&cs);
  1080. if (cs.bracket_deepth == iExpectBracket &&
  1081. strEqu(cs.token1.pyload, sDevide)) {
  1082. i++;
  1083. Cursor_iterEnd(&cs);
  1084. continue;
  1085. }
  1086. if (i == index) {
  1087. aOut = arg_strAppend(aOut, cs.token1.pyload);
  1088. }
  1089. Cursor_iterEnd(&cs);
  1090. }
  1091. Cursor_deinit(&cs);
  1092. /* if not found, return origin string */
  1093. if (i == 0) {
  1094. arg_deinit(aOut);
  1095. aOut = arg_newStr(sStmt);
  1096. }
  1097. return strsCacheArg(buffs, aOut);
  1098. }
  1099. static void Slice_getPars(Args* outBuffs,
  1100. char* sInner,
  1101. char** sStart_p,
  1102. char** sEnd_p,
  1103. char** sStep_p) {
  1104. #if PIKA_NANO_ENABLE
  1105. return;
  1106. #endif
  1107. Args buffs = {0};
  1108. *sStart_p = "";
  1109. *sEnd_p = "";
  1110. *sStep_p = "";
  1111. /* slice */
  1112. uint8_t uColonIndex = 0;
  1113. Cursor_forEach(cs, sInner) {
  1114. Cursor_iterStart(&cs);
  1115. if (strEqu(cs.token1.pyload, ":") && cs.bracket_deepth == 0) {
  1116. uColonIndex++;
  1117. goto __iter_continue1;
  1118. }
  1119. if (uColonIndex == 0) {
  1120. *sStart_p = strsAppend(&buffs, *sStart_p, cs.token1.pyload);
  1121. }
  1122. if (uColonIndex == 1) {
  1123. *sEnd_p = strsAppend(&buffs, *sEnd_p, cs.token1.pyload);
  1124. }
  1125. if (uColonIndex == 2) {
  1126. *sStep_p = strsAppend(&buffs, *sStep_p, cs.token1.pyload);
  1127. }
  1128. __iter_continue1:
  1129. Cursor_iterEnd(&cs);
  1130. }
  1131. Cursor_deinit(&cs);
  1132. if (uColonIndex == 1) {
  1133. *sStep_p = "1";
  1134. if (strEqu(*sStart_p, "")) {
  1135. *sStart_p = "0";
  1136. }
  1137. if (strEqu(*sEnd_p, "")) {
  1138. *sEnd_p = "-1";
  1139. }
  1140. }
  1141. if (uColonIndex == 0) {
  1142. *sEnd_p = strsAppend(&buffs, *sStart_p, " + 1");
  1143. *sStep_p = "1";
  1144. }
  1145. /* slice with step */
  1146. /* output */
  1147. *sStart_p = strsCopy(outBuffs, *sStart_p);
  1148. *sEnd_p = strsCopy(outBuffs, *sEnd_p);
  1149. *sStep_p = strsCopy(outBuffs, *sStep_p);
  1150. /* clean */
  1151. strsDeinit(&buffs);
  1152. }
  1153. char* Suger_leftSlice(Args* outBuffs, char* sRight, char** sLeft_p) {
  1154. #if !PIKA_SYNTAX_SLICE_ENABLE
  1155. return sRight;
  1156. #endif
  1157. /* init objects */
  1158. Args buffs = {0};
  1159. Arg* aRight = arg_newStr("");
  1160. char* sLeft = *sLeft_p;
  1161. pika_bool bInBrancket = 0;
  1162. args_setStr(&buffs, "inner", "");
  1163. pika_bool bMatched = 0;
  1164. char* sRightRes = NULL;
  1165. /* exit when NULL */
  1166. if (NULL == sLeft) {
  1167. arg_deinit(aRight);
  1168. aRight = arg_newStr(sRight);
  1169. goto __exit;
  1170. }
  1171. /* exit when not match
  1172. (symble|iteral)'['
  1173. */
  1174. Cursor_forEach(cs, sLeft) {
  1175. Cursor_iterStart(&cs);
  1176. if (strEqu(cs.token2.pyload, "[")) {
  1177. if (TOKEN_symbol == cs.token1.type ||
  1178. TOKEN_literal == cs.token1.type) {
  1179. bMatched = 1;
  1180. Cursor_iterEnd(&cs);
  1181. break;
  1182. }
  1183. }
  1184. Cursor_iterEnd(&cs);
  1185. }
  1186. Cursor_deinit(&cs);
  1187. if (!bMatched) {
  1188. /* not contain '[', return origin */
  1189. arg_deinit(aRight);
  1190. aRight = arg_newStr(sRight);
  1191. goto __exit;
  1192. }
  1193. /* matched [] */
  1194. Cursor_forEachExistPs(cs, sLeft) {
  1195. Cursor_iterStart(&cs);
  1196. /* found '[' */
  1197. if ((TOKEN_devider == cs.token2.type) &&
  1198. (strEqu(cs.token2.pyload, "["))) {
  1199. /* get 'obj' from obj[] */
  1200. args_setStr(&buffs, "obj", cs.token1.pyload);
  1201. bInBrancket = 1;
  1202. /* fond ']' */
  1203. } else if ((TOKEN_devider == cs.token2.type) &&
  1204. (strEqu(cs.token2.pyload, "]"))) {
  1205. bInBrancket = 0;
  1206. char* sInner = args_getStr(&buffs, "inner");
  1207. Arg* aInner = arg_newStr(sInner);
  1208. aInner = arg_strAppend(aInner, cs.token1.pyload);
  1209. args_setStr(&buffs, "inner", arg_getStr(aInner));
  1210. arg_deinit(aInner);
  1211. /* update inner pointer */
  1212. sInner = args_getStr(&buffs, "inner");
  1213. char* sStart = NULL;
  1214. char* sEnd = NULL;
  1215. char* sStep = NULL;
  1216. Slice_getPars(&buffs, sInner, &sStart, &sEnd, &sStep);
  1217. /* obj = __setitem__(obj, key, val) */
  1218. aRight = arg_strAppend(aRight, "__setitem__(");
  1219. aRight = arg_strAppend(aRight, args_getStr(&buffs, "obj"));
  1220. aRight = arg_strAppend(aRight, ",");
  1221. aRight = arg_strAppend(aRight, sStart);
  1222. aRight = arg_strAppend(aRight, ",");
  1223. aRight = arg_strAppend(aRight, sRight);
  1224. aRight = arg_strAppend(aRight, ")");
  1225. /* clean the inner */
  1226. args_setStr(&buffs, "inner", "");
  1227. /* in brancket and found '[' */
  1228. } else if (bInBrancket && (!strEqu(cs.token1.pyload, "["))) {
  1229. char* sInner = args_getStr(&buffs, "inner");
  1230. Arg* aIndex = arg_newStr(sInner);
  1231. aIndex = arg_strAppend(aIndex, cs.token1.pyload);
  1232. args_setStr(&buffs, "inner", arg_getStr(aIndex));
  1233. arg_deinit(aIndex);
  1234. /* out of brancket and not found ']' */
  1235. } else if (!bInBrancket && (!strEqu(cs.token1.pyload, "]"))) {
  1236. if (TOKEN_strEnd != cs.token1.type) {
  1237. aRight = arg_strAppend(aRight, cs.token1.pyload);
  1238. }
  1239. }
  1240. Cursor_iterEnd(&cs);
  1241. }
  1242. Cursor_deinit(&cs);
  1243. /* clean the left */
  1244. for (size_t i = 0; i < strGetSize(sLeft); i++) {
  1245. if (sLeft[i] == '[') {
  1246. sLeft[i] = '\0';
  1247. break;
  1248. }
  1249. }
  1250. __exit:
  1251. /* clean and return */
  1252. sRightRes = strsCopy(outBuffs, arg_getStr(aRight));
  1253. arg_deinit(aRight);
  1254. strsDeinit(&buffs);
  1255. return sRightRes;
  1256. }
  1257. char* Suger_format(Args* outBuffs, char* sRight) {
  1258. #if !PIKA_SYNTAX_FORMAT_ENABLE
  1259. return sRight;
  1260. #endif
  1261. /* quick skip */
  1262. if (!strIsContain(sRight, '%')) {
  1263. return sRight;
  1264. }
  1265. pika_bool bFormat = pika_false;
  1266. Cursor_forEach(ps1, sRight) {
  1267. Cursor_iterStart(&ps1);
  1268. if (ps1.bracket_deepth == 0 && strEqu(ps1.token1.pyload, "%")) {
  1269. bFormat = pika_true;
  1270. }
  1271. Cursor_iterEnd(&ps1);
  1272. }
  1273. Cursor_deinit(&ps1);
  1274. if (pika_false == bFormat) {
  1275. return sRight;
  1276. }
  1277. char* sRes = sRight;
  1278. Arg* aStrBuf = arg_newStr("");
  1279. Arg* aVarBuf = arg_newStr("");
  1280. pika_bool bInFormat = pika_false;
  1281. pika_bool bTuple = pika_false;
  1282. pika_bool bOutVars = pika_false;
  1283. Args buffs = {0};
  1284. char* sFmt = NULL;
  1285. Cursor_forEach(cs, sRight) {
  1286. char* sItem = "";
  1287. Cursor_iterStart(&cs);
  1288. if (pika_false == bInFormat) {
  1289. if (cs.token1.type != TOKEN_literal) {
  1290. sItem = cs.token1.pyload;
  1291. goto __iter_continue;
  1292. }
  1293. if (cs.token1.pyload[0] != '\'' && cs.token1.pyload[0] != '"') {
  1294. sItem = cs.token1.pyload;
  1295. goto __iter_continue;
  1296. }
  1297. if (!strEqu(cs.token2.pyload, "%")) {
  1298. sItem = cs.token1.pyload;
  1299. goto __iter_continue;
  1300. }
  1301. /* found the format stmt */
  1302. bInFormat = pika_true;
  1303. sFmt = strsCopy(&buffs, cs.token1.pyload);
  1304. goto __iter_continue;
  1305. }
  1306. if (pika_true == bInFormat) {
  1307. /* check the format vars */
  1308. if (strEqu(cs.token1.pyload, "%")) {
  1309. /* is a tuple */
  1310. if (strEqu(cs.token2.pyload, "(")) {
  1311. bTuple = pika_true;
  1312. } else {
  1313. aVarBuf = arg_strAppend(aVarBuf, cs.token2.pyload);
  1314. }
  1315. goto __iter_continue;
  1316. }
  1317. /* found the end of tuple */
  1318. if (cs.iter_index == cs.length) {
  1319. bOutVars = pika_true;
  1320. bInFormat = pika_false;
  1321. } else {
  1322. /* push the vars inner the tuple */
  1323. aVarBuf = arg_strAppend(aVarBuf, cs.token2.pyload);
  1324. }
  1325. if (bOutVars) {
  1326. if (bTuple) {
  1327. aStrBuf = arg_strAppend(aStrBuf, "cformat(");
  1328. aStrBuf = arg_strAppend(aStrBuf, sFmt);
  1329. aStrBuf = arg_strAppend(aStrBuf, ",");
  1330. aStrBuf = arg_strAppend(aStrBuf, arg_getStr(aVarBuf));
  1331. } else {
  1332. aStrBuf = arg_strAppend(aStrBuf, "cformat(");
  1333. aStrBuf = arg_strAppend(aStrBuf, sFmt);
  1334. aStrBuf = arg_strAppend(aStrBuf, ",");
  1335. aStrBuf = arg_strAppend(aStrBuf, arg_getStr(aVarBuf));
  1336. aStrBuf = arg_strAppend(aStrBuf, ")");
  1337. }
  1338. }
  1339. }
  1340. __iter_continue:
  1341. if (!bInFormat) {
  1342. aStrBuf = arg_strAppend(aStrBuf, sItem);
  1343. }
  1344. Cursor_iterEnd(&cs);
  1345. }
  1346. Cursor_deinit(&cs);
  1347. sRes = strsCopy(outBuffs, arg_getStr(aStrBuf));
  1348. arg_deinit(aStrBuf);
  1349. arg_deinit(aVarBuf);
  1350. strsDeinit(&buffs);
  1351. return sRes;
  1352. }
  1353. #define SELF_OPERATORES_LEN 4
  1354. static const char selfOperators[][SELF_OPERATORES_LEN] = {
  1355. "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=", "**=", "//="};
  1356. uint8_t Suger_selfOperator(Args* outbuffs,
  1357. char* sStmt,
  1358. char** sRight_p,
  1359. char** sLeft_p) {
  1360. char* sLeftNew = NULL;
  1361. char* sRightNew = NULL;
  1362. Arg* aLeft = arg_newStr("");
  1363. Arg* aRight = arg_newStr("");
  1364. Arg* aRightNew = arg_newStr("");
  1365. pika_bool bLeftExist = 0;
  1366. Args buffs = {0};
  1367. char _sOperator[3] = {0};
  1368. char* sOperator = (char*)_sOperator;
  1369. pika_bool bRight = 0;
  1370. for (uint8_t i = 0; i < sizeof(selfOperators) / SELF_OPERATORES_LEN; i++) {
  1371. if (Cursor_isContain(sStmt, TOKEN_operator, (char*)selfOperators[i])) {
  1372. pika_platform_memcpy(sOperator, selfOperators[i],
  1373. strGetSize((char*)selfOperators[i]) - 1);
  1374. break;
  1375. }
  1376. }
  1377. /* not found self operator */
  1378. if (sOperator[0] == 0) {
  1379. goto __exit;
  1380. }
  1381. /* found self operator */
  1382. bLeftExist = 1;
  1383. Cursor_forEach(cs, sStmt) {
  1384. Cursor_iterStart(&cs);
  1385. for (uint8_t i = 0; i < sizeof(selfOperators) / SELF_OPERATORES_LEN;
  1386. i++) {
  1387. if (strEqu(cs.token1.pyload, (char*)selfOperators[i])) {
  1388. bRight = 1;
  1389. goto __iter_continue;
  1390. }
  1391. }
  1392. if (!bRight) {
  1393. aLeft = arg_strAppend(aLeft, cs.token1.pyload);
  1394. } else {
  1395. aRight = arg_strAppend(aRight, cs.token1.pyload);
  1396. }
  1397. __iter_continue:
  1398. Cursor_iterEnd(&cs);
  1399. }
  1400. Cursor_deinit(&cs);
  1401. /* connect right */
  1402. aRightNew = arg_strAppend(aRightNew, arg_getStr(aLeft));
  1403. aRightNew = arg_strAppend(aRightNew, sOperator);
  1404. aRightNew = arg_strAppend(aRightNew, "(");
  1405. aRightNew = arg_strAppend(aRightNew, arg_getStr(aRight));
  1406. aRightNew = arg_strAppend(aRightNew, ")");
  1407. /* collect left_new and right_new */
  1408. sLeftNew = arg_getStr(aLeft);
  1409. sRightNew = arg_getStr(aRightNew);
  1410. __exit:
  1411. strsDeinit(&buffs);
  1412. if (NULL != sRightNew) {
  1413. *(sRight_p) = strsCopy(outbuffs, sRightNew);
  1414. ;
  1415. }
  1416. if (NULL != sLeftNew) {
  1417. *(sLeft_p) = strsCopy(outbuffs, sLeftNew);
  1418. }
  1419. arg_deinit(aRight);
  1420. arg_deinit(aLeft);
  1421. arg_deinit(aRightNew);
  1422. return bLeftExist;
  1423. }
  1424. AST* AST_create(void) {
  1425. return New_queueObj();
  1426. }
  1427. PIKA_RES AST_setNodeAttr(AST* ast, char* sAttrName, char* sAttrVal) {
  1428. return obj_setStr(ast, sAttrName, sAttrVal);
  1429. }
  1430. char* AST_getNodeAttr(AST* ast, char* sAttrName) {
  1431. return obj_getStr(ast, sAttrName);
  1432. }
  1433. PIKA_RES AST_setNodeBlock(AST* ast, char* sNodeContent) {
  1434. return AST_setNodeAttr(ast, "block", sNodeContent);
  1435. }
  1436. char* AST_getThisBlock(AST* ast) {
  1437. return obj_getStr(ast, "block");
  1438. }
  1439. PIKA_RES AST_parseSubStmt(AST* ast, char* sNodeContent) {
  1440. queueObj_pushObj(ast, (char*)"stmt");
  1441. AST_parseStmt(queueObj_getCurrentObj(ast), sNodeContent);
  1442. return PIKA_RES_OK;
  1443. }
  1444. char* Parser_popSubStmt(Args* outbuffs, char** sStmt_p, char* sDelimiter) {
  1445. Arg* aSubstmt = arg_newStr("");
  1446. Arg* aNewStmt = arg_newStr("");
  1447. char* sStmt = *sStmt_p;
  1448. pika_bool bIsGetSubstmt = pika_false;
  1449. Args buffs = {0};
  1450. Cursor_forEach(cs, sStmt) {
  1451. Cursor_iterStart(&cs);
  1452. if (bIsGetSubstmt) {
  1453. /* get new stmt */
  1454. aNewStmt = arg_strAppend(aNewStmt, cs.token1.pyload);
  1455. Cursor_iterEnd(&cs);
  1456. continue;
  1457. }
  1458. if (cs.bracket_deepth > 0) {
  1459. /* ignore */
  1460. aSubstmt = arg_strAppend(aSubstmt, cs.token1.pyload);
  1461. Cursor_iterEnd(&cs);
  1462. continue;
  1463. }
  1464. if (strEqu(cs.token1.pyload, sDelimiter)) {
  1465. /* found delimiter */
  1466. bIsGetSubstmt = pika_true;
  1467. Cursor_iterEnd(&cs);
  1468. continue;
  1469. }
  1470. /* collect substmt */
  1471. aSubstmt = arg_strAppend(aSubstmt, cs.token1.pyload);
  1472. Cursor_iterEnd(&cs);
  1473. }
  1474. Cursor_deinit(&cs);
  1475. strsDeinit(&buffs);
  1476. char* sSubstmt = strsCacheArg(outbuffs, aSubstmt);
  1477. char* sNewstmt = strsCacheArg(outbuffs, aNewStmt);
  1478. *sStmt_p = sNewstmt;
  1479. return sSubstmt;
  1480. }
  1481. int Parser_getSubStmtNum(char* sSubStmts, char* sDelimiter) {
  1482. if (strEqu(sSubStmts, ",")) {
  1483. return 0;
  1484. }
  1485. return _Cursor_count(sSubStmts, TOKEN_devider, sDelimiter, pika_true);
  1486. }
  1487. char* _Parser_popLastSubStmt(Args* outbuffs,
  1488. char** sStmt_p,
  1489. char* sDelimiter,
  1490. pika_bool bSkipBracket) {
  1491. uint8_t uLastStmtI = 0;
  1492. char* stmt = *sStmt_p;
  1493. Cursor_forEach(cs, stmt) {
  1494. Cursor_iterStart(&cs);
  1495. if (strIsStartWith(cs.token1.pyload, sDelimiter)) {
  1496. /* found delimiter */
  1497. if (bSkipBracket && cs.bracket_deepth > 0) {
  1498. /* skip bracket */
  1499. Cursor_iterEnd(&cs);
  1500. continue;
  1501. }
  1502. /* for "[" */
  1503. if (cs.bracket_deepth > 1) {
  1504. /* ignore */
  1505. Cursor_iterEnd(&cs);
  1506. continue;
  1507. }
  1508. uLastStmtI = cs.iter_index;
  1509. }
  1510. Cursor_iterEnd(&cs);
  1511. }
  1512. Cursor_deinit(&cs);
  1513. Arg* aMainStmt = arg_newStr("");
  1514. Arg* aLastStmt = arg_newStr("");
  1515. {
  1516. Cursor_forEach(cs, stmt) {
  1517. Cursor_iterStart(&cs);
  1518. if (cs.iter_index < uLastStmtI) {
  1519. aMainStmt = arg_strAppend(aMainStmt, cs.token1.pyload);
  1520. }
  1521. if (cs.iter_index >= uLastStmtI) {
  1522. aLastStmt = arg_strAppend(aLastStmt, cs.token1.pyload);
  1523. }
  1524. Cursor_iterEnd(&cs);
  1525. }
  1526. Cursor_deinit(&cs);
  1527. }
  1528. *sStmt_p = strsCacheArg(outbuffs, aMainStmt);
  1529. return strsCacheArg(outbuffs, aLastStmt);
  1530. }
  1531. char* Parser_popLastSubStmt(Args* outbuffs, char** sStmt_p, char* sDelimiter) {
  1532. return _Parser_popLastSubStmt(outbuffs, sStmt_p, sDelimiter, pika_true);
  1533. }
  1534. static void _AST_parse_list(AST* ast, Args* buffs, char* sStmt) {
  1535. #if !PIKA_BUILTIN_STRUCT_ENABLE
  1536. return;
  1537. #endif
  1538. AST_setNodeAttr(ast, (char*)"list", "list");
  1539. char* sSubStmts = strsCut(buffs, sStmt, '[', ']');
  1540. sSubStmts = strsAppend(buffs, sSubStmts, ",");
  1541. while (1) {
  1542. char* sSubStmt = Parser_popSubStmt(buffs, &sSubStmts, ",");
  1543. AST_parseSubStmt(ast, sSubStmt);
  1544. if (strEqu(sSubStmts, "")) {
  1545. break;
  1546. }
  1547. }
  1548. return;
  1549. }
  1550. static void _AST_parse_comprehension(AST* ast, Args* outBuffs, char* sStmt) {
  1551. #if PIKA_NANO_ENABLE
  1552. return;
  1553. #endif
  1554. /* [ substmt1 for substmt2 in substmt3 ] */
  1555. Args buffs = {0};
  1556. AST_setNodeAttr(ast, (char*)"comprehension", "");
  1557. char* sSubStmts = strsCut(&buffs, sStmt, '[', ']');
  1558. char* sSubStms1 = Cursor_splitCollect(&buffs, sSubStmts, " for ", 0);
  1559. char* sSubStms23 = Cursor_splitCollect(&buffs, sSubStmts, " for ", 1);
  1560. char* sSubStms2 = Cursor_splitCollect(&buffs, sSubStms23, " in ", 0);
  1561. char* sSubStms3 = Cursor_splitCollect(&buffs, sSubStms23, " in ", 1);
  1562. AST_setNodeAttr(ast, (char*)"substmt1", sSubStms1);
  1563. AST_setNodeAttr(ast, (char*)"substmt2", sSubStms2);
  1564. AST_setNodeAttr(ast, (char*)"substmt3", sSubStms3);
  1565. strsDeinit(&buffs);
  1566. return;
  1567. }
  1568. static void _AST_parse_list_comprehension(AST* ast, Args* buffs, char* sStmt) {
  1569. if (Cursor_isContain(sStmt, TOKEN_keyword, " for ")) {
  1570. _AST_parse_comprehension(ast, buffs, sStmt);
  1571. return;
  1572. }
  1573. _AST_parse_list(ast, buffs, sStmt);
  1574. }
  1575. static void _AST_parse_dict(AST* ast, Args* buffs, char* sStmt) {
  1576. #if !PIKA_BUILTIN_STRUCT_ENABLE
  1577. return;
  1578. #endif
  1579. AST_setNodeAttr(ast, (char*)"dict", "dict");
  1580. char* subStmts = strsCut(buffs, sStmt, '{', '}');
  1581. subStmts = strsAppend(buffs, subStmts, ",");
  1582. while (1) {
  1583. char* sSubStmt = Parser_popSubStmt(buffs, &subStmts, ",");
  1584. char* sKey = Parser_popSubStmt(buffs, &sSubStmt, ":");
  1585. char* sValue = sSubStmt;
  1586. AST_parseSubStmt(ast, sKey);
  1587. AST_parseSubStmt(ast, sValue);
  1588. if (strEqu(subStmts, "")) {
  1589. break;
  1590. }
  1591. }
  1592. }
  1593. static void _AST_parse_slice(AST* ast, Args* buffs, char* sStmt) {
  1594. #if !PIKA_SYNTAX_SLICE_ENABLE
  1595. return;
  1596. #endif
  1597. AST_setNodeAttr(ast, (char*)"slice", "slice");
  1598. sStmt = strsCopy(buffs, sStmt);
  1599. char* sLaststmt = _Parser_popLastSubStmt(buffs, &sStmt, "[", pika_false);
  1600. AST_parseSubStmt(ast, sStmt);
  1601. char* sSliceList = strsCut(buffs, sLaststmt, '[', ']');
  1602. pika_assert(sSliceList != NULL);
  1603. sSliceList = strsAppend(buffs, sSliceList, ":");
  1604. int iIndex = 0;
  1605. while (1) {
  1606. char* sSlice = Parser_popSubStmt(buffs, &sSliceList, ":");
  1607. if (iIndex == 0 && strEqu(sSlice, "")) {
  1608. AST_parseSubStmt(ast, "0");
  1609. } else if (iIndex == 1 && strEqu(sSlice, "")) {
  1610. AST_parseSubStmt(ast, "-99999");
  1611. } else {
  1612. AST_parseSubStmt(ast, sSlice);
  1613. }
  1614. iIndex++;
  1615. if (strEqu("", sSliceList)) {
  1616. break;
  1617. }
  1618. }
  1619. }
  1620. char* _Suger_process(Args* out_buffs,
  1621. char* sLine,
  1622. char* sToken1,
  1623. char* sToken2,
  1624. char* sFormat) {
  1625. char* sRet = sLine;
  1626. char* sStmt1 = "";
  1627. char* sStmt2 = "";
  1628. pika_bool bGotTokens = pika_false;
  1629. pika_bool bSkip = pika_false;
  1630. Args buffs = {0};
  1631. if (1 != Cursor_count(sLine, TOKEN_operator, sToken1)) {
  1632. sRet = sLine;
  1633. goto __exit;
  1634. }
  1635. if (1 != Cursor_count(sLine, TOKEN_operator, sToken2)) {
  1636. sRet = sLine;
  1637. goto __exit;
  1638. }
  1639. Cursor_forEach(cs, sLine) {
  1640. Cursor_iterStart(&cs);
  1641. if (!bGotTokens) {
  1642. if (strEqu(cs.token1.pyload, sToken1) &&
  1643. strEqu(cs.token2.pyload, sToken2)) {
  1644. bGotTokens = pika_true;
  1645. Cursor_iterEnd(&cs);
  1646. continue;
  1647. }
  1648. sStmt1 = strsAppend(&buffs, sStmt1, cs.token1.pyload);
  1649. } else {
  1650. if (!bSkip) {
  1651. bSkip = pika_true;
  1652. Cursor_iterEnd(&cs);
  1653. continue;
  1654. }
  1655. sStmt2 = strsAppend(&buffs, sStmt2, cs.token1.pyload);
  1656. }
  1657. Cursor_iterEnd(&cs);
  1658. }
  1659. Cursor_deinit(&cs);
  1660. if (!bGotTokens) {
  1661. sRet = sLine;
  1662. goto __exit;
  1663. }
  1664. sRet = strsFormat(out_buffs,
  1665. strGetSize(sLine) + strlen(sToken1) + strlen(sToken2),
  1666. sFormat, sStmt1, sStmt2);
  1667. __exit:
  1668. strsDeinit(&buffs);
  1669. return sRet;
  1670. }
  1671. char* Suger_not_in(Args* out_buffs, char* sLine) {
  1672. return _Suger_process(out_buffs, sLine, " not ", " in ", " not %s in %s");
  1673. }
  1674. char* Suger_is_not(Args* out_buffs, char* sLine) {
  1675. return _Suger_process(out_buffs, sLine, " is ", " not ", " not %s is %s");
  1676. }
  1677. AST* AST_parseStmt(AST* ast, char* sStmt) {
  1678. Args buffs = {0};
  1679. char* assignment = Cursor_splitCollect(&buffs, sStmt, "(", 0);
  1680. char* sMethod = NULL;
  1681. char* sRef = NULL;
  1682. char* sStr = NULL;
  1683. char* sNum = NULL;
  1684. char* sLeft = NULL;
  1685. char* sRight = NULL;
  1686. char* sImport = NULL;
  1687. char* sInhert = NULL;
  1688. enum StmtType eStmtType = STMT_none;
  1689. PIKA_RES eResult = PIKA_RES_OK;
  1690. sRight = sStmt;
  1691. /* solve check direct */
  1692. pika_bool bLeftExist = 0;
  1693. if (Parser_checkIsDirect(assignment)) {
  1694. bLeftExist = 1;
  1695. sLeft = strsCopy(&buffs, "");
  1696. sRight = strsCopy(&buffs, "");
  1697. pika_bool bMeetEqu = 0;
  1698. Cursor_forEach(cs, sStmt) {
  1699. Cursor_iterStart(&cs);
  1700. if (!bMeetEqu && strEqu(cs.token1.pyload, "=") &&
  1701. cs.token1.type == TOKEN_operator) {
  1702. bMeetEqu = 1;
  1703. Cursor_iterEnd(&cs);
  1704. continue;
  1705. }
  1706. if (0 == bMeetEqu) {
  1707. sLeft = strsAppend(&buffs, sLeft, cs.token1.pyload);
  1708. }
  1709. if (1 == bMeetEqu) {
  1710. sRight = strsAppend(&buffs, sRight, cs.token1.pyload);
  1711. }
  1712. Cursor_iterEnd(&cs);
  1713. }
  1714. Cursor_deinit(&cs);
  1715. }
  1716. /* solve the += -= /= *= stmt */
  1717. if (!bLeftExist) {
  1718. bLeftExist = Suger_selfOperator(&buffs, sStmt, &sRight, &sLeft);
  1719. }
  1720. /* remove hint */
  1721. if (bLeftExist) {
  1722. sLeft = Cursor_splitCollect(&buffs, sLeft, ":", 0);
  1723. }
  1724. /* solve the [] stmt */
  1725. sRight = Suger_leftSlice(&buffs, sRight, &sLeft);
  1726. sRight = Suger_format(&buffs, sRight);
  1727. /* set left */
  1728. if (bLeftExist) {
  1729. if (strEqu(sLeft, "")) {
  1730. eResult = PIKA_RES_ERR_SYNTAX_ERROR;
  1731. goto __exit;
  1732. }
  1733. AST_setNodeAttr(ast, (char*)"left", sLeft);
  1734. }
  1735. /* match statment type */
  1736. eStmtType = Lexer_matchStmtType(sRight);
  1737. if (STMT_tuple == eStmtType) {
  1738. sRight = strsFormat(&buffs, PIKA_LINE_BUFF_SIZE, "(%s)", sRight);
  1739. eStmtType = STMT_method;
  1740. }
  1741. /* solve operator stmt */
  1742. if (STMT_operator == eStmtType) {
  1743. sRight = Suger_not_in(&buffs, sRight);
  1744. sRight = Suger_is_not(&buffs, sRight);
  1745. char* rightWithoutSubStmt = _remove_sub_stmt(&buffs, sRight);
  1746. char* operator= Lexer_getOperator(&buffs, rightWithoutSubStmt);
  1747. if (NULL == operator) {
  1748. eResult = PIKA_RES_ERR_SYNTAX_ERROR;
  1749. goto __exit;
  1750. }
  1751. AST_setNodeAttr(ast, (char*)"operator", operator);
  1752. char* sRightBuff = strsCopy(&buffs, sRight);
  1753. char* sSubStmt2 = Cursor_popLastToken(&buffs, &sRightBuff, operator);
  1754. char* sSubStmt1 = sRightBuff;
  1755. AST_parseSubStmt(ast, sSubStmt1);
  1756. AST_parseSubStmt(ast, sSubStmt2);
  1757. goto __exit;
  1758. }
  1759. /* solve list stmt */
  1760. if (STMT_list == eStmtType) {
  1761. _AST_parse_list_comprehension(ast, &buffs, sRight);
  1762. goto __exit;
  1763. }
  1764. /* solve dict stmt */
  1765. if (STMT_dict == eStmtType) {
  1766. _AST_parse_dict(ast, &buffs, sRight);
  1767. goto __exit;
  1768. }
  1769. /* solve method chain */
  1770. if (STMT_chain == eStmtType) {
  1771. char* sHost = strsCopy(&buffs, sRight);
  1772. char* sMethodStmt = Parser_popLastSubStmt(&buffs, &sHost, ".");
  1773. AST_parseSubStmt(ast, sHost);
  1774. AST_parseStmt(ast, sMethodStmt);
  1775. goto __exit;
  1776. }
  1777. if (STMT_slice == eStmtType) {
  1778. /* solve slice stmt */
  1779. _AST_parse_slice(ast, &buffs, sRight);
  1780. goto __exit;
  1781. }
  1782. /* solve method stmt */
  1783. if (STMT_method == eStmtType) {
  1784. char* sRealType = "method";
  1785. char* sMethodStmt = strsCopy(&buffs, sRight);
  1786. char* sLastStmt = sMethodStmt;
  1787. /* for method()() */
  1788. int iBracketNum =
  1789. _Cursor_count(sMethodStmt, TOKEN_devider, "(", pika_true) +
  1790. _Cursor_count(sMethodStmt, TOKEN_devider, "[", pika_true);
  1791. if (iBracketNum > 1) {
  1792. sLastStmt =
  1793. _Parser_popLastSubStmt(&buffs, &sMethodStmt, "(", pika_false);
  1794. /* for (...) */
  1795. if (_Cursor_count(sLastStmt, TOKEN_devider, "(", pika_false) == 1) {
  1796. char* sMethodCheck = strsGetFirstToken(&buffs, sLastStmt, '(');
  1797. if (strEqu(sMethodCheck, "")) {
  1798. sLastStmt = strsAppend(&buffs, ".", sLastStmt);
  1799. }
  1800. }
  1801. AST_parseSubStmt(ast, sMethodStmt);
  1802. }
  1803. sMethod = strsGetFirstToken(&buffs, sLastStmt, '(');
  1804. char* sSubStmts = strsCut(&buffs, sLastStmt, '(', ')');
  1805. if (NULL == sSubStmts) {
  1806. eResult = PIKA_RES_ERR_SYNTAX_ERROR;
  1807. goto __exit;
  1808. }
  1809. /* add ',' at the end */
  1810. sSubStmts = strsAppend(&buffs, sSubStmts, ",");
  1811. int iSubStmtsNum = Parser_getSubStmtNum(sSubStmts, ",");
  1812. for (int i = 0; i < iSubStmtsNum; i++) {
  1813. char* substmt = Parser_popSubStmt(&buffs, &sSubStmts, ",");
  1814. AST_parseSubStmt(ast, substmt);
  1815. if (strOnly(sSubStmts, ',')) {
  1816. if (i < iSubStmtsNum - 2) {
  1817. eResult = PIKA_RES_ERR_SYNTAX_ERROR;
  1818. goto __exit;
  1819. }
  1820. if (i == iSubStmtsNum - 2 && strEqu(sMethod, "")) {
  1821. sRealType = "tuple";
  1822. }
  1823. break;
  1824. }
  1825. if (strEqu("", sSubStmts)) {
  1826. if (i != iSubStmtsNum - 1) {
  1827. eResult = PIKA_RES_ERR_SYNTAX_ERROR;
  1828. goto __exit;
  1829. }
  1830. break;
  1831. }
  1832. }
  1833. AST_setNodeAttr(ast, (char*)sRealType, sMethod);
  1834. goto __exit;
  1835. }
  1836. /* solve reference stmt */
  1837. if (STMT_reference == eStmtType) {
  1838. sRef = sRight;
  1839. /* filter for type hint */
  1840. sRef = Cursor_splitCollect(&buffs, sRef, ":", 0);
  1841. if (!strEqu(sRef, sRight)) {
  1842. goto __exit;
  1843. }
  1844. AST_setNodeAttr(ast, (char*)"ref", sRef);
  1845. goto __exit;
  1846. }
  1847. /* solve import stmt */
  1848. if (STMT_import == eStmtType) {
  1849. sImport = strsGetLastToken(&buffs, sRight, ' ');
  1850. AST_setNodeAttr(ast, (char*)"import", sImport);
  1851. goto __exit;
  1852. }
  1853. /* solve @inh stmt (from <module> import *) */
  1854. if (STMT_inhert == eStmtType) {
  1855. sInhert = strsGetLastToken(&buffs, sRight, ' ');
  1856. AST_setNodeAttr(ast, (char*)"inhert", sInhert);
  1857. goto __exit;
  1858. }
  1859. /* solve str/bytes stmt */
  1860. if (STMT_string == eStmtType || STMT_bytes == eStmtType) {
  1861. sStr = strsCopy(&buffs, sRight);
  1862. /* remove the first char */
  1863. char firstChar = sStr[0];
  1864. switch (eStmtType) {
  1865. case STMT_string:
  1866. sStr = sStr + 1;
  1867. break;
  1868. case STMT_bytes:
  1869. sStr = sStr + 2;
  1870. break;
  1871. default:
  1872. // never reach
  1873. pika_assert(0);
  1874. }
  1875. /* remove the last char */
  1876. sStr[strGetSize(sStr) - 1] = '\0';
  1877. /* replace */
  1878. if (strIsContain(sStr, '\\')) {
  1879. switch (firstChar) {
  1880. case '\'':
  1881. sStr = strsReplace(&buffs, sStr, "\\\'", "\'");
  1882. break;
  1883. case '\"':
  1884. sStr = strsReplace(&buffs, sStr, "\\\"", "\"");
  1885. break;
  1886. }
  1887. }
  1888. if (STMT_string == eStmtType) {
  1889. AST_setNodeAttr(ast, (char*)"string", sStr);
  1890. } else if (STMT_bytes == eStmtType) {
  1891. AST_setNodeAttr(ast, (char*)"bytes", sStr);
  1892. }
  1893. goto __exit;
  1894. }
  1895. /* solve number stmt */
  1896. if (STMT_number == eStmtType) {
  1897. sNum = sRight;
  1898. AST_setNodeAttr(ast, (char*)"num", sNum);
  1899. goto __exit;
  1900. }
  1901. __exit:
  1902. strsDeinit(&buffs);
  1903. if (eResult != PIKA_RES_OK) {
  1904. AST_deinit(ast);
  1905. return NULL;
  1906. }
  1907. return ast;
  1908. }
  1909. static int32_t Parser_getPyLineBlockDeepth(char* sLine) {
  1910. int32_t iSpaceNum = strGetIndent(sLine);
  1911. if (0 == iSpaceNum % PIKA_BLOCK_SPACE) {
  1912. return iSpaceNum / PIKA_BLOCK_SPACE;
  1913. }
  1914. /* space Num is not 4N, error*/
  1915. return -1;
  1916. }
  1917. char* Parser_removeComment(char* sLine) {
  1918. pika_bool bAnnotationExit = 0;
  1919. pika_bool bInSingleQuotes = 0;
  1920. pika_bool bInDoubleQuotesDeepth = 0;
  1921. pika_bool bPrevCharWasBackslash = 0;
  1922. for (uint32_t i = 0; i < strGetSize(sLine); i++) {
  1923. if (bPrevCharWasBackslash) {
  1924. bPrevCharWasBackslash = 0;
  1925. continue;
  1926. }
  1927. if ('\\' == sLine[i]) {
  1928. bPrevCharWasBackslash = 1;
  1929. continue;
  1930. }
  1931. if ('\'' == sLine[i] && !bInDoubleQuotesDeepth) {
  1932. bInSingleQuotes = !bInSingleQuotes;
  1933. continue;
  1934. }
  1935. if ('"' == sLine[i] && !bInSingleQuotes) {
  1936. bInDoubleQuotesDeepth = !bInDoubleQuotesDeepth;
  1937. continue;
  1938. }
  1939. if (!(bInSingleQuotes == 0 && bInDoubleQuotesDeepth == 0)) {
  1940. continue;
  1941. }
  1942. if ('#' == sLine[i]) {
  1943. sLine[i] = 0;
  1944. bAnnotationExit = 1;
  1945. break;
  1946. }
  1947. }
  1948. if (!bAnnotationExit) {
  1949. return sLine;
  1950. }
  1951. for (uint32_t i = 0; i < strGetSize(sLine); i++) {
  1952. if (' ' != sLine[i]) {
  1953. return sLine;
  1954. }
  1955. }
  1956. sLine = "@annotation";
  1957. return sLine;
  1958. }
  1959. char* _defGetDefault(Args* outBuffs, char** sDeclearOut_p) {
  1960. #if PIKA_NANO_ENABLE
  1961. return "";
  1962. #endif
  1963. Args buffs = {0};
  1964. char* sDeclear = strsCopy(&buffs, *sDeclearOut_p);
  1965. char* sFnName = strsGetFirstToken(&buffs, sDeclear, '(');
  1966. Arg* aDeclear = arg_strAppend(arg_newStr(sFnName), "(");
  1967. Arg* aDefault = arg_newStr("");
  1968. char* sArgList = strsCut(&buffs, sDeclear, '(', ')');
  1969. char* sDefaultOut = NULL;
  1970. pika_assert(NULL != sArgList);
  1971. int iArgNum = _Cursor_count(sArgList, TOKEN_devider, ",", pika_true) + 1;
  1972. for (int i = 0; i < iArgNum; i++) {
  1973. char* sItem = Cursor_popToken(&buffs, &sArgList, ",");
  1974. char* sDefaultVal = NULL;
  1975. char* sDefaultKey = NULL;
  1976. pika_bool bDefault = 0;
  1977. if (Cursor_isContain(sItem, TOKEN_operator, "=")) {
  1978. /* has default value */
  1979. sDefaultVal = Cursor_splitCollect(&buffs, sItem, "=", 1);
  1980. sDefaultKey = Cursor_splitCollect(&buffs, sItem, "=", 0);
  1981. sDefaultKey = Cursor_splitCollect(&buffs, sDefaultKey, ":", 0);
  1982. aDefault = arg_strAppend(aDefault, sDefaultKey);
  1983. aDefault = arg_strAppend(aDefault, "=");
  1984. aDefault = arg_strAppend(aDefault, sDefaultVal);
  1985. aDefault = arg_strAppend(aDefault, ",");
  1986. bDefault = 1;
  1987. } else {
  1988. sDefaultKey = sItem;
  1989. }
  1990. aDeclear = arg_strAppend(aDeclear, sDefaultKey);
  1991. if (bDefault) {
  1992. aDeclear = arg_strAppend(aDeclear, "=");
  1993. }
  1994. aDeclear = arg_strAppend(aDeclear, ",");
  1995. }
  1996. strPopLastToken(arg_getStr(aDeclear), ',');
  1997. aDeclear = arg_strAppend(aDeclear, ")");
  1998. *sDeclearOut_p = strsCopy(outBuffs, arg_getStr(aDeclear));
  1999. sDefaultOut = strsCopy(outBuffs, arg_getStr(aDefault));
  2000. strPopLastToken(sDefaultOut, ',');
  2001. arg_deinit(aDeclear);
  2002. arg_deinit(aDefault);
  2003. strsDeinit(&buffs);
  2004. return sDefaultOut;
  2005. }
  2006. static char* Suger_multiReturn(Args* out_buffs, char* sLine) {
  2007. #if PIKA_NANO_ENABLE
  2008. return sLine;
  2009. #endif
  2010. Cursor_forEach(cs, sLine) {
  2011. Cursor_iterStart(&cs);
  2012. if (cs.bracket_deepth == 0 && strEqu(cs.token1.pyload, ",")) {
  2013. sLine = strsFormat(out_buffs, strGetSize(sLine) + 3, "(%s)", sLine);
  2014. Cursor_iterEnd(&cs);
  2015. break;
  2016. }
  2017. Cursor_iterEnd(&cs);
  2018. }
  2019. Cursor_deinit(&cs);
  2020. return sLine;
  2021. }
  2022. /* match block start keywords */
  2023. const char control_keywords[][9] = {"break", "continue"};
  2024. /* normal keyward */
  2025. const char normal_keywords[][7] = {"while", "if", "elif"};
  2026. AST* parser_line2Ast(Parser* self, char* sLine) {
  2027. BlockState* blockState = &self->blockState;
  2028. /* line is not exist */
  2029. if (sLine == NULL) {
  2030. return NULL;
  2031. }
  2032. /* init data */
  2033. AST* oAst = AST_create();
  2034. Args buffs = {0};
  2035. int8_t iBlockDeepthNow, iBlockDeepthLast = -1;
  2036. char *sLineStart, *sStmt;
  2037. /* docsting */
  2038. if (strIsStartWith(sLine, "@docstring")) {
  2039. AST_setNodeAttr(oAst, "docstring", sLine + sizeof("@docstring"));
  2040. sStmt = "";
  2041. goto __block_matched;
  2042. }
  2043. /* get block deepth */
  2044. iBlockDeepthNow = Parser_getPyLineBlockDeepth(sLine);
  2045. if (self->blockDeepthOrigin == _VAL_NEED_INIT) {
  2046. self->blockDeepthOrigin = iBlockDeepthNow;
  2047. }
  2048. /* set block deepth */
  2049. if (iBlockDeepthNow == -1) {
  2050. /* get block_deepth error */
  2051. pika_platform_printf(
  2052. "IndentationError: unexpected indent, only support 4 "
  2053. "spaces\r\n");
  2054. obj_deinit(oAst);
  2055. oAst = NULL;
  2056. goto __exit;
  2057. }
  2058. iBlockDeepthNow += blockState->deepth;
  2059. obj_setInt(oAst, "blockDeepth", iBlockDeepthNow);
  2060. /* check if exit block */
  2061. if (0 != stack_getTop(blockState->stack)) {
  2062. iBlockDeepthLast = stack_getTop(blockState->stack) +
  2063. blockState->deepth + self->blockDeepthOrigin;
  2064. /* exit each block */
  2065. for (int i = 0; i < iBlockDeepthLast - iBlockDeepthNow; i++) {
  2066. QueueObj* exit_block_queue = obj_getObj(oAst, "exitBlock");
  2067. /* create an exit_block queue */
  2068. if (NULL == exit_block_queue) {
  2069. obj_newObj(oAst, "exitBlock", "", New_TinyObj);
  2070. exit_block_queue = obj_getObj(oAst, "exitBlock");
  2071. queueObj_init(exit_block_queue);
  2072. }
  2073. char buff[10] = {0};
  2074. char* sBlockType = stack_popStr(blockState->stack, buff);
  2075. /* push exit block type to exit_block queue */
  2076. queueObj_pushStr(exit_block_queue, sBlockType);
  2077. }
  2078. }
  2079. sLineStart = sLine + (iBlockDeepthNow - blockState->deepth) * 4;
  2080. sStmt = sLineStart;
  2081. // "while" "if" "elif"
  2082. for (uint32_t i = 0; i < sizeof(normal_keywords) / 7; i++) {
  2083. char* sKeyword = (char*)normal_keywords[i];
  2084. uint8_t sKeywordLen = strGetSize(sKeyword);
  2085. if (strIsStartWith(sLineStart, sKeyword) &&
  2086. (sLineStart[sKeywordLen] == ' ')) {
  2087. sStmt = strsCut(&buffs, sLineStart, ' ', ':');
  2088. AST_setNodeBlock(oAst, sKeyword);
  2089. stack_pushStr(blockState->stack, sKeyword);
  2090. goto __block_matched;
  2091. }
  2092. }
  2093. /* contral keyward */
  2094. /* "break", "continue" */
  2095. for (uint32_t i = 0; i < sizeof(control_keywords) / 8; i++) {
  2096. char* sKeyward = (char*)control_keywords[i];
  2097. uint8_t keyward_size = strGetSize(sKeyward);
  2098. if ((strIsStartWith(sLineStart, sKeyward)) &&
  2099. ((sLineStart[keyward_size] == ' ') ||
  2100. (sLineStart[keyward_size] == 0))) {
  2101. AST_setNodeAttr(oAst, sKeyward, "");
  2102. sStmt = "";
  2103. goto __block_matched;
  2104. }
  2105. }
  2106. /* for */
  2107. if (strIsStartWith(sLineStart, "for ")) {
  2108. Args* list_buffs = New_strBuff();
  2109. char* sLineBuff = strsCopy(list_buffs, sLineStart + 4);
  2110. sLineBuff = Cursor_getCleanStmt(list_buffs, sLineBuff);
  2111. if (strCountSign(sLineBuff, ':') < 1) {
  2112. args_deinit(list_buffs);
  2113. obj_deinit(oAst);
  2114. oAst = NULL;
  2115. goto __exit;
  2116. }
  2117. char* sArgIn = strsPopToken(list_buffs, &sLineBuff, ' ');
  2118. AST_setNodeAttr(oAst, "arg_in", sArgIn);
  2119. strsPopToken(list_buffs, &sLineBuff, ' ');
  2120. char* sListIn = Cursor_splitCollect(list_buffs, sLineBuff, ":", 0);
  2121. sListIn = strsAppend(list_buffs, "iter(", sListIn);
  2122. sListIn = strsAppend(list_buffs, sListIn, ")");
  2123. sListIn = strsCopy(&buffs, sListIn);
  2124. args_deinit(list_buffs);
  2125. AST_setNodeBlock(oAst, "for");
  2126. AST_setNodeAttr(oAst, "list_in", sListIn);
  2127. stack_pushStr(blockState->stack, "for");
  2128. sStmt = sListIn;
  2129. goto __block_matched;
  2130. }
  2131. /* else */
  2132. if (strIsStartWith(sLineStart, "else")) {
  2133. if ((sLineStart[4] == ' ') || (sLineStart[4] == ':')) {
  2134. sStmt = "";
  2135. AST_setNodeBlock(oAst, "else");
  2136. stack_pushStr(blockState->stack, "else");
  2137. }
  2138. goto __block_matched;
  2139. }
  2140. #if PIKA_SYNTAX_EXCEPTION_ENABLE
  2141. /* try */
  2142. if (strIsStartWith(sLineStart, "try")) {
  2143. if ((sLineStart[3] == ' ') || (sLineStart[3] == ':')) {
  2144. sStmt = "";
  2145. AST_setNodeBlock(oAst, "try");
  2146. stack_pushStr(blockState->stack, "try");
  2147. }
  2148. goto __block_matched;
  2149. }
  2150. /* except */
  2151. if (strIsStartWith(sLineStart, "except")) {
  2152. if ((sLineStart[6] == ' ') || (sLineStart[6] == ':')) {
  2153. sStmt = "";
  2154. AST_setNodeBlock(oAst, "except");
  2155. stack_pushStr(blockState->stack, "except");
  2156. }
  2157. goto __block_matched;
  2158. }
  2159. #endif
  2160. if (strEqu(sLineStart, "return")) {
  2161. AST_setNodeAttr(oAst, "return", "");
  2162. sStmt = "";
  2163. goto __block_matched;
  2164. }
  2165. if (strIsStartWith(sLineStart, "return ")) {
  2166. char* lineBuff = strsCopy(&buffs, sLineStart);
  2167. strsPopToken(&buffs, &lineBuff, ' ');
  2168. sStmt = lineBuff;
  2169. sStmt = Suger_multiReturn(&buffs, sStmt);
  2170. AST_setNodeAttr(oAst, "return", "");
  2171. goto __block_matched;
  2172. }
  2173. #if PIKA_SYNTAX_EXCEPTION_ENABLE
  2174. if (strEqu(sLineStart, "raise")) {
  2175. AST_setNodeAttr(oAst, "raise", "");
  2176. sStmt = "RuntimeError";
  2177. goto __block_matched;
  2178. }
  2179. if (strIsStartWith(sLineStart, "raise ")) {
  2180. AST_setNodeAttr(oAst, "raise", "");
  2181. char* sLineBuff = strsCopy(&buffs, sLineStart);
  2182. strsPopToken(&buffs, &sLineBuff, ' ');
  2183. sStmt = sLineBuff;
  2184. if (strEqu("", sStmt)) {
  2185. sStmt = "RuntimeError";
  2186. }
  2187. goto __block_matched;
  2188. }
  2189. /* assert */
  2190. if (strIsStartWith(sLineStart, "assert ")) {
  2191. sStmt = "";
  2192. AST_setNodeAttr(oAst, "assert", "");
  2193. char* sLineBuff = strsCopy(&buffs, sLineStart + 7);
  2194. /* assert expr [, msg] */
  2195. while (1) {
  2196. char* sSubStmt = Parser_popSubStmt(&buffs, &sLineBuff, ",");
  2197. AST_parseSubStmt(oAst, sSubStmt);
  2198. if (strEqu(sLineBuff, "")) {
  2199. break;
  2200. }
  2201. }
  2202. goto __block_matched;
  2203. }
  2204. #endif
  2205. if (strIsStartWith(sLineStart, "global ")) {
  2206. sStmt = "";
  2207. char* sGlobalList = sLineStart + 7;
  2208. sGlobalList = Cursor_getCleanStmt(&buffs, sGlobalList);
  2209. AST_setNodeAttr(oAst, "global", sGlobalList);
  2210. goto __block_matched;
  2211. }
  2212. if (strIsStartWith(sLineStart, "del ") ||
  2213. strIsStartWith(sLineStart, "del(")) {
  2214. sStmt = "";
  2215. char* sDelDir = strsCut(&buffs, sLineStart, '(', ')');
  2216. if (!sDelDir) {
  2217. sDelDir = sLineStart + sizeof("del ") - 1;
  2218. }
  2219. sDelDir = Cursor_getCleanStmt(&buffs, sDelDir);
  2220. AST_setNodeAttr(oAst, "del", sDelDir);
  2221. goto __block_matched;
  2222. }
  2223. if (strIsStartWith(sLineStart, (char*)"def ")) {
  2224. sStmt = "";
  2225. char* sDeclare = strsCut(&buffs, sLineStart, ' ', ':');
  2226. if (NULL == sDeclare) {
  2227. obj_deinit(oAst);
  2228. oAst = NULL;
  2229. goto __exit;
  2230. }
  2231. sDeclare = Cursor_getCleanStmt(&buffs, sDeclare);
  2232. AST_setNodeAttr(oAst, "raw", sDeclare);
  2233. if (!strIsContain(sDeclare, '(') || !strIsContain(sDeclare, ')')) {
  2234. obj_deinit(oAst);
  2235. oAst = NULL;
  2236. goto __exit;
  2237. }
  2238. char* sDefaultStmt = _defGetDefault(&buffs, &sDeclare);
  2239. AST_setNodeBlock(oAst, "def");
  2240. AST_setNodeAttr(oAst, "declare", sDeclare);
  2241. if (sDefaultStmt[0] != '\0') {
  2242. AST_setNodeAttr(oAst, "default", sDefaultStmt);
  2243. }
  2244. stack_pushStr(blockState->stack, "def");
  2245. goto __block_matched;
  2246. }
  2247. if (strIsStartWith(sLineStart, (char*)"class ")) {
  2248. sStmt = "";
  2249. char* sDeclare = strsCut(&buffs, sLineStart, ' ', ':');
  2250. if (NULL == sDeclare) {
  2251. obj_deinit(oAst);
  2252. oAst = NULL;
  2253. goto __exit;
  2254. }
  2255. sDeclare = Cursor_getCleanStmt(&buffs, sDeclare);
  2256. AST_setNodeBlock(oAst, "class");
  2257. AST_setNodeAttr(oAst, "declare", sDeclare);
  2258. stack_pushStr(blockState->stack, "class");
  2259. goto __block_matched;
  2260. }
  2261. __block_matched:
  2262. if (NULL == sStmt) {
  2263. AST_deinit(oAst);
  2264. oAst = NULL;
  2265. goto __exit;
  2266. }
  2267. sStmt = Cursor_getCleanStmt(&buffs, sStmt);
  2268. oAst = AST_parseStmt(oAst, sStmt);
  2269. goto __exit;
  2270. __exit:
  2271. strsDeinit(&buffs);
  2272. return oAst;
  2273. }
  2274. static AST* line2Ast_withBlockDeepth(char* sLine, int iBlockDeepth) {
  2275. Parser* parser = parser_create();
  2276. parser->blockState.deepth = iBlockDeepth;
  2277. AST* ast = parser_line2Ast(parser, sLine);
  2278. parser_deinit(parser);
  2279. return ast;
  2280. }
  2281. int AST_getBlockDeepthNow(AST* ast) {
  2282. return obj_getInt(ast, "blockDeepth");
  2283. }
  2284. AST* line2Ast(char* sLine) {
  2285. return line2Ast_withBlockDeepth(sLine, 0);
  2286. }
  2287. static char* Suger_import_as(Args* out_buffs, char* sLine) {
  2288. #if !PIKA_SYNTAX_IMPORT_EX_ENABLE
  2289. return sLine;
  2290. #endif
  2291. Args buffs = {0};
  2292. char* sLineOut = sLine;
  2293. char* sAlias = NULL;
  2294. char* sOrigin = NULL;
  2295. char* sStmt = sLine + 7;
  2296. /* not import, exit */
  2297. if (!strIsStartWith(sLine, "import ")) {
  2298. sLineOut = sLine;
  2299. goto __exit;
  2300. }
  2301. if (!Cursor_isContain(sStmt, TOKEN_operator, " as ")) {
  2302. sLineOut = sLine;
  2303. goto __exit;
  2304. }
  2305. /* {origin} as {alias} */
  2306. sOrigin = Cursor_popToken(&buffs, &sStmt, " as ");
  2307. sAlias = sStmt;
  2308. /* 'import' and 'as' */
  2309. sLineOut = strsFormat(&buffs, PIKA_LINE_BUFF_SIZE, "import %s\n%s = %s",
  2310. sOrigin, sAlias, sOrigin);
  2311. __exit:
  2312. return strsReturnOut(&buffs, out_buffs, sLineOut);
  2313. }
  2314. static pika_bool _check_is_multi_assign(char* sArgList) {
  2315. #if PIKA_NANO_ENABLE
  2316. return pika_false;
  2317. #endif
  2318. pika_bool bRes = pika_false;
  2319. Cursor_forEach(cs, sArgList) {
  2320. Cursor_iterStart(&cs);
  2321. if ((cs.bracket_deepth == 0 && strEqu(cs.token1.pyload, ","))) {
  2322. bRes = pika_true;
  2323. }
  2324. Cursor_iterEnd(&cs);
  2325. }
  2326. Cursor_deinit(&cs);
  2327. return bRes;
  2328. }
  2329. Arg* arg_strAddIndent(Arg* aStrIn, int indent) {
  2330. if (0 == indent) {
  2331. return aStrIn;
  2332. }
  2333. /* add space */
  2334. char* sSpaces = pikaMalloc(indent + 1);
  2335. pika_platform_memset(sSpaces, ' ', indent);
  2336. sSpaces[indent] = '\0';
  2337. Arg* aRet = arg_newStr(sSpaces);
  2338. aRet = arg_strAppend(aRet, arg_getStr(aStrIn));
  2339. pikaFree(sSpaces, indent + 1);
  2340. arg_deinit(aStrIn);
  2341. return aRet;
  2342. }
  2343. Arg* arg_strAddIndentMulti(Arg* aStrInMuti, int indent) {
  2344. if (0 == indent) {
  2345. return aStrInMuti;
  2346. }
  2347. char* sStrInMuti = arg_getStr(aStrInMuti);
  2348. char* sLine = NULL;
  2349. int iLineNum = strGetLineNum(sStrInMuti);
  2350. Arg* aStrOut = arg_newStr("");
  2351. Args buffs = {0};
  2352. for (int i = 0; i < iLineNum; i++) {
  2353. sLine = strsPopLine(&buffs, &sStrInMuti);
  2354. Arg* aLine = arg_newStr(sLine);
  2355. aLine = arg_strAddIndent(aLine, indent);
  2356. sLine = arg_getStr(aLine);
  2357. aStrOut = arg_strAppend(aStrOut, sLine);
  2358. if (i != iLineNum - 1) {
  2359. aStrOut = arg_strAppend(aStrOut, "\n");
  2360. }
  2361. arg_deinit(aLine);
  2362. }
  2363. strsDeinit(&buffs);
  2364. arg_deinit(aStrInMuti);
  2365. return aStrOut;
  2366. }
  2367. static char* Suger_multiAssign(Args* out_buffs, char* sLine) {
  2368. #if PIKA_NANO_ENABLE
  2369. return sLine;
  2370. #endif
  2371. if (!strIsContain(sLine, '=') || !strIsContain(sLine, ',')) {
  2372. return sLine;
  2373. }
  2374. Args buffs = {0};
  2375. char* sLineOut = sLine;
  2376. int iIndent = strGetIndent(sLine);
  2377. pika_bool bAssign = pika_false;
  2378. Arg* aStmt = arg_newStr("");
  2379. Arg* aOutList = arg_newStr("");
  2380. Arg* aOutItem = arg_newStr("");
  2381. Arg* aLineOut = arg_newStr("");
  2382. char* sLineItem = NULL;
  2383. char* sOutList = NULL;
  2384. int iOutNum = 0;
  2385. Cursor_forEach(cs, sLine) {
  2386. Cursor_iterStart(&cs);
  2387. if (cs.bracket_deepth == 0 && strEqu(cs.token1.pyload, "=")) {
  2388. bAssign = pika_true;
  2389. Cursor_iterEnd(&cs);
  2390. continue;
  2391. }
  2392. if (bAssign) {
  2393. aStmt = arg_strAppend(aStmt, cs.token1.pyload);
  2394. }
  2395. if (!bAssign) {
  2396. aOutList = arg_strAppend(aOutList, cs.token1.pyload);
  2397. }
  2398. Cursor_iterEnd(&cs);
  2399. }
  2400. Cursor_deinit(&cs);
  2401. if (!bAssign) {
  2402. sLineOut = sLine;
  2403. goto __exit;
  2404. }
  2405. if (!_check_is_multi_assign(arg_getStr(aOutList))) {
  2406. sLineOut = sLine;
  2407. goto __exit;
  2408. }
  2409. sLineItem = strsFormat(&buffs, PIKA_LINE_BUFF_SIZE, "$tmp= %s\n",
  2410. arg_getStr(aStmt));
  2411. /* add space */
  2412. aLineOut = arg_strAppend(aLineOut, sLineItem);
  2413. sOutList = arg_getStr(aOutList);
  2414. while (1) {
  2415. char* item = Cursor_popToken(&buffs, &sOutList, ",");
  2416. if (item[0] == '\0') {
  2417. break;
  2418. }
  2419. char* sLineItem = strsFormat(&buffs, PIKA_LINE_BUFF_SIZE,
  2420. "%s = $tmp[%d]\n", item, iOutNum);
  2421. /* add space */
  2422. aLineOut = arg_strAppend(aLineOut, sLineItem);
  2423. iOutNum++;
  2424. }
  2425. /* add space */
  2426. aLineOut = arg_strAppend(aLineOut, "del $tmp");
  2427. aLineOut = arg_strAddIndentMulti(aLineOut, iIndent);
  2428. sLineOut = strsCopy(out_buffs, arg_getStr(aLineOut));
  2429. goto __exit;
  2430. __exit:
  2431. arg_deinit(aStmt);
  2432. arg_deinit(aOutList);
  2433. arg_deinit(aOutItem);
  2434. arg_deinit(aLineOut);
  2435. strsDeinit(&buffs);
  2436. return sLineOut;
  2437. }
  2438. static char* Suger_from_import_as(Args* buffs_p, char* sLine) {
  2439. #if !PIKA_SYNTAX_IMPORT_EX_ENABLE
  2440. return sLine;
  2441. #endif
  2442. Args buffs = {0};
  2443. Arg* aLineOut = NULL;
  2444. char* sLineOut = sLine;
  2445. char* sClass = NULL;
  2446. char* sModule = NULL;
  2447. char* sAlias = NULL;
  2448. char* sStmt = sLine + 5;
  2449. char* sClassAfter = "";
  2450. if (!strIsStartWith(sLine, "from ")) {
  2451. sLineOut = sLine;
  2452. goto __exit;
  2453. }
  2454. sModule = Cursor_popToken(&buffs, &sStmt, " import ");
  2455. if (!Cursor_isContain(sStmt, TOKEN_operator, " as ")) {
  2456. sClass = sStmt;
  2457. } else {
  2458. sClass = Cursor_popToken(&buffs, &sStmt, " as ");
  2459. sAlias = sStmt;
  2460. }
  2461. if (NULL == sAlias) {
  2462. sAlias = sClass;
  2463. }
  2464. /* skip PikaObj */
  2465. if (strEqu(sModule, "PikaObj")) {
  2466. sLineOut = strsCopy(buffs_p, "");
  2467. goto __exit;
  2468. }
  2469. /* solve from module import * */
  2470. if (strEqu(sClass, "*")) {
  2471. sLineOut =
  2472. strsFormat(&buffs, PIKA_LINE_BUFF_SIZE, "@inh %s\n", sModule);
  2473. sLineOut = strsCopy(buffs_p, sLineOut);
  2474. goto __exit;
  2475. }
  2476. while (1) {
  2477. char* sClassItem = Cursor_popToken(&buffs, &sClass, ",");
  2478. if (sClassItem[0] == '\0') {
  2479. break;
  2480. }
  2481. sClassItem = strsFormat(&buffs, PIKA_LINE_BUFF_SIZE, "%s.%s,", sModule,
  2482. sClassItem);
  2483. sClassAfter = strsAppend(&buffs, sClassAfter, sClassItem);
  2484. }
  2485. sClassAfter[strGetSize(sClassAfter) - 1] = '\0';
  2486. sClass = sClassAfter;
  2487. aLineOut = arg_newStr("import ");
  2488. aLineOut = arg_strAppend(aLineOut, sClass);
  2489. aLineOut = arg_strAppend(aLineOut, "\n");
  2490. aLineOut = arg_strAppend(aLineOut, sAlias);
  2491. aLineOut = arg_strAppend(aLineOut, " = ");
  2492. aLineOut = arg_strAppend(aLineOut, sClass);
  2493. sLineOut = arg_getStr(aLineOut);
  2494. sLineOut = strsCopy(buffs_p, sLineOut);
  2495. __exit:
  2496. if (NULL != aLineOut) {
  2497. arg_deinit(aLineOut);
  2498. }
  2499. strsDeinit(&buffs);
  2500. return sLineOut;
  2501. }
  2502. static char* Suger_import(Args* outbuffs, char* sLine) {
  2503. #if !PIKA_SYNTAX_IMPORT_EX_ENABLE
  2504. return sLine;
  2505. #endif
  2506. sLine = Suger_import_as(outbuffs, sLine);
  2507. sLine = Suger_from_import_as(outbuffs, sLine);
  2508. Arg* aLineBuff = arg_newStr("");
  2509. while (1) {
  2510. char* sSingleLine = strPopFirstToken(&sLine, '\n');
  2511. if (sSingleLine[0] == '\0') {
  2512. break;
  2513. }
  2514. if (strIsStartWith(sSingleLine, "import ")) {
  2515. if (strIsContain(sSingleLine, ',')) {
  2516. sSingleLine = sSingleLine + 7;
  2517. while (1) {
  2518. char* single_import = strPopFirstToken(&sSingleLine, ',');
  2519. if (single_import[0] == '\0') {
  2520. break;
  2521. }
  2522. aLineBuff = arg_strAppend(aLineBuff, "import ");
  2523. aLineBuff = arg_strAppend(aLineBuff, single_import);
  2524. aLineBuff = arg_strAppend(aLineBuff, "\n");
  2525. }
  2526. char* sLineAfter = arg_getStr(aLineBuff);
  2527. sLineAfter[strlen(sLineAfter) - 1] = '\0';
  2528. }
  2529. }
  2530. aLineBuff = arg_strAppend(aLineBuff, sSingleLine);
  2531. aLineBuff = arg_strAppend(aLineBuff, "\n");
  2532. }
  2533. char* sLineAfter = arg_getStr(aLineBuff);
  2534. sLineAfter[strlen(sLineAfter) - 1] = '\0';
  2535. sLineAfter = strsCopy(outbuffs, sLineAfter);
  2536. arg_deinit(aLineBuff);
  2537. return sLineAfter;
  2538. }
  2539. static char* Suger_semicolon(Args* outbuffs, char* sLine) {
  2540. Args buffs = {0};
  2541. char* sStmt = sLine;
  2542. char* sStmtAfter = "";
  2543. if (Cursor_count(sLine, TOKEN_devider, ";") < 1) {
  2544. return sLine;
  2545. }
  2546. while (1) {
  2547. char* sStmtItem = Cursor_popToken(&buffs, &sStmt, ";");
  2548. if (sStmtItem[0] == '\0') {
  2549. break;
  2550. };
  2551. sStmtItem = strsAppend(&buffs, sStmtItem, "\n");
  2552. sStmtAfter = strsAppend(&buffs, sStmtAfter, sStmtItem);
  2553. }
  2554. sStmtAfter[strGetSize(sStmtAfter) - 1] = '\0';
  2555. sStmtAfter = strsCopy(outbuffs, sStmtAfter);
  2556. strsDeinit(&buffs);
  2557. return sStmtAfter;
  2558. }
  2559. typedef char* (*Suger_processor)(Args*, char*);
  2560. const Suger_processor Suger_processor_list[] = {Suger_import, Suger_semicolon,
  2561. Suger_multiAssign};
  2562. static char* Parser_sugerProcessOnce(Args* outbuffs, char* sLine) {
  2563. for (uint32_t i = 0; i < sizeof(Suger_processor_list) / sizeof(void*);
  2564. i++) {
  2565. sLine = Suger_processor_list[i](outbuffs, sLine);
  2566. if (strCountSign(sLine, '\n') > 1) {
  2567. break;
  2568. }
  2569. }
  2570. return sLine;
  2571. }
  2572. static char* Parser_sugerProcess(Args* outbuffs, char* sLine) {
  2573. /* process import */
  2574. int32_t block_deepth = Parser_getPyLineBlockDeepth(sLine);
  2575. if (block_deepth < 0) {
  2576. return NULL;
  2577. }
  2578. char* sLineOrigin = sLine;
  2579. sLine = sLine + block_deepth * PIKA_BLOCK_SPACE;
  2580. sLine = Parser_sugerProcessOnce(outbuffs, sLine);
  2581. if (strEqu(sLineOrigin, sLine)) {
  2582. return sLine;
  2583. }
  2584. /* process multi assign */
  2585. int iLineNum = strCountSign(sLine, '\n') + 1;
  2586. Arg* aLine = arg_newStr("");
  2587. for (int i = 0; i < iLineNum; i++) {
  2588. if (i > 0) {
  2589. aLine = arg_strAppend(aLine, "\n");
  2590. }
  2591. char* sSingleLine = strsPopToken(outbuffs, &sLine, '\n');
  2592. sSingleLine = Parser_sugerProcess(outbuffs, sSingleLine);
  2593. aLine = arg_strAppend(aLine, sSingleLine);
  2594. }
  2595. sLine = strsCopy(outbuffs, arg_getStr(aLine));
  2596. sLine =
  2597. strsAddIndentation(outbuffs, sLine, block_deepth * PIKA_BLOCK_SPACE);
  2598. if (NULL != aLine) {
  2599. arg_deinit(aLine);
  2600. }
  2601. return sLine;
  2602. }
  2603. static char* Parser_linePreProcess(Args* outbuffs, char* sLine) {
  2604. sLine = Parser_removeComment(sLine);
  2605. /* check syntex error */
  2606. if (Lexer_isError(sLine)) {
  2607. sLine = NULL;
  2608. goto __exit;
  2609. }
  2610. /* process EOL */
  2611. sLine = strsDeleteChar(outbuffs, sLine, '\r');
  2612. /* process syntax sugar */
  2613. sLine = Parser_sugerProcess(outbuffs, sLine);
  2614. __exit:
  2615. return sLine;
  2616. }
  2617. char* parser_line2Target(Parser* self, char* sLine) {
  2618. char* sOut = NULL;
  2619. AST* oAst = NULL;
  2620. uint8_t uLineNum = 0;
  2621. /* docsting */
  2622. if (strIsStartWith(sLine, "@docstring")) {
  2623. oAst = parser_line2Ast(self, sLine);
  2624. char* sBackendCode = self->fn_ast2Target(self, oAst);
  2625. if (NULL == oAst) {
  2626. sOut = "";
  2627. goto __exit;
  2628. }
  2629. AST_deinit(oAst);
  2630. sOut = sBackendCode;
  2631. goto __exit;
  2632. }
  2633. /* pre process */
  2634. sLine = Parser_linePreProcess(&self->lineBuffs, sLine);
  2635. if (NULL == sLine) {
  2636. /* preprocess error */
  2637. goto __exit;
  2638. }
  2639. if (strEqu("@annotation", sLine)) {
  2640. sOut = "";
  2641. goto __exit;
  2642. }
  2643. /*
  2644. solve more lines
  2645. preprocess may generate more lines
  2646. */
  2647. uLineNum = strCountSign(sLine, '\n') + 1;
  2648. for (int i = 0; i < uLineNum; i++) {
  2649. char* sSingleLine = strsPopToken(&self->lineBuffs, &sLine, '\n');
  2650. /* parse line to AST */
  2651. oAst = parser_line2Ast(self, sSingleLine);
  2652. if (NULL == oAst) {
  2653. /* parse error */
  2654. goto __exit;
  2655. }
  2656. /* gen ASM from AST */
  2657. char* sBackendCode = self->fn_ast2Target(self, oAst);
  2658. if (sOut == NULL) {
  2659. sOut = sBackendCode;
  2660. } else {
  2661. sOut = strsAppend(&self->lineBuffs, sOut, sBackendCode);
  2662. }
  2663. if (NULL != oAst) {
  2664. AST_deinit(oAst);
  2665. }
  2666. }
  2667. __exit:
  2668. return sOut;
  2669. }
  2670. static int Parser_isVoidLine(char* sLine) {
  2671. for (uint32_t i = 0; i < strGetSize(sLine); i++) {
  2672. if (sLine[i] != ' ') {
  2673. return 0;
  2674. }
  2675. }
  2676. return 1;
  2677. }
  2678. static uint8_t Parser_checkIsDocstring(char* line,
  2679. Args* outbuffs,
  2680. uint8_t bIsInDocstring,
  2681. uint8_t* pbIsSingleDocstring,
  2682. char** psDocstring) {
  2683. pika_bool bIsDocstring = 0;
  2684. uint32_t i = 0;
  2685. int32_t iDocstringStart = 0;
  2686. int32_t iDocstringEnd = -1;
  2687. uint32_t uLineSize = strGetSize(line);
  2688. char* sDocstring = NULL;
  2689. while (i + 2 < uLineSize) {
  2690. /* not match ' or " */
  2691. if ((line[i] != '\'') && (line[i] != '"')) {
  2692. if (!bIsDocstring && !bIsInDocstring && !charIsBlank(line[i])) {
  2693. break;
  2694. }
  2695. i++;
  2696. continue;
  2697. }
  2698. /* not match ''' or """ */
  2699. if (!((line[i + 1] == line[i]) && (line[i + 2] == line[i]))) {
  2700. i++;
  2701. continue;
  2702. }
  2703. if (bIsDocstring) {
  2704. *pbIsSingleDocstring = 1;
  2705. iDocstringEnd = i;
  2706. break;
  2707. }
  2708. bIsDocstring = 1;
  2709. if (bIsInDocstring) {
  2710. iDocstringEnd = i;
  2711. } else {
  2712. iDocstringStart = i + 3;
  2713. }
  2714. i++;
  2715. }
  2716. if (bIsDocstring) {
  2717. sDocstring = strsCopy(outbuffs, line + iDocstringStart);
  2718. if (iDocstringEnd != -1) {
  2719. sDocstring[iDocstringEnd - iDocstringStart] = '\0';
  2720. }
  2721. *psDocstring = sDocstring;
  2722. }
  2723. return bIsDocstring;
  2724. }
  2725. char* parser_lines2Target(Parser* self, char* sPyLines) {
  2726. Arg* aBackendCode = arg_newStr("");
  2727. Arg* aLineConnection = arg_newStr("");
  2728. Arg* aDocstring = arg_newStr("");
  2729. uint32_t uLinesOffset = 0;
  2730. uint16_t uLinesNum = strCountSign(sPyLines, '\n') + 1;
  2731. uint16_t uLinesIndex = 0;
  2732. uint8_t bIsInDocstring = 0;
  2733. uint8_t bIsSingleDocstring = 0;
  2734. uint8_t bIsLineConnection = 0;
  2735. uint8_t bIsLineConnectionForBracket = 0;
  2736. char* sOut = NULL;
  2737. char* sBackendCode = NULL;
  2738. char* sDocstring = NULL;
  2739. uint32_t uLineSize = 0;
  2740. /* parse each line */
  2741. while (1) {
  2742. uLinesIndex++;
  2743. char* sLineOrigin = NULL;
  2744. char* sLine = NULL;
  2745. /* add void line to the end */
  2746. if (uLinesIndex >= uLinesNum + 1) {
  2747. sLine = "";
  2748. goto __parse_line;
  2749. }
  2750. /* get single line by pop multiline */
  2751. sLineOrigin =
  2752. strsGetFirstToken(&self->lineBuffs, sPyLines + uLinesOffset, '\n');
  2753. sLine = strsCopy(&self->lineBuffs, sLineOrigin);
  2754. /* line connection */
  2755. if (bIsLineConnection) {
  2756. if (bIsLineConnectionForBracket) {
  2757. sLine = Parser_removeComment(sLine);
  2758. if (strEqu(sLine, "@annotation")) {
  2759. sLine = "";
  2760. }
  2761. }
  2762. aLineConnection = arg_strAppend(aLineConnection, sLine);
  2763. sLine = strsCopy(&self->lineBuffs, arg_getStr(aLineConnection));
  2764. /* reflash the line_connection_arg */
  2765. arg_deinit(aLineConnection);
  2766. aLineConnection = arg_newStr("");
  2767. bIsLineConnection = 0;
  2768. bIsLineConnectionForBracket = 0;
  2769. }
  2770. /* check connection */
  2771. if ('\\' == sLine[strGetSize(sLine) - 1]) {
  2772. /* remove the '\\' */
  2773. sLine[strGetSize(sLine) - 1] = '\0';
  2774. bIsLineConnection = 1;
  2775. aLineConnection = arg_strAppend(aLineConnection, sLine);
  2776. goto __next_line;
  2777. }
  2778. /* filter for not end \n */
  2779. if (Parser_isVoidLine(sLine)) {
  2780. goto __next_line;
  2781. }
  2782. /* filter for docstring ''' or """ */
  2783. if (Parser_checkIsDocstring(sLine, &self->lineBuffs, bIsInDocstring,
  2784. &bIsSingleDocstring, &sDocstring)) {
  2785. bIsInDocstring = ~bIsInDocstring;
  2786. if (sDocstring[0] != '\0') {
  2787. aDocstring = arg_strAppend(aDocstring, sDocstring);
  2788. aDocstring = arg_strAppend(aDocstring, "\n");
  2789. }
  2790. /* one line docstring */
  2791. if (bIsSingleDocstring) {
  2792. bIsInDocstring = 0;
  2793. bIsSingleDocstring = 0;
  2794. }
  2795. if (!bIsInDocstring) {
  2796. /* multi line docstring */
  2797. sLine = strsAppend(&self->lineBuffs, "@docstring\n",
  2798. arg_getStr(aDocstring));
  2799. /* reflash the docstring_arg */
  2800. arg_deinit(aDocstring);
  2801. aDocstring = arg_newStr("");
  2802. goto __parse_line;
  2803. }
  2804. goto __next_line;
  2805. }
  2806. /* skip docstring */
  2807. if (bIsInDocstring) {
  2808. aDocstring = arg_strAppend(aDocstring, sLine);
  2809. aDocstring = arg_strAppend(aDocstring, "\n");
  2810. goto __next_line;
  2811. }
  2812. /* support Tab */
  2813. sLine = strsReplace(&self->lineBuffs, sLine, "\t", " ");
  2814. /* remove \r */
  2815. sLine = strsReplace(&self->lineBuffs, sLine, "\r", "");
  2816. /* check auto connection */
  2817. Cursor_forEach(c, sLine) {
  2818. Cursor_iterStart(&c);
  2819. Cursor_iterEnd(&c);
  2820. }
  2821. Cursor_deinit(&c);
  2822. /* auto connection */
  2823. if (uLinesIndex < uLinesNum) {
  2824. if (c.bracket_deepth > 0) {
  2825. aLineConnection = arg_strAppend(aLineConnection, sLine);
  2826. bIsLineConnection = 1;
  2827. bIsLineConnectionForBracket = 1;
  2828. goto __next_line;
  2829. }
  2830. }
  2831. /* bracket match failed */
  2832. if (c.bracket_deepth != 0) {
  2833. sBackendCode = NULL;
  2834. goto __parse_after;
  2835. }
  2836. __parse_line:
  2837. /* parse single Line to Asm */
  2838. if (strEqu(sLine, "#!label")) {
  2839. self->bytecode_frame->label_pc =
  2840. self->bytecode_frame->instruct_array.size;
  2841. goto __next_line;
  2842. }
  2843. sBackendCode = parser_line2Target(self, sLine);
  2844. __parse_after:
  2845. if (NULL == sBackendCode) {
  2846. sOut = NULL;
  2847. pika_platform_printf(
  2848. "----------[%d]----------\r\n%s\r\n-------------------------"
  2849. "\r\n",
  2850. uLinesIndex, sLine);
  2851. strsDeinit(&self->lineBuffs);
  2852. goto __exit;
  2853. }
  2854. if (self->isGenBytecode) {
  2855. /* store ByteCode */
  2856. byteCodeFrame_appendFromAsm(self->bytecode_frame, sBackendCode);
  2857. } else {
  2858. /* store ASM */
  2859. aBackendCode = arg_strAppend(aBackendCode, sBackendCode);
  2860. }
  2861. __next_line:
  2862. if (uLinesIndex < uLinesNum) {
  2863. uLineSize = strGetSize(sLineOrigin);
  2864. uLinesOffset = uLinesOffset + uLineSize + 1;
  2865. }
  2866. strsDeinit(&self->lineBuffs);
  2867. /* exit when finished */
  2868. if (uLinesIndex >= uLinesNum + 1) {
  2869. break;
  2870. }
  2871. }
  2872. if (self->isGenBytecode) {
  2873. /* generate bytecode success */
  2874. sOut = (char*)1;
  2875. } else {
  2876. /* load stored ASM */
  2877. sOut = strsCopy(&self->genBuffs, arg_getStr(aBackendCode));
  2878. }
  2879. goto __exit;
  2880. __exit:
  2881. if (NULL != aBackendCode) {
  2882. arg_deinit(aBackendCode);
  2883. }
  2884. if (NULL != aLineConnection) {
  2885. arg_deinit(aLineConnection);
  2886. }
  2887. if (NULL != aDocstring) {
  2888. arg_deinit(aDocstring);
  2889. }
  2890. return sOut;
  2891. };
  2892. char* parser_lines2Asm(Parser* self, char* sPyLines) {
  2893. self->fn_ast2Target = parser_ast2Asm;
  2894. return parser_lines2Target(self, sPyLines);
  2895. }
  2896. PIKA_RES pika_lines2Bytes(ByteCodeFrame* bf, char* py_lines) {
  2897. #if PIKA_BYTECODE_ONLY_ENABLE
  2898. pika_platform_printf(
  2899. "Error: In bytecode-only mode, can not parse python script.\r\n");
  2900. pika_platform_printf(
  2901. " Note: Please check PIKA_BYTECODE_ONLY_ENABLE config.\r\n");
  2902. return PIKA_RES_ERR_SYNTAX_ERROR;
  2903. #else
  2904. Parser* parser = parser_create();
  2905. parser->isGenBytecode = pika_true;
  2906. parser->bytecode_frame = bf;
  2907. if (1 == (uintptr_t)parser_lines2Target(parser, py_lines)) {
  2908. parser_deinit(parser);
  2909. return PIKA_RES_OK;
  2910. }
  2911. parser_deinit(parser);
  2912. return PIKA_RES_ERR_SYNTAX_ERROR;
  2913. #endif
  2914. }
  2915. char* pika_lines2Asm(Args* outBuffs, char* multi_line) {
  2916. Parser* parser = parser_create();
  2917. parser->isGenBytecode = pika_false;
  2918. char* sAsm = parser_lines2Target(parser, multi_line);
  2919. if (NULL == sAsm) {
  2920. parser_deinit(parser);
  2921. return NULL;
  2922. }
  2923. sAsm = strsCopy(outBuffs, sAsm);
  2924. parser_deinit(parser);
  2925. return sAsm;
  2926. }
  2927. char* pika_file2Target(Args* outBuffs,
  2928. char* filename,
  2929. fn_parser_Lines2Target fn) {
  2930. Args buffs = {0};
  2931. Arg* file_arg = arg_loadFile(NULL, filename);
  2932. pika_assert(NULL != file_arg);
  2933. if (NULL == file_arg) {
  2934. pika_platform_printf("Error: Can not open file: %s\r\n", filename);
  2935. return NULL;
  2936. }
  2937. char* lines = (char*)arg_getBytes(file_arg);
  2938. /* replace the "\r\n" to "\n" */
  2939. lines = strsReplace(&buffs, lines, "\r\n", "\n");
  2940. /* clear the void line */
  2941. lines = strsReplace(&buffs, lines, "\n\n", "\n");
  2942. /* add '\n' at the end */
  2943. lines = strsAppend(&buffs, lines, "\n\n");
  2944. Parser* parser = parser_create();
  2945. char* res = fn(parser, lines);
  2946. if (NULL == res) {
  2947. goto __exit;
  2948. }
  2949. res = strsCopy(outBuffs, res);
  2950. __exit:
  2951. parser_deinit(parser);
  2952. arg_deinit(file_arg);
  2953. strsDeinit(&buffs);
  2954. return res;
  2955. }
  2956. int parser_file2TargetFile(Parser* self,
  2957. char* sPyFile,
  2958. char* sDocFile,
  2959. fn_parser_Lines2Target fn) {
  2960. char* sBackendCode = pika_file2Target(&self->genBuffs, sPyFile, fn);
  2961. FILE* fp = pika_platform_fopen(sDocFile, "wb");
  2962. if (NULL == fp) {
  2963. return -1;
  2964. }
  2965. pika_assert(NULL != sBackendCode);
  2966. pika_platform_fwrite(sBackendCode, 1, strGetSize(sBackendCode), fp);
  2967. pika_platform_fclose(fp);
  2968. return 0;
  2969. }
  2970. char* pika_file2Asm(Args* outBuffs, char* filename) {
  2971. return pika_file2Target(outBuffs, filename, parser_lines2Asm);
  2972. }
  2973. char* parser_file2Doc(Parser* self, char* sPyFile) {
  2974. return pika_file2Target(&self->genBuffs, sPyFile, parser_lines2Doc);
  2975. }
  2976. char* _comprehension2Asm(Args* outBuffs,
  2977. int iBlockDeepth,
  2978. char* sSubStmt1,
  2979. char* sSbuStmt2,
  2980. char* sSubStmt3) {
  2981. Args buffs = {0};
  2982. /*
  2983. * generate code for comprehension:
  2984. * $tmp = []
  2985. * for <substmt2> in <substmt3>:
  2986. * $tmp.append(<substmt1>)
  2987. */
  2988. Arg* aLineOut = arg_newStr("$tmp = []\n");
  2989. aLineOut = arg_strAppend(
  2990. aLineOut, strsFormat(&buffs, PIKA_LINE_BUFF_SIZE, "for %s in %s:\n",
  2991. sSbuStmt2, sSubStmt3));
  2992. aLineOut = arg_strAppend(
  2993. aLineOut, strsFormat(&buffs, PIKA_LINE_BUFF_SIZE,
  2994. " $tmp.append(%s)\npass\n", sSubStmt1));
  2995. aLineOut = arg_strAddIndentMulti(aLineOut, 4 * iBlockDeepth);
  2996. char* sLineOut = arg_getStr(aLineOut);
  2997. Parser* parser = parser_create();
  2998. char* sAsmOut = parser_lines2Asm(parser, sLineOut);
  2999. size_t lenAsmOut = strGetSize(sAsmOut);
  3000. /* strip B0 */
  3001. sAsmOut[lenAsmOut - 3] = '\0';
  3002. sAsmOut = strsAppend(&buffs, sAsmOut, "0 REF $tmp\n");
  3003. /* skip B0\n */
  3004. sAsmOut = strsCopy(outBuffs, sAsmOut + 3);
  3005. parser_deinit(parser);
  3006. arg_deinit(aLineOut);
  3007. strsDeinit(&buffs);
  3008. return sAsmOut;
  3009. }
  3010. char* AST_genAsm(AST* oAST, AST* subAst, Args* outBuffs, char* sPikaAsm) {
  3011. int deepth = obj_getInt(oAST, "deepth");
  3012. Args buffs = {0};
  3013. char* buff = args_getBuff(&buffs, PIKA_SPRINTF_BUFF_SIZE);
  3014. /* comprehension */
  3015. if (NULL != AST_getNodeAttr(subAst, "list")) {
  3016. pika_sprintf(buff, "%d %s \n", deepth, "LST");
  3017. sPikaAsm = strsAppend(&buffs, sPikaAsm, buff);
  3018. }
  3019. /* Solve sub stmt */
  3020. while (1) {
  3021. QueueObj* subStmt = queueObj_popObj(subAst);
  3022. if (NULL == subStmt) {
  3023. break;
  3024. }
  3025. obj_setInt(oAST, "deepth", deepth + 1);
  3026. sPikaAsm = AST_genAsm(oAST, subStmt, &buffs, sPikaAsm);
  3027. }
  3028. /* Byte code generate rules */
  3029. const GenRule rules_after[] = {
  3030. {.ins = "RUN", .type = VAL_DYNAMIC, .ast = "method"},
  3031. {.ins = "OPT", .type = VAL_DYNAMIC, .ast = "operator"},
  3032. {.ins = "BYT", .type = VAL_DYNAMIC, .ast = "bytes"},
  3033. {.ins = "NUM", .type = VAL_DYNAMIC, .ast = "num"},
  3034. {.ins = "IMP", .type = VAL_DYNAMIC, .ast = "import"},
  3035. {.ins = "INH", .type = VAL_DYNAMIC, .ast = "inhert"},
  3036. {.ins = "REF", .type = VAL_DYNAMIC, .ast = "ref"},
  3037. {.ins = "STR", .type = VAL_DYNAMIC, .ast = "string"},
  3038. {.ins = "SLC", .type = VAL_NONEVAL, .ast = "slice"},
  3039. {.ins = "DCT", .type = VAL_NONEVAL, .ast = "dict"},
  3040. {.ins = "TPL", .type = VAL_NONEVAL, .ast = "tuple"},
  3041. {.ins = "NLS", .type = VAL_NONEVAL, .ast = "list"},
  3042. {.ins = "OUT", .type = VAL_DYNAMIC, .ast = "left"}};
  3043. /* comprehension */
  3044. if (NULL != AST_getNodeAttr(oAST, "comprehension")) {
  3045. int iBlockDeepth = AST_getBlockDeepthNow(oAST);
  3046. char* sSubStmt1 = AST_getNodeAttr(oAST, "substmt1");
  3047. char* sSubStmt2 = AST_getNodeAttr(oAST, "substmt2");
  3048. char* sSubStmt3 = AST_getNodeAttr(oAST, "substmt3");
  3049. sPikaAsm =
  3050. strsAppend(&buffs, sPikaAsm,
  3051. _comprehension2Asm(&buffs, iBlockDeepth, sSubStmt1,
  3052. sSubStmt2, sSubStmt3));
  3053. }
  3054. /* append the syntax item */
  3055. for (size_t i = 0; i < sizeof(rules_after) / sizeof(GenRule); i++) {
  3056. GenRule rule = rules_after[i];
  3057. char* sNodeVal = AST_getNodeAttr(subAst, rule.ast);
  3058. if (NULL != sNodeVal) {
  3059. /* e.g. "0 RUN print \n" */
  3060. pika_sprintf(buff, "%d %s ", deepth, rule.ins);
  3061. Arg* aBuff = arg_newStr(buff);
  3062. if (rule.type == VAL_DYNAMIC) {
  3063. aBuff = arg_strAppend(aBuff, sNodeVal);
  3064. }
  3065. aBuff = arg_strAppend(aBuff, "\n");
  3066. sPikaAsm = strsAppend(&buffs, sPikaAsm, arg_getStr(aBuff));
  3067. arg_deinit(aBuff);
  3068. }
  3069. }
  3070. obj_setInt(oAST, "deepth", deepth - 1);
  3071. goto __exit;
  3072. __exit:
  3073. sPikaAsm = strsCopy(outBuffs, sPikaAsm);
  3074. strsDeinit(&buffs);
  3075. return sPikaAsm;
  3076. }
  3077. char* ASM_addBlockDeepth(AST* ast,
  3078. Args* buffs_p,
  3079. char* pikaAsm,
  3080. uint8_t deepthOffset) {
  3081. pikaAsm = strsAppend(buffs_p, pikaAsm, (char*)"B");
  3082. char buff[11];
  3083. pikaAsm =
  3084. strsAppend(buffs_p, pikaAsm,
  3085. fast_itoa(buff, AST_getBlockDeepthNow(ast) + deepthOffset));
  3086. pikaAsm = strsAppend(buffs_p, pikaAsm, (char*)"\n");
  3087. return pikaAsm;
  3088. }
  3089. char* GenRule_toAsm(GenRule rule,
  3090. Args* buffs,
  3091. AST* ast,
  3092. char* pikaAsm,
  3093. int deepth) {
  3094. char* buff = args_getBuff(buffs, PIKA_SPRINTF_BUFF_SIZE);
  3095. /* parse stmt ast */
  3096. pikaAsm = AST_genAsm(ast, ast, buffs, pikaAsm);
  3097. /* e.g. "0 CTN \n" */
  3098. pika_sprintf(buff, "%d %s ", deepth, rule.ins);
  3099. Arg* aBuff = arg_newStr(buff);
  3100. if (rule.type == VAL_DYNAMIC) {
  3101. aBuff = arg_strAppend(aBuff, obj_getStr(ast, rule.ast));
  3102. }
  3103. if (rule.type == VAL_STATIC_) {
  3104. aBuff = arg_strAppend(aBuff, rule.val);
  3105. }
  3106. aBuff = arg_strAppend(aBuff, "\n");
  3107. pikaAsm = strsAppend(buffs, pikaAsm, arg_getStr(aBuff));
  3108. arg_deinit(aBuff);
  3109. return pikaAsm;
  3110. }
  3111. char* AST_genAsm_top(AST* oAST, Args* outBuffs) {
  3112. const GenRule rules_topAst[] = {
  3113. {.ins = "CTN", .type = VAL_NONEVAL, .ast = "continue"},
  3114. {.ins = "BRK", .type = VAL_NONEVAL, .ast = "break"},
  3115. {.ins = "DEL", .type = VAL_DYNAMIC, .ast = "del"},
  3116. {.ins = "GLB", .type = VAL_DYNAMIC, .ast = "global"},
  3117. {.ins = "RIS", .type = VAL_DYNAMIC, .ast = "raise"},
  3118. {.ins = "ASS", .type = VAL_NONEVAL, .ast = "assert"},
  3119. {.ins = "RET", .type = VAL_NONEVAL, .ast = "return"}};
  3120. /* generate code for block ast */
  3121. const GenRule rules_block[] = {
  3122. {.ins = "TRY", .type = VAL_NONEVAL, .ast = "try"},
  3123. {.ins = "EXP", .type = VAL_NONEVAL, .ast = "except"},
  3124. {.ins = "NEL", .type = VAL_STATIC_, .ast = "else", .val = "1"},
  3125. {.ins = "JEZ", .type = VAL_STATIC_, .ast = "if", .val = "1"},
  3126. {.ins = "JEZ", .type = VAL_STATIC_, .ast = "while", .val = "2"},
  3127. };
  3128. Args buffs = {0};
  3129. char* sPikaAsm = strsCopy(&buffs, "");
  3130. QueueObj* oExitBlock;
  3131. pika_bool bblockMatched = 0;
  3132. if (NULL == oAST) {
  3133. sPikaAsm = NULL;
  3134. goto __exit;
  3135. }
  3136. /* skip for docsting */
  3137. if (NULL != AST_getNodeAttr(oAST, "docstring")) {
  3138. goto __exit;
  3139. }
  3140. oExitBlock = obj_getObj(oAST, "exitBlock");
  3141. /* exiting from block */
  3142. if (oExitBlock != NULL) {
  3143. while (1) {
  3144. uint8_t uDeepthOffset = obj_getInt(oExitBlock, "top") -
  3145. obj_getInt(oExitBlock, "bottom") - 1;
  3146. char* sBlockType = queueObj_popStr(oExitBlock);
  3147. if (NULL == sBlockType) {
  3148. break;
  3149. }
  3150. /* goto the while start when exit while block */
  3151. if (strEqu(sBlockType, "while")) {
  3152. sPikaAsm =
  3153. ASM_addBlockDeepth(oAST, outBuffs, sPikaAsm, uDeepthOffset);
  3154. sPikaAsm = strsAppend(outBuffs, sPikaAsm, (char*)"0 JMP -1\n");
  3155. }
  3156. #if PIKA_SYNTAX_EXCEPTION_ENABLE
  3157. /* goto the while start when exit while block */
  3158. if (strEqu(sBlockType, "try")) {
  3159. sPikaAsm =
  3160. ASM_addBlockDeepth(oAST, outBuffs, sPikaAsm, uDeepthOffset);
  3161. sPikaAsm = strsAppend(outBuffs, sPikaAsm, (char*)"0 NTR \n");
  3162. sPikaAsm = strsAppend(outBuffs, sPikaAsm, (char*)"0 GER \n");
  3163. sPikaAsm = strsAppend(outBuffs, sPikaAsm, (char*)"0 JEZ 2\n");
  3164. }
  3165. #endif
  3166. /* goto the while start when exit while block */
  3167. if (strEqu(sBlockType, "for")) {
  3168. sPikaAsm =
  3169. ASM_addBlockDeepth(oAST, outBuffs, sPikaAsm, uDeepthOffset);
  3170. sPikaAsm = strsAppend(outBuffs, sPikaAsm, (char*)"0 JMP -1\n");
  3171. /* garbage collect for the list */
  3172. sPikaAsm =
  3173. ASM_addBlockDeepth(oAST, outBuffs, sPikaAsm, uDeepthOffset);
  3174. char _l_x[] = "$lx";
  3175. char block_deepth_char =
  3176. AST_getBlockDeepthNow(oAST) + uDeepthOffset + '0';
  3177. _l_x[sizeof(_l_x) - 2] = block_deepth_char;
  3178. sPikaAsm = strsAppend(outBuffs, sPikaAsm, (char*)"0 DEL ");
  3179. sPikaAsm = strsAppend(outBuffs, sPikaAsm, (char*)_l_x);
  3180. sPikaAsm = strsAppend(outBuffs, sPikaAsm, (char*)"\n");
  3181. }
  3182. /* return when exit method */
  3183. if (strEqu(sBlockType, "def")) {
  3184. sPikaAsm = ASM_addBlockDeepth(oAST, outBuffs, sPikaAsm,
  3185. uDeepthOffset + 1);
  3186. sPikaAsm = strsAppend(outBuffs, sPikaAsm, (char*)"0 RET \n");
  3187. }
  3188. /* return when exit class */
  3189. if (strEqu(sBlockType, "class")) {
  3190. sPikaAsm = ASM_addBlockDeepth(oAST, outBuffs, sPikaAsm,
  3191. uDeepthOffset + 1);
  3192. sPikaAsm =
  3193. strsAppend(outBuffs, sPikaAsm, (char*)"0 RAS $origin\n");
  3194. sPikaAsm = ASM_addBlockDeepth(oAST, outBuffs, sPikaAsm, 1);
  3195. sPikaAsm =
  3196. strsAppend(outBuffs, sPikaAsm, (char*)"0 NEW self\n");
  3197. sPikaAsm = strsAppend(outBuffs, sPikaAsm, (char*)"0 RET \n");
  3198. }
  3199. }
  3200. }
  3201. /* add block deepth */
  3202. /* example: B0 */
  3203. sPikaAsm = ASM_addBlockDeepth(oAST, outBuffs, sPikaAsm, 0);
  3204. /* "deepth" is invoke deepth, not the blockDeepth */
  3205. obj_setInt(oAST, "deepth", 0);
  3206. /* match block */
  3207. bblockMatched = 0;
  3208. if (strEqu(AST_getThisBlock(oAST), "for")) {
  3209. /* for "for" iter */
  3210. char* sArgIn = AST_getNodeAttr(oAST, "arg_in");
  3211. #if !PIKA_NANO_ENABLE
  3212. char* sArgInKv = NULL;
  3213. #endif
  3214. Arg* aNewAsm = arg_newStr("");
  3215. char _l_x[] = "$lx";
  3216. char sBlockDeepthCHar = '0';
  3217. sBlockDeepthCHar += AST_getBlockDeepthNow(oAST);
  3218. _l_x[sizeof(_l_x) - 2] = sBlockDeepthCHar;
  3219. /* init iter */
  3220. /* get the iter(_l<x>) */
  3221. sPikaAsm = AST_genAsm(oAST, oAST, &buffs, sPikaAsm);
  3222. aNewAsm = arg_strAppend(aNewAsm, "0 OUT ");
  3223. aNewAsm = arg_strAppend(aNewAsm, _l_x);
  3224. aNewAsm = arg_strAppend(aNewAsm, "\n");
  3225. sPikaAsm = strsAppend(&buffs, sPikaAsm, arg_getStr(aNewAsm));
  3226. arg_deinit(aNewAsm);
  3227. aNewAsm = arg_newStr("");
  3228. /* get next */
  3229. /* run next(_l<x>) */
  3230. /* check item is exist */
  3231. /*
  3232. $n = $lx.next()
  3233. EST $n
  3234. k, v = $n
  3235. DEL $n
  3236. */
  3237. #if !PIKA_NANO_ENABLE
  3238. if (_check_is_multi_assign(sArgIn)) {
  3239. sArgInKv = sArgIn;
  3240. sArgIn = "$tmp";
  3241. }
  3242. #endif
  3243. sPikaAsm = ASM_addBlockDeepth(oAST, outBuffs, sPikaAsm, 0);
  3244. aNewAsm = arg_strAppend(aNewAsm, "0 RUN ");
  3245. aNewAsm = arg_strAppend(aNewAsm, _l_x);
  3246. aNewAsm = arg_strAppend(aNewAsm,
  3247. ".__next__\n"
  3248. "0 OUT ");
  3249. aNewAsm = arg_strAppend(aNewAsm, sArgIn);
  3250. aNewAsm = arg_strAppend(aNewAsm,
  3251. "\n"
  3252. "0 EST ");
  3253. aNewAsm = arg_strAppend(aNewAsm, sArgIn);
  3254. aNewAsm = arg_strAppend(aNewAsm, "\n0 JEZ 2\n");
  3255. sPikaAsm = strsAppend(&buffs, sPikaAsm, arg_getStr(aNewAsm));
  3256. arg_deinit(aNewAsm);
  3257. #if !PIKA_NANO_ENABLE
  3258. if (NULL != sArgInKv) {
  3259. int out_num = 0;
  3260. while (1) {
  3261. char* item = Cursor_popToken(&buffs, &sArgInKv, ",");
  3262. if (item[0] == '\0') {
  3263. break;
  3264. }
  3265. char* stmt = strsFormat(&buffs, PIKA_LINE_BUFF_SIZE,
  3266. "%s = $tmp[%d]\n", item, out_num);
  3267. AST* ast_this = line2Ast_withBlockDeepth(
  3268. stmt, AST_getBlockDeepthNow(oAST) + 1);
  3269. sPikaAsm = strsAppend(&buffs, sPikaAsm,
  3270. AST_genAsm_top(ast_this, &buffs));
  3271. AST_deinit(ast_this);
  3272. out_num++;
  3273. }
  3274. sPikaAsm = ASM_addBlockDeepth(oAST, outBuffs, sPikaAsm, 1);
  3275. sPikaAsm = strsAppend(&buffs, sPikaAsm, "0 DEL $tmp\n");
  3276. }
  3277. #endif
  3278. bblockMatched = 1;
  3279. goto __exit;
  3280. }
  3281. if (strEqu(AST_getThisBlock(oAST), "elif")) {
  3282. /* skip if __else is 0 */
  3283. sPikaAsm = strsAppend(&buffs, sPikaAsm, "0 NEL 1\n");
  3284. /* parse stmt ast */
  3285. sPikaAsm = AST_genAsm(oAST, oAST, &buffs, sPikaAsm);
  3286. /* skip if stmt is 0 */
  3287. sPikaAsm = strsAppend(&buffs, sPikaAsm, "0 JEZ 1\n");
  3288. bblockMatched = 1;
  3289. goto __exit;
  3290. }
  3291. if (strEqu(AST_getThisBlock(oAST), "def")) {
  3292. #if !PIKA_NANO_ENABLE
  3293. char* sDefaultStmts = AST_getNodeAttr(oAST, "default");
  3294. #endif
  3295. sPikaAsm = strsAppend(&buffs, sPikaAsm, "0 DEF ");
  3296. sPikaAsm =
  3297. strsAppend(&buffs, sPikaAsm, AST_getNodeAttr(oAST, "declare"));
  3298. sPikaAsm = strsAppend(&buffs, sPikaAsm,
  3299. "\n"
  3300. "0 JMP 1\n");
  3301. #if !PIKA_NANO_ENABLE
  3302. if (NULL != sDefaultStmts) {
  3303. int iStmtNum =
  3304. _Cursor_count(sDefaultStmts, TOKEN_devider, ",", pika_true) + 1;
  3305. for (int i = 0; i < iStmtNum; i++) {
  3306. char* sStmt = Cursor_popToken(&buffs, &sDefaultStmts, ",");
  3307. char* sArgName = strsGetFirstToken(&buffs, sStmt, '=');
  3308. sPikaAsm = ASM_addBlockDeepth(oAST, &buffs, sPikaAsm, 1);
  3309. sPikaAsm = strsAppend(&buffs, sPikaAsm, "0 EST ");
  3310. sPikaAsm = strsAppend(&buffs, sPikaAsm, sArgName);
  3311. sPikaAsm = strsAppend(&buffs, sPikaAsm, "\n");
  3312. sPikaAsm = strsAppend(&buffs, sPikaAsm, "0 JNZ 2\n");
  3313. AST* ast_this = line2Ast_withBlockDeepth(
  3314. sStmt, AST_getBlockDeepthNow(oAST) + 1);
  3315. sPikaAsm = strsAppend(&buffs, sPikaAsm,
  3316. AST_genAsm_top(ast_this, &buffs));
  3317. AST_deinit(ast_this);
  3318. }
  3319. }
  3320. #endif
  3321. bblockMatched = 1;
  3322. goto __exit;
  3323. }
  3324. if (strEqu(AST_getThisBlock(oAST), "class")) {
  3325. char* sDeclare = obj_getStr(oAST, "declare");
  3326. char* sThisClass = NULL;
  3327. char* sSuperClass = NULL;
  3328. if (strIsContain(sDeclare, '(')) {
  3329. sThisClass = strsGetFirstToken(&buffs, sDeclare, '(');
  3330. sSuperClass = strsCut(&buffs, sDeclare, '(', ')');
  3331. } else {
  3332. sThisClass = sDeclare;
  3333. sSuperClass = "";
  3334. }
  3335. if (strEqu("", sSuperClass)) {
  3336. /* default superClass */
  3337. sSuperClass = "TinyObj";
  3338. }
  3339. if (strEqu("TinyObj", sSuperClass)) {
  3340. /* default superClass */
  3341. sSuperClass = "TinyObj";
  3342. }
  3343. sPikaAsm = strsAppend(&buffs, sPikaAsm, "0 CLS ");
  3344. sPikaAsm = strsAppend(&buffs, sPikaAsm,
  3345. strsAppend(&buffs, sThisClass,
  3346. "()\n"
  3347. "0 JMP 1\n"));
  3348. char sBlockDeepth[] = "B0\n";
  3349. /* goto deeper block */
  3350. sBlockDeepth[1] += AST_getBlockDeepthNow(oAST) + 1;
  3351. sPikaAsm = strsAppend(&buffs, sPikaAsm, sBlockDeepth);
  3352. sPikaAsm = strsAppend(&buffs, sPikaAsm, "0 RUN ");
  3353. sPikaAsm = strsAppend(&buffs, sPikaAsm, sSuperClass);
  3354. sPikaAsm = strsAppend(&buffs, sPikaAsm, "\n");
  3355. sPikaAsm = strsAppend(&buffs, sPikaAsm, "0 OUT self\n");
  3356. sPikaAsm = strsAppend(&buffs, sPikaAsm, sBlockDeepth);
  3357. sPikaAsm = strsAppend(&buffs, sPikaAsm, "0 RAS self\n");
  3358. bblockMatched = 1;
  3359. goto __exit;
  3360. }
  3361. for (size_t i = 0; i < sizeof(rules_block) / sizeof(GenRule); i++) {
  3362. GenRule rule = rules_block[i];
  3363. if (strEqu(AST_getThisBlock(oAST), rule.ast)) {
  3364. sPikaAsm = GenRule_toAsm(rule, &buffs, oAST, sPikaAsm, 0);
  3365. bblockMatched = 1;
  3366. goto __exit;
  3367. }
  3368. }
  3369. /* generate code for top level ast */
  3370. for (size_t i = 0; i < sizeof(rules_topAst) / sizeof(rules_topAst[0]);
  3371. i++) {
  3372. GenRule item = rules_topAst[i];
  3373. if (obj_isArgExist(oAST, item.ast)) {
  3374. sPikaAsm = GenRule_toAsm(item, &buffs, oAST, sPikaAsm, 0);
  3375. bblockMatched = 1;
  3376. goto __exit;
  3377. }
  3378. }
  3379. __exit:
  3380. if (NULL == sPikaAsm) {
  3381. strsDeinit(&buffs);
  3382. return NULL;
  3383. }
  3384. if (!bblockMatched) {
  3385. /* parse stmt ast */
  3386. sPikaAsm = AST_genAsm(oAST, oAST, &buffs, sPikaAsm);
  3387. }
  3388. /* output pikaAsm */
  3389. sPikaAsm = strsCopy(outBuffs, sPikaAsm);
  3390. strsDeinit(&buffs);
  3391. return sPikaAsm;
  3392. }
  3393. #define IS_SPACE_OR_TAB(ch) ((ch) == ' ' || (ch) == '\t')
  3394. static pika_bool _strCheckCodeBlockFlag(char* sLine) {
  3395. pika_bool bStart = pika_false, bEnd = pika_false;
  3396. char *sStart = sLine, *pEnd = sLine + strlen(sLine) - 1;
  3397. while (sStart <= pEnd && IS_SPACE_OR_TAB(*sStart)) {
  3398. sStart++;
  3399. }
  3400. while (pEnd >= sStart && IS_SPACE_OR_TAB(*pEnd)) {
  3401. pEnd--;
  3402. }
  3403. if (pEnd - sStart < 2) {
  3404. return pika_false;
  3405. }
  3406. if (strncmp(sStart, "```", 3) == 0) {
  3407. bStart = pika_true;
  3408. }
  3409. if (pEnd - sStart >= 5 && strncmp(pEnd - 2, "```", 3) == 0) {
  3410. bEnd = pika_true;
  3411. }
  3412. if (bStart && bEnd) {
  3413. return pika_false;
  3414. }
  3415. if (bStart || bEnd) {
  3416. return pika_true;
  3417. }
  3418. return pika_false;
  3419. }
  3420. static char* _parser_fixDocStringIndent(Parser* self,
  3421. char* sDocString,
  3422. int iIndent) {
  3423. Args buffs = {0};
  3424. char* sBuff = strsCopy(&buffs, sDocString);
  3425. Arg* aOut = arg_newStr("");
  3426. char* sOut = NULL;
  3427. uint32_t iLineNum = strCountSign(sBuff, '\n');
  3428. pika_bool bInCodeBlock = pika_false;
  3429. int iIndentCodeBlock = 0;
  3430. for (int i = 0; i < iLineNum; i++) {
  3431. char* sLine = strsPopToken(&buffs, &sBuff, '\n');
  3432. if (strIsBlank(sLine)) {
  3433. continue;
  3434. }
  3435. int iIndentThis = strGetIndent(sLine);
  3436. int iIndentStrip = iIndentThis;
  3437. pika_bool bCodeBlockFlag = _strCheckCodeBlockFlag(sLine);
  3438. if (bCodeBlockFlag) {
  3439. bInCodeBlock = !bInCodeBlock;
  3440. iIndentCodeBlock = iIndentStrip;
  3441. }
  3442. if (bInCodeBlock) {
  3443. iIndentStrip = iIndentCodeBlock;
  3444. }
  3445. if (strGetIndent(sLine) >= iIndentStrip) {
  3446. sLine = sLine + iIndentStrip;
  3447. }
  3448. for (int k = 0; k < iIndent; k++) {
  3449. aOut = arg_strAppend(aOut, " ");
  3450. }
  3451. aOut = arg_strAppend(aOut, sLine);
  3452. aOut = arg_strAppend(aOut, "\n");
  3453. if (!bInCodeBlock) {
  3454. iIndentCodeBlock = 0;
  3455. aOut = arg_strAppend(aOut, "\n");
  3456. }
  3457. }
  3458. sOut = strsCopy(&self->lineBuffs, arg_getStr(aOut));
  3459. strsDeinit(&buffs);
  3460. arg_deinit(aOut);
  3461. return sOut;
  3462. }
  3463. char* parser_ast2Doc(Parser* self, AST* oAST) {
  3464. if (strEqu(AST_getThisBlock(oAST), "def")) {
  3465. char* sDeclare = AST_getNodeAttr(oAST, "raw");
  3466. int blockDeepth = AST_getBlockDeepthNow(oAST);
  3467. self->thisBlockDeepth = blockDeepth;
  3468. char* sOut =
  3469. strsFormat(&self->lineBuffs, 2048, "def %s:...\r\n", sDeclare);
  3470. for (int i = 0; i < blockDeepth; i++) {
  3471. sOut = strsAppend(&self->lineBuffs, "", sOut);
  3472. }
  3473. sOut =
  3474. strsFormat(&self->lineBuffs, 2048, "``` python\n%s```\n\n", sOut);
  3475. return sOut;
  3476. };
  3477. if (strEqu(AST_getThisBlock(oAST), "class")) {
  3478. char* sDeclare = obj_getStr(oAST, "declare");
  3479. int blockDeepth = AST_getBlockDeepthNow(oAST);
  3480. self->thisBlockDeepth = blockDeepth;
  3481. return strsFormat(&self->lineBuffs, 2048, "### class %s:\r\n",
  3482. sDeclare);
  3483. };
  3484. char* sDocString = AST_getNodeAttr(oAST, "docstring");
  3485. if (NULL != sDocString) {
  3486. sDocString = _parser_fixDocStringIndent(self, sDocString, 0);
  3487. return strsAppend(&self->lineBuffs, sDocString, "\n");
  3488. }
  3489. return "";
  3490. }
  3491. int parser_file2DocFile(Parser* self, char* sPyFile, char* sDocFile) {
  3492. return parser_file2TargetFile(self, sPyFile, sDocFile, parser_lines2Doc);
  3493. }
  3494. char* parser_lines2Doc(Parser* self, char* sPyLines) {
  3495. self->fn_ast2Target = parser_ast2Doc;
  3496. return parser_lines2Target(self, sPyLines);
  3497. }
  3498. char* parser_ast2Asm(Parser* self, AST* ast) {
  3499. return AST_genAsm_top(ast, &self->lineBuffs);
  3500. }
  3501. int32_t AST_deinit(AST* ast) {
  3502. return obj_deinit(ast);
  3503. }
  3504. ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self,
  3505. char* sPikaAsm) {
  3506. Asmer asmer = {
  3507. .asm_code = sPikaAsm,
  3508. .block_deepth_now = 0,
  3509. .is_new_line = 0,
  3510. .line_pointer = sPikaAsm,
  3511. };
  3512. uint16_t uConstPoolOffset;
  3513. uint16_t uExistOffset;
  3514. for (int i = 0; i < strCountSign(sPikaAsm, '\n'); i++) {
  3515. Args buffs = {0};
  3516. char* sLine = strsGetLine(&buffs, asmer.line_pointer);
  3517. char* sData = NULL;
  3518. char sIns[4] = "";
  3519. char sInvokeDeepth[3] = "";
  3520. uint8_t uSpaceNum = 0;
  3521. uint8_t iInvokeDeepth = 0;
  3522. uint8_t iInsStr = 0;
  3523. Arg* aLineBuff = arg_newStr(sLine);
  3524. strsDeinit(&buffs);
  3525. sLine = arg_getStr(aLineBuff);
  3526. InstructUnit ins_unit = {0};
  3527. /* remove '\r' */
  3528. if (sLine[strGetSize(sLine) - 1] == '\r') {
  3529. sLine[strGetSize(sLine) - 1] = 0;
  3530. }
  3531. /* process block deepth flag*/
  3532. if ('B' == sLine[0]) {
  3533. asmer.block_deepth_now = fast_atoi(sLine + 1);
  3534. asmer.is_new_line = 1;
  3535. goto __next_line;
  3536. }
  3537. /* process each ins */
  3538. /* get constPool offset */
  3539. uConstPoolOffset = 0;
  3540. for (int i = 0; i < (int)strGetSize(sLine); i++) {
  3541. if (uSpaceNum < 2) {
  3542. if (sLine[i] == ' ') {
  3543. uSpaceNum++;
  3544. if (uSpaceNum == 2) {
  3545. sData = sLine + i + 1;
  3546. break;
  3547. }
  3548. continue;
  3549. }
  3550. }
  3551. if (uSpaceNum == 0) {
  3552. sInvokeDeepth[iInvokeDeepth++] = sLine[i];
  3553. continue;
  3554. }
  3555. if (uSpaceNum == 1) {
  3556. sIns[iInsStr++] = sLine[i];
  3557. continue;
  3558. }
  3559. }
  3560. uExistOffset = constPool_getOffsetByData(&(self->const_pool), sData);
  3561. /* get const offset */
  3562. if (strEqu(sData, "")) {
  3563. /* not need const value */
  3564. uConstPoolOffset = 0;
  3565. } else if (65535 == uExistOffset) {
  3566. /* push new const value */
  3567. uConstPoolOffset = constPool_getLastOffset(&(self->const_pool));
  3568. /* load const to const pool buff */
  3569. constPool_append(&(self->const_pool), sData);
  3570. } else {
  3571. /* use exist const value */
  3572. uConstPoolOffset = uExistOffset;
  3573. }
  3574. iInvokeDeepth = fast_atoi(sInvokeDeepth);
  3575. /* load Asm to byte code unit */
  3576. instructUnit_setBlockDeepth(&ins_unit, asmer.block_deepth_now);
  3577. instructUnit_setInvokeDeepth(&ins_unit, iInvokeDeepth);
  3578. instructUnit_setConstPoolIndex(&ins_unit, uConstPoolOffset);
  3579. instructUnit_setInstruct(&ins_unit, pikaVM_getInstructFromAsm(sIns));
  3580. if (asmer.is_new_line) {
  3581. instructUnit_setIsNewLine(&ins_unit, 1);
  3582. asmer.is_new_line = 0;
  3583. }
  3584. /* append instructUnit to instructArray */
  3585. instructArray_append(&(self->instruct_array), &ins_unit);
  3586. __next_line:
  3587. /* point to next line */
  3588. asmer.line_pointer += strGetLineSize(asmer.line_pointer) + 1;
  3589. arg_deinit(aLineBuff);
  3590. }
  3591. return self;
  3592. }
  3593. char* pika_lines2Array(char* sLines) {
  3594. ByteCodeFrame bytecode_frame;
  3595. byteCodeFrame_init(&bytecode_frame);
  3596. pika_lines2Bytes(&bytecode_frame, sLines);
  3597. /* do something */
  3598. byteCodeFrame_print(&bytecode_frame);
  3599. pika_platform_printf("\n\n/* clang-format off */\n");
  3600. pika_platform_printf("PIKA_PYTHON(\n");
  3601. pika_platform_printf("%s\n", sLines);
  3602. pika_platform_printf(")\n");
  3603. pika_platform_printf("/* clang-format on */\n");
  3604. byteCodeFrame_printAsArray(&bytecode_frame);
  3605. /* deinit */
  3606. byteCodeFrame_deinit(&bytecode_frame);
  3607. pika_platform_printf("\n\n");
  3608. return NULL;
  3609. }
  3610. Parser* parser_create(void) {
  3611. Parser* self = (Parser*)pikaMalloc(sizeof(Parser));
  3612. pika_platform_memset(self, 0, sizeof(Parser));
  3613. self->blockState.stack = pikaMalloc(sizeof(Stack));
  3614. /* generate asm as default */
  3615. self->fn_ast2Target = parser_ast2Asm;
  3616. pika_platform_memset(self->blockState.stack, 0, sizeof(Stack));
  3617. stack_init(self->blockState.stack);
  3618. /* -1 means not inited */
  3619. self->blockDeepthOrigin = _VAL_NEED_INIT;
  3620. return self;
  3621. }
  3622. int parser_deinit(Parser* self) {
  3623. stack_deinit(self->blockState.stack);
  3624. strsDeinit(&self->genBuffs);
  3625. pikaFree(self->blockState.stack, sizeof(Stack));
  3626. pikaFree(self, sizeof(Parser));
  3627. return 0;
  3628. }