syntax.c 120 KB

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