diff --git a/version3.0/main.cpp b/version3.0/main.cpp index b5497bf..cf8c1ea 100644 --- a/version3.0/main.cpp +++ b/version3.0/main.cpp @@ -46,7 +46,7 @@ void lex_func() if(!lexer.get_error()) lexer.print_token(); else - std::cout<<">> [lexer] error occurred,stop.\n"; + std::cout<<">> [lexer] error(s) occurred,stop.\n"; return; } @@ -58,10 +58,10 @@ void par_func() parse.set_toklist(lexer.get_token_list()); parse.main_process(); if(parse.get_error()) - std::cout<<">> [parse] error occurred,stop.\n"; + std::cout<<">> [parse] error(s) occurred,stop.\n"; } else - std::cout<<">> [lexer] error occurred,stop.\n"; + std::cout<<">> [lexer] error(s) occurred,stop.\n"; return; } @@ -70,6 +70,7 @@ int main() #ifdef _WIN32 // use chcp 65001 to use unicode io system("chcp 65001"); + system("cls"); #endif // this curve looks really cool logo(); diff --git a/version3.0/nasal.ebnf b/version3.0/nasal.ebnf index 5bf9e52..d7a3d4a 100644 --- a/version3.0/nasal.ebnf +++ b/version3.0/nasal.ebnf @@ -100,7 +100,7 @@ multi_scalar::= '(' {',' calculation} ')' ; multi_assignment::= - multi_scalar '=' multi_scalar + multi_scalar '=' (multi_scalar|calculation) ; loop::= while_loop @@ -114,7 +114,7 @@ for_loop::= for '(' [definition|calculation] ';' [calculation] ';' [calculation] ')' expressions ; forei_loop::= - (forindex | foreach) '(' (definition | assignment) ';' calculation ')' expressions + (forindex | foreach) '(' (definition | calculation) ';' calculation ')' expressions ; conditional::= if '(' calculation ')' expressions diff --git a/version3.0/nasal_enum.h b/version3.0/nasal_enum.h index 56e91e8..76d63df 100644 --- a/version3.0/nasal_enum.h +++ b/version3.0/nasal_enum.h @@ -32,7 +32,7 @@ enum ast_node ast_unary_sub,ast_unary_not, ast_trinocular, ast_for,ast_forindex,ast_foreach,ast_while, - ast_if,ast_elsif,ast_else, + ast_conditional,ast_if,ast_elsif,ast_else, ast_multi_id,ast_multi_scalar, ast_definition,ast_multi_assign, ast_continue,ast_break,ast_return, @@ -56,7 +56,12 @@ enum parse_error lack_scalar, lack_identifier, lack_calculation, + lack_exprs, lack_token, + lack_args, + default_arg_not_end, + dynamic_id_not_end, + multi_assign_lack_val }; void error_info(int line,int error_type,std::string error_str="") @@ -66,23 +71,28 @@ void error_info(int line,int error_type,std::string error_str="") std::cout<=tok_list_size) + { + ++error; + error_info(error_line,lack_right_curve); + } + + std::string args_format="func("; + int node_child_size=node.get_children().size(); + for(int i=0;i=tok_list_size) + { + ++error; + error_info(error_line,lack_exprs); + return node; + } node.set_line(tok_list[ptr].line); node.set_type(ast_block); if(tok_list[ptr].type==tok_left_brace) { int left_brace_line=tok_list[ptr].line; + ++ptr; while(ptr=tok_list_size) break; + if(tok_list[ptr].type==tok_semi) ++ptr; + else if(node.get_children().empty() || !check_function_end(node.get_children().back())) { ++error; error_info(error_line,lack_semi); + break; } } - if(ptr>=tok_list_size) + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_brace) { - error_info(left_brace_line,exprs_lack_rbrace); ++error; + error_info(left_brace_line,exprs_lack_rbrace); } } else @@ -480,6 +527,12 @@ nasal_ast nasal_parse::exprs_gen() nasal_ast nasal_parse::calculation() { nasal_ast node; + if(ptr>=tok_list_size) + { + ++error; + error_info(error_line,lack_calculation); + return node; + } node=or_expr(); ++ptr; if(ptr=tok_list_size) + { + ++error; + error_info(error_line,lack_scalar); + return node; + } + if(tok_list[ptr].type==tok_left_curve) + node.add_child(check_multi_scalar()?multi_scalar():calculation()); + else + node.add_child(calculation()); return node; } nasal_ast nasal_parse::normal_def() @@ -951,11 +1015,53 @@ nasal_ast nasal_parse::multi_id() nasal_ast nasal_parse::multi_scalar() { nasal_ast node; + node.set_line(tok_list[ptr].line); + node.set_type(ast_multi_scalar); + ++ptr; + while(ptr=tok_list_size) break; + if(tok_list[ptr].type==tok_comma) ++ptr; + else if(tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_curve) + { + ++error; + error_info(error_line,lack_comma); + break; + } + } + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) + { + ++error; + error_info(error_line,lack_right_curve); + } return node; } nasal_ast nasal_parse::multi_assgin() { nasal_ast node; + node.set_line(tok_list[ptr].line); + node.set_type(ast_multi_assign); + node.add_child(multi_scalar()); + ++ptr; + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_equal) + { + ++error; + error_info(error_line,lack_equal); + return node; + } + ++ptr; + if(ptr>=tok_list_size) + { + ++error; + error_info(error_line,multi_assign_lack_val); + return node; + } + if(tok_list[ptr].type==tok_left_curve) + node.add_child(check_multi_scalar()?multi_scalar():calculation()); + else + node.add_child(calculation()); return node; } nasal_ast nasal_parse::loop() @@ -991,12 +1097,14 @@ nasal_ast nasal_parse::while_loop() { ++error; error_info(error_line,lack_left_curve); + return node; } ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) { ++error; error_info(error_line,lack_right_curve); + return node; } ++ptr; node.add_child(exprs_gen()); @@ -1012,6 +1120,7 @@ nasal_ast nasal_parse::for_loop() { ++error; error_info(error_line,lack_left_curve); + return node; } ++ptr; // unfinished @@ -1031,6 +1140,7 @@ nasal_ast nasal_parse::forei_loop() { ++error; error_info(error_line,lack_left_curve); + return node; } ++ptr; // unfinished @@ -1039,8 +1149,73 @@ nasal_ast nasal_parse::forei_loop() nasal_ast nasal_parse::conditional() { nasal_ast node; + nasal_ast tmp; node.set_line(tok_list[ptr].line); - node.set_type(ast_if); + node.set_type(ast_conditional); + tmp.set_line(tok_list[ptr].line); + tmp.set_type(ast_if); + ++ptr; + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) + { + ++error; + error_info(error_line,lack_left_curve); + return node; + } + ++ptr; + tmp.add_child(calculation()); + ++ptr; + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) + { + ++error; + error_info(error_line,lack_right_curve); + return node; + } + ++ptr; + 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) + { + ++error; + error_info(error_line,lack_left_curve); + return node; + } + tmp.set_line(tok_list[ptr].line); + tmp.set_type(ast_elsif); + tmp.get_children().clear(); + ++ptr; + tmp.add_child(calculation()); + ++ptr; + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) + { + ++error; + error_info(error_line,lack_right_curve); + return node; + } + ++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 + // for example + // else if(scalar){} else {} + // ptr^ + if(ptr