diff --git a/nasal_parse.h b/nasal_parse.h index 3b74755..235453c 100644 --- a/nasal_parse.h +++ b/nasal_parse.h @@ -39,7 +39,7 @@ class nasal_parse { #define error_line (tok_list[ptr>=tok_list_size? tok_list_size-1:ptr].line) - +#define is_call(type) ((type)==tok_left_curve || (type)==tok_left_bracket || (type)==tok_dot) private: int tok_list_size; int ptr; @@ -186,8 +186,8 @@ bool nasal_parse::check_multi_definition() bool nasal_parse::check_multi_scalar() { - int check_ptr=ptr+1,curve_cnt=1,bracket_cnt=0,brace_cnt=0; - while(check_ptr=tok_list_size || tok_list[ptr].type!=tok_colon) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_colon) { die(error_line,"expected \":\""); return node; @@ -387,8 +384,7 @@ nasal_ast nasal_parse::args_list_gen() return node; } tmp=id_gen(); - ++ptr; - if(ptr\"");break; - case ast_greater_equal:num=1;break; - } - tmp.set_type(ast_number); - tmp.set_num(num); - tmp.get_children().clear(); - } - else if( - (type1==ast_nil && type2==ast_string) || - (type1==ast_string && type2==ast_nil) || - (type1==ast_nil && type2==ast_number) || - (type1==ast_number && type2==ast_nil) - ) - { - double num=(tmp.get_type()==ast_cmp_not_equal); - tmp.set_type(ast_number); - tmp.set_num(num); - tmp.get_children().clear(); - } - else if(type1==ast_number && type2==ast_number) - { - double num; - double num1=tmp.get_children()[0].get_num(); - double num2=tmp.get_children()[1].get_num(); - switch(tmp.get_type()) - { - case ast_cmp_equal:num=(num1==num2);break; - case ast_cmp_not_equal:num=(num1!=num2);break; - case ast_less_than:num=(num1num2);break; - case ast_greater_equal:num=(num1>=num2);break; - } - tmp.set_type(ast_number); - tmp.set_num(num); - tmp.get_children().clear(); - } - else if(type1==ast_number && type2==ast_string) - { - double num; - double num1=tmp.get_children()[0].get_num(); - double num2=trans_string_to_number(tmp.get_children()[1].get_str()); - if(std::isnan(num2)) - die(tmp.get_children()[1].get_line(),"cannot compare number and string"); - switch(tmp.get_type()) - { - case ast_cmp_equal:num=(num1==num2);break; - case ast_cmp_not_equal:num=(num1!=num2);break; - case ast_less_than:num=(num1num2);break; - case ast_greater_equal:num=(num1>=num2);break; - } - tmp.set_type(ast_number); - tmp.set_num(num); - tmp.get_children().clear(); - } - else if(type1==ast_string && type2==ast_number) - { - double num; - double num1=trans_string_to_number(tmp.get_children()[0].get_str()); - double num2=tmp.get_children()[1].get_num(); - if(std::isnan(num2)) - die(tmp.get_children()[0].get_line(),"cannot compare number and string"); - switch(tmp.get_type()) - { - case ast_cmp_equal:num=(num1==num2);break; - case ast_cmp_not_equal:num=(num1!=num2);break; - case ast_less_than:num=(num1num2);break; - case ast_greater_equal:num=(num1>=num2);break; - } - tmp.set_type(ast_number); - tmp.set_num(num); - tmp.get_children().clear(); - } - else if(type1==ast_string && type2==ast_string) - { - double num; - std::string str1=tmp.get_children()[0].get_str(); - std::string str2=tmp.get_children()[1].get_str(); - switch(tmp.get_type()) - { - case ast_cmp_equal:num=(str1==str2);break; - case ast_cmp_not_equal:num=(str1!=str2);break; - case ast_less_than:num=(str1str2);break; - case ast_greater_equal:num=(str1>=str2);break; - } - tmp.set_type(ast_number); - tmp.set_num(num); - tmp.get_children().clear(); - } node=tmp; - ++ptr; } --ptr; return node; @@ -778,8 +661,7 @@ nasal_ast nasal_parse::additive_expr() { nasal_ast node; node=multive_expr(); - ++ptr; - while(ptr=tok_list_size || tok_list[ptr].type!=tok_equal) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_equal) { die(error_line,"expected \"=\" / don\'t call identifier in definition"); return node; @@ -1193,10 +940,8 @@ nasal_ast nasal_parse::definition() if(node.get_children()[0].get_type()==ast_identifier && node.get_children()[1].get_type()==ast_multi_scalar) die(node.get_children()[1].get_line(),"one identifier cannot accept too many values"); else if(node.get_children()[0].get_type()==ast_multi_id && node.get_children()[1].get_type()==ast_multi_scalar) - { if(node.get_children()[0].get_children().size()!=node.get_children()[1].get_children().size()) die(node.get_children()[0].get_line(),"too much or lack values in multi-definition"); - } return node; } nasal_ast nasal_parse::var_incurve_def() @@ -1209,8 +954,7 @@ nasal_ast nasal_parse::var_incurve_def() return node; } node=multi_id(); - ++ptr; - if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_curve) die(error_line,"expected \")\""); @@ -1219,15 +963,13 @@ nasal_ast nasal_parse::var_incurve_def() nasal_ast nasal_parse::var_outcurve_def() { nasal_ast node; - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) { die(error_line,"expected identifier"); return node; } node=multi_id(); - ++ptr; - if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_curve) die(error_line,"expected \")\""); @@ -1282,8 +1024,7 @@ nasal_ast nasal_parse::multi_assgin() { nasal_ast node(tok_list[ptr].line,ast_multi_assign); node.add_child(multi_scalar(true)); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_equal) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_equal) { die(error_line,"expected \"=\""); return node; @@ -1297,11 +1038,8 @@ nasal_ast nasal_parse::multi_assgin() node.add_child(check_multi_scalar()?multi_scalar(false):calculation()); else node.add_child(calculation()); - if(node.get_children()[1].get_type()==ast_multi_scalar) - { - if(node.get_children()[0].get_children().size()!=node.get_children()[1].get_children().size()) - die(node.get_children()[0].get_line(),"too much or lack values in multi-assignment"); - } + if(node.get_children()[1].get_type()==ast_multi_scalar && node.get_children()[0].get_children().size()!=node.get_children()[1].get_children().size()) + die(node.get_children()[0].get_line(),"too much or lack values in multi-assignment"); return node; } nasal_ast nasal_parse::loop() @@ -1321,16 +1059,14 @@ nasal_ast nasal_parse::loop() nasal_ast nasal_parse::while_loop() { nasal_ast node(tok_list[ptr].line,ast_while); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) { die(error_line,"expected \"(\""); return node; } ++ptr; node.add_child(calculation()); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) { die(error_line,"expected \")\""); return node; @@ -1342,15 +1078,13 @@ nasal_ast nasal_parse::while_loop() nasal_ast nasal_parse::for_loop() { nasal_ast node(tok_list[ptr].line,ast_for); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) { die(error_line,"expected \"(\""); return node; } // first expression - ++ptr; - if(ptr>=tok_list_size) + if(++ptr>=tok_list_size) { die(error_line,"expected definition"); return node; @@ -1366,8 +1100,7 @@ nasal_ast nasal_parse::for_loop() node.add_child(check_multi_definition()?definition():(check_multi_scalar()?multi_assgin():calculation())); else node.add_child(calculation()); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) { die(error_line,"expected \";\""); return node; @@ -1385,8 +1118,7 @@ nasal_ast nasal_parse::for_loop() } else node.add_child(calculation()); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) { die(error_line,"expected \";\""); return node; @@ -1422,23 +1154,20 @@ nasal_ast nasal_parse::forei_loop() case tok_forindex: node.set_type(ast_forindex);break; case tok_foreach: node.set_type(ast_foreach); break; } - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) { die(error_line,"expected \"(\""); return node; } // first expression // foreach/forindex must have an iterator to loop through - ++ptr; - if(ptr>=tok_list_size || (tok_list[ptr].type!=tok_var && tok_list[ptr].type!=tok_identifier)) + if(++ptr>=tok_list_size || (tok_list[ptr].type!=tok_var && tok_list[ptr].type!=tok_identifier)) { die(error_line,"expected iterable value"); return node; } node.add_child(new_iter_gen()); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) { die(error_line,"expected \";\""); return node; @@ -1449,8 +1178,7 @@ nasal_ast nasal_parse::forei_loop() return node; } node.add_child(calculation()); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) { die(error_line,"expected \")\""); return node; @@ -1466,8 +1194,7 @@ nasal_ast nasal_parse::new_iter_gen() if(tok_list[ptr].type==tok_var) { node.set_type(ast_new_iter); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) { die(error_line,"expected identifier"); return node; @@ -1478,12 +1205,8 @@ nasal_ast nasal_parse::new_iter_gen() { node.set_type(ast_call); node.add_child(id_gen()); - ++ptr; - while(ptr=tok_list_size || tok_list[ptr].type!=tok_left_curve) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) { die(error_line,"expected \"(\""); return node; } ++ptr; tmp.add_child(calculation()); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) { die(error_line,"expected \")\""); return node; @@ -1511,24 +1232,19 @@ nasal_ast nasal_parse::conditional() tmp.add_child(exprs_gen()); node.add_child(tmp); // end of if-expression - ++ptr; - while(ptr=tok_list_size || tok_list[ptr].type!=tok_left_curve) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) { die(error_line,"expected \"(\""); return node; } - tmp.set_line(tok_list[ptr].line); - tmp.set_type(ast_elsif); - tmp.get_children().clear(); + nasal_ast tmp(tok_list[ptr].line,ast_elsif); ++ptr; tmp.add_child(calculation()); - ++ptr; - if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) + if(++ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) { die(error_line,"expected \")\""); return node; @@ -1536,7 +1252,6 @@ nasal_ast nasal_parse::conditional() ++ptr; tmp.add_child(exprs_gen()); node.add_child(tmp); - ++ptr; } // end of elsif-expression // after this process,ptr will point to the next token of exprs_gen()'s last token @@ -1545,9 +1260,7 @@ nasal_ast nasal_parse::conditional() // ptr^ if(ptr