syntax.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207
  1. #include"token.h"
  2. #include"lex.h"
  3. #include"../inter/interpreter.h"
  4. void factor(p_status *status, token_node *list);
  5. void power(p_status *status, token_node *list);
  6. void call_down(p_status *status, token_node *list);
  7. void paser_value(p_status *status, token_node *list);
  8. void element(p_status *status, token_node *list);
  9. void var_token(p_status *status, token_node *list);
  10. void polynomial(p_status *status, token_node *list);
  11. void bit_move(p_status *status, token_node *list);
  12. void command(p_status *status, token_node *list);
  13. void while_(p_status *status, token_node *list);
  14. void if_(p_status *status, token_node *list);
  15. void for_(p_status *status, token_node *list);
  16. void elif_(p_status *status, token_node *list);
  17. void block_(p_status *status, token_node *list);
  18. void top_exp(p_status *status, token_node *list);
  19. void negative(p_status *status, token_node *list);
  20. void bit_not(p_status *status, token_node *list);
  21. void bit_notor(p_status *status, token_node *list);
  22. void bit_or(p_status *status, token_node *list);
  23. void bit_and(p_status *status, token_node *list);
  24. void compare(p_status *status, token_node *list);
  25. void bool_and(p_status *status, token_node *list);
  26. void bool_or(p_status *status, token_node *list);
  27. void bool_not(p_status *status, token_node *list);
  28. void eq_number(p_status *status, token_node *list);
  29. void call_back_(p_status *status, token_node *list);
  30. void def_class(p_status *status, token_node *list);
  31. void ctrl_(p_status *status, token_node *list);
  32. void var_ctrl_(p_status *status, token_node *list);
  33. void return_(p_status *status, token_node *list);
  34. void formal_parameter(p_status *status, token_node *list);
  35. void list_(p_status *status, token_node *list);
  36. void dict_(p_status *status, token_node *list);
  37. void hide_list(p_status *status, token_node *list);
  38. void do_while_(p_status *status, token_node *list);
  39. void try_(p_status *status, token_node *list);
  40. void out_exception(p_status *status, token_node *list);
  41. void self_exp(p_status *status, token_node *list);
  42. void lambda_(p_status *status, token_node *list);
  43. void import_include(p_status *status, token_node *list);
  44. void chose_exp_(p_status *status, token_node *list);
  45. void svar_token(p_status *status, token_node *list);
  46. void paser_error(p_status *status, char *text);
  47. /*
  48. command_list : command
  49. | command_list command
  50. */
  51. void command_list(p_status *status, token_node *list){ // 多项式
  52. fprintf(status_log, "[info][grammar] mode status: command_list\n", text);
  53. token left, right, new_token;
  54. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  55. if(left.type == NON_command_list){ // 模式2
  56. fprintf(status_log, "[info][grammar] (command_list)reduce right\n");
  57. get_right_token(status, list, command, right); // 回调右边
  58. if(right.type == NON_command){
  59. new_token.type = NON_command_list;
  60. new_token.data_type = empty;
  61. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  62. return command_list(status, list); // 回调自己
  63. }
  64. else{ // 递归跳出[EOF_token]
  65. fprintf(status_log, "[info][grammar] (command_list)out\n");
  66. back_one_token(list, left);
  67. back_again(list, right);
  68. return;
  69. }
  70. }
  71. else if(left.type == EOF_token){ // 递归跳出的条件
  72. fprintf(status_log, "[info][grammar] (command_list)out again\n");
  73. back_one_token(list, left);
  74. return;
  75. }
  76. else{ // 模式1
  77. fprintf(status_log, "[info][grammar] (command_list)back one token to (command)\n");
  78. back_one_token(list, left);
  79. get_base_token(status, list, command, new_token);
  80. if(new_token.type != NON_command){
  81. back_one_token(list, new_token); // 往回[不匹配类型]
  82. return;
  83. }
  84. new_token.type = NON_command_list;
  85. add_node(list, new_token);
  86. return command_list(status, list); // 回调自己
  87. }
  88. }
  89. /*
  90. command : top_exp <ENTER>
  91. */
  92. void command(p_status *old_status, token_node *list){ // 多项式
  93. p_status status;
  94. status = *old_status;
  95. status.is_left = false; // is_left复位[只有top_exp会开启以及element会开启]
  96. fprintf(status_log, "[info][grammar] mode status: command\n", text);
  97. token left, new_token;
  98. left = pop_node(list); // 先弹出一个token 检查token
  99. if(left.type == WHILE_PASER){ // 是while类型的数据
  100. fprintf(status_log, "[info][grammar] (command)back one token to (while)\n");
  101. back_one_token(list, left);
  102. get_base_token(&status, list, while_, new_token);
  103. get_stop_token(status, list);
  104. push_statement(old_status->statement_base, new_token);
  105. }
  106. else if(left.type == IF_PASER){
  107. fprintf(status_log, "[info][grammar] (command)back one token to (if)\n");
  108. back_one_token(list, left);
  109. get_base_token(&status, list, if_, new_token);
  110. get_stop_token(status, list);
  111. push_statement(old_status->statement_base, new_token);
  112. }
  113. else if(left.type == FOR_PASER){
  114. fprintf(status_log, "[info][grammar] (command)back one token to (for)\n");
  115. back_one_token(list, left);
  116. get_base_token(&status, list, for_, new_token);
  117. get_stop_token(status, list);
  118. push_statement(old_status->statement_base, new_token);
  119. }
  120. else if(left.type == DEF_PASER || left.type == FUNC_PASER || left.type == CLASS_PASER || left.type == CLS_PASER || left.type == ACTION_PASER || left.type == SETUP_PASER || left.type == INLINE_PASER){
  121. fprintf(status_log, "[info][grammar] (command)back one token to (def_class)\n");
  122. back_one_token(list, left);
  123. get_base_token(&status, list, def_class, new_token);
  124. get_stop_token(status, list);
  125. push_statement(old_status->statement_base, new_token);
  126. }
  127. else if(left.type == BREAK_PASER || left.type == BROKEN_PASER || left.type == CONTINUE_PASER || left.type == CONTINUED_PASER ||
  128. left.type == RESTART_PASER || left.type == RESTARTED_PASER || left.type == REGO_PASER || left.type == REWENT_PASER){
  129. fprintf(status_log, "[info][grammar] (command)back one token to (ctrl_)\n");
  130. back_one_token(list, left);
  131. get_base_token(&status, list, ctrl_, new_token);
  132. get_stop_token(status, list);
  133. push_statement(old_status->statement_base, new_token);
  134. }
  135. else if(left.type == GLOBAL_PASER || left.type == DEFAULT_PASER || left.type == NOLOCAL_PASER){
  136. fprintf(status_log, "[info][grammar] (command)back one token to (var_ctrl_)\n");
  137. back_one_token(list, left);
  138. get_base_token(&status, list, var_ctrl_, new_token);
  139. get_stop_token(status, list);
  140. push_statement(old_status->statement_base, new_token);
  141. }
  142. else if(left.type == IMPORT_PASER || left.type == INCLUDE_PASER){
  143. fprintf(status_log, "[info][grammar] (command)back one token to (import_include)\n");
  144. back_one_token(list, left);
  145. get_base_token(&status, list, import_include, new_token);
  146. get_stop_token(status, list);
  147. push_statement(old_status->statement_base, new_token);
  148. }
  149. else if(left.type == RETURN_PASER){
  150. fprintf(status_log, "[info][grammar] (command)back one token to (return_)\n");
  151. back_one_token(list, left);
  152. get_base_token(&status, list, return_, new_token);
  153. get_stop_token(status, list);
  154. push_statement(old_status->statement_base, new_token);
  155. }
  156. else if(left.type == TRY_PASER){
  157. fprintf(status_log, "[info][grammar] (command)back one token to (try_)\n");
  158. back_one_token(list, left);
  159. get_base_token(&status, list, try_, new_token);
  160. get_stop_token(status, list);
  161. push_statement(old_status->statement_base, new_token);
  162. }
  163. else if(left.type == DO_PASER){
  164. fprintf(status_log, "[info][grammar] (command)back one token to (do/do...while)\n");
  165. back_one_token(list, left);
  166. get_base_token(&status, list, do_while_, new_token);
  167. get_stop_token(status, list);
  168. push_statement(old_status->statement_base, new_token);
  169. }
  170. else if(left.type == RAISE_PASER || left.type == THROW_PASER || left.type == ASSERT_PASER){
  171. fprintf(status_log, "[info][grammar] (command)back one token to (out_exception)\n");
  172. back_one_token(list, left);
  173. get_base_token(&status, list, out_exception, new_token);
  174. get_stop_token(status, list);
  175. push_statement(old_status->statement_base, new_token);
  176. }
  177. else if(left.type == ENTER_PASER){
  178. fprintf(status_log, "[info][grammar] (command)back <ENTER>\n");
  179. }
  180. else if(left.type == EOF_token){
  181. fprintf(status_log, "[info][grammar] (command)back <EOF>\n");
  182. back_one_token(list, left);
  183. goto return_back;
  184. }
  185. else{ // 表达式
  186. fprintf(status_log, "[info][grammar] (command)back one token to (top_exp)\n");
  187. back_one_token(list, left);
  188. status.is_left = true;
  189. get_base_token(&status, list, top_exp, new_token);
  190. status.is_left = false;
  191. if(new_token.type != NON_top_exp){
  192. back_one_token(list, new_token); // 往回[不匹配类型]
  193. return;
  194. }
  195. get_stop_token(status, list);
  196. push_statement(old_status->statement_base, new_token);
  197. }
  198. new_token.type = NON_command;
  199. add_node(list, new_token);
  200. return_back:
  201. return; // 回调自己
  202. }
  203. /*
  204. if_ : IF LB top_exp RB block
  205. */
  206. void if_(p_status *status, token_node *list){
  207. fprintf(status_log, "[info][grammar] mode status: if_\n");
  208. token if_t, exp_t, block_t, next_t, child_t, new_token;
  209. if_t = pop_node(list);
  210. if(if_t.type == IF_PASER){
  211. get_right_token(status,list,top_exp,exp_t);
  212. if(exp_t.type != NON_top_exp){ // 不是表达式
  213. paser_error(status, "Don't get 'top_exp'");
  214. }
  215. get_right_token(status,list,block_,block_t);
  216. if(block_t.type != NON_block){ // 不是表达式
  217. paser_error(status, "Don't get '{'");
  218. }
  219. statement *if_tmp = make_statement();
  220. if_tmp->type = if_branch;
  221. if_tmp->code.if_branch.done = make_if(exp_t.data.statement_value, block_t.data.statement_value);
  222. // 检查else和elseif
  223. el_again:
  224. get_pop_token(status,list,next_t);
  225. if(next_t.type == ENTER_PASER){ // 忽略Enter
  226. goto el_again;
  227. }
  228. if(next_t.type == ELIF_PASER || next_t.type == ELSE_PASER){ // elif
  229. back_one_token(list, next_t);
  230. get_base_token(status,list,elif_,child_t);
  231. if(child_t.type == NON_elif){
  232. append_elif(child_t.data.if_list_base, if_tmp->code.if_branch.done);
  233. goto el_again;
  234. }
  235. }
  236. else{
  237. back_again(list, next_t); // 下一次读取需要用safe_get_token
  238. }
  239. new_token.type = NON_if;
  240. new_token.data_type = statement_value;
  241. new_token.data.statement_value = if_tmp;
  242. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  243. token tmp_enter;
  244. tmp_enter.type = ENTER_PASER;
  245. tmp_enter.data_type = empty;
  246. back_again(list, tmp_enter); // push入一个ENTER
  247. return;
  248. }
  249. else{
  250. back_one_token(list, if_t);
  251. return;
  252. }
  253. }
  254. /*
  255. elif_ : ELIF LB top_exp RB block
  256. */
  257. void elif_(p_status *status, token_node *list){
  258. fprintf(status_log, "[info][grammar] mode status: elif_\n");
  259. token elif_t, exp_t, block_t, next_t, new_token;
  260. elif_t = pop_node(list);
  261. if(elif_t.type == ELIF_PASER){
  262. get_right_token(status,list,top_exp,exp_t);
  263. if(exp_t.type != NON_top_exp){ // 不是表达式
  264. paser_error(status, "Don't get 'top_exp'");
  265. }
  266. get_right_token(status,list,block_,block_t);
  267. if(block_t.type != NON_block){ // 不是表达式
  268. paser_error(status, "Don't get '{'");
  269. }
  270. if_list *if_tmp = make_if(exp_t.data.statement_value, block_t.data.statement_value);
  271. new_token.type = NON_elif;
  272. new_token.data_type = if_list_base;
  273. new_token.data.if_list_base = if_tmp;
  274. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  275. return;
  276. }
  277. if(elif_t.type == ELSE_PASER){ // else
  278. get_right_token(status,list,block_,block_t);
  279. if(block_t.type != NON_block){ // 不是表达式
  280. paser_error(status, "Don't get '{'");
  281. }
  282. if_list *if_tmp = make_if(NULL, block_t.data.statement_value);
  283. new_token.type = NON_elif;
  284. new_token.data_type = if_list_base;
  285. new_token.data.if_list_base = if_tmp;
  286. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  287. return;
  288. }
  289. else{
  290. back_one_token(list, elif_t);
  291. return;
  292. }
  293. }
  294. /*
  295. for_ : FOR LB top_exp COMMA top_exp COMMA top_exp RB block
  296. */
  297. void for_(p_status *status, token_node *list){
  298. fprintf(status_log, "[info][grammar] mode status: for_\n");
  299. token for_t, exp_1, exp_2, exp_3, block_t, next_t, child_t, comma_t,new_token;
  300. statement *exp_a, *exp_b, *exp_c;
  301. for_t = pop_node(list);
  302. if(for_t.type == FOR_PASER){
  303. bool is_for_in = false; // 是否为for in模式
  304. get_pop_token(status, list, exp_1);
  305. if(exp_1.type == COMMA_PASER){
  306. exp_a = NULL; // exp_1 = NULL;
  307. }
  308. else{
  309. back_one_token(list, exp_1);
  310. p_status new_status;
  311. new_status = *status;
  312. new_status.is_for = true;
  313. get_base_token(&new_status,list,top_exp,exp_1); // 不是使用right token,不需要执行safe_get_token
  314. new_status.is_for = false;
  315. if(exp_1.type != NON_top_exp){ // 不是表达式
  316. paser_error(status, "Don't get 'top_exp'");
  317. }
  318. get_pop_token(status, list, comma_t);
  319. if(comma_t.type == IN_PASER){ // for in 模式 -> exp1不可以为NULL
  320. is_for_in = true;
  321. exp_a = exp_1.data.statement_value;
  322. goto exp3;
  323. }
  324. else if(comma_t.type != COMMA_PASER){
  325. paser_error(status, "Don't get ',' in for cycle");
  326. }
  327. exp_a = exp_1.data.statement_value;
  328. }
  329. get_pop_token(status, list, exp_2);
  330. if(exp_2.type == COMMA_PASER){
  331. exp_b = NULL; // exp_1 = NULL;
  332. }
  333. else{
  334. back_one_token(list, exp_2);
  335. p_status new_status;
  336. new_status = *status;
  337. new_status.is_for = true;
  338. get_base_token(&new_status,list,top_exp,exp_2); // 不是使用right token,不需要执行safe_get_token
  339. new_status.is_for = false;
  340. if(exp_2.type != NON_top_exp){ // 不是表达式
  341. paser_error(status, "Don't get 'top_exp'");
  342. }
  343. get_pop_token(status, list, comma_t);
  344. if(comma_t.type != COMMA_PASER){
  345. paser_error(status, "Don't get ',' in for cycle");
  346. }
  347. exp_b = exp_2.data.statement_value;
  348. }
  349. exp3:
  350. get_pop_token(status, list, exp_3);
  351. if(exp_3.type == COMMA_PASER){
  352. if(is_for_in){
  353. paser_error(status, "Don't get iter object");
  354. }
  355. exp_c = NULL; // exp_1 = NULL;
  356. }
  357. else{
  358. back_one_token(list, exp_3);
  359. p_status new_status;
  360. new_status = *status;
  361. if(!is_for_in) new_status.is_for = true;
  362. get_base_token(&new_status,list,top_exp,exp_3); // 不是使用right token,不需要执行safe_get_token
  363. if(!is_for_in) new_status.is_for = false;
  364. if(exp_3.type != NON_top_exp){ // 不是表达式
  365. paser_error(status, "Don't get 'top_exp'");
  366. }
  367. if(!is_for_in){ // for in 不需要匹配“,”
  368. get_pop_token(status, list, comma_t);
  369. if(comma_t.type != COMMA_PASER){
  370. paser_error(status, "Don't get ',' in for cycle");
  371. }
  372. }
  373. exp_c = exp_3.data.statement_value;
  374. }
  375. get_right_token(status,list,block_,block_t);
  376. if(block_t.type != NON_block){ // 不是表达式
  377. paser_error(status, "Don't get '{'");
  378. }
  379. statement *else_do = NULL;
  380. el_again:
  381. get_pop_token(status,list,next_t);
  382. if(next_t.type == ENTER_PASER){ // 忽略Enter
  383. goto el_again;
  384. }
  385. if(next_t.type == ELSE_PASER){ // else
  386. get_right_token(status,list,block_,child_t);
  387. if(child_t.type != NON_block){
  388. paser_error(status, "Don't get '{'\n");
  389. }
  390. else_do = child_t.data.statement_value;
  391. }
  392. else{
  393. back_again(list, next_t); // 下一次读取需要用safe_get_token
  394. }
  395. statement *for_tmp = make_statement();
  396. if(is_for_in){
  397. for_tmp->type = for_in_cycle;
  398. for_tmp->code.for_in_cycle.var = exp_a;
  399. for_tmp->code.for_in_cycle.iter = exp_c;
  400. for_tmp->code.for_in_cycle.done = block_t.data.statement_value;
  401. for_tmp->code.for_in_cycle.else_do = else_do;
  402. }
  403. else{
  404. for_tmp->type = for_cycle;
  405. for_tmp->code.for_cycle.first = exp_a;
  406. for_tmp->code.for_cycle.condition = exp_b;
  407. for_tmp->code.for_cycle.after = exp_c;
  408. for_tmp->code.for_cycle.done = block_t.data.statement_value;
  409. for_tmp->code.for_cycle.else_do = else_do;
  410. }
  411. new_token.type = NON_for;
  412. new_token.data_type = statement_value;
  413. new_token.data.statement_value = for_tmp;
  414. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  415. token tmp_enter;
  416. tmp_enter.type = ENTER_PASER;
  417. tmp_enter.data_type = empty;
  418. back_again(list, tmp_enter); // push入一个ENTER
  419. return;
  420. }
  421. else{
  422. back_one_token(list, for_t);
  423. return;
  424. }
  425. }
  426. /*
  427. def_ : DEF LB RB block
  428. */
  429. void def_class(p_status *status, token_node *list){
  430. fprintf(status_log, "[info][grammar] mode status: def_\n");
  431. token def_t, lb_t, rb_t, block_t, name_t, parameter_t, new_token;
  432. parameter *p_list;
  433. def_t = pop_node(list);
  434. if(def_t.type == SETUP_PASER){ // 动态函数
  435. get_right_token(status,list,block_,block_t); // 获取初始化信息
  436. if(block_t.type != NON_block){ // 不是表达式
  437. paser_error(status, "Don't get '{'");
  438. }
  439. token next, main_func;
  440. again:
  441. get_pop_token(status, list, next);
  442. if(next.type == DEF_PASER || next.type == FUNC_PASER || next.type == CLS_PASER || next.type == ACTION_PASER){ // 许可的loop
  443. back_one_token(list, next);
  444. get_base_token(status, list, def_class, main_func); // 不需要safe_get_token
  445. if(main_func.type != NON_def){
  446. goto error;
  447. }
  448. new_token = main_func;
  449. new_token.data.statement_value->code.def.setup = block_t.data.statement_value; // 设置setup
  450. }
  451. else if(next.type == ENTER_PASER){
  452. goto again;
  453. }
  454. else{
  455. error: paser_error(status, "Don't get main func for setup");
  456. }
  457. new_token.type = NON_def;
  458. new_token.data_type = statement_value;
  459. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  460. return;
  461. }
  462. if(def_t.type == INLINE_PASER){ // 动态函数
  463. token next, main_func;
  464. get_pop_token(status, list, next);
  465. if(next.type == DEF_PASER || next.type == FUNC_PASER || next.type == CLS_PASER || next.type == ACTION_PASER){ // 许可的loop
  466. back_one_token(list, next);
  467. get_base_token(status, list, def_class, main_func); // 不需要safe_get_token
  468. if(main_func.type != NON_def){
  469. goto error_inline;
  470. }
  471. new_token = main_func;
  472. new_token.data.statement_value->code.def.is_inline = true; // 设置setup
  473. }
  474. else{
  475. error_inline: paser_error(status, "Don't func for inline func");
  476. }
  477. new_token.type = NON_def;
  478. new_token.data_type = statement_value;
  479. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  480. return;
  481. }
  482. else if(def_t.type == DEF_PASER || def_t.type == FUNC_PASER || def_t.type == CLASS_PASER || def_t.type == CLS_PASER || def_t.type == ACTION_PASER){
  483. p_status new_status;
  484. new_status = *status;
  485. new_status.not_match_tuple = true;
  486. new_status.not_match_call = true;
  487. get_right_token(&new_status,list,top_exp,name_t); // 避免了top_exp把括号捕捉为call_back,不过,可以使用list设置status参数从而让call_back不捕捉[未实现]
  488. if(name_t.type != NON_top_exp){ // 不是表达式
  489. paser_error(status, "Don't get 'top_exp'");
  490. }
  491. get_pop_token(status, list, lb_t);
  492. if(lb_t.type != LB_PASER){
  493. paser_error(status, "Don't get '('");
  494. }
  495. get_pop_token(status, list, rb_t);
  496. if(rb_t.type != RB_PASER){ // 带参数
  497. back_one_token(list, rb_t);
  498. get_base_token(status,list,formal_parameter,parameter_t); // 不需要safe_get_token
  499. if(parameter_t.type != NON_parameter){
  500. paser_error(status, "Don't get formal_parameter");
  501. }
  502. get_pop_token(status, list, rb_t);
  503. if(rb_t.type != RB_PASER){
  504. paser_error(status, "Don't get ')'[1]");
  505. }
  506. p_list = parameter_t.data.parameter_list;
  507. }
  508. else{
  509. p_list = NULL; // 没参数
  510. }
  511. get_right_token(status,list,block_,block_t);
  512. if(block_t.type != NON_block){ // 不是表达式
  513. paser_error(status, "Don't get '{'");
  514. }
  515. statement *def_tmp = make_statement();
  516. if(def_t.type == DEF_PASER || def_t.type == FUNC_PASER || def_t.type == CLS_PASER || def_t.type == ACTION_PASER){
  517. def_tmp->type = def;
  518. def_tmp->code.def.var = name_t.data.statement_value;
  519. def_tmp->code.def.parameter_list = p_list;
  520. def_tmp->code.def.done = block_t.data.statement_value;
  521. def_tmp->code.def.setup = NULL;
  522. def_tmp->code.def.is_inline = false;
  523. switch(def_t.type){
  524. case DEF_PASER:
  525. def_tmp->code.def.type = auto_func;
  526. break;
  527. case CLS_PASER:
  528. def_tmp->code.def.type = cls;
  529. break;
  530. case FUNC_PASER:
  531. def_tmp->code.def.type = function;
  532. break;
  533. case ACTION_PASER:
  534. def_tmp->code.def.type = action;
  535. break;
  536. }
  537. }
  538. else{
  539. def_tmp->type = set_class;
  540. def_tmp->code.set_class.var = name_t.data.statement_value;
  541. def_tmp->code.set_class.father_list = p_list;
  542. def_tmp->code.set_class.done = block_t.data.statement_value;
  543. }
  544. new_token.type = NON_def;
  545. new_token.data_type = statement_value;
  546. new_token.data.statement_value = def_tmp;
  547. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  548. return;
  549. }
  550. else{
  551. back_one_token(list, def_t);
  552. return;
  553. }
  554. }
  555. void formal_parameter(p_status *status, token_node *list){ // 因试分解
  556. fprintf(status_log, "[info][grammar] mode status: formal_parameter\n");
  557. token left, next, comma, before, eq, value_token, new_token;
  558. int mode = only_value;
  559. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  560. if(left.type == NON_parameter){ // 模式2/3
  561. fprintf(status_log, "[info][grammar] (formal_parameter)reduce right\n");
  562. get_pop_token(status, list, comma);
  563. if(comma.type == (status->is_slice ? COLON_PASER : COMMA_PASER)){
  564. get_pop_token(status, list, before);
  565. if(before.type == MUL_PASER){
  566. if(status->match_dict){
  567. paser_error(status, "dict shouldn't get '*'");
  568. }
  569. mode = put_args;
  570. }
  571. else if(before.type == POW_PASER){
  572. if(status->match_list){
  573. paser_error(status, "list shouldn't get '*'");
  574. }
  575. mode = put_kwargs;
  576. }
  577. else{
  578. mode = only_value;
  579. back_again(list,before); // 回退
  580. }
  581. p_status new_status;
  582. new_status = *status;
  583. new_status.not_match_eq = true;
  584. new_status.not_match_tuple = true;
  585. token tmp_next;
  586. get_right_token(&new_status, list, top_exp, tmp_next);
  587. if(tmp_next.type != NON_top_exp){ // 结尾分号 -> 虚解包,或者slice填充None值
  588. if(!status->is_args){
  589. back_again(list, tmp_next);
  590. next.type = NON_top_exp;
  591. next.data_type = statement_value;
  592. next.data.statement_value = make_statement(); // NULL返回None
  593. }
  594. else{
  595. paser_error(status, "args shouldn't get * or ** with not var name");
  596. }
  597. }
  598. else{
  599. next = tmp_next;
  600. }
  601. new_token = left;
  602. parameter *tmp = NULL;
  603. get_pop_token(status, list, eq);
  604. if(eq.type == ((status->match_dict || status->is_peq) ? COLON_PASER : EQ_PASER)){ // name_value模式
  605. if(status->match_list){
  606. paser_error(status, "list shouldn't get '='");
  607. }
  608. p_status new_status;
  609. new_status = *status;
  610. if(!status->match_dict) new_status.not_match_eq = true; // dict使用了 : 代替 =
  611. new_status.not_match_tuple = true;
  612. get_right_token(&new_status, list, top_exp, value_token);
  613. if(value_token.type != NON_top_exp){
  614. paser_error(status, "Don't get a top_exp");
  615. return;
  616. }
  617. tmp = append_parameter_name(next.data.statement_value, new_token.data.parameter_list);
  618. tmp->type = name_value;
  619. tmp->u.value = value_token.data.statement_value;
  620. }
  621. else{
  622. if(status->match_dict){
  623. paser_error(status, "dict should get ':'[1]");
  624. }
  625. back_again(list,eq); // 回退[如果使用back_one_token则会导致add_node在EQ的后面]
  626. tmp = append_parameter_value(next.data.statement_value, new_token.data.parameter_list);
  627. tmp->type = mode;
  628. tmp->u.var = tmp->u.value;
  629. }
  630. add_node(list, new_token);
  631. return formal_parameter(status, list); // 回调自己
  632. }
  633. else{ // 递归跳出
  634. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  635. fprintf(status_log, "[info][grammar] (formal_parameter)out\n");
  636. back_one_token(list, left);
  637. back_again(list, comma);
  638. return;
  639. }
  640. }
  641. else if(left.type == POW_PASER || left.type == MUL_PASER){ // 模式1
  642. fprintf(status_log, "[info][grammar] (formal_parameter)back one token to (top_exp)[**/*]\n");
  643. p_status new_status;
  644. new_status = *status;
  645. new_status.not_match_eq = true;
  646. new_status.not_match_tuple = true;
  647. token tmp_next;
  648. get_right_token(&new_status, list, top_exp, tmp_next);
  649. if(tmp_next.type != NON_top_exp){ // 结尾分号 -> 虚解包
  650. if(!status->is_args){
  651. back_again(list, tmp_next);
  652. next.type = NON_top_exp;
  653. next.data_type = statement_value;
  654. next.data.statement_value = make_statement(); // NULL返回None
  655. }
  656. else{
  657. paser_error(status, "args shouldn't get * or ** with not var name");
  658. }
  659. }
  660. else{
  661. next = tmp_next;
  662. }
  663. new_token.type = NON_parameter;
  664. new_token.data_type = parameter_list;
  665. new_token.data.parameter_list = make_parameter_value(next.data.statement_value);
  666. new_token.data.parameter_list->u.var = new_token.data.parameter_list->u.value;
  667. if(left.type == POW_PASER){
  668. if(status->match_list){
  669. paser_error(status, "list shouldn't get '**'");
  670. }
  671. new_token.data.parameter_list->type = put_kwargs;
  672. }
  673. else{
  674. if(status->match_dict){
  675. paser_error(status, "dict shouldn't get '*'");
  676. }
  677. new_token.data.parameter_list->type = put_args;
  678. }
  679. add_node(list, new_token);
  680. return formal_parameter(status, list); // 回调自己
  681. }
  682. else{ // 模式1
  683. fprintf(status_log, "[info][grammar] (formal_parameter)back one token to (top_exp)\n");
  684. back_one_token(list, left);
  685. p_status new_status;
  686. new_status = *status;
  687. new_status.not_match_eq = true;
  688. new_status.not_match_tuple = true;
  689. get_base_token(&new_status, list, top_exp, next);
  690. if(next.type != NON_top_exp){
  691. back_one_token(list, next); // 往回[不匹配类型]
  692. return;
  693. }
  694. new_token.type = NON_parameter;
  695. new_token.data_type = parameter_list;
  696. parameter *tmp = NULL;
  697. get_pop_token(status, list, eq);
  698. if(eq.type == ((status->match_dict || status->is_peq) ? COLON_PASER : EQ_PASER)){ // name_value模式
  699. if(status->match_list){
  700. paser_error(status, "list shouldn't get '='");
  701. }
  702. p_status new_status;
  703. new_status = *status;
  704. reset_status(new_status);
  705. new_status.is_left = false;
  706. new_status.not_match_tuple = true;
  707. if(!status->match_dict) new_status.not_match_eq = true;
  708. get_right_token(&new_status, list, top_exp, value_token);
  709. if(value_token.type != NON_top_exp){
  710. paser_error(status, "Don't get a top_exp");
  711. return;
  712. }
  713. new_token.data.parameter_list = make_parameter_name(next.data.statement_value);
  714. new_token.data.parameter_list->u.value = value_token.data.statement_value;
  715. new_token.data.parameter_list->type = name_value;
  716. }
  717. else{
  718. if(status->match_dict){
  719. paser_error(status, "dict should get ':'[3]");
  720. }
  721. back_again(list,eq); // 回退[如果使用back_one_token则会导致add_node在EQ的后面]
  722. new_token.data.parameter_list = make_parameter_value(next.data.statement_value);
  723. new_token.data.parameter_list->u.var = new_token.data.parameter_list->u.value;
  724. new_token.data.parameter_list->type = only_value;
  725. }
  726. add_node(list, new_token);
  727. return formal_parameter(status, list); // 回调自己
  728. }
  729. }
  730. /*
  731. import “aaa” as AA 和 include 语句
  732. */
  733. void import_include(p_status *status, token_node *list){
  734. fprintf(status_log, "[info][grammar] mode status: while_\n");
  735. token type_t, str_t, as_t, var_t, new_token;
  736. type_t = pop_node(list);
  737. if(type_t.type == IMPORT_PASER || type_t.type == INCLUDE_PASER){
  738. get_right_token(status,list,top_exp,str_t);
  739. if(str_t.type != NON_top_exp){ // 不是表达式
  740. paser_error(status, "Don't get top_exp");
  741. }
  742. statement *tmp = make_statement();
  743. if(type_t.type == IMPORT_PASER){ // import才有as top_exp
  744. get_pop_token(status, list, as_t);
  745. if(as_t.type != AS_PASER){
  746. paser_error(status, "Don't get as");
  747. }
  748. get_right_token(status,list,top_exp,var_t);
  749. if(var_t.type != NON_top_exp){ // 不是表达式
  750. paser_error(status, "Don't get top_exp");
  751. }
  752. tmp->type = import_class;
  753. tmp->code.import_class.file = str_t.data.statement_value;
  754. tmp->code.import_class.var = var_t.data.statement_value;
  755. }
  756. else{
  757. tmp->type = include_import;
  758. tmp->code.include_import.file = str_t.data.statement_value;
  759. }
  760. new_token.type = NON_import;
  761. new_token.data_type = statement_value;
  762. new_token.data.statement_value = tmp;
  763. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  764. return;
  765. }
  766. else{
  767. back_one_token(list, type_t);
  768. return;
  769. }
  770. }
  771. /*
  772. while_ : WHILE LB top_exp RB block
  773. */
  774. void do_while_(p_status *status, token_node *list){
  775. fprintf(status_log, "[info][grammar] mode status: while_\n");
  776. token do_t, while_t, exp_t, block_t, next_t, child_t, new_token;
  777. do_t = pop_node(list);
  778. int mode = 0; // mode为1模式需要push enter
  779. if(do_t.type == DO_PASER){
  780. get_right_token(status,list,block_,block_t);
  781. if(block_t.type != NON_block){ // 不是表达式
  782. paser_error(status, "Don't get '{'");
  783. }
  784. statement *tmp = make_statement();
  785. get_pop_token(status, list, while_t);
  786. if(while_t.type != WHILE_PASER){ // block模式
  787. back_again(list,while_t);
  788. tmp->type = code_block;
  789. tmp->code.code_block.done = block_t.data.statement_value;
  790. mode = 0;
  791. }
  792. else{
  793. get_right_token(status,list,top_exp,exp_t);
  794. if(exp_t.type != NON_top_exp){ // 不是表达式
  795. paser_error(status, "Don't get 'top_exp'");
  796. }
  797. statement *else_do = NULL;
  798. el_again:
  799. get_pop_token(status,list,next_t);
  800. if(next_t.type == ENTER_PASER){ // 忽略Enter
  801. goto el_again;
  802. }
  803. if(next_t.type == ELSE_PASER){ // else
  804. get_right_token(status,list,block_,child_t);
  805. if(child_t.type != NON_block){
  806. paser_error(status, "Don't get '{'\n");
  807. }
  808. else_do = child_t.data.statement_value;
  809. }
  810. else{
  811. back_again(list, next_t); // 下一次读取需要用safe_get_token
  812. }
  813. tmp->type = while_cycle;
  814. tmp->code.while_cycle.condition = exp_t.data.statement_value;
  815. tmp->code.while_cycle.done = block_t.data.statement_value;
  816. tmp->code.while_cycle.first_do = true;
  817. tmp->code.while_cycle.else_do = else_do;
  818. mode = 1;
  819. }
  820. new_token.type = NON_do_while;
  821. new_token.data_type = statement_value;
  822. new_token.data.statement_value = tmp;
  823. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  824. if(mode){
  825. token tmp_enter;
  826. tmp_enter.type = ENTER_PASER;
  827. tmp_enter.data_type = empty;
  828. back_again(list, tmp_enter); // push入一个ENTER
  829. }
  830. return;
  831. }
  832. else{
  833. back_one_token(list, do_t);
  834. return;
  835. }
  836. }
  837. /*
  838. while_ : WHILE LB top_exp RB block
  839. */
  840. void while_(p_status *status, token_node *list){
  841. fprintf(status_log, "[info][grammar] mode status: while_\n");
  842. token while_t, exp_t, block_t, next_t, child_t, new_token;
  843. while_t = pop_node(list);
  844. if(while_t.type == WHILE_PASER){
  845. get_right_token(status,list,top_exp,exp_t);
  846. if(exp_t.type != NON_top_exp){ // 不是表达式
  847. paser_error(status, "Don't get 'top_exp'");
  848. }
  849. get_right_token(status,list,block_,block_t);
  850. if(block_t.type != NON_block){ // 不是表达式
  851. paser_error(status, "Don't get '{'");
  852. }
  853. statement *else_do = NULL;
  854. el_again:
  855. get_pop_token(status,list,next_t);
  856. if(next_t.type == ENTER_PASER){ // 忽略Enter
  857. goto el_again;
  858. }
  859. if(next_t.type == ELSE_PASER){ // else
  860. get_right_token(status,list,block_,child_t);
  861. if(child_t.type != NON_block){
  862. paser_error(status, "Don't get '{'\n");
  863. }
  864. else_do = child_t.data.statement_value;
  865. }
  866. else{
  867. back_again(list, next_t); // 下一次读取需要用safe_get_token
  868. }
  869. statement *while_tmp = make_statement();
  870. while_tmp->type = while_cycle;
  871. while_tmp->code.while_cycle.condition = exp_t.data.statement_value;
  872. while_tmp->code.while_cycle.done = block_t.data.statement_value;
  873. while_tmp->code.while_cycle.first_do = false;
  874. while_tmp->code.while_cycle.else_do = else_do;
  875. new_token.type = NON_while;
  876. new_token.data_type = statement_value;
  877. new_token.data.statement_value = while_tmp;
  878. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  879. token tmp_enter;
  880. tmp_enter.type = ENTER_PASER;
  881. tmp_enter.data_type = empty;
  882. back_again(list, tmp_enter); // push入一个ENTER
  883. return;
  884. }
  885. else{
  886. back_one_token(list, while_t);
  887. return;
  888. }
  889. }
  890. /*
  891. try_ : TRY block EXCEPT AS top_exp block
  892. */
  893. void try_(p_status *status, token_node *list){
  894. fprintf(status_log, "[info][grammar] mode status: while_\n");
  895. token try_t, try_block, except_t, as_t, var_t, except_block, child_t, next_t, new_token;
  896. statement *else_do = NULL ,*finally_do = NULL, *except_do = NULL, *var_do = NULL;
  897. bool mode_else = true, mode_finally = true;
  898. try_t = pop_node(list);
  899. if(try_t.type == TRY_PASER){
  900. get_right_token(status, list, block_, try_block);
  901. if(try_block.type != NON_block){
  902. paser_error(status, "Don't get '{'");
  903. }
  904. except_again:
  905. get_pop_token(status,list,except_t);
  906. if(except_t.type == ENTER_PASER){
  907. goto except_again;
  908. }
  909. else if(except_t.type != EXCEPT_PASER){ // 不是except
  910. back_again(list, except_t);
  911. goto el_again;
  912. }
  913. get_pop_token(status,list,as_t);
  914. if(as_t.type != AS_PASER){ // 不是except
  915. back_again(list, as_t);
  916. goto not_var;
  917. }
  918. get_right_token(status, list, top_exp, var_t);
  919. if(var_t.type != NON_top_exp){
  920. paser_error(status, "Don't get top_exp");
  921. }
  922. else{
  923. var_do = var_t.data.statement_value;
  924. }
  925. not_var:
  926. get_right_token(status,list,block_,except_block);
  927. if(except_block.type != NON_block){ // 不是表达式
  928. paser_error(status, "Don't get '{'");
  929. }
  930. else{
  931. except_do = except_block.data.statement_value;
  932. }
  933. el_again:
  934. get_pop_token(status,list,next_t);
  935. if(next_t.type == ENTER_PASER){ // 忽略Enter
  936. goto el_again;
  937. }
  938. if(next_t.type == ELSE_PASER){ // else
  939. if(mode_else){
  940. get_right_token(status,list,block_,child_t);
  941. if(child_t.type != NON_block){
  942. paser_error(status, "Don't get '{'\n");
  943. }
  944. else_do = child_t.data.statement_value;
  945. mode_else = false;
  946. goto el_again;
  947. }
  948. else{
  949. paser_error(status, "Get 'else' twict\n");
  950. }
  951. }
  952. if(next_t.type == FINALLY_PASER){ // else
  953. if(mode_finally){
  954. get_right_token(status,list,block_,child_t);
  955. if(child_t.type != NON_block){
  956. paser_error(status, "Don't get '{'\n");
  957. }
  958. finally_do = child_t.data.statement_value;
  959. mode_finally = false;
  960. goto el_again;
  961. }
  962. else{
  963. paser_error(status, "Get 'finally' twict\n");
  964. }
  965. }
  966. else{
  967. back_again(list, next_t); // 下一次读取需要用safe_get_token
  968. }
  969. statement *try_tmp = make_statement();
  970. try_tmp->type = try_code;
  971. try_tmp->code.try_code.try = try_block.data.statement_value;
  972. try_tmp->code.try_code.except = except_do;
  973. try_tmp->code.try_code.var = var_do;
  974. try_tmp->code.try_code.else_do = else_do;
  975. try_tmp->code.try_code.finally_do = finally_do;
  976. new_token.type = NON_try;
  977. new_token.data_type = statement_value;
  978. new_token.data.statement_value = try_tmp;
  979. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  980. token tmp_enter;
  981. tmp_enter.type = ENTER_PASER;
  982. tmp_enter.data_type = empty;
  983. back_again(list, tmp_enter); // push入一个ENTER
  984. return;
  985. }
  986. else{
  987. back_one_token(list, try_t);
  988. return;
  989. }
  990. }
  991. /*
  992. block_ : LP command_list RB
  993. */
  994. void block_(p_status *status, token_node *list){
  995. fprintf(status_log, "[info][grammar] mode status: block_\n");
  996. token lp_t, rp_t, new_token, command_list_t;
  997. lp_t = pop_node(list);
  998. again:
  999. if(lp_t.type == LP_PASER){
  1000. statement *block_tmp = make_statement();
  1001. p_status new_status = *status; // 继承file_p等值
  1002. reset_status(new_status); // 不会影响 *staus
  1003. new_status.dict_to_enter = true;
  1004. new_status.statement_base = block_tmp; // 设置新的statement
  1005. get_right_token(&new_status,list,command_list,command_list_t); // 要把command_list也弹出来
  1006. get_pop_token(status, list, rp_t);
  1007. if(rp_t.type != RP_PASER){
  1008. paser_error(status, "Don't get '}'");
  1009. }
  1010. new_token.type = NON_block;
  1011. new_token.data_type = statement_value;
  1012. new_token.data.statement_value = block_tmp;
  1013. add_node(list, new_token); // 压入节点
  1014. return;
  1015. }
  1016. if(lp_t.type == ENTER_PASER){
  1017. get_pop_token(status,list,lp_t);
  1018. goto again;
  1019. }
  1020. else{
  1021. back_one_token(list, lp_t);
  1022. return;
  1023. }
  1024. }
  1025. // var_ctrl_ 包含诸如:global, nolocal,之类的
  1026. void var_ctrl_(p_status *status, token_node *list){
  1027. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  1028. token left, var, right, new_token;
  1029. statement *times = NULL;
  1030. char *var_name = NULL;
  1031. left = pop_node(list);
  1032. if(left.type == GLOBAL_PASER || left.type == DEFAULT_PASER || left.type == NOLOCAL_PASER){
  1033. fprintf(status_log, "[info][grammar] (ctrl_)reduce right\n");
  1034. statement *base_tmp = NULL;
  1035. again:
  1036. {
  1037. p_status new_status;
  1038. new_status = *status;
  1039. new_status.not_match_tuple = true; // 不捕捉,
  1040. get_right_token(&new_status, list, top_exp, var); // 取得base_var
  1041. if(var.type != NON_top_exp && var.data.statement_value->type != base_var){
  1042. paser_error(status, "Don't get var");
  1043. }
  1044. else{
  1045. var_name = malloc(sizeof(var.data.statement_value->code.base_var.var_name));
  1046. strcpy(var_name, var.data.statement_value->code.base_var.var_name);
  1047. times = var.data.statement_value->code.base_var.from;
  1048. // TODO:: 本质上没有完全释放
  1049. free(var.data.statement_value->code.base_var.var_name);
  1050. free(var.data.statement_value);
  1051. }
  1052. if(left.type == DEFAULT_PASER){ // 设置times
  1053. get_right_token(&new_status, list, top_exp, right); // 回调右边
  1054. if(right.type != NON_top_exp){
  1055. back_again(list, right); // 不是期望的数字
  1056. }
  1057. else{
  1058. times = right.data.statement_value;
  1059. }
  1060. }
  1061. // 逻辑操作
  1062. new_token.type = NON_ctrl;
  1063. new_token.data_type = statement_value;
  1064. statement *code_tmp = make_statement();
  1065. if(base_tmp == NULL){
  1066. base_tmp = code_tmp;
  1067. }
  1068. else{
  1069. append_statement(base_tmp, code_tmp);
  1070. }
  1071. switch (left.type)
  1072. {
  1073. case GLOBAL_PASER:
  1074. code_tmp->type = set_global;
  1075. code_tmp->code.set_global.name = var_name;
  1076. break;
  1077. case DEFAULT_PASER:
  1078. code_tmp->type = set_default;
  1079. code_tmp->code.set_default.name = var_name;
  1080. code_tmp->code.set_default.times = times;
  1081. break;
  1082. case NOLOCAL_PASER:
  1083. code_tmp->type = set_nonlocal;
  1084. code_tmp->code.set_nonlocal.name = var_name;
  1085. break;
  1086. default:
  1087. break;
  1088. }
  1089. }
  1090. token comma_t;
  1091. get_pop_token(status,list,comma_t);
  1092. if(comma_t.type == COMMA_PASER){
  1093. goto again;
  1094. }
  1095. else{
  1096. back_again(list, comma_t);
  1097. }
  1098. new_token.data.statement_value = base_tmp;
  1099. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1100. return; // 回调自己
  1101. }
  1102. else{ // 模式1
  1103. back_one_token(list, left);
  1104. return;
  1105. }
  1106. }
  1107. // raise throw 和 assert
  1108. void out_exception(p_status *status, token_node *list){
  1109. fprintf(status_log, "[info][grammar] mode status: out_exception\n");
  1110. token left, exp1, exp2, new_token;
  1111. left = pop_node(list);
  1112. if(left.type == RAISE_PASER || left.type == THROW_PASER || left.type == ASSERT_PASER){
  1113. fprintf(status_log, "[info][grammar] (out_exception)reduce right\n");
  1114. get_right_token(status, list, top_exp, exp1); // 取得base_var
  1115. if(exp1.type != NON_top_exp){
  1116. paser_error(status, "Don't get top_exp");
  1117. }
  1118. if(left.type != THROW_PASER){ // 设置times
  1119. get_right_token(status, list, top_exp, exp2); // 回调右边
  1120. if(exp2.type != NON_top_exp){
  1121. paser_error(status, "Don't get top_exp");
  1122. }
  1123. }
  1124. // 逻辑操作
  1125. new_token.type = NON_exception;
  1126. new_token.data_type = statement_value;
  1127. statement *code_tmp = make_statement();
  1128. switch (left.type)
  1129. {
  1130. case RAISE_PASER:
  1131. code_tmp->type = raise_e;
  1132. code_tmp->code.raise_e.done = exp1.data.statement_value;
  1133. code_tmp->code.raise_e.info = exp2.data.statement_value;
  1134. break;
  1135. case THROW_PASER:
  1136. code_tmp->type = throw_e;
  1137. code_tmp->code.throw_e.done = exp1.data.statement_value;
  1138. break;
  1139. case ASSERT_PASER:
  1140. code_tmp->type = assert_e;
  1141. code_tmp->code.assert_e.condition = exp1.data.statement_value;
  1142. code_tmp->code.assert_e.info = exp2.data.statement_value;
  1143. break;
  1144. default:
  1145. break;
  1146. }
  1147. new_token.data.statement_value = code_tmp;
  1148. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1149. return; // 回调自己
  1150. }
  1151. else{ // 模式1
  1152. back_one_token(list, left);
  1153. return;
  1154. }
  1155. }
  1156. void return_(p_status *status, token_node *list){
  1157. fprintf(status_log, "[info][grammar] mode status: return_\n");
  1158. token left, right, value, new_token;
  1159. statement *times = NULL, *back_value = NULL;
  1160. left = pop_node(list);
  1161. if(left.type == RETURN_PASER){
  1162. fprintf(status_log, "[info][grammar] (return_)reduce right\n");
  1163. get_right_token(status, list, top_exp, value); // 回调右边
  1164. if(value.type != NON_top_exp){
  1165. back_again(list, value); // 不是期望的数字,就默认使用NULL,并且回退
  1166. times = NULL;
  1167. back_value = NULL;
  1168. goto not_times;
  1169. }
  1170. else{
  1171. back_value = value.data.statement_value;
  1172. }
  1173. get_right_token(status, list, top_exp, right); // 回调右边
  1174. if(right.type != NON_top_exp){
  1175. back_again(list, right); // 不是期望的数字,就默认使用NULL,并且回退
  1176. times = NULL;
  1177. }
  1178. else{
  1179. times = right.data.statement_value;
  1180. }
  1181. not_times:
  1182. new_token.type = NON_return;
  1183. new_token.data_type = statement_value;
  1184. statement *code_tmp = make_statement();
  1185. code_tmp->type = return_code;
  1186. code_tmp->code.return_code.times = times;
  1187. code_tmp->code.return_code.value = back_value;
  1188. new_token.data.statement_value = code_tmp;
  1189. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1190. return; // 回调自己
  1191. }
  1192. else{ // 模式1
  1193. back_one_token(list, left);
  1194. return;
  1195. }
  1196. }
  1197. // ctrl_包含诸如:break,broken,之类的
  1198. void ctrl_(p_status *status, token_node *list){
  1199. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  1200. token left, right, new_token;
  1201. statement *times = NULL;
  1202. left = pop_node(list);
  1203. if(left.type == BREAK_PASER || left.type == BROKEN_PASER || left.type == CONTINUE_PASER || left.type == CONTINUED_PASER ||
  1204. left.type == RESTART_PASER || left.type == RESTARTED_PASER || left.type == REGO_PASER || left.type == REWENT_PASER){
  1205. fprintf(status_log, "[info][grammar] (ctrl_)reduce right\n");
  1206. if(left.type != REGO_PASER && left.type != REWENT_PASER){
  1207. get_right_token(status, list, top_exp, right); // 回调右边
  1208. if(right.type != NON_top_exp){
  1209. back_again(list, right); // 不是期望的数字,就默认使用NULL,并且回退
  1210. times = NULL;
  1211. }
  1212. else{
  1213. times = right.data.statement_value;
  1214. }
  1215. }
  1216. // 逻辑操作
  1217. new_token.type = NON_ctrl;
  1218. new_token.data_type = statement_value;
  1219. statement *code_tmp = make_statement();
  1220. switch (left.type)
  1221. {
  1222. case BREAK_PASER:
  1223. code_tmp->type = break_cycle;
  1224. code_tmp->code.break_cycle.times = times;
  1225. break;
  1226. case BROKEN_PASER:
  1227. code_tmp->type = broken;
  1228. code_tmp->code.broken.times = times;
  1229. break;
  1230. case CONTINUE_PASER:
  1231. code_tmp->type = continue_cycle;
  1232. code_tmp->code.continue_cycle.times = times;
  1233. break;
  1234. case CONTINUED_PASER:
  1235. code_tmp->type = continued;
  1236. code_tmp->code.continued.times = times;
  1237. break;
  1238. case RESTART_PASER:
  1239. code_tmp->type = restart;
  1240. code_tmp->code.restart.times = times;
  1241. break;
  1242. case RESTARTED_PASER:
  1243. code_tmp->type = restarted;
  1244. code_tmp->code.restarted.times = times;
  1245. break;
  1246. case REGO_PASER:
  1247. code_tmp->type = rego;
  1248. break;
  1249. case REWENT_PASER:
  1250. code_tmp->type = rewent;
  1251. break;
  1252. default:
  1253. break;
  1254. }
  1255. new_token.data.statement_value = code_tmp;
  1256. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1257. return; // 回调自己
  1258. }
  1259. else{ // 模式1
  1260. back_one_token(list, left);
  1261. return;
  1262. }
  1263. }
  1264. /*
  1265. top_exp : polynomial
  1266. */
  1267. void top_exp(p_status *status, token_node *list){
  1268. fprintf(status_log, "[info][grammar] mode status: top_exp\n");
  1269. token exp;
  1270. get_base_token(status,list,eq_number,exp);
  1271. if(exp.type != NON_eq){
  1272. back_one_token(list, exp);
  1273. return;
  1274. }
  1275. exp.type = NON_top_exp;
  1276. add_node(list, exp); // 压入节点
  1277. return;
  1278. }
  1279. /*
  1280. eq_number : bool_or
  1281. | eq_number AND bool_or
  1282. */
  1283. void eq_number(p_status *status, token_node *list){ // 因试分解
  1284. fprintf(status_log, "[info][grammar] mode status: eq_number\n");
  1285. token left, right, symbol, new_token;
  1286. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1287. if(left.type == NON_eq){ // 模式2/3
  1288. fprintf(status_log, "[info][grammar] (eq_number)reduce right\n");
  1289. get_pop_token(status, list, symbol);
  1290. if((symbol.type == EQ_PASER && !status->not_match_eq) || symbol.type == AADD_PASER || symbol.type == ASUB_PASER || symbol.type == AMUL_PASER || symbol.type == ADIV_PASER||
  1291. symbol.type == AMOD_PASER || symbol.type == AINTDIV_PASER || symbol.type == APOW_PASER || symbol.type == ABITAND_PASER || symbol.type == ABITOR_PASER || symbol.type == ABITNOTOR_PASER ||
  1292. symbol.type == ALEFT_PASER || symbol.type == ARIGHT_PASER){ // 模式2/3
  1293. get_right_token(status, list, hide_list, right); // 回调右边
  1294. if(right.type != NON_hide_list){
  1295. paser_error(status, "Don't get a hide_list");
  1296. }
  1297. // 逻辑操作
  1298. new_token.type = NON_eq;
  1299. new_token.data_type = statement_value;
  1300. statement *code_tmp = make_statement();
  1301. code_tmp->type = operation;
  1302. switch (symbol.type)
  1303. {
  1304. case EQ_PASER:
  1305. code_tmp->code.operation.type = ASSIGnMENT_func;
  1306. break;
  1307. case AADD_PASER:
  1308. code_tmp->code.operation.type = AADD_func;
  1309. break;
  1310. case ASUB_PASER:
  1311. code_tmp->code.operation.type = ASUB_func;
  1312. break;
  1313. case AMUL_PASER:
  1314. code_tmp->code.operation.type = AMUL_func;
  1315. break;
  1316. case ADIV_PASER:
  1317. code_tmp->code.operation.type = ADIV_func;
  1318. break;
  1319. case AMOD_PASER:
  1320. code_tmp->code.operation.type = AMOD_func;
  1321. break;
  1322. case AINTDIV_PASER:
  1323. code_tmp->code.operation.type = AINTDIV_func;
  1324. break;
  1325. case APOW_PASER:
  1326. code_tmp->code.operation.type = APOW_func;
  1327. break;
  1328. case ABITAND_PASER:
  1329. code_tmp->code.operation.type = ABITAND_func;
  1330. break;
  1331. case ABITOR_PASER:
  1332. code_tmp->code.operation.type = ABITOR_func;
  1333. break;
  1334. case ABITNOTOR_PASER:
  1335. code_tmp->code.operation.type = ABITNOTOR_func;
  1336. break;
  1337. case ALEFT_PASER:
  1338. code_tmp->code.operation.type = ABITLEFT_func;
  1339. break;
  1340. case ARIGHT_PASER:
  1341. code_tmp->code.operation.type = ABITRIGHT_func;
  1342. break;
  1343. }
  1344. code_tmp->code.operation.left_exp = left.data.statement_value;
  1345. code_tmp->code.operation.right_exp = right.data.statement_value;
  1346. new_token.data.statement_value = code_tmp;
  1347. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1348. return eq_number(status, list); // 回调自己
  1349. }
  1350. else{ // 递归跳出
  1351. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1352. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  1353. back_one_token(list, left);
  1354. back_again(list, symbol);
  1355. return;
  1356. }
  1357. }
  1358. else{ // 模式1
  1359. fprintf(status_log, "[info][grammar] (eq_number)back one token to (hide_list)\n");
  1360. back_one_token(list, left);
  1361. get_base_token(status, list, hide_list, new_token); // hide_list会处理is_left
  1362. if(new_token.type != NON_hide_list){
  1363. back_one_token(list, new_token); // 往回[不匹配类型]
  1364. return;
  1365. }
  1366. if(status->is_left){ // 必须是在最左边 -> 进入解包赋值
  1367. status->is_left = false;
  1368. token comma, p_left, eq_t, p_right, tmp;
  1369. parameter *the_right;
  1370. get_pop_token(status, list,comma);
  1371. if(comma.type == COMMA_PASER){ // a,b = [1,2]的赋值方式
  1372. back_one_token(list, new_token); // 先把new_token和comma一起回退
  1373. back_again(list, comma);
  1374. p_status new_status;
  1375. new_status = *status;
  1376. new_status.is_peq = true;
  1377. get_base_token(&new_status,list,formal_parameter,p_left);
  1378. if(p_left.type != NON_parameter){
  1379. paser_error(status, "Dont get formal_parameter");
  1380. }
  1381. get_pop_token(status, list, eq_t);
  1382. if(eq_t.type != EQ_PASER || status->not_match_eq){ // 变成hide_list
  1383. back_again(list, eq_t);
  1384. statement *code_tmp = make_statement();
  1385. code_tmp->type = base_tuple;
  1386. code_tmp->code.base_tuple.value = p_left.data.parameter_list;
  1387. new_token.data.statement_value = code_tmp;
  1388. new_token.data_type = statement_value;
  1389. goto return_back;
  1390. }
  1391. get_pop_token(status,list,tmp);
  1392. if(tmp.type == POW_PASER || tmp.type == MUL_PASER || tmp.type == COLON_PASER){
  1393. if(tmp.type != COLON_PASER){
  1394. back_again(list, tmp);
  1395. }
  1396. get_right_token(status,list,formal_parameter,p_right);
  1397. if(p_right.type != NON_parameter){
  1398. paser_error(status, "Don't get formal_parameter");
  1399. }
  1400. the_right = p_right.data.parameter_list;
  1401. }
  1402. else{
  1403. back_again(list, tmp);
  1404. get_right_token(status,list,hide_list,p_right); // 把后面匹配为list
  1405. if(p_right.type != NON_hide_list){
  1406. paser_error(status, "Don't get hide_list");
  1407. }
  1408. the_right = make_parameter_value(p_right.data.statement_value);
  1409. the_right->type = put_args;
  1410. the_right->u.var = the_right->u.value;
  1411. }
  1412. new_token.data_type = statement_value;
  1413. new_token.data.statement_value = make_statement();
  1414. new_token.data.statement_value->type = pack_eq;
  1415. new_token.data.statement_value->code.pack_eq.left = p_left.data.parameter_list;
  1416. new_token.data.statement_value->code.pack_eq.right = the_right;
  1417. }
  1418. else{
  1419. back_again(list, comma); // 回退comma 使用back_again目的是让new_token在comma之前
  1420. }
  1421. }
  1422. return_back:
  1423. new_token.type = NON_eq;
  1424. add_node(list, new_token);
  1425. return eq_number(status, list); // 回调自己
  1426. }
  1427. }
  1428. void hide_list(p_status *status, token_node *list){
  1429. fprintf(status_log, "[info][grammar] mode status: hide_list\n");
  1430. token exp;
  1431. bool old_is_left = status->is_left;
  1432. p_status new_status;
  1433. new_status = *status;
  1434. new_status.is_left = false; // 接下去的运算 is_left均为false
  1435. get_base_token(&new_status,list,lambda_,exp);
  1436. new_status.is_left = old_is_left;
  1437. if(exp.type != NON_lambda){
  1438. back_one_token(list, exp);
  1439. return;
  1440. }
  1441. if(!status->not_match_tuple && !status->is_left && !status->is_for){ // 几个会用到逗号的选项先排除
  1442. token comma;
  1443. get_pop_token(status,list,comma);
  1444. if(comma.type == COMMA_PASER){
  1445. token new_token;
  1446. back_one_token(list, exp);
  1447. back_again(list, comma);
  1448. p_status new_status;
  1449. new_status = *status;
  1450. new_status.not_match_tuple = true;
  1451. get_base_token(&new_status, list, formal_parameter, new_token);
  1452. if(new_token.type != NON_parameter){
  1453. paser_error(status, "Don't get formal_parameter");
  1454. }
  1455. statement *code_tmp = make_statement();
  1456. code_tmp->type = base_tuple;
  1457. code_tmp->code.base_tuple.value = new_token.data.parameter_list;
  1458. exp.data.statement_value = code_tmp;
  1459. exp.data_type = statement_value;
  1460. }
  1461. else{
  1462. back_again(list, comma);
  1463. }
  1464. }
  1465. exp.type = NON_hide_list;
  1466. add_node(list, exp); // 压入节点
  1467. return;
  1468. }
  1469. void lambda_(p_status *status, token_node *list){
  1470. fprintf(status_log, "[info][grammar] mode status: lambda_\n");
  1471. token left, lambda_t, block_t, symbol, new_token;
  1472. left = pop_node(list);
  1473. if(left.type != NON_chose){
  1474. back_one_token(list, left);
  1475. get_base_token(status, list, chose_exp_, left);
  1476. if(left.type != NON_chose){
  1477. back_one_token(list, left);
  1478. return;
  1479. }
  1480. }
  1481. get_pop_token(status, list, lambda_t);
  1482. if(lambda_t.type != LAMBDA_PASER){ // 不是lambda表达式
  1483. left.type = NON_lambda;
  1484. back_one_token(list, left);
  1485. back_again(list, lambda_t);
  1486. return;
  1487. }
  1488. get_right_token(status, list, block_, block_t);
  1489. if(block_t.type != NON_block){
  1490. paser_error(status, "Do't get '{");
  1491. }
  1492. statement *lambda_tmp = make_statement();
  1493. lambda_tmp->type = lambda_func;
  1494. if(left.data.statement_value->type == base_tuple){
  1495. lambda_tmp->code.lambda_func.parameter_list = left.data.statement_value->code.base_tuple.value;
  1496. }
  1497. else{
  1498. lambda_tmp->code.lambda_func.parameter_list = NULL;
  1499. }
  1500. free(left.data.statement_value);
  1501. lambda_tmp->code.lambda_func.done = block_t.data.statement_value;
  1502. new_token.type = NON_lambda;
  1503. new_token.data_type = statement_value;
  1504. new_token.data.statement_value = lambda_tmp;
  1505. add_node(list, new_token);
  1506. return;
  1507. }
  1508. // xxx if yyy else zzz
  1509. void chose_exp_(p_status *status, token_node *list){
  1510. fprintf(status_log, "[info][grammar] mode status: chose_exp\n");
  1511. token left, if_t, else_t, condition, right, new_token;
  1512. left = pop_node(list);
  1513. if(left.type != NON_bool_or){
  1514. back_one_token(list, left);
  1515. get_base_token(status, list, bool_or, left);
  1516. if(left.type != NON_bool_or){
  1517. back_one_token(list, left);
  1518. return;
  1519. }
  1520. }
  1521. get_pop_token(status, list, if_t);
  1522. if(if_t.type != IF_PASER){ // 不是if else表达式
  1523. left.type = NON_chose;
  1524. back_one_token(list, left);
  1525. back_again(list, if_t);
  1526. return;
  1527. }
  1528. get_right_token(status, list, bool_or, condition);
  1529. if(condition.type != NON_bool_or){
  1530. paser_error(status, "Do't get condition");
  1531. }
  1532. get_pop_token(status, list, else_t);
  1533. if(else_t.type != ELSE_PASER){ // 不是if else表达式
  1534. paser_error(status, "Do't get else");
  1535. return;
  1536. }
  1537. get_right_token(status, list, bool_or, right);
  1538. if(right.type != NON_bool_or){
  1539. paser_error(status, "Do't get bool or");
  1540. }
  1541. statement *chose_tmp = make_statement();
  1542. chose_tmp->type = chose_exp;
  1543. chose_tmp->code.chose_exp.condition = condition.data.statement_value;
  1544. chose_tmp->code.chose_exp.true_do = left.data.statement_value;
  1545. chose_tmp->code.chose_exp.false_do = right.data.statement_value;
  1546. new_token.type = NON_chose;
  1547. new_token.data_type = statement_value;
  1548. new_token.data.statement_value = chose_tmp;
  1549. add_node(list, new_token);
  1550. return;
  1551. }
  1552. /*
  1553. bool_or : bool_and
  1554. | bool_or AND bool_and
  1555. */
  1556. void bool_or(p_status *status, token_node *list){ // 因试分解
  1557. fprintf(status_log, "[info][grammar] mode status: bool_or\n");
  1558. token left, right, symbol, new_token;
  1559. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1560. if(left.type == NON_bool_or){ // 模式2/3
  1561. fprintf(status_log, "[info][grammar] (bool_or)reduce right\n");
  1562. get_pop_token(status, list, symbol);
  1563. if(symbol.type == OR_PASER || symbol.type == BOOLSOR_PASER){ // 模式2/3
  1564. get_right_token(status, list, bool_and, right); // 回调右边
  1565. if(right.type != NON_bool_and){
  1566. paser_error(status, "Don't get a compare");
  1567. }
  1568. // 逻辑操作
  1569. new_token.type = NON_bool_or;
  1570. new_token.data_type = statement_value;
  1571. statement *code_tmp = make_statement();
  1572. code_tmp->type = operation;
  1573. code_tmp->code.operation.type = ((symbol.type == OR_PASER) ? OR_func : BOOLSOR_func);
  1574. code_tmp->code.operation.left_exp = left.data.statement_value;
  1575. code_tmp->code.operation.right_exp = right.data.statement_value;
  1576. new_token.data.statement_value = code_tmp;
  1577. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1578. return bool_or(status, list); // 回调自己
  1579. }
  1580. else{ // 递归跳出
  1581. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1582. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  1583. back_one_token(list, left);
  1584. back_again(list, symbol);
  1585. return;
  1586. }
  1587. }
  1588. else{ // 模式1
  1589. fprintf(status_log, "[info][grammar] (bool_or)back one token to (bool_and)\n");
  1590. back_one_token(list, left);
  1591. get_base_token(status, list, bool_and, new_token);
  1592. if(new_token.type != NON_bool_and){
  1593. back_one_token(list, new_token); // 往回[不匹配类型]
  1594. return;
  1595. }
  1596. new_token.type = NON_bool_or;
  1597. add_node(list, new_token);
  1598. return bool_or(status, list); // 回调自己
  1599. }
  1600. }
  1601. /*
  1602. bool_and : bool_not
  1603. | bool_and AND bool_not
  1604. */
  1605. void bool_and(p_status *status, token_node *list){ // 因试分解
  1606. fprintf(status_log, "[info][grammar] mode status: bool_and\n");
  1607. token left, right, symbol, new_token;
  1608. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1609. if(left.type == NON_bool_and){ // 模式2/3
  1610. fprintf(status_log, "[info][grammar] (bool_and)reduce right\n");
  1611. get_pop_token(status, list, symbol);
  1612. if(symbol.type == AND_PASER || symbol.type == BOOLSAND_PASER){ // 模式2/3
  1613. get_right_token(status, list, bool_not, right); // 回调右边
  1614. if(right.type != NON_bool_not){
  1615. paser_error(status, "Don't get a bool_not");
  1616. }
  1617. // 逻辑操作
  1618. new_token.type = NON_bool_and;
  1619. new_token.data_type = statement_value;
  1620. statement *code_tmp = make_statement();
  1621. code_tmp->type = operation;
  1622. code_tmp->code.operation.type = ((symbol.type == AND_PASER) ? AND_func : BOOLSAND_func);
  1623. code_tmp->code.operation.left_exp = left.data.statement_value;
  1624. code_tmp->code.operation.right_exp = right.data.statement_value;
  1625. new_token.data.statement_value = code_tmp;
  1626. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1627. return bool_and(status, list); // 回调自己
  1628. }
  1629. else{ // 递归跳出
  1630. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1631. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  1632. back_one_token(list, left);
  1633. back_again(list, symbol);
  1634. return;
  1635. }
  1636. }
  1637. else{ // 模式1
  1638. fprintf(status_log, "[info][grammar] (bool_and)back one token to (compare)\n");
  1639. back_one_token(list, left);
  1640. get_base_token(status, list, bool_not, new_token);
  1641. if(new_token.type != NON_bool_not){
  1642. back_one_token(list, new_token); // 往回[不匹配类型]
  1643. return;
  1644. }
  1645. new_token.type = NON_bool_and;
  1646. add_node(list, new_token);
  1647. return bool_and(status, list); // 回调自己
  1648. }
  1649. }
  1650. /*
  1651. bool_not : compare
  1652. | BITNOT bool_not
  1653. */
  1654. void bool_not(p_status *status, token_node *list){
  1655. fprintf(status_log, "[info][grammar] mode status: negative\n");
  1656. token left, right, new_token;
  1657. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1658. if(left.type == NOT_PASER){ // 模式2
  1659. fprintf(status_log, "[info][grammar] (bool_not)reduce right\n");
  1660. get_right_token(status, list, bool_not, right); // 回调右边
  1661. if(right.type != NON_bool_not){
  1662. paser_error(status, "Don't get a bool_not");
  1663. }
  1664. // 逻辑操作
  1665. new_token.type = NON_bool_not;
  1666. new_token.data_type = statement_value;
  1667. statement *code_tmp = make_statement();
  1668. code_tmp->type = operation;
  1669. code_tmp->code.operation.type = NOT_func;
  1670. code_tmp->code.operation.left_exp = NULL;
  1671. code_tmp->code.operation.right_exp = right.data.statement_value;
  1672. new_token.data.statement_value = code_tmp;
  1673. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1674. return; // 回调自己
  1675. }
  1676. else{ // 模式1
  1677. fprintf(status_log, "[info][grammar] (negative)back one token to (compare)\n");
  1678. back_one_token(list, left);
  1679. get_base_token(status, list, compare, new_token);
  1680. if(new_token.type != NON_compare){
  1681. back_one_token(list, new_token); // 往回[不匹配类型]
  1682. return;
  1683. }
  1684. new_token.type = NON_bool_not;
  1685. add_node(list, new_token);
  1686. return;
  1687. }
  1688. }
  1689. void compare(p_status *status, token_node *list){ // 多项式
  1690. fprintf(status_log, "[info][grammar] mode status: compare\n");
  1691. token left, right, symbol, new_token;
  1692. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1693. if(left.type == NON_compare){ // 模式2/3
  1694. fprintf(status_log, "[info][grammar] (compare)reduce right\n");
  1695. get_pop_token(status, list, symbol);
  1696. if(symbol.type == EQEQ_PASER || symbol.type == MOREEQ_PASER || symbol.type == LESSEQ_PASER ||
  1697. symbol.type == MORE_PASER || symbol.type == LESS_PASER || symbol.type == NOTEQ_PASER ||
  1698. symbol.type == ISLEFT_PASER || symbol.type == ISRIGHT_PASER || symbol.type == BOOLIS_PASER ||
  1699. symbol.type == ILEFT_PASER || symbol.type == IRIGHT_PASER || symbol.type == BOOLNOTOR_PASER ||
  1700. symbol.type == IS_PASER || (symbol.type == IN_PASER && !status->is_for)){
  1701. get_right_token(status, list, bit_notor, right); // 回调右边
  1702. if(right.type != NON_bit_notor){
  1703. paser_error(status, "Don't get a bit_notor");
  1704. }
  1705. new_token.type = NON_compare;
  1706. new_token.data_type = statement_value;
  1707. statement *code_tmp = make_statement();
  1708. code_tmp->type = operation;
  1709. switch (symbol.type)
  1710. {
  1711. case EQEQ_PASER:
  1712. code_tmp->code.operation.type = EQUAL_func;
  1713. break;
  1714. case MOREEQ_PASER:
  1715. code_tmp->code.operation.type = MOREEQ_func;
  1716. break;
  1717. case LESSEQ_PASER:
  1718. code_tmp->code.operation.type = LESSEQ_func;
  1719. break;
  1720. case MORE_PASER:
  1721. code_tmp->code.operation.type = MORE_func;
  1722. break;
  1723. case LESS_PASER:
  1724. code_tmp->code.operation.type = LESS_func;
  1725. break;
  1726. case NOTEQ_PASER:
  1727. code_tmp->code.operation.type = NOTEQ_func;
  1728. break;
  1729. case ISLEFT_PASER:
  1730. code_tmp->code.operation.type = ISLEFT_func;
  1731. break;
  1732. case ISRIGHT_PASER:
  1733. code_tmp->code.operation.type = ISRIGHT_func;
  1734. break;
  1735. case ILEFT_PASER:
  1736. code_tmp->code.operation.type = ILEFT_func;
  1737. break;
  1738. case IRIGHT_PASER:
  1739. code_tmp->code.operation.type = IRIGHT_func;
  1740. break;
  1741. case BOOLIS_PASER:
  1742. code_tmp->code.operation.type = BOOLIS_func;
  1743. break;
  1744. case BOOLNOTOR_PASER:
  1745. code_tmp->code.operation.type = BOOLNOTOR_func;
  1746. case IS_PASER:
  1747. code_tmp->code.operation.type = IS_func;
  1748. break;
  1749. case IN_PASER:
  1750. code_tmp->code.operation.type = IN_func;
  1751. break;
  1752. }
  1753. code_tmp->code.operation.left_exp = left.data.statement_value;
  1754. code_tmp->code.operation.right_exp = right.data.statement_value;
  1755. new_token.data.statement_value = code_tmp;
  1756. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1757. return compare(status, list); // 回调自己
  1758. }
  1759. else{ // 递归跳出
  1760. fprintf(status_log, "[info][grammar] (compare)out\n");
  1761. back_one_token(list, left);
  1762. back_again(list, symbol);
  1763. return;
  1764. }
  1765. }
  1766. else{ // 模式1
  1767. fprintf(status_log, "[info][grammar] (compare)back one token to (bit_notor)\n");
  1768. back_one_token(list, left);
  1769. get_base_token(status, list, bit_notor, new_token);
  1770. if(new_token.type != NON_bit_notor){
  1771. back_one_token(list, new_token); // 往回[不匹配类型]
  1772. return;
  1773. }
  1774. new_token.type = NON_compare;
  1775. add_node(list, new_token);
  1776. return compare(status, list); // 回调自己
  1777. }
  1778. }
  1779. /*
  1780. bit_notor : bit_or
  1781. | bit_notor BITOR bit_or
  1782. */
  1783. void bit_notor(p_status *status, token_node *list){ // 因试分解
  1784. fprintf(status_log, "[info][grammar] mode status: bit_notor\n");
  1785. token left, right, symbol, new_token;
  1786. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1787. if(left.type == NON_bit_notor){ // 模式2/3
  1788. fprintf(status_log, "[info][grammar] (bit_notor)reduce right\n");
  1789. get_pop_token(status, list, symbol);
  1790. if(symbol.type == BITNOTOR_PASER){ // 模式2/3
  1791. get_right_token(status, list, bit_or, right); // 回调右边
  1792. if(right.type != NON_bit_or){
  1793. paser_error(status, "Don't get a bit_or");
  1794. }
  1795. // 逻辑操作
  1796. new_token.type = NON_bit_notor;
  1797. new_token.data_type = statement_value;
  1798. statement *code_tmp = make_statement();
  1799. code_tmp->type = operation;
  1800. code_tmp->code.operation.type = BITNOTOR_func;
  1801. code_tmp->code.operation.left_exp = left.data.statement_value;
  1802. code_tmp->code.operation.right_exp = right.data.statement_value;
  1803. new_token.data.statement_value = code_tmp;
  1804. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1805. return bit_notor(status, list); // 回调自己
  1806. }
  1807. else{ // 递归跳出
  1808. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1809. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  1810. back_one_token(list, left);
  1811. back_again(list, symbol);
  1812. return;
  1813. }
  1814. }
  1815. else{ // 模式1
  1816. fprintf(status_log, "[info][grammar] (bit_notor)back one token to (bit_and)\n");
  1817. back_one_token(list, left);
  1818. get_base_token(status, list, bit_or, new_token);
  1819. if(new_token.type != NON_bit_or){
  1820. back_one_token(list, new_token); // 往回[不匹配类型]
  1821. return;
  1822. }
  1823. new_token.type = NON_bit_notor;
  1824. add_node(list, new_token);
  1825. return bit_notor(status, list); // 回调自己
  1826. }
  1827. }
  1828. /*
  1829. bit_or : bit_and
  1830. | bit_or BITOR bit_and
  1831. */
  1832. void bit_or(p_status *status, token_node *list){ // 因试分解
  1833. fprintf(status_log, "[info][grammar] mode status: bit_or\n");
  1834. token left, right, symbol, new_token;
  1835. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1836. if(left.type == NON_bit_or){ // 模式2/3
  1837. fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
  1838. get_pop_token(status, list, symbol);
  1839. if(symbol.type == BITOR_PASER){ // 模式2/3
  1840. get_right_token(status, list, bit_and, right); // 回调右边
  1841. if(right.type != NON_bit_and){
  1842. paser_error(status, "Don't get a bit_and");
  1843. }
  1844. // 逻辑操作
  1845. new_token.type = NON_bit_or;
  1846. new_token.data_type = statement_value;
  1847. statement *code_tmp = make_statement();
  1848. code_tmp->type = operation;
  1849. code_tmp->code.operation.type = BITOR_func;
  1850. code_tmp->code.operation.left_exp = left.data.statement_value;
  1851. code_tmp->code.operation.right_exp = right.data.statement_value;
  1852. new_token.data.statement_value = code_tmp;
  1853. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1854. return bit_or(status, list); // 回调自己
  1855. }
  1856. else{ // 递归跳出
  1857. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1858. fprintf(status_log, "[info][grammar] (bit_or)out\n");
  1859. back_one_token(list, left);
  1860. back_again(list, symbol);
  1861. return;
  1862. }
  1863. }
  1864. else{ // 模式1
  1865. fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
  1866. back_one_token(list, left);
  1867. get_base_token(status, list, bit_and, new_token);
  1868. if(new_token.type != NON_bit_and){
  1869. back_one_token(list, new_token); // 往回[不匹配类型]
  1870. return;
  1871. }
  1872. new_token.type = NON_bit_or;
  1873. add_node(list, new_token);
  1874. return bit_or(status, list); // 回调自己
  1875. }
  1876. }
  1877. /*
  1878. bit_and : bit_move
  1879. | bit_and BITAND bit_move
  1880. */
  1881. void bit_and(p_status *status, token_node *list){ // 因试分解
  1882. fprintf(status_log, "[info][grammar] mode status: bit_and\n");
  1883. token left, right, symbol, new_token;
  1884. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1885. if(left.type == NON_bit_and){ // 模式2/3
  1886. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  1887. get_pop_token(status, list, symbol);
  1888. if(symbol.type == BITAND_PASER){ // 模式2/3
  1889. get_right_token(status, list, bit_move, right); // 回调右边
  1890. if(right.type != NON_bit_move){
  1891. paser_error(status, "Don't get a bit_move");
  1892. }
  1893. // 逻辑操作
  1894. new_token.type = NON_bit_and;
  1895. new_token.data_type = statement_value;
  1896. statement *code_tmp = make_statement();
  1897. code_tmp->type = operation;
  1898. code_tmp->code.operation.type = BITAND_func;
  1899. code_tmp->code.operation.left_exp = left.data.statement_value;
  1900. code_tmp->code.operation.right_exp = right.data.statement_value;
  1901. new_token.data.statement_value = code_tmp;
  1902. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1903. return bit_and(status, list); // 回调自己
  1904. }
  1905. else{ // 递归跳出
  1906. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1907. fprintf(status_log, "[info][grammar] (bit_and)out\n");
  1908. back_one_token(list, left);
  1909. back_again(list, symbol);
  1910. return;
  1911. }
  1912. }
  1913. else{ // 模式1
  1914. fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
  1915. back_one_token(list, left);
  1916. get_base_token(status, list, bit_move, new_token);
  1917. if(new_token.type != NON_bit_move){
  1918. back_one_token(list, new_token); // 往回[不匹配类型]
  1919. return;
  1920. }
  1921. new_token.type = NON_bit_and;
  1922. add_node(list, new_token);
  1923. return bit_and(status, list); // 回调自己
  1924. }
  1925. }
  1926. /*
  1927. bit_move : power
  1928. | bit_move BITRIGHT factor
  1929. | bit_move BITLEFT factor
  1930. */
  1931. void bit_move(p_status *status, token_node *list){ // 因试分解
  1932. fprintf(status_log, "[info][grammar] mode status: bit_move\n");
  1933. token left, right, symbol, new_token;
  1934. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1935. if(left.type == NON_bit_move){ // 模式2/3
  1936. fprintf(status_log, "[info][grammar] (bit_move)reduce right\n");
  1937. get_pop_token(status, list, symbol);
  1938. if(symbol.type == BITRIGHT_PASER || symbol.type == BITLEFT_PASER){ // 模式2/3
  1939. get_right_token(status, list, polynomial, right); // 回调右边
  1940. if(right.type != NON_polynomial){
  1941. paser_error(status, "Don't get a polynomial");
  1942. }
  1943. // 逻辑操作
  1944. new_token.type = NON_bit_move;
  1945. new_token.data_type = statement_value;
  1946. statement *code_tmp = make_statement();
  1947. code_tmp->type = operation;
  1948. if(symbol.type == BITRIGHT_PASER){
  1949. code_tmp->code.operation.type = BITRIGHT_func;
  1950. }
  1951. else{
  1952. code_tmp->code.operation.type = BITLEFT_func;
  1953. }
  1954. code_tmp->code.operation.left_exp = left.data.statement_value;
  1955. code_tmp->code.operation.right_exp = right.data.statement_value;
  1956. new_token.data.statement_value = code_tmp;
  1957. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1958. return bit_move(status, list); // 回调自己
  1959. }
  1960. else{ // 递归跳出
  1961. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1962. fprintf(status_log, "[info][grammar] (bit_move)out\n");
  1963. back_one_token(list, left);
  1964. back_again(list, symbol);
  1965. return;
  1966. }
  1967. }
  1968. else{ // 模式1
  1969. fprintf(status_log, "[info][grammar] (bit_move)back one token to (polynomial)\n");
  1970. back_one_token(list, left);
  1971. get_base_token(status, list, polynomial, new_token);
  1972. if(new_token.type != NON_polynomial){
  1973. back_one_token(list, new_token); // 往回[不匹配类型]
  1974. return;
  1975. }
  1976. new_token.type = NON_bit_move;
  1977. add_node(list, new_token);
  1978. return bit_move(status, list); // 回调自己
  1979. }
  1980. }
  1981. /*
  1982. polynomial : factor
  1983. | polynomial ADD factor
  1984. | polynomial SUB factor
  1985. */
  1986. void polynomial(p_status *status, token_node *list){ // 多项式
  1987. fprintf(status_log, "[info][grammar] mode status: polynomial\n");
  1988. token left, right, symbol, new_token;
  1989. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1990. if(left.type == NON_polynomial){ // 模式2/3
  1991. fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
  1992. get_pop_token(status, list, symbol);
  1993. if(symbol.type == ADD_PASER || symbol.type == SUB_PASER){ // 模式2/3
  1994. get_right_token(status, list, factor, right); // 回调右边
  1995. if(right.type != NON_factor){
  1996. paser_error(status, "Don't get a factor");
  1997. }
  1998. new_token.type = NON_polynomial;
  1999. new_token.data_type = statement_value;
  2000. statement *code_tmp = make_statement();
  2001. code_tmp->type = operation;
  2002. if(symbol.type == ADD_PASER){
  2003. code_tmp->code.operation.type = ADD_func;
  2004. }
  2005. else{
  2006. code_tmp->code.operation.type = SUB_func;
  2007. }
  2008. code_tmp->code.operation.left_exp = left.data.statement_value;
  2009. code_tmp->code.operation.right_exp = right.data.statement_value;
  2010. new_token.data.statement_value = code_tmp;
  2011. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  2012. return polynomial(status, list); // 回调自己
  2013. }
  2014. else{ // 递归跳出
  2015. fprintf(status_log, "[info][grammar] (polynomial)out\n");
  2016. back_one_token(list, left);
  2017. back_again(list, symbol);
  2018. return;
  2019. }
  2020. }
  2021. else{ // 模式1
  2022. fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
  2023. back_one_token(list, left);
  2024. get_base_token(status, list, factor, new_token);
  2025. if(new_token.type != NON_factor){
  2026. back_one_token(list, new_token); // 往回[不匹配类型]
  2027. return;
  2028. }
  2029. new_token.type = NON_polynomial;
  2030. add_node(list, new_token);
  2031. return polynomial(status, list); // 回调自己
  2032. }
  2033. }
  2034. /*
  2035. factor : power
  2036. | factor MUL power
  2037. | factor DIV power
  2038. */
  2039. void factor(p_status *status, token_node *list){ // 因试分解
  2040. fprintf(status_log, "[info][grammar] mode status: factor\n");
  2041. token left, right, symbol, new_token;
  2042. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  2043. if(left.type == NON_factor){ // 模式2/3
  2044. fprintf(status_log, "[info][grammar] (power)reduce right\n");
  2045. get_pop_token(status, list, symbol);
  2046. if(symbol.type == MUL_PASER || symbol.type == DIV_PASER || symbol.type == INTDIV_PASER || symbol.type == MOD_PASER){ // 模式2/3
  2047. get_right_token(status, list, power, right); // 回调右边
  2048. if(right.type != NON_power){
  2049. paser_error(status, "Don't get a value");
  2050. }
  2051. // 逻辑操作
  2052. new_token.type = NON_power;
  2053. new_token.data_type = statement_value;
  2054. statement *code_tmp = make_statement();
  2055. code_tmp->type = operation;
  2056. if(symbol.type == MUL_PASER){
  2057. code_tmp->code.operation.type = MUL_func;
  2058. }
  2059. else if(symbol.type == DIV_PASER){
  2060. code_tmp->code.operation.type = DIV_func;
  2061. }
  2062. else if(symbol.type == INTDIV_PASER){
  2063. code_tmp->code.operation.type = INTDIV_func;
  2064. }
  2065. else{
  2066. code_tmp->code.operation.type = MOD_func;
  2067. }
  2068. code_tmp->code.operation.left_exp = left.data.statement_value;
  2069. code_tmp->code.operation.right_exp = right.data.statement_value;
  2070. new_token.data.statement_value = code_tmp;
  2071. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  2072. return factor(status, list); // 回调自己
  2073. }
  2074. else{ // 递归跳出
  2075. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  2076. fprintf(status_log, "[info][grammar] (factor)out\n");
  2077. back_one_token(list, left);
  2078. back_again(list, symbol);
  2079. return;
  2080. }
  2081. }
  2082. else{ // 模式1
  2083. fprintf(status_log, "[info][grammar] (factor)back one token to (element)\n");
  2084. back_one_token(list, left);
  2085. get_base_token(status, list, power, new_token);
  2086. if(new_token.type != NON_power){
  2087. back_one_token(list, new_token); // 往回[不匹配类型]
  2088. return;
  2089. }
  2090. new_token.type = NON_factor;
  2091. add_node(list, new_token);
  2092. return factor(status, list); // 回调自己
  2093. }
  2094. }
  2095. /*
  2096. power : element
  2097. | power POW element
  2098. | power LOG element
  2099. | power SQRT element
  2100. */
  2101. void power(p_status *status, token_node *list){
  2102. fprintf(status_log, "[info][grammar] mode status: power\n");
  2103. token left, right, symbol, new_token;
  2104. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  2105. if(left.type == NON_power){ // 模式2/3
  2106. fprintf(status_log, "[info][grammar] (negative)reduce right\n");
  2107. get_pop_token(status, list, symbol);
  2108. if(symbol.type == POW_PASER || symbol.type == LOG_PASER || symbol.type == SQRT_PASER){ // 模式2/3/4
  2109. get_right_token(status, list, negative, right); // 回调右边
  2110. if(right.type != NON_negative){
  2111. paser_error(status, "Don't get a negative");
  2112. }
  2113. // 逻辑操作
  2114. new_token.type = NON_power;
  2115. new_token.data_type = statement_value;
  2116. statement *code_tmp = make_statement();
  2117. code_tmp->type = operation;
  2118. if(symbol.type == POW_PASER){
  2119. code_tmp->code.operation.type = POW_func;
  2120. }
  2121. else if(symbol.type == LOG_PASER){
  2122. code_tmp->code.operation.type = LOG_func;
  2123. }
  2124. else{
  2125. code_tmp->code.operation.type = SQRT_func;
  2126. }
  2127. code_tmp->code.operation.left_exp = left.data.statement_value;
  2128. code_tmp->code.operation.right_exp = right.data.statement_value;
  2129. new_token.data.statement_value = code_tmp;
  2130. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  2131. return power(status, list); // 回调自己
  2132. }
  2133. else{ // 递归跳出
  2134. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  2135. fprintf(status_log, "[info][grammar] (power)out\n");
  2136. back_one_token(list, left);
  2137. back_again(list, symbol);
  2138. return;
  2139. }
  2140. }
  2141. else{ // 模式1
  2142. fprintf(status_log, "[info][grammar] (power)back one token to (negative)\n");
  2143. back_one_token(list, left);
  2144. get_base_token(status, list, negative, new_token);
  2145. if(new_token.type != NON_negative){
  2146. back_one_token(list, new_token); // 往回[不匹配类型]
  2147. return;
  2148. }
  2149. new_token.type = NON_power;
  2150. add_node(list, new_token);
  2151. return power(status, list); // 回调自己
  2152. }
  2153. }
  2154. /*
  2155. negative : bit_not
  2156. | SUB_PASER bit_not
  2157. | ADD_PASER bit_not
  2158. */
  2159. void negative(p_status *status, token_node *list){
  2160. fprintf(status_log, "[info][grammar] mode status: negative\n");
  2161. token left, right, new_token;
  2162. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  2163. if(left.type == SUB_PASER){ // 模式2
  2164. fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
  2165. get_right_token(status, list, negative, right); // 回调右边
  2166. if(right.type != NON_negative){
  2167. paser_error(status, "Don't get a negative");
  2168. }
  2169. // 逻辑操作
  2170. new_token.type = NON_negative;
  2171. new_token.data_type = statement_value;
  2172. statement *code_tmp = make_statement();
  2173. code_tmp->type = operation;
  2174. code_tmp->code.operation.type = NEGATIVE_func;
  2175. code_tmp->code.operation.left_exp = NULL;
  2176. code_tmp->code.operation.right_exp = right.data.statement_value;
  2177. new_token.data.statement_value = code_tmp;
  2178. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  2179. return;
  2180. }
  2181. else{ // 模式1
  2182. fprintf(status_log, "[info][grammar] (negative)back one token to (bit_not)\n");
  2183. if(left.type != ADD_PASER){ // 取正运算符
  2184. back_one_token(list, left);
  2185. get_base_token(status, list, bit_not, new_token);
  2186. }
  2187. else{ // 需要safe_get_token
  2188. get_right_token(status, list, negative, new_token);
  2189. }
  2190. if(new_token.type != NON_bit_not && new_token.type != NON_negative){
  2191. back_one_token(list, new_token); // 往回[不匹配类型]
  2192. return;
  2193. }
  2194. new_token.type = NON_negative;
  2195. add_node(list, new_token);
  2196. return;
  2197. }
  2198. }
  2199. /*
  2200. bit_not : self_exp
  2201. | BITNOT self_exp
  2202. */
  2203. void bit_not(p_status *status, token_node *list){
  2204. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  2205. token left, right, new_token;
  2206. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  2207. if(left.type == BITNOT_PASER){ // 模式2
  2208. fprintf(status_log, "[info][grammar] (self_exp)reduce right\n");
  2209. get_right_token(status, list, self_exp, right); // 回调右边
  2210. if(right.type != NON_self_exp){
  2211. paser_error(status, "Don't get a self_exp");
  2212. }
  2213. // 逻辑操作
  2214. new_token.type = NON_bit_not;
  2215. new_token.data_type = statement_value;
  2216. statement *code_tmp = make_statement();
  2217. code_tmp->type = operation;
  2218. code_tmp->code.operation.type = BITNOT_func;
  2219. code_tmp->code.operation.left_exp = NULL;
  2220. code_tmp->code.operation.right_exp = right.data.statement_value;
  2221. new_token.data.statement_value = code_tmp;
  2222. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  2223. return; // 回调自己
  2224. }
  2225. else{ // 模式1
  2226. fprintf(status_log, "[info][grammar] (bit_not)back one token to (self_exp)\n");
  2227. back_one_token(list, left);
  2228. get_base_token(status, list, self_exp, new_token);
  2229. if(new_token.type != NON_self_exp){
  2230. back_one_token(list, new_token); // 往回[不匹配类型]
  2231. return;
  2232. }
  2233. new_token.type = NON_bit_not;
  2234. add_node(list, new_token);
  2235. return;
  2236. }
  2237. }
  2238. /*
  2239. self_exp : hide_list
  2240. | FADD self_exp
  2241. | FSUB self_exp
  2242. | self_exp FADD
  2243. | self_exp FSUB
  2244. */
  2245. void self_exp(p_status *status, token_node *list){
  2246. fprintf(status_log, "[info][grammar] mode status: self_exp\n");
  2247. token left, right, new_token;
  2248. parameter *p_list;
  2249. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  2250. if(left.type == FADD_PASER || left.type == FSUB_PASER){ // ++a和--a
  2251. get_right_token(status, list, self_exp, right); // 递归自己,比如----a匹配为--new_token,new_token匹配为--a
  2252. if(right.type != NON_self_exp){
  2253. paser_error(status, "Don't get self_exp");
  2254. }
  2255. statement *code_tmp = make_statement();
  2256. code_tmp->type = operation;
  2257. code_tmp->code.operation.type = (left.type == FADD_PASER) ? FADD_func : FSUB_func;
  2258. code_tmp->code.operation.left_exp = NULL;
  2259. code_tmp->code.operation.right_exp = right.data.statement_value;
  2260. new_token.data.statement_value = code_tmp;
  2261. new_token.data_type = statement_value;
  2262. new_token.type = NON_self_exp;
  2263. add_node(list, new_token); // 压入节点
  2264. return;
  2265. }
  2266. else if(left.type == NON_self_exp){
  2267. get_pop_token(status, list, right);
  2268. if(right.type == FADD_PASER || right.type == FSUB_PASER){ // a++和a--
  2269. statement *code_tmp = make_statement();
  2270. code_tmp->type = operation;
  2271. code_tmp->code.operation.type = (right.type == FADD_PASER) ? LADD_func : LSUB_func;
  2272. code_tmp->code.operation.left_exp = left.data.statement_value;
  2273. code_tmp->code.operation.right_exp = NULL;
  2274. new_token.data.statement_value = code_tmp;
  2275. new_token.data_type = statement_value;
  2276. new_token.type = NON_self_exp;
  2277. add_node(list, new_token); // 压入节点
  2278. return self_exp(status, list); // 回调自己,比如a----匹配为self_exp--,再匹配为self_exp
  2279. }
  2280. else{ // 跳出递归
  2281. back_one_token(list, left);
  2282. back_again(list, right);
  2283. return;
  2284. }
  2285. }
  2286. else{ // a--和a++
  2287. back_one_token(list,left);
  2288. get_base_token(status, list, call_back_, new_token);
  2289. if(new_token.type != NON_call){
  2290. back_one_token(list, new_token); // 往回[不匹配类型]
  2291. return;
  2292. }
  2293. new_token.type = NON_self_exp;
  2294. add_node(list, new_token);
  2295. return self_exp(status, list); // 回调自己
  2296. }
  2297. }
  2298. /*
  2299. call_back_ : bool_or
  2300. | call_back_ LB RB
  2301. */
  2302. void call_back_(p_status *status, token_node *list){ // 因试分解
  2303. fprintf(status_log, "[info][grammar] mode status: call_back_\n");
  2304. token left, symbol, rb_t, new_token, parameter_t;
  2305. parameter *p_list;
  2306. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  2307. if(left.type == NON_call){
  2308. fprintf(status_log, "[info][grammar] (call_back_)reduce right\n");
  2309. get_pop_token(status, list, symbol);
  2310. if(symbol.type == LB_PASER && !status->not_match_call){
  2311. get_pop_token(status, list, rb_t);
  2312. if(rb_t.type != RB_PASER){ // 带参数
  2313. back_again(list, rb_t);
  2314. p_status new_status = *status; // 继承file_p等值
  2315. reset_status(new_status); // 不会影响 *staus
  2316. new_status.not_match_tuple = true;
  2317. new_status.is_left = false;
  2318. new_status.is_args = true;
  2319. get_right_token(&new_status,list,formal_parameter,parameter_t);
  2320. if(parameter_t.type != NON_parameter){
  2321. paser_error(status, "Don't get formal_parameter");
  2322. }
  2323. get_pop_token(status, list, rb_t);
  2324. if(rb_t.type != RB_PASER){
  2325. paser_error(status, "Don't get ')'[2]");
  2326. }
  2327. p_list = parameter_t.data.parameter_list;
  2328. }
  2329. else{
  2330. p_list = NULL; // 没参数
  2331. }
  2332. // 逻辑操作
  2333. new_token.type = NON_call;
  2334. new_token.data_type = statement_value;
  2335. statement *code_tmp = make_statement();
  2336. code_tmp->type = call;
  2337. code_tmp->code.call.func = left.data.statement_value;
  2338. code_tmp->code.call.parameter_list = p_list;
  2339. new_token.data.statement_value = code_tmp;
  2340. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  2341. return call_back_(status, list); // 回调自己
  2342. }
  2343. else if(symbol.type == VAR_PASER || symbol.type == SVAR_PASER){ // 运算符重载,如 yyy add xxx
  2344. back_one_token(list, symbol);
  2345. token call_func, second_args;
  2346. get_base_token(status, list, element, call_func);
  2347. if(call_func.type != NON_element){
  2348. paser_error(status, "Don't get the element");
  2349. }
  2350. get_right_token(status, list, call_down, second_args);
  2351. if(second_args.type != NON_call_down){
  2352. paser_error(status, "Don't get the call_down[1]");
  2353. }
  2354. parameter *first, *second;
  2355. first = make_parameter_value(left.data.statement_value);
  2356. first->u.var = first->u.value;
  2357. second = make_parameter_value(second_args.data.statement_value);
  2358. second->u.var = second->u.value;
  2359. first->next = second;
  2360. new_token.type = NON_call;
  2361. new_token.data_type = statement_value;
  2362. statement *code_tmp = make_statement();
  2363. code_tmp->type = call;
  2364. code_tmp->code.call.func = call_func.data.statement_value;
  2365. code_tmp->code.call.parameter_list = first;
  2366. new_token.data.statement_value = code_tmp;
  2367. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  2368. return call_back_(status, list); // 回调自己
  2369. }
  2370. if(symbol.type == POINT_PASER){ // 模式2/3
  2371. token right;
  2372. get_right_token(status, list, call_down, right); // 回调右边
  2373. if(right.type != NON_call_down){
  2374. paser_error(status, "Don't get a call_down");
  2375. }
  2376. fprintf(debug, "[tag 1]\n");
  2377. // 逻辑操作
  2378. new_token.type = NON_call;
  2379. new_token.data_type = statement_value;
  2380. statement *code_tmp = make_statement();
  2381. code_tmp->type = point;
  2382. code_tmp->code.point.base_var = left.data.statement_value;
  2383. code_tmp->code.point.child_var = right.data.statement_value;
  2384. new_token.data.statement_value = code_tmp;
  2385. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  2386. return call_back_(status, list); // 回调自己
  2387. }
  2388. else{ // 递归跳出
  2389. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  2390. fprintf(status_log, "[info][grammar] (call_back_)out\n");
  2391. back_one_token(list, left);
  2392. back_again(list, symbol);
  2393. return;
  2394. }
  2395. }
  2396. else{ // 模式1
  2397. fprintf(status_log, "[info][grammar] (call_back_)back one token to (call_down)\n");
  2398. back_one_token(list, left);
  2399. get_base_token(status, list, call_down, new_token);
  2400. if(new_token.type != NON_call_down){
  2401. back_one_token(list, new_token); // 往回[不匹配类型]
  2402. return;
  2403. }
  2404. new_token.type = NON_call;
  2405. add_node(list, new_token);
  2406. return call_back_(status, list); // 回调自己
  2407. }
  2408. }
  2409. /*
  2410. call_down : element
  2411. | call_down LI top_exp RI
  2412. */
  2413. void call_down(p_status *status, token_node *list){ // 因试分解
  2414. fprintf(status_log, "[info][grammar] mode status: call_down\n");
  2415. token left, lb_t, rb_t, new_token, parameter_t;
  2416. parameter *p_list = NULL;
  2417. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  2418. if(left.type == NON_call_down){
  2419. fprintf(status_log, "[info][grammar] (call_down)reduce right\n");
  2420. get_pop_token(status, list, lb_t);
  2421. if(lb_t.type == LI_PASER){
  2422. p_status new_status;
  2423. new_status = *status;
  2424. new_status.not_match_tuple = true;
  2425. get_right_token(&new_status, list, top_exp, parameter_t); // 回调右边
  2426. if(parameter_t.type != NON_top_exp){
  2427. paser_error(status, "Don't get a top_exp");
  2428. }
  2429. statement *code_tmp = make_statement();
  2430. get_pop_token(status, list, rb_t);
  2431. if(rb_t.type == RI_PASER || rb_t.type == COMMA_PASER){ // a[1,2,3,4]模式
  2432. back_one_token(list, parameter_t);
  2433. back_again(list, rb_t);
  2434. p_status new_status;
  2435. new_status = *status;
  2436. reset_status(new_status);
  2437. get_base_token(&new_status,list,formal_parameter,parameter_t);
  2438. if(parameter_t.type != NON_parameter){
  2439. paser_error(status, "Don't get formal_parameter");
  2440. }
  2441. get_pop_token(status, list, rb_t); // 把rb重新弹出来
  2442. p_list = parameter_t.data.parameter_list;
  2443. code_tmp->type = down;
  2444. code_tmp->code.down.base_var = left.data.statement_value;
  2445. code_tmp->code.down.child_var = p_list;
  2446. }
  2447. else if(rb_t.type == COLON_PASER){ // a[1:2:3:4]模式
  2448. back_one_token(list, parameter_t);
  2449. back_again(list, rb_t);
  2450. p_status new_status;
  2451. new_status = *status;
  2452. reset_status(new_status); // 避免正在dict和list模式中
  2453. new_status.is_slice = true;
  2454. get_base_token(&new_status,list,formal_parameter,parameter_t);
  2455. if(parameter_t.type != NON_parameter){
  2456. paser_error(status, "Don't get formal_parameter");
  2457. }
  2458. get_pop_token(status, list, rb_t); // 把rb重新弹出来
  2459. if(rb_t.type != RI_PASER){
  2460. goto error;
  2461. }
  2462. p_list = parameter_t.data.parameter_list;
  2463. code_tmp->type = slice;
  2464. code_tmp->code.slice.base_var = left.data.statement_value;
  2465. code_tmp->code.slice.value = p_list;
  2466. }
  2467. else{
  2468. free(code_tmp);
  2469. error: paser_error(status, "Don't get ']', ':' or ','");
  2470. }
  2471. // 逻辑操作
  2472. new_token.type = NON_call_down;
  2473. new_token.data_type = statement_value;
  2474. new_token.data.statement_value = code_tmp;
  2475. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  2476. return call_down(status, list); // 回调自己
  2477. }
  2478. else{ // 递归跳出
  2479. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  2480. fprintf(status_log, "[info][grammar] (call_back_)out\n");
  2481. back_one_token(list, left);
  2482. back_again(list, lb_t);
  2483. return;
  2484. }
  2485. }
  2486. else{ // 模式1
  2487. fprintf(status_log, "[info][grammar] (call_down)back one token to (element)\n");
  2488. back_one_token(list, left);
  2489. get_base_token(status, list, element, new_token);
  2490. if(new_token.type != NON_element){
  2491. back_one_token(list, new_token); // 往回[不匹配类型]
  2492. return;
  2493. }
  2494. new_token.type = NON_call_down;
  2495. add_node(list, new_token);
  2496. return call_down(status, list); // 回调自己
  2497. }
  2498. }
  2499. /*
  2500. element : number
  2501. | LB top_exp RB
  2502. */
  2503. void element(p_status *status, token_node *list){ // 数字归约
  2504. fprintf(status_log, "[info][grammar] mode status: element\n");
  2505. token gett, new_token;
  2506. gett = pop_node(list); // 取得一个token
  2507. if(gett.type == LB_PASER){ // 模式3
  2508. fprintf(status_log, "[info][grammar] (element)get LB\n");
  2509. p_status new_status = *status; // 继承file_p等值
  2510. reset_status(new_status); // 不会影响 *staus
  2511. new_status.ignore_enter = true; // 括号内忽略回车
  2512. get_right_token(&new_status, list, top_exp, new_token);
  2513. if(new_token.type == RB_PASER){
  2514. statement *code_tmp = make_statement(); // 默认
  2515. new_token.data_type = statement_value;
  2516. new_token.data.statement_value = code_tmp;
  2517. goto return_back;
  2518. }
  2519. else if(new_token.type != NON_top_exp){
  2520. paser_error(status, "Don't get 'top_exp'");
  2521. }
  2522. token rb;
  2523. get_pop_token(status, list ,rb);
  2524. if(rb.type != RB_PASER){ // 匹配失败
  2525. paser_error(status, "Don't get ')'");
  2526. }
  2527. return_back:
  2528. new_token.type = NON_element;
  2529. add_node(list, new_token); // 压入节点
  2530. return;
  2531. }
  2532. else if(gett.type == VAR_PASER || gett.type == PROTECT_PASER || gett.type == PUBLIC_PASER || gett.type == PRIVATE_PASER){ // a
  2533. back_one_token(list, gett);
  2534. get_base_token(status, list, var_token, new_token);
  2535. if(new_token.type != NON_base_var){
  2536. back_one_token(list, new_token); // 往回[不匹配类型]
  2537. return;
  2538. }
  2539. new_token.type = NON_element;
  2540. add_node(list, new_token);
  2541. return;
  2542. }
  2543. else if(gett.type == SVAR_PASER){ // a
  2544. back_one_token(list, gett);
  2545. get_base_token(status, list, svar_token, new_token);
  2546. if(new_token.type != NON_svar){
  2547. back_one_token(list, new_token); // 往回[不匹配类型]
  2548. return;
  2549. }
  2550. new_token.type = NON_element;
  2551. add_node(list, new_token);
  2552. return;
  2553. }
  2554. else if(gett.type == LP_PASER){ // dict
  2555. back_one_token(list, gett);
  2556. p_status new_status = *status; // 继承file_p等值
  2557. reset_status(new_status); // 不会影响 *staus
  2558. new_status.ignore_enter = true; // 括号内忽略回车
  2559. get_base_token(&new_status,list,dict_,new_token); // 不需要safe_get_token
  2560. if(new_token.type != NON_dict){
  2561. paser_error(status, "Don't get a dict_");
  2562. }
  2563. new_token.type = NON_element;
  2564. add_node(list, new_token);
  2565. return;
  2566. }
  2567. else if(gett.type == LI_PASER){ // [1]a或[1]列表或[1,2,3,4]列表
  2568. token exp_token, rb, tmp_var;
  2569. p_status new_status;
  2570. new_status = *status;
  2571. new_status.match_list = true; // 防止top_exp收走逗号
  2572. new_status.not_match_tuple = true;
  2573. new_status.ignore_enter = true; // 括号内忽略回车
  2574. get_right_token(&new_status, list, top_exp, exp_token);
  2575. if(exp_token.type == RI_PASER){ //可以认定为空list
  2576. back_one_token(list, gett);
  2577. back_again(list, exp_token);
  2578. get_base_token(status,list,list_,new_token); // 返回空列表
  2579. if(new_token.type != NON_list){
  2580. paser_error(status, "Don't get a list_");
  2581. }
  2582. goto back;
  2583. }
  2584. else if(exp_token.type != NON_top_exp){
  2585. paser_error(status, "Don't get 'top_exp'");
  2586. }
  2587. get_pop_token(status, list ,rb);
  2588. if(rb.type == RI_PASER){ // 匹配失败 TODO:: 检查是不是[1,2,3,4]的列表类型
  2589. get_pop_token(status, list ,tmp_var);
  2590. if(tmp_var.type == VAR_PASER){ // a
  2591. back_one_token(list, tmp_var);
  2592. get_base_token(status, list, var_token, new_token);
  2593. if(new_token.type != NON_base_var){
  2594. paser_error(status, "Don't get var");
  2595. }
  2596. new_token.data.statement_value->code.base_var.from = exp_token.data.statement_value;
  2597. goto back;
  2598. }
  2599. else if(tmp_var.type == SVAR_PASER){ // a
  2600. back_one_token(list, tmp_var);
  2601. get_base_token(status, list, svar_token, new_token);
  2602. if(new_token.type != NON_svar){
  2603. paser_error(status, "Don't get svar");
  2604. }
  2605. new_token.data.statement_value->code.base_svar.from = exp_token.data.statement_value;
  2606. goto back;
  2607. }
  2608. else{
  2609. // back again连用的时候要倒过来使用
  2610. /* 原因:
  2611. 先back_one_token的时候,token流变成
  2612. <之前的> - <A> - <NULL[seek/index]>
  2613. 先back_again的时候,token流变成
  2614. <之前的> - <A> - <B[seek]> - <NULL[index]>
  2615. 再back_again的时候,token流变成
  2616. <之前的> - <A> - <C[seek]> - <B> - <NULL[index]>
  2617. 其实还有一种解决方法:
  2618. 连用三次back_one_token
  2619. 然后再用两次back_token手动回退
  2620. */
  2621. back_one_token(list, gett);
  2622. back_again(list, tmp_var);
  2623. back_again(list, rb);
  2624. back_again(list, exp_token);
  2625. get_base_token(&new_status,list,list_,new_token); // 不需要safe_get_token
  2626. if(new_token.type != NON_list){
  2627. paser_error(status, "Don't get a list_");
  2628. }
  2629. goto back;
  2630. }
  2631. }
  2632. else{
  2633. back_one_token(list, gett);
  2634. back_again(list, rb);
  2635. back_again(list, exp_token);
  2636. get_base_token(&new_status,list,list_,new_token); // 不需要safe_get_token
  2637. if(new_token.type != NON_list){
  2638. paser_error(status, "Don't get a list_");
  2639. }
  2640. goto back;
  2641. }
  2642. back:
  2643. new_token.type = NON_element;
  2644. add_node(list, new_token);
  2645. return;
  2646. }
  2647. else{
  2648. fprintf(status_log, "[info][grammar] (element)back one token to (paser_value)\n");
  2649. back_one_token(list, gett);
  2650. get_base_token(status, list, paser_value, new_token);
  2651. if(new_token.type != NON_base_value){
  2652. back_one_token(list, new_token); // 往回[不匹配类型]
  2653. return;
  2654. }
  2655. new_token.type = NON_element;
  2656. add_node(list, new_token);
  2657. return;
  2658. }
  2659. }
  2660. /*
  2661. list_ : LI formal_parameter RI
  2662. */
  2663. void list_(p_status *status, token_node *list){ // 数字归约
  2664. fprintf(status_log, "[info][grammar] mode status: list_\n");
  2665. token gett, list_core, ri_t, new_token;
  2666. parameter *base = NULL;
  2667. gett = pop_node(list); // 取得一个token
  2668. if(gett.type == LI_PASER){ // var类型
  2669. p_status new_status;
  2670. new_status = *status;
  2671. new_status.match_list = true;
  2672. new_status.not_match_tuple = true;
  2673. get_right_token(&new_status,list,formal_parameter,list_core);
  2674. if(list_core.type == RI_PASER){ // 空列表
  2675. base = NULL;
  2676. goto make_list;
  2677. }
  2678. else if(list_core.type == NON_parameter){
  2679. base = list_core.data.parameter_list;
  2680. }
  2681. else{
  2682. paser_error(status, "Don't get formal_parameter");
  2683. }
  2684. get_pop_token(status,list,ri_t);
  2685. if(ri_t.type != RI_PASER){
  2686. paser_error(status, "Don't get ']'");
  2687. }
  2688. make_list:
  2689. new_token.type = NON_list;
  2690. statement *code_tmp = make_statement();
  2691. code_tmp->type = base_list;
  2692. code_tmp->code.base_list.value = base;
  2693. new_token.data.statement_value = code_tmp;
  2694. new_token.data_type = statement_value;
  2695. fprintf(status_log, "[info][grammar] (list_)out\n");
  2696. add_node(list, new_token); // 压入节点
  2697. return;
  2698. }
  2699. else{ // 不是期望值
  2700. fprintf(status_log, "[info][grammar] (list_)back one token\n");
  2701. back_one_token(list, gett);
  2702. return;
  2703. }
  2704. }
  2705. /*
  2706. dict_ : LP formal_parameter RP
  2707. */
  2708. void dict_(p_status *status, token_node *list){ // 数字归约
  2709. fprintf(status_log, "[info][grammar] mode status: dict_\n");
  2710. token gett, dict_core, rp_t, new_token;
  2711. parameter *base = NULL;
  2712. gett = pop_node(list); // 取得一个token
  2713. if(gett.type == LP_PASER){ // var类型
  2714. p_status new_status;
  2715. new_status = *status;
  2716. new_status.match_dict = true;
  2717. new_status.not_match_tuple = true;
  2718. get_right_token(&new_status,list,formal_parameter,dict_core);
  2719. if(dict_core.type == RP_PASER){ // 空列表
  2720. base = NULL;
  2721. goto make_dict;
  2722. }
  2723. else if(dict_core.type == NON_parameter){
  2724. base = dict_core.data.parameter_list;
  2725. }
  2726. else{
  2727. paser_error(status, "Don't get formal_parameter");
  2728. }
  2729. get_pop_token(status,list,rp_t);
  2730. if(rp_t.type != RP_PASER){
  2731. paser_error(status, "Don't get '}'");
  2732. }
  2733. make_dict:
  2734. new_token.type = NON_dict;
  2735. statement *code_tmp = make_statement();
  2736. code_tmp->type = base_dict;
  2737. code_tmp->code.base_dict.value = base;
  2738. new_token.data.statement_value = code_tmp;
  2739. new_token.data_type = statement_value;
  2740. fprintf(status_log, "[info][grammar] (dict_)out\n");
  2741. add_node(list, new_token); // 压入节点
  2742. return;
  2743. }
  2744. else{ // 不是期望值
  2745. fprintf(status_log, "[info][grammar] (dict_)back one token\n");
  2746. back_one_token(list, gett);
  2747. return;
  2748. }
  2749. }
  2750. void svar_token(p_status *status, token_node *list){ // 数字归约
  2751. fprintf(status_log, "[info][grammar] mode status: svar_token\n");
  2752. token gett, var_t, new_token;
  2753. gett = pop_node(list); // 取得一个token
  2754. if(gett.type == SVAR_PASER){ // var类型
  2755. int lock = auto_token;
  2756. new_token.type = NON_svar;
  2757. get_pop_token(status, list, var_t);
  2758. if(var_t.type == PROTECT_PASER || var_t.type == PUBLIC_PASER || var_t.type == PRIVATE_PASER){
  2759. switch (var_t.type)
  2760. {
  2761. case PROTECT_PASER:
  2762. lock = protect_token;
  2763. break;
  2764. case PUBLIC_PASER:
  2765. lock = public_token;
  2766. break;
  2767. case PRIVATE_PASER:
  2768. lock = private_token;
  2769. break;
  2770. default:
  2771. break;
  2772. }
  2773. get_pop_token(status, list, var_t);
  2774. if(var_t.type != COLON_PASER){
  2775. paser_error(status, "Don't get ':'");
  2776. }
  2777. get_right_token(status, list, element, var_t);
  2778. if(var_t.type != NON_element){
  2779. paser_error(status, "Don't get element[1]");
  2780. }
  2781. }
  2782. else{
  2783. back_one_token(list, var_t);
  2784. get_base_token(status, list, element, var_t);
  2785. if(var_t.type != NON_element){
  2786. paser_error(status, "Don't get element[2]");
  2787. }
  2788. }
  2789. statement *code_tmp = make_statement();
  2790. code_tmp->type = base_svar;
  2791. code_tmp->code.base_svar.var = var_t.data.statement_value;
  2792. code_tmp->code.base_svar.from = NULL;
  2793. code_tmp->code.base_svar.lock_token = lock;
  2794. new_token.data.statement_value = code_tmp;
  2795. new_token.data_type = statement_value;
  2796. fprintf(status_log, "[info][grammar] (svar_token)out\n");
  2797. add_node(list, new_token); // 压入节点
  2798. return;
  2799. }
  2800. else{ // 不是期望值
  2801. fprintf(status_log, "[info][grammar] (svar_token)back one token\n");
  2802. back_one_token(list, gett);
  2803. return;
  2804. }
  2805. }
  2806. /*
  2807. var_token : VAR
  2808. */
  2809. void var_token(p_status *status, token_node *list){ // 数字归约
  2810. fprintf(status_log, "[info][grammar] mode status: var_token\n");
  2811. token gett, new_token;
  2812. int lock = auto_token;
  2813. gett = pop_node(list); // 取得一个token
  2814. if(gett.type == VAR_PASER || gett.type == PROTECT_PASER || gett.type == PUBLIC_PASER || gett.type == PRIVATE_PASER){ // var类型
  2815. if(gett.type != VAR_PASER){
  2816. switch (gett.type)
  2817. {
  2818. case PROTECT_PASER:
  2819. lock = protect_token;
  2820. break;
  2821. case PUBLIC_PASER:
  2822. lock = public_token;
  2823. break;
  2824. case PRIVATE_PASER:
  2825. lock = private_token;
  2826. break;
  2827. default:
  2828. break;
  2829. }
  2830. get_pop_token(status, list, gett);
  2831. if(gett.type != COLON_PASER){
  2832. paser_error(status, "Don't get ':'");
  2833. }
  2834. get_pop_token(status, list, gett);
  2835. if(gett.type != VAR_PASER){
  2836. paser_error(status, "Don't get var");
  2837. }
  2838. }
  2839. new_token.type = NON_base_var;
  2840. statement *code_tmp = make_statement();
  2841. code_tmp->type = base_var;
  2842. code_tmp->code.base_var.var_name = gett.data.text;
  2843. code_tmp->code.base_var.from = NULL;
  2844. code_tmp->code.base_var.lock_token = lock;
  2845. new_token.data.statement_value = code_tmp;
  2846. new_token.data_type = statement_value;
  2847. fprintf(status_log, "[info][grammar] (var_token)out\n");
  2848. add_node(list, new_token); // 压入节点
  2849. return;
  2850. }
  2851. else{ // 不是期望值
  2852. fprintf(status_log, "[info][grammar] (var_token)back one token\n");
  2853. back_one_token(list, gett);
  2854. return;
  2855. }
  2856. }
  2857. /*
  2858. number : INT_PASER
  2859. | DOUBLE_PASER
  2860. */
  2861. void paser_value(p_status *status, token_node *list){ // 数字归约
  2862. fprintf(status_log, "[info][grammar] mode status: paser_value\n");
  2863. token gett, new_token;
  2864. gett = pop_node(list); // 取得一个token
  2865. if(gett.type == INT_PASER){ // int类型
  2866. new_token.type = NON_base_value;
  2867. GWARF_value tmp_value = GWARF_value_reset;
  2868. tmp_value.type = INT_value;
  2869. tmp_value.value.int_value = atoi(gett.data.text);
  2870. statement *code_tmp = make_statement();
  2871. code_tmp->type = call;
  2872. code_tmp->code.call.func = pack_call_name("int", NULL);
  2873. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  2874. new_token.data.statement_value = code_tmp;
  2875. new_token.data_type = statement_value;
  2876. }
  2877. else if(gett.type == DOUBLE_PASER){
  2878. new_token.type = NON_base_value;
  2879. GWARF_value tmp_value = GWARF_value_reset;
  2880. tmp_value.type = NUMBER_value;
  2881. tmp_value.value.double_value = atof(gett.data.text);
  2882. statement *code_tmp = make_statement();
  2883. code_tmp->type = call;
  2884. code_tmp->code.call.func = pack_call_name("double", NULL);
  2885. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  2886. new_token.data.statement_value = code_tmp;
  2887. new_token.data_type = statement_value;
  2888. }
  2889. else if(gett.type == STR_PASER){
  2890. new_token.type = NON_base_value;
  2891. GWARF_value tmp_value = GWARF_value_reset;
  2892. tmp_value.type = STRING_value;
  2893. if(gett.data.text == NULL){
  2894. tmp_value.value.string = "";
  2895. }
  2896. else{
  2897. tmp_value.value.string = gett.data.text;
  2898. }
  2899. statement *code_tmp = make_statement();
  2900. code_tmp->type = call;
  2901. code_tmp->code.call.func = pack_call_name("str", NULL);
  2902. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  2903. new_token.data.statement_value = code_tmp;
  2904. new_token.data_type = statement_value;
  2905. goto not_free;
  2906. }
  2907. else if(gett.type == TRUE_PASER || gett.type == FALSE_PASER){
  2908. new_token.type = NON_base_value;
  2909. GWARF_value tmp_value = GWARF_value_reset;
  2910. tmp_value.type = BOOL_value;
  2911. if(gett.type == TRUE_PASER){
  2912. tmp_value.value.bool_value = true;
  2913. }
  2914. else{
  2915. tmp_value.value.bool_value = false;
  2916. }
  2917. statement *code_tmp = make_statement();
  2918. code_tmp->type = call;
  2919. code_tmp->code.call.func = pack_call_name("bool", NULL);
  2920. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  2921. new_token.data.statement_value = code_tmp;
  2922. new_token.data_type = statement_value;
  2923. }
  2924. else if(gett.type == NONE_PASER){
  2925. new_token.type = NON_base_value;
  2926. statement *code_tmp = make_statement();
  2927. code_tmp->type = base_value;
  2928. code_tmp->code.base_value.value.type = NULL_value;
  2929. code_tmp->code.base_value.value.value.int_value = 0;
  2930. new_token.data.statement_value = code_tmp;
  2931. new_token.data_type = statement_value;
  2932. }
  2933. else{ // 不是期望值
  2934. fprintf(status_log, "[info][grammar] (paser_value)back one token\n");
  2935. back_one_token(list, gett);
  2936. return;
  2937. }
  2938. free(gett.data.text); // 释放字符串
  2939. not_free:
  2940. fprintf(status_log, "[info][grammar] (paser_value)add one token\n");
  2941. add_node(list, new_token); // 压入节点
  2942. }
  2943. void paser_error(p_status *status, char *text){
  2944. fprintf(status_log, "[error][grammar] paser error : %s\n\n", text);
  2945. fprintf(inter_info, "[error][grammar] paser error : %s\n\n", text);
  2946. longjmp(status->buf, 1);
  2947. }