12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126 |
- #include"token.h"
- #include"lex.h"
- #include"../inter/interpreter.h"
- void factor(p_status *status, token_node *list);
- void power(p_status *status, token_node *list);
- void call_down(p_status *status, token_node *list);
- void paser_value(p_status *status, token_node *list);
- void element(p_status *status, token_node *list);
- void var_token(p_status *status, token_node *list);
- void polynomial(p_status *status, token_node *list);
- void bit_move(p_status *status, token_node *list);
- void command(p_status *status, token_node *list);
- void while_(p_status *status, token_node *list);
- void if_(p_status *status, token_node *list);
- void for_(p_status *status, token_node *list);
- void elif_(p_status *status, token_node *list);
- void block_(p_status *status, token_node *list);
- void top_exp(p_status *status, token_node *list);
- void negative(p_status *status, token_node *list);
- void bit_not(p_status *status, token_node *list);
- void bit_notor(p_status *status, token_node *list);
- void bit_or(p_status *status, token_node *list);
- void bit_and(p_status *status, token_node *list);
- void compare(p_status *status, token_node *list);
- void bool_and(p_status *status, token_node *list);
- void bool_or(p_status *status, token_node *list);
- void bool_not(p_status *status, token_node *list);
- void eq_number(p_status *status, token_node *list);
- void call_back_(p_status *status, token_node *list);
- void def_(p_status *status, token_node *list);
- void ctrl_(p_status *status, token_node *list);
- void var_ctrl_(p_status *status, token_node *list);
- void return_(p_status *status, token_node *list);
- void formal_parameter(p_status *status, token_node *list);
- void list_(p_status *status, token_node *list);
- void dict_(p_status *status, token_node *list);
- void paser_error(char *text);
- /*
- command_list : command
- | command_list command
- */
- void command_list(p_status *status, token_node *list){ // 多项式
- fprintf(status_log, "[info][grammar] mode status: command_list\n", text);
- token left, right, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_command_list){ // 模式2
- fprintf(status_log, "[info][grammar] (command_list)reduce right\n");
- get_right_token(status, list, command, right); // 回调右边
- if(right.type == NON_command){
- new_token.type = NON_command_list;
- new_token.data_type = empty;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return command_list(status, list); // 回调自己
- }
- else{ // 递归跳出[EOF_token]
- printf("right.type = %d\n", right.type);
- fprintf(status_log, "[info][grammar] (command_list)out\n");
- back_one_token(list, left);
- back_again(list, right);
- return;
- }
- }
- else if(left.type == EOF_token){ // 递归跳出的条件
- fprintf(status_log, "[info][grammar] (command_list)out again\n");
- back_one_token(list, left);
- return;
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (command_list)back one token to (command)\n");
- back_one_token(list, left);
- get_base_token(status, list, command, new_token);
- if(new_token.type != NON_command){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_command_list;
- add_node(list, new_token);
- return command_list(status, list); // 回调自己
- }
- }
- /*
- command : top_exp <ENTER>
- */
- void command(p_status *status, token_node *list){ // 多项式
- fprintf(status_log, "[info][grammar] mode status: command\n", text);
- token left, new_token;
- left = pop_node(list); // 先弹出一个token 检查token
- if(left.type == WHILE_PASER){ // 是while类型的数据
- fprintf(status_log, "[info][grammar] (command)back one token to (while)\n");
- back_one_token(list, left);
- get_base_token(status, list, while_, new_token);
- get_stop_token();
- push_statement(statement_base, new_token);
- }
- else if(left.type == IF_PASER){
- fprintf(status_log, "[info][grammar] (command)back one token to (if)\n");
- back_one_token(list, left);
- get_base_token(status, list, if_, new_token);
- get_stop_token();
- push_statement(statement_base, new_token);
- }
- else if(left.type == FOR_PASER){
- fprintf(status_log, "[info][grammar] (command)back one token to (for)\n");
- back_one_token(list, left);
- get_base_token(status, list, for_, new_token);
- get_stop_token();
- push_statement(statement_base, new_token);
- }
- else if(left.type == DEF_PASER){
- fprintf(status_log, "[info][grammar] (command)back one token to (def_)\n");
- back_one_token(list, left);
- get_base_token(status, list, def_, new_token);
- get_stop_token();
- push_statement(statement_base, new_token);
- }
- else if(left.type == BREAK_PASER || left.type == BROKEN_PASER || left.type == CONTINUE_PASER || left.type == CONTINUED_PASER ||
- left.type == RESTART_PASER || left.type == RESTARTED_PASER || left.type == REGO_PASER || left.type == REWENT_PASER){
- fprintf(status_log, "[info][grammar] (command)back one token to (ctrl_)\n");
- back_one_token(list, left);
- get_base_token(status, list, ctrl_, new_token);
- get_stop_token();
- push_statement(statement_base, new_token);
- }
- else if(left.type == GLOBAL_PASER || left.type == DEFAULT_PASER || left.type == NOLOCAL_PASER){
- fprintf(status_log, "[info][grammar] (command)back one token to (var_ctrl_)\n");
- back_one_token(list, left);
- get_base_token(status, list, var_ctrl_, new_token);
- get_stop_token();
- push_statement(statement_base, new_token);
- }
- else if(left.type == RETURN_PASER){
- fprintf(status_log, "[info][grammar] (command)back one token to (return_)\n");
- back_one_token(list, left);
- get_base_token(status, list, return_, new_token);
- get_stop_token();
- push_statement(statement_base, new_token);
- }
- else if(left.type == ENTER_PASER){
- fprintf(status_log, "[info][grammar] (command)back <ENTER>\n");
- }
- else if(left.type == EOF_token){
- fprintf(status_log, "[info][grammar] (command)back <EOF>\n");
- back_one_token(list, left);
- goto return_back;
- }
- else{ // 表达式
- fprintf(status_log, "[info][grammar] (command)back one token to (top_exp)\n");
- back_one_token(list, left);
- get_base_token(status, list, top_exp, new_token);
- if(new_token.type != NON_top_exp){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- get_stop_token();
- push_statement(statement_base, new_token);
- }
- new_token.type = NON_command;
- add_node(list, new_token);
- return_back:
- return; // 回调自己
- }
- /*
- if_ : IF LB top_exp RB block
- */
- void if_(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: if_\n");
- token if_t, lb_t, exp_t, rb_t, block_t, next_t, child_t, new_token;
- if_t = pop_node(list);
- if(if_t.type == IF_PASER){
- get_pop_token(status, list, lb_t);
- if(lb_t.type != LB_PASER){
- paser_error("Don't get '('");
- }
- get_right_token(status,list,top_exp,exp_t);
- if(exp_t.type != NON_top_exp){ // 不是表达式
- paser_error("Don't get 'top_exp'");
- }
- get_pop_token(status, list, rb_t);
- if(rb_t.type != RB_PASER){
- paser_error("Don't get ')'");
- }
- get_right_token(status,list,block_,block_t);
- if(block_t.type != NON_block){ // 不是表达式
- paser_error("Don't get '{'");
- }
- statement *if_tmp = make_statement();
- if_tmp->type = if_branch;
- if_tmp->code.if_branch.done = make_if(exp_t.data.statement_value, block_t.data.statement_value);
- // 检查else和elseif
- el_again:
- get_pop_token(status,list,next_t);
- if(next_t.type == ENTER_PASER){ // 忽略Enter
- goto el_again;
- }
- printf("next_t.type = %d\n", next_t.type);
- if(next_t.type == ELIF_PASER || next_t.type == ELSE_PASER){ // elif
- back_one_token(list, next_t);
- get_base_token(status,list,elif_,child_t);
- if(child_t.type == NON_elif){
- append_elif(child_t.data.if_list_base, if_tmp->code.if_branch.done);
- goto el_again;
- }
- }
- else{
- back_again(list, next_t); // 下一次读取需要用safe_get_token
- }
- new_token.type = NON_if;
- new_token.data_type = statement_value;
- new_token.data.statement_value = if_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
-
- token tmp_enter;
- tmp_enter.type = ENTER_PASER;
- tmp_enter.data_type = empty;
- back_again(list, tmp_enter); // push入一个ENTER
- return;
- }
- else{
- back_one_token(list, if_t);
- return;
- }
- }
- /*
- elif_ : ELIF LB top_exp RB block
- */
- void elif_(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: elif_\n");
- token elif_t, lb_t, exp_t, rb_t, block_t, next_t, new_token;
- elif_t = pop_node(list);
- if(elif_t.type == ELIF_PASER){
- get_pop_token(status, list, lb_t);
- if(lb_t.type != LB_PASER){
- paser_error("Don't get '('");
- }
- get_right_token(status,list,top_exp,exp_t);
- if(exp_t.type != NON_top_exp){ // 不是表达式
- paser_error("Don't get 'top_exp'");
- }
- get_pop_token(status, list, rb_t);
- if(rb_t.type != RB_PASER){
- paser_error("Don't get ')'");
- }
- get_right_token(status,list,block_,block_t);
- if(block_t.type != NON_block){ // 不是表达式
- paser_error("Don't get '{'");
- }
- if_list *if_tmp = make_if(exp_t.data.statement_value, block_t.data.statement_value);
- new_token.type = NON_elif;
- new_token.data_type = if_list_base;
- new_token.data.if_list_base = if_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return;
- }
- if(elif_t.type == ELSE_PASER){ // else
- get_right_token(status,list,block_,block_t);
- if(block_t.type != NON_block){ // 不是表达式
- paser_error("Don't get '{'");
- }
- if_list *if_tmp = make_if(NULL, block_t.data.statement_value);
- new_token.type = NON_elif;
- new_token.data_type = if_list_base;
- new_token.data.if_list_base = if_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return;
- }
- else{
- back_one_token(list, elif_t);
- return;
- }
- }
- /*
- for_ : FOR LB top_exp COMMA top_exp COMMA top_exp RB block
- */
- void for_(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: while_\n");
- token for_t, exp_1, exp_2, exp_3, block_t, lb_t, rb_t, comma_t,new_token;
- statement *exp_a, *exp_b, *exp_c;
- for_t = pop_node(list);
- if(for_t.type == FOR_PASER){
- get_pop_token(status, list, lb_t);
- if(lb_t.type != LB_PASER){
- paser_error("Don't get '('");
- }
- get_pop_token(status, list, exp_1);
- if(exp_1.type == COMMA_PASER){
- exp_a = NULL; // exp_1 = NULL;
- }
- else{
- back_one_token(list, exp_1);
- get_base_token(status,list,top_exp,exp_1); // 不是使用right token,不需要执行safe_get_token
- if(exp_1.type != NON_top_exp){ // 不是表达式
- paser_error("Don't get 'top_exp'");
- }
- get_pop_token(status, list, comma_t);
- if(comma_t.type != COMMA_PASER){
- paser_error("Don't get ';' in for cycle");
- }
- exp_a = exp_1.data.statement_value;
- }
- get_pop_token(status, list, exp_2);
- if(exp_2.type == COMMA_PASER){
- exp_b = NULL; // exp_1 = NULL;
- }
- else{
- back_one_token(list, exp_2);
- get_base_token(status,list,top_exp,exp_2); // 不是使用right token,不需要执行safe_get_token
- if(exp_2.type != NON_top_exp){ // 不是表达式
- paser_error("Don't get 'top_exp'");
- }
- get_pop_token(status, list, comma_t);
- if(comma_t.type != COMMA_PASER){
- paser_error("Don't get ';' in for cycle");
- }
- exp_b = exp_2.data.statement_value;
- }
- get_pop_token(status, list, exp_3);
- if(exp_3.type == RB_PASER){
- exp_c = NULL; // exp_1 = NULL;
- back_one_token(list, exp_3);
- }
- else{
- back_one_token(list, exp_3);
- get_base_token(status,list,top_exp,exp_3); // 不是使用right token,不需要执行safe_get_token
- if(exp_3.type != NON_top_exp){ // 不是表达式
- paser_error("Don't get 'top_exp'");
- }
- exp_c = exp_3.data.statement_value;
- }
- get_pop_token(status, list, rb_t);
- if(rb_t.type != RB_PASER){
- paser_error("Don't get ')'");
- }
- get_right_token(status,list,block_,block_t);
- if(block_t.type != NON_block){ // 不是表达式
- paser_error("Don't get '{'");
- }
- statement *for_tmp = make_statement();
- for_tmp->type = for_cycle;
- for_tmp->code.for_cycle.first = exp_a;
- for_tmp->code.for_cycle.condition = exp_b;
- for_tmp->code.for_cycle.after = exp_c;
- for_tmp->code.for_cycle.done = block_t.data.statement_value;
- new_token.type = NON_for;
- new_token.data_type = statement_value;
- new_token.data.statement_value = for_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return;
- }
- else{
- back_one_token(list, for_t);
- return;
- }
- }
- /*
- def_ : DEF LB RB block
- */
- void def_(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: def_\n");
- token def_t, lb_t, rb_t, block_t, name_t, parameter_t, new_token;
- parameter *p_list;
- def_t = pop_node(list);
- if(def_t.type == DEF_PASER){
- status->is_func = true;
- get_right_token(status,list,top_exp,name_t); // 避免了top_exp把括号捕捉为call_back,不过,可以使用list设置status参数从而让call_back不捕捉[未实现]
- status->is_func = false;
- if(name_t.type != NON_top_exp){ // 不是表达式
- paser_error("Don't get 'top_exp'");
- }
- get_pop_token(status, list, lb_t);
- if(lb_t.type != LB_PASER){
- printf("lb_t.type = %d\n", lb_t.type);
- paser_error("Don't get '('");
- }
- get_pop_token(status, list, rb_t);
- if(rb_t.type != RB_PASER){ // 带参数
- back_one_token(list, rb_t);
- get_base_token(status,list,formal_parameter,parameter_t); // 不需要safe_get_token
- if(parameter_t.type != NON_parameter){
- paser_error("Don't get formal_parameter");
- }
- get_pop_token(status, list, rb_t);
- printf("rb_t.type = %d\n", rb_t.type);
- if(rb_t.type != RB_PASER){
- paser_error("Don't get ')'[1]");
- }
- p_list = parameter_t.data.parameter_list;
- }
- else{
- p_list = NULL; // 没参数
- }
- get_right_token(status,list,block_,block_t);
- if(block_t.type != NON_block){ // 不是表达式
- paser_error("Don't get '{'");
- }
- statement *def_tmp = make_statement();
- def_tmp->type = def;
- def_tmp->code.def.var = name_t.data.statement_value;
- def_tmp->code.def.parameter_list = p_list;
- def_tmp->code.def.done = block_t.data.statement_value;
- new_token.type = NON_def;
- new_token.data_type = statement_value;
- new_token.data.statement_value = def_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return;
- }
- else{
- back_one_token(list, def_t);
- return;
- }
- }
- void formal_parameter(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: formal_parameter\n");
- token left, next, comma, before, eq, value_token, new_token;
- int mode = only_value;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_parameter){ // 模式2/3
- fprintf(status_log, "[info][grammar] (formal_parameter)reduce right\n");
- get_pop_token(status, list, comma);
- if(comma.type == COMMA_PASER){
- get_pop_token(status, list, before);
- if(before.type == MUL_PASER){
- if(status->is_dict){
- paser_error("dict shouldn't get '*'");
- }
- mode = put_args;
- }
- else if(before.type == POW_PASER){
- if(status->is_list){
- paser_error("list shouldn't get '*'");
- }
- mode = put_kwargs;
- }
- else{
- mode = only_value;
- back_again(list,before); // 回退
- }
- status->is_parameter = true;
- get_right_token(status, list, top_exp, next);
- status->is_parameter = false;
- if(next.type != NON_top_exp){
- paser_error("Don't get a top_exp");
- return;
- }
- new_token = left;
- parameter *tmp = NULL;
- get_pop_token(status, list, eq);
- if(eq.type == (status->is_dict ? COLON_PASER : EQ_PASER)){ // name_value模式
- if(status->is_list){
- paser_error("list shouldn't get '='");
- }
- get_right_token(status, list, top_exp, value_token);
- if(value_token.type != NON_top_exp){
- paser_error("Don't get a top_exp");
- return;
- }
- tmp = append_parameter_name(next.data.statement_value, new_token.data.parameter_list);
- tmp->type = name_value;
- tmp->u.value = value_token.data.statement_value;
- }
- else{
- if(status->is_dict){
- paser_error("dict should get ':'[1]");
- }
- back_again(list,eq); // 回退[如果使用back_one_token则会导致add_node在EQ的后面]
- tmp = append_parameter_value(next.data.statement_value, new_token.data.parameter_list);
- tmp->type = mode;
- tmp->u.var = tmp->u.value;
- }
- add_node(list, new_token);
- return formal_parameter(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (formal_parameter)out\n");
- back_one_token(list, left);
- back_again(list, comma);
- return;
- }
- }
- else if(left.type == POW_PASER || left.type == MUL_PASER){ // 模式1
- fprintf(status_log, "[info][grammar] (formal_parameter)back one token to (top_exp)[**/*]\n");
- status->is_parameter = true;
- get_right_token(status, list, top_exp, next); // 不需要back_one_token
- status->is_parameter = false;
- if(next.type != NON_top_exp){
- back_one_token(list, next); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_parameter;
- new_token.data_type = parameter_list;
- new_token.data.parameter_list = make_parameter_value(next.data.statement_value);
- new_token.data.parameter_list->u.var = new_token.data.parameter_list->u.value;
- if(left.type == POW_PASER){
- if(status->is_list){
- paser_error("list shouldn't get '**'");
- }
- new_token.data.parameter_list->type = put_kwargs;
- }
- else{
- if(status->is_dict){
- paser_error("dict shouldn't get '*'");
- }
- new_token.data.parameter_list->type = put_args;
- }
- add_node(list, new_token);
- return formal_parameter(status, list); // 回调自己
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (formal_parameter)back one token to (top_exp)\n");
- back_one_token(list, left);
- printf("left.type = %d\n", left.type);
- status->is_parameter = true;
- get_base_token(status, list, top_exp, next);
- status->is_parameter = false;
- if(next.type != NON_top_exp){
- back_one_token(list, next); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_parameter;
- new_token.data_type = parameter_list;
- parameter *tmp = NULL;
- get_pop_token(status, list, eq);
- printf("eq.type = %d\n", eq.type);
- if(eq.type == (status->is_dict ? COLON_PASER : EQ_PASER)){ // name_value模式
- if(status->is_list){
- paser_error("list shouldn't get '='");
- }
- get_right_token(status, list, top_exp, value_token);
- if(value_token.type != NON_top_exp){
- paser_error("Don't get a top_exp");
- return;
- }
- new_token.data.parameter_list = make_parameter_name(next.data.statement_value);
- new_token.data.parameter_list->u.value = value_token.data.statement_value;
- new_token.data.parameter_list->type = name_value;
- }
- else{
- if(status->is_dict){
- paser_error("dict should get ':'[2]");
- }
- back_again(list,eq); // 回退[如果使用back_one_token则会导致add_node在EQ的后面]
- new_token.data.parameter_list = make_parameter_value(next.data.statement_value);
- new_token.data.parameter_list->u.var = new_token.data.parameter_list->u.value;
- new_token.data.parameter_list->type = only_value;
- }
- add_node(list, new_token);
- return formal_parameter(status, list); // 回调自己
- }
- }
- /*
- while_ : WHILE LB top_exp RB block
- */
- void while_(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: while_\n");
- token while_t, lb_t, exp_t, rb_t, block_t, new_token;
- while_t = pop_node(list);
- if(while_t.type == WHILE_PASER){
- get_pop_token(status, list, lb_t);
- if(lb_t.type != LB_PASER){
- paser_error("Don't get '('");
- }
- get_right_token(status,list,top_exp,exp_t);
- if(exp_t.type != NON_top_exp){ // 不是表达式
- paser_error("Don't get 'top_exp'");
- }
- get_pop_token(status, list, rb_t);
- if(rb_t.type != RB_PASER){
- paser_error("Don't get ')'");
- }
- get_right_token(status,list,block_,block_t);
- if(block_t.type != NON_block){ // 不是表达式
- paser_error("Don't get '{'");
- }
- statement *while_tmp = make_statement();
- while_tmp->type = while_cycle;
- while_tmp->code.while_cycle.condition = exp_t.data.statement_value;
- while_tmp->code.while_cycle.done = block_t.data.statement_value;
- new_token.type = NON_while;
- new_token.data_type = statement_value;
- new_token.data.statement_value = while_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return;
- }
- else{
- back_one_token(list, while_t);
- return;
- }
- }
- /*
- block_ : LP command_list RB
- */
- void block_(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: block_\n");
- token lp_t, rp_t, new_token, command_list_t;
- lp_t = pop_node(list);
- again:
- if(lp_t.type == LP_PASER){
- statement *block_tmp = make_statement();
- statement_base = append_statement_list(block_tmp, statement_base);
-
- get_right_token(status,list,command_list,command_list_t); // 要把command_list也弹出来
- statement_base = free_statement_list(statement_base); // 重新释放
- get_pop_token(status, list, rp_t);
- if(rp_t.type != RP_PASER){
- paser_error("Don't get '}'");
- }
- new_token.type = NON_block;
- new_token.data_type = statement_value;
- new_token.data.statement_value = block_tmp;
- add_node(list, new_token); // 压入节点
- return;
- }
- if(lp_t.type == ENTER_PASER){
- get_pop_token(status,list,lp_t);
- goto again;
- }
- else{
- printf("lp_t.type = %d != %d\n", lp_t.type, LP_PASER);
- back_one_token(list, lp_t);
- return;
- }
- }
- // var_ctrl_ 包含诸如:global, nolocal,之类的
- void var_ctrl_(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: bit_not\n");
- token left, var, right, new_token;
- statement *times = NULL;
- char *var_name = NULL;
- left = pop_node(list);
- if(left.type == GLOBAL_PASER || left.type == DEFAULT_PASER || left.type == NOLOCAL_PASER){
- fprintf(status_log, "[info][grammar] (ctrl_)reduce right\n");
- get_right_token(status, list, top_exp, var); // 取得base_var
- if(var.type != NON_top_exp && var.data.statement_value->type != base_var){
- paser_error("Don't get var");
- }
- else{
- var_name = malloc(sizeof(var.data.statement_value->code.base_var.var_name));
- strcpy(var_name, var.data.statement_value->code.base_var.var_name);
- times = var.data.statement_value->code.base_var.from;
- // TODO:: 本质上没有完全释放
- free(var.data.statement_value->code.base_var.var_name);
- free(var.data.statement_value);
- }
- if(left.type == DEFAULT_PASER){ // 设置times
- get_right_token(status, list, top_exp, right); // 回调右边
- if(right.type != NON_top_exp){
- back_again(list, right); // 不是期望的数字
- }
- else{
- times = right.data.statement_value;
- }
- }
- // 逻辑操作
- new_token.type = NON_ctrl;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- switch (left.type)
- {
- case GLOBAL_PASER:
- code_tmp->type = set_global;
- code_tmp->code.set_global.name = var_name;
- break;
- case DEFAULT_PASER:
- code_tmp->type = set_default;
- code_tmp->code.set_default.name = var_name;
- code_tmp->code.set_default.times = times;
- break;
- case NOLOCAL_PASER:
- code_tmp->type = set_nonlocal;
- code_tmp->code.set_nonlocal.name = var_name;
- break;
- default:
- break;
- }
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return; // 回调自己
- }
- else{ // 模式1
- back_one_token(list, left);
- return;
- }
- }
- void return_(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: return_\n");
- token left, right, value, new_token;
- statement *times = NULL, *back_value = NULL;
- left = pop_node(list);
- if(left.type == RETURN_PASER){
- fprintf(status_log, "[info][grammar] (return_)reduce right\n");
- get_right_token(status, list, top_exp, value); // 回调右边
- if(value.type != NON_top_exp){
- back_again(list, value); // 不是期望的数字,就默认使用NULL,并且回退
- times = NULL;
- back_value = NULL;
- goto not_times;
- }
- else{
- back_value = value.data.statement_value;
- }
- get_right_token(status, list, top_exp, right); // 回调右边
- if(right.type != NON_top_exp){
- back_again(list, right); // 不是期望的数字,就默认使用NULL,并且回退
- times = NULL;
- }
- else{
- times = right.data.statement_value;
- }
- not_times:
- new_token.type = NON_return;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = return_code;
- code_tmp->code.return_code.times = times;
- code_tmp->code.return_code.value = back_value;
-
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return; // 回调自己
- }
- else{ // 模式1
- back_one_token(list, left);
- return;
- }
- }
- // ctrl_包含诸如:break,broken,之类的
- void ctrl_(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: bit_not\n");
- token left, right, new_token;
- statement *times = NULL;
- left = pop_node(list);
- if(left.type == BREAK_PASER || left.type == BROKEN_PASER || left.type == CONTINUE_PASER || left.type == CONTINUED_PASER ||
- left.type == RESTART_PASER || left.type == RESTARTED_PASER || left.type == REGO_PASER || left.type == REWENT_PASER){
- fprintf(status_log, "[info][grammar] (ctrl_)reduce right\n");
- if(left.type != REGO_PASER && left.type != REWENT_PASER){
- get_right_token(status, list, top_exp, right); // 回调右边
- if(right.type != NON_top_exp){
- back_again(list, right); // 不是期望的数字,就默认使用NULL,并且回退
- times = NULL;
- }
- else{
- times = right.data.statement_value;
- }
- }
- // 逻辑操作
- new_token.type = NON_ctrl;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- switch (left.type)
- {
- case BREAK_PASER:
- code_tmp->type = break_cycle;
- code_tmp->code.break_cycle.times = times;
- break;
- case BROKEN_PASER:
- code_tmp->type = broken;
- code_tmp->code.broken.times = times;
- break;
- case CONTINUE_PASER:
- code_tmp->type = continue_cycle;
- code_tmp->code.continue_cycle.times = times;
- break;
- case CONTINUED_PASER:
- code_tmp->type = continued;
- code_tmp->code.continued.times = times;
- break;
- case RESTART_PASER:
- code_tmp->type = restart;
- code_tmp->code.restart.times = times;
- break;
- case RESTARTED_PASER:
- code_tmp->type = restarted;
- code_tmp->code.restarted.times = times;
- break;
- case REGO_PASER:
- code_tmp->type = rego;
- break;
- case REWENT_PASER:
- code_tmp->type = rewent;
- break;
- default:
- break;
- }
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return; // 回调自己
- }
- else{ // 模式1
- back_one_token(list, left);
- return;
- }
- }
- /*
- top_exp : polynomial
- */
- void top_exp(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: top_exp\n");
- token exp;
- get_base_token(status,list,eq_number,exp);
- if(exp.type != NON_eq){
- back_one_token(list, exp);
- return;
- }
- exp.type = NON_top_exp;
- add_node(list, exp); // 压入节点
- return;
- }
- /*
- eq_number : bool_or
- | eq_number AND bool_or
- */
- void eq_number(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: eq_number\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_eq){ // 模式2/3
- fprintf(status_log, "[info][grammar] (eq_number)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == EQ_PASER && !status->is_parameter){ // 模式2/3
- get_right_token(status, list, call_back_, right); // 回调右边
- if(right.type != NON_call){
- paser_error("Don't get a call_back_");
- }
- // 逻辑操作
- new_token.type = NON_eq;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- code_tmp->code.operation.type = ASSIGnMENT_func;
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
-
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return eq_number(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (bit_notor)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (bool_or)back one token to (bool_and)\n");
- back_one_token(list, left);
- get_base_token(status, list, call_back_, new_token);
- if(new_token.type != NON_call){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_eq;
- add_node(list, new_token);
- return eq_number(status, list); // 回调自己
- }
- }
- /*
- call_back_ : bool_or
- | call_back_ LB RB
- */
- void call_back_(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: call_back_\n");
- token left, symbol, rb_t, new_token, parameter_t;
- parameter *p_list;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_call){
- fprintf(status_log, "[info][grammar] (call_back_)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == LB_PASER && !status->is_func){
- get_pop_token(status, list, rb_t);
- if(rb_t.type != RB_PASER){ // 带参数
- back_again(list, rb_t);
- get_right_token(status,list,formal_parameter,parameter_t);
- if(parameter_t.type != NON_parameter){
- paser_error("Don't get formal_parameter");
- }
- printf("rb_t.type = %d\n", rb_t.type);
- get_pop_token(status, list, rb_t);
- if(rb_t.type != RB_PASER){
- paser_error("Don't get ')'[2]");
- }
- p_list = parameter_t.data.parameter_list;
- }
- else{
- p_list = NULL; // 没参数
- }
- // 逻辑操作
- new_token.type = NON_call;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = call;
- code_tmp->code.call.func = left.data.statement_value;
- code_tmp->code.call.parameter_list = p_list;
-
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return call_back_(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (call_back_)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (call_back_)back one token to (bool_or)\n");
- back_one_token(list, left);
- get_base_token(status, list, bool_or, new_token);
- if(new_token.type != NON_bool_or){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_call;
- add_node(list, new_token);
- return call_back_(status, list); // 回调自己
- }
- }
- /*
- bool_or : bool_and
- | bool_or AND bool_and
- */
- void bool_or(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: bool_or\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_bool_or){ // 模式2/3
- fprintf(status_log, "[info][grammar] (bool_or)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == OR_PASER){ // 模式2/3
- get_right_token(status, list, bool_and, right); // 回调右边
- if(right.type != NON_bool_and){
- paser_error("Don't get a compare");
- }
- // 逻辑操作
- new_token.type = NON_bool_or;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- code_tmp->code.operation.type = OR_func;
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
-
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return bool_or(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (bit_notor)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (bool_or)back one token to (bool_and)\n");
- back_one_token(list, left);
- get_base_token(status, list, bool_and, new_token);
- if(new_token.type != NON_bool_and){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_bool_or;
- add_node(list, new_token);
- return bool_or(status, list); // 回调自己
- }
- }
- /*
- bool_and : bool_not
- | bool_and AND bool_not
- */
- void bool_and(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: bool_and\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_bool_and){ // 模式2/3
- fprintf(status_log, "[info][grammar] (bool_and)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == AND_PASER){ // 模式2/3
- get_right_token(status, list, bool_not, right); // 回调右边
- if(right.type != NON_bool_not){
- paser_error("Don't get a bool_not");
- }
- // 逻辑操作
- new_token.type = NON_bool_and;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- code_tmp->code.operation.type = AND_func;
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
-
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return bool_and(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (bit_notor)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (bool_and)back one token to (compare)\n");
- back_one_token(list, left);
- get_base_token(status, list, bool_not, new_token);
- if(new_token.type != NON_bool_not){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_bool_and;
- add_node(list, new_token);
- return bool_and(status, list); // 回调自己
- }
- }
- /*
- bool_not : compare
- | BITNOT bool_not
- */
- void bool_not(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: negative\n");
- token left, right, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NOT_PASER){ // 模式2
- fprintf(status_log, "[info][grammar] (bool_not)reduce right\n");
- get_right_token(status, list, bool_not, right); // 回调右边
- if(right.type != NON_bool_not){
- paser_error("Don't get a bool_not");
- }
- // 逻辑操作
- new_token.type = NON_bool_not;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- code_tmp->code.operation.type = NOT_func;
- code_tmp->code.operation.left_exp = NULL;
- code_tmp->code.operation.right_exp = right.data.statement_value;
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return; // 回调自己
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (negative)back one token to (compare)\n");
- back_one_token(list, left);
- get_base_token(status, list, compare, new_token);
- if(new_token.type != NON_compare){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_bool_not;
- add_node(list, new_token);
- return;
- }
- }
- void compare(p_status *status, token_node *list){ // 多项式
- fprintf(status_log, "[info][grammar] mode status: polynomial\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_compare){ // 模式2/3
- fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == EQEQ_PASER || symbol.type == MOREEQ_PASER || symbol.type == LESSEQ_PASER ||
- symbol.type == MORE_PASER || symbol.type == LESS_PASER || symbol.type == NOTEQ_PASER){ // 模式2/3
- get_right_token(status, list, bit_notor, right); // 回调右边
- if(right.type != NON_bit_notor){
- paser_error("Don't get a bit_notor");
- }
- new_token.type = NON_compare;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- if(symbol.type == EQEQ_PASER){
- code_tmp->code.operation.type = EQUAL_func;
- }
- else if(symbol.type == MOREEQ_PASER){
- code_tmp->code.operation.type = MOREEQ_func;
- }
- else if(symbol.type == LESSEQ_PASER){
- code_tmp->code.operation.type = LESSEQ_func;
- }
- else if(symbol.type == MORE_PASER){
- code_tmp->code.operation.type = MORE_func;
- }
- else if(symbol.type == LESS_PASER){
- code_tmp->code.operation.type = LESS_func;
- }
- else{
- code_tmp->code.operation.type = NOTEQ_func;
- }
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return compare(status, list); // 回调自己
- }
- else{ // 递归跳出
- fprintf(status_log, "[info][grammar] (polynomial)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
- back_one_token(list, left);
- get_base_token(status, list, bit_notor, new_token);
- if(new_token.type != NON_bit_notor){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_compare;
- add_node(list, new_token);
- return compare(status, list); // 回调自己
- }
- }
- /*
- bit_notor : bit_or
- | bit_notor BITOR bit_or
- */
- void bit_notor(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: bit_or\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_bit_notor){ // 模式2/3
- fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == BITNOTOR_PASER){ // 模式2/3
- get_right_token(status, list, bit_or, right); // 回调右边
- if(right.type != NON_bit_or){
- paser_error("Don't get a bit_or");
- }
- // 逻辑操作
- new_token.type = NON_bit_notor;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- code_tmp->code.operation.type = BITNOTOR_func;
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
-
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return bit_notor(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (bit_notor)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
- back_one_token(list, left);
- get_base_token(status, list, bit_or, new_token);
- if(new_token.type != NON_bit_or){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_bit_notor;
- add_node(list, new_token);
- return bit_notor(status, list); // 回调自己
- }
- }
- /*
- bit_or : bit_and
- | bit_or BITOR bit_and
- */
- void bit_or(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: bit_or\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_bit_or){ // 模式2/3
- fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == BITOR_PASER){ // 模式2/3
- get_right_token(status, list, bit_and, right); // 回调右边
- if(right.type != NON_bit_and){
- paser_error("Don't get a bit_and");
- }
- // 逻辑操作
- new_token.type = NON_bit_or;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- code_tmp->code.operation.type = BITOR_func;
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
-
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return bit_or(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (bit_or)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
- back_one_token(list, left);
- get_base_token(status, list, bit_and, new_token);
- if(new_token.type != NON_bit_and){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_bit_or;
- add_node(list, new_token);
- return bit_or(status, list); // 回调自己
- }
- }
- /*
- bit_and : bit_move
- | bit_and BITAND bit_move
- */
- void bit_and(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: bit_and\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_bit_and){ // 模式2/3
- fprintf(status_log, "[info][grammar] (factor)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == BITAND_PASER){ // 模式2/3
- get_right_token(status, list, bit_move, right); // 回调右边
- if(right.type != NON_bit_move){
- paser_error("Don't get a bit_move");
- }
- // 逻辑操作
- new_token.type = NON_bit_and;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- code_tmp->code.operation.type = BITAND_func;
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
-
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return bit_and(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (bit_and)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
- back_one_token(list, left);
- get_base_token(status, list, bit_move, new_token);
- if(new_token.type != NON_bit_move){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_bit_and;
- add_node(list, new_token);
- return bit_and(status, list); // 回调自己
- }
- }
- /*
- bit_move : power
- | bit_move BITRIGHT factor
- | bit_move BITLEFT factor
- */
- void bit_move(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: factor\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_bit_move){ // 模式2/3
- fprintf(status_log, "[info][grammar] (factor)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == BITRIGHT_PASER || symbol.type == BITLEFT_PASER){ // 模式2/3
- get_right_token(status, list, polynomial, right); // 回调右边
- if(right.type != NON_polynomial){
- paser_error("Don't get a polynomial");
- }
- // 逻辑操作
- new_token.type = NON_bit_move;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- if(symbol.type == BITRIGHT_PASER){
- code_tmp->code.operation.type = BITRIGHT_func;
- }
- else{
- code_tmp->code.operation.type = BITLEFT_func;
- }
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return bit_move(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (bit_move)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
- back_one_token(list, left);
- get_base_token(status, list, polynomial, new_token);
- if(new_token.type != NON_polynomial){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_bit_move;
- add_node(list, new_token);
- return bit_move(status, list); // 回调自己
- }
- }
- /*
- polynomial : factor
- | polynomial ADD factor
- | polynomial SUB factor
- */
- void polynomial(p_status *status, token_node *list){ // 多项式
- fprintf(status_log, "[info][grammar] mode status: polynomial\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_polynomial){ // 模式2/3
- fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == ADD_PASER || symbol.type == SUB_PASER){ // 模式2/3
- get_right_token(status, list, factor, right); // 回调右边
- if(right.type != NON_factor){
- paser_error("Don't get a factor");
- }
- new_token.type = NON_polynomial;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- if(symbol.type == ADD_PASER){
- code_tmp->code.operation.type = ADD_func;
- }
- else{
- code_tmp->code.operation.type = SUB_func;
- }
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return polynomial(status, list); // 回调自己
- }
- else{ // 递归跳出
- fprintf(status_log, "[info][grammar] (polynomial)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
- back_one_token(list, left);
- get_base_token(status, list, factor, new_token);
- if(new_token.type != NON_factor){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_polynomial;
- add_node(list, new_token);
- return polynomial(status, list); // 回调自己
- }
- }
- /*
- factor : power
- | factor MUL power
- | factor DIV power
- */
- void factor(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: factor\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_factor){ // 模式2/3
- fprintf(status_log, "[info][grammar] (factor)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == MUL_PASER || symbol.type == DIV_PASER){ // 模式2/3
- get_right_token(status, list, negative, right); // 回调右边
- if(right.type != NON_negative){
- paser_error("Don't get a value");
- }
- // 逻辑操作
- new_token.type = NON_factor;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- if(symbol.type == MUL_PASER){
- code_tmp->code.operation.type = MUL_func;
- }
- else{
- code_tmp->code.operation.type = DIV_func;
- }
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return factor(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (factor)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (factor)back one token to (element)\n");
- back_one_token(list, left);
- get_base_token(status, list, negative, new_token);
- if(new_token.type != NON_negative){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_factor;
- add_node(list, new_token);
- return factor(status, list); // 回调自己
- }
- }
- /*
- negative : bit_not
- | BITNOT bit_not
- */
- void negative(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: negative\n");
- token left, right, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == SUB_PASER){ // 模式2
- fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
- get_right_token(status, list, negative, right); // 回调右边
- if(right.type != NON_negative){
- paser_error("Don't get a negative");
- }
- // 逻辑操作
- new_token.type = NON_negative;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- code_tmp->code.operation.type = NEGATIVE_func;
- code_tmp->code.operation.left_exp = NULL;
- code_tmp->code.operation.right_exp = right.data.statement_value;
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return; // 回调自己
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (negative)back one token to (bit_not)\n");
- back_one_token(list, left);
- get_base_token(status, list, bit_not, new_token);
- if(new_token.type != NON_bit_not){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_negative;
- add_node(list, new_token);
- return;
- }
- }
- /*
- bit_not : power
- | BITNOT power
- */
- void bit_not(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: bit_not\n");
- token left, right, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == BITNOT_PASER){ // 模式2
- fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
- get_right_token(status, list, bit_not, right); // 回调右边
- if(right.type != NON_bit_not){
- paser_error("Don't get a bit_not");
- }
- // 逻辑操作
- new_token.type = NON_bit_not;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- code_tmp->code.operation.type = BITNOT_func;
- code_tmp->code.operation.left_exp = NULL;
- code_tmp->code.operation.right_exp = right.data.statement_value;
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return; // 回调自己
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (bit_not)back one token to (power)\n");
- back_one_token(list, left);
- get_base_token(status, list, power, new_token);
- if(new_token.type != NON_power){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_bit_not;
- add_node(list, new_token);
- return;
- }
- }
- /*
- power : element
- | power POW element
- | power LOG element
- | power SQRT element
- */
- void power(p_status *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: power\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_power){ // 模式2/3
- fprintf(status_log, "[info][grammar] (power)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == POW_PASER || symbol.type == LOG_PASER || symbol.type == SQRT_PASER){ // 模式2/3/4
- get_right_token(status, list, call_down, right); // 回调右边
- if(right.type != NON_call_down){
- paser_error("Don't get a call_down");
- }
- // 逻辑操作
- new_token.type = NON_power;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- if(symbol.type == POW_PASER){
- code_tmp->code.operation.type = POW_func;
- }
- else if(symbol.type == LOG_PASER){
- code_tmp->code.operation.type = LOG_func;
- }
- else{
- code_tmp->code.operation.type = SQRT_func;
- }
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return power(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (power)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (power)back one token to (element)\n");
- back_one_token(list, left);
- get_base_token(status, list, call_down, new_token);
- if(new_token.type != NON_call_down){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_power;
- add_node(list, new_token);
- return power(status, list); // 回调自己
- }
- }
- /*
- call_down : element
- | call_down LI top_exp RI
- */
- void call_down(p_status *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: call_down\n");
- token left, lb_t, rb_t, new_token, parameter_t;
- parameter *p_list;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_call_down){
- fprintf(status_log, "[info][grammar] (call_down)reduce right\n");
- get_pop_token(status, list, lb_t);
- if(lb_t.type == LI_PASER){
- get_right_token(status, list, top_exp, parameter_t); // 回调右边
- if(parameter_t.type != NON_top_exp){
- paser_error("Don't get a top_exp");
- }
- get_pop_token(status, list, rb_t);
- if(rb_t.type != RI_PASER){ // 带参数
- paser_error("Don't get ']'");
- }
- // 逻辑操作
- new_token.type = NON_call_down;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = down;
- code_tmp->code.down.base_var = left.data.statement_value;
- code_tmp->code.down.child_var = parameter_t.data.statement_value;
-
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return call_down(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (call_back_)out\n");
- back_one_token(list, left);
- back_again(list, lb_t);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (call_down)back one token to (element)\n");
- back_one_token(list, left);
- get_base_token(status, list, element, new_token);
- if(new_token.type != NON_element){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_call_down;
- add_node(list, new_token);
- return call_down(status, list); // 回调自己
- }
- }
- /*
- element : number
- | LB top_exp RB
- */
- void element(p_status *status, token_node *list){ // 数字归约
- fprintf(status_log, "[info][grammar] mode status: element\n");
- token gett, new_token;
- gett = pop_node(list); // 取得一个token
- if(gett.type == LB_PASER){ // 模式3
- fprintf(status_log, "[info][grammar] (element)get LB\n");
- get_right_token(status, list, top_exp, new_token);
- if(new_token.type != NON_top_exp){
- paser_error("Don't get 'top_exp'");
- }
- token rb;
- get_pop_token(status, list ,rb);
- if(rb.type != RB_PASER){ // 匹配失败
- paser_error("Don't get ')'");
- }
- new_token.type = NON_element;
- add_node(list, new_token); // 压入节点
- return;
- }
- else if(gett.type == VAR_PASER){ // a
- back_one_token(list, gett);
- get_base_token(status, list, var_token, new_token);
- if(new_token.type != NON_base_var){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_element;
- add_node(list, new_token);
- return;
- }
- else if(gett.type == LP_PASER){
- back_one_token(list, gett);
- get_base_token(status,list,dict_,new_token); // 不需要safe_get_token
- if(new_token.type != NON_dict){
- paser_error("Don't get a dict_");
- }
- new_token.type = NON_element;
- add_node(list, new_token);
- return;
- }
- else if(gett.type == LI_PASER){ // [1]a或[1]列表或[1,2,3,4]列表
- token exp_token, rb, tmp_var;
- get_right_token(status, list, top_exp, exp_token);
- if(exp_token.type == RI_PASER){ //可以认定为空list
- back_one_token(list, gett);
- back_again(list, exp_token);
- get_base_token(status,list,list_,new_token); // 不需要safe_get_token
- if(new_token.type != NON_list){
- paser_error("Don't get a list_");
- }
- goto back;
- }
- else if(exp_token.type != NON_top_exp){
- paser_error("Don't get 'top_exp'");
- }
-
- get_pop_token(status, list ,rb);
- if(rb.type == RI_PASER){ // 匹配失败 TODO:: 检查是不是[1,2,3,4]的列表类型
- get_pop_token(status, list ,tmp_var);
- if(tmp_var.type == VAR_PASER){ // a
- back_one_token(list, tmp_var);
- get_base_token(status, list, var_token, new_token);
- if(new_token.type != NON_base_var){
- paser_error("Don't get var");
- }
- new_token.data.statement_value->code.base_var.from = exp_token.data.statement_value;
- goto back;
- }
- else{
- // back again连用的时候要倒过来使用
- /* 原因:
- 先back_one_token的时候,token流变成
- <之前的> - <A> - <NULL[seek/index]>
- 先back_again的时候,token流变成
- <之前的> - <A> - <B[seek]> - <NULL[index]>
- 再back_again的时候,token流变成
- <之前的> - <A> - <C[seek]> - <B> - <NULL[index]>
- 其实还有一种解决方法:
- 连用三次back_one_token
- 然后再用两次back_token手动回退
- */
- back_one_token(list, gett);
- back_again(list, tmp_var);
- back_again(list, rb);
- back_again(list, exp_token);
- get_base_token(status,list,list_,new_token); // 不需要safe_get_token
- if(new_token.type != NON_list){
- paser_error("Don't get a list_");
- }
- goto back;
- }
- }
- else{
- back_one_token(list, gett);
- back_again(list, rb);
- back_again(list, exp_token);
- get_base_token(status,list,list_,new_token); // 不需要safe_get_token
- if(new_token.type != NON_list){
- paser_error("Don't get a list_");
- }
- goto back;
- }
- back:
- new_token.type = NON_element;
- add_node(list, new_token);
- return;
- }
- else{
- fprintf(status_log, "[info][grammar] (element)back one token to (paser_value)\n");
- back_one_token(list, gett);
- get_base_token(status, list, paser_value, new_token);
- if(new_token.type != NON_base_value){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_element;
- add_node(list, new_token);
- return;
- }
- }
- /*
- list_ : LI formal_parameter RI
- */
- void list_(p_status *status, token_node *list){ // 数字归约
- fprintf(status_log, "[info][grammar] mode status: list_\n");
- token gett, list_core, ri_t, new_token;
- parameter *base = NULL;
- gett = pop_node(list); // 取得一个token
- if(gett.type == LI_PASER){ // var类型
- status->is_list = true;
- get_right_token(status,list,formal_parameter,list_core);
- status->is_list = false;
- if(list_core.type == RI_PASER){ // 空列表
- base = NULL;
- goto make_list;
- }
- else if(list_core.type == NON_parameter){
- base = list_core.data.parameter_list;
- }
- else{
- paser_error("Don't get formal_parameter");
- }
- get_pop_token(status,list,ri_t);
- if(ri_t.type != RI_PASER){
- paser_error("Don't get ']'");
- }
- make_list:
- new_token.type = NON_list;
- statement *code_tmp = make_statement();
- code_tmp->type = base_list;
- code_tmp->code.base_list.value = base;
-
- new_token.data.statement_value = code_tmp;
- new_token.data_type = statement_value;
- fprintf(status_log, "[info][grammar] (list_)out\n");
- add_node(list, new_token); // 压入节点
- return;
- }
- else{ // 不是期望值
- fprintf(status_log, "[info][grammar] (list_)back one token\n");
- back_one_token(list, gett);
- return;
- }
- }
- /*
- dict_ : LP formal_parameter RP
- */
- void dict_(p_status *status, token_node *list){ // 数字归约
- fprintf(status_log, "[info][grammar] mode status: dict_\n");
- token gett, dict_core, rp_t, new_token;
- parameter *base = NULL;
- gett = pop_node(list); // 取得一个token
- if(gett.type == LP_PASER){ // var类型
- status->is_dict = true;
- get_right_token(status,list,formal_parameter,dict_core);
- status->is_dict = false;
- if(dict_core.type == RP_PASER){ // 空列表
- base = NULL;
- goto make_dict;
- }
- else if(dict_core.type == NON_parameter){
- base = dict_core.data.parameter_list;
- }
- else{
- paser_error("Don't get formal_parameter");
- }
- get_pop_token(status,list,rp_t);
- if(rp_t.type != RP_PASER){
- paser_error("Don't get '}'");
- }
- make_dict:
- new_token.type = NON_dict;
- statement *code_tmp = make_statement();
- code_tmp->type = base_dict;
- code_tmp->code.base_dict.value = base;
-
- new_token.data.statement_value = code_tmp;
- new_token.data_type = statement_value;
- fprintf(status_log, "[info][grammar] (dict_)out\n");
- add_node(list, new_token); // 压入节点
- return;
- }
- else{ // 不是期望值
- fprintf(status_log, "[info][grammar] (dict_)back one token\n");
- back_one_token(list, gett);
- return;
- }
- }
- /*
- var_token : VAR
- */
- void var_token(p_status *status, token_node *list){ // 数字归约
- fprintf(status_log, "[info][grammar] mode status: var_token\n");
- token gett, new_token;
- gett = pop_node(list); // 取得一个token
- if(gett.type == VAR_PASER){ // var类型
- new_token.type = NON_base_var;
- GWARF_value tmp_value;
- tmp_value.type = INT_value;
- tmp_value.value.int_value = atoi(gett.data.text);
- statement *code_tmp = make_statement();
- code_tmp->type = base_var;
- code_tmp->code.base_var.var_name = gett.data.text;
- code_tmp->code.base_var.from = NULL;
-
- new_token.data.statement_value = code_tmp;
- new_token.data_type = statement_value;
- fprintf(status_log, "[info][grammar] (var_token)out\n");
- add_node(list, new_token); // 压入节点
- return;
- }
- else{ // 不是期望值
- fprintf(status_log, "[info][grammar] (var_token)back one token\n");
- back_one_token(list, gett);
- return;
- }
- }
- /*
- number : INT_PASER
- | DOUBLE_PASER
- | LB top_exp RB
- */
- void paser_value(p_status *status, token_node *list){ // 数字归约
- fprintf(status_log, "[info][grammar] mode status: paser_value\n");
- token gett, new_token;
- gett = pop_node(list); // 取得一个token
- if(gett.type == INT_PASER){ // int类型
- new_token.type = NON_base_value;
- GWARF_value tmp_value;
- tmp_value.type = INT_value;
- tmp_value.value.int_value = atoi(gett.data.text);
- statement *code_tmp = make_statement();
- code_tmp->type = call;
- code_tmp->code.call.func = pack_call_name("int", NULL);
- code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
- new_token.data.statement_value = code_tmp;
- new_token.data_type = statement_value;
- }
- else if(gett.type == DOUBLE_PASER){
- new_token.type = NON_base_value;
- GWARF_value tmp_value;
- tmp_value.type = NUMBER_value;
- tmp_value.value.double_value = atof(gett.data.text);
- statement *code_tmp = make_statement();
- code_tmp->type = call;
- code_tmp->code.call.func = pack_call_name("double", NULL);
- code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
- new_token.data.statement_value = code_tmp;
- new_token.data_type = statement_value;
- }
- else if(gett.type == STR_PASER){
- new_token.type = NON_base_value;
- GWARF_value tmp_value;
- tmp_value.type = STRING_value;
- if(gett.data.text == NULL){
- tmp_value.value.string = "";
- }
- else{
- tmp_value.value.string = gett.data.text;
- }
- printf("tmp_value.value.string = %s, %s\n", tmp_value.value.string, gett.data.text);
- statement *code_tmp = make_statement();
- code_tmp->type = call;
- code_tmp->code.call.func = pack_call_name("str", NULL);
- code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
- new_token.data.statement_value = code_tmp;
- new_token.data_type = statement_value;
- goto not_free;
- // printf("[info][grammar] (paser_value)get str: %s\n", tmp_value.value.string);
- }
- else if(gett.type == TRUE_PASER || gett.type == FALSE_PASER){
- new_token.type = NON_base_value;
- GWARF_value tmp_value;
- tmp_value.type = BOOL_value;
- if(gett.type == TRUE_PASER){
- tmp_value.value.bool_value = true;
- }
- else{
- tmp_value.value.bool_value = false;
- }
- statement *code_tmp = make_statement();
- code_tmp->type = call;
- code_tmp->code.call.func = pack_call_name("bool", NULL);
- code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
- new_token.data.statement_value = code_tmp;
- new_token.data_type = statement_value;
- }
- else if(gett.type == NONE_PASER){
- new_token.type = NON_base_value;
- statement *code_tmp = make_statement();
- code_tmp->type = base_value;
- code_tmp->code.base_value.value.type = NULL_value;
- code_tmp->code.base_value.value.value.int_value = 0;
- new_token.data.statement_value = code_tmp;
- new_token.data_type = statement_value;
- }
- else{ // 不是期望值
- fprintf(status_log, "[info][grammar] (paser_value)back one token\n");
- back_one_token(list, gett);
- return;
- }
- free(gett.data.text); // 释放字符串
- not_free:
- fprintf(status_log, "[info][grammar] (paser_value)add one token\n");
- add_node(list, new_token); // 压入节点
- }
- void paser_error(char *text){
- fprintf(status_log, "[error][grammar] paser error : %s\n\n", text);
- printf("[error][grammar] paser error : %s\n\n", text);
- exit(1);
- }
|