syntax.c 113 KB

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