From 8b4514ab2e1bd9dfcd6420b5491c6bce11dfbe0f Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Mon, 29 Jun 2020 00:03:10 -0700 Subject: [PATCH] update --- version3.0/nasal.ebnf | 4 +- version3.0/nasal_enum.h | 7 ++ version3.0/nasal_parse.h | 235 ++++++++++++++++++++++++++++++--------- 3 files changed, 194 insertions(+), 52 deletions(-) diff --git a/version3.0/nasal.ebnf b/version3.0/nasal.ebnf index fcf370d..5bf9e52 100644 --- a/version3.0/nasal.ebnf +++ b/version3.0/nasal.ebnf @@ -94,10 +94,10 @@ definition::= |'(' var multi_id ')' '=' (calculation | multi_scalar) ; multi_id::= - {id ','} + {',' id} ; multi_scalar::= - '(' {calculation ','} ')' + '(' {',' calculation} ')' ; multi_assignment::= multi_scalar '=' multi_scalar diff --git a/version3.0/nasal_enum.h b/version3.0/nasal_enum.h index 1486d5d..06bb846 100644 --- a/version3.0/nasal_enum.h +++ b/version3.0/nasal_enum.h @@ -33,6 +33,7 @@ enum ast_node ast_trinocular, ast_for,ast_forindex,ast_foreach,ast_while, ast_if,ast_elsif,ast_else, + ast_multi_id,ast_multi_scalar, ast_definition,ast_multi_assign,ast_calculation, ast_continue,ast_break,ast_return, }; @@ -44,11 +45,14 @@ enum parse_error lack_left_curve, lack_right_curve, lack_left_bracket, + lack_right_bracket, lack_left_brace, lack_right_brace, + exprs_lack_rbrace, lack_semi, lack_comma, lack_colon, + lack_equal, lack_scalar, lack_identifier, lack_calculation, @@ -67,11 +71,14 @@ void error_info(int line,int error_type,std::string error_str="") case lack_left_curve: detail="expected \'(\'."; break; case lack_right_curve: detail="expected \')\'."; break; case lack_left_bracket: detail="expected \'[\'."; break; + case lack_right_bracket:detail="expected \']\'."; break; case lack_left_brace: detail="expected \'{\'."; break; case lack_right_brace: detail="expected \'}\'."; break; + case exprs_lack_rbrace: detail="expected \'}\' with this line\'s \'{\'.";break; case lack_semi: detail="expected \';\'."; break; case lack_comma: detail="expected \',\'."; break; case lack_colon: detail="expected \':\'."; break; + case lack_equal: detail="expected \'=\'."; break; case lack_scalar: detail="expected scalar here."; break; case lack_identifier: detail="expected identifier here."; break; case lack_calculation: detail="expected arithmetic-expression here."; break; diff --git a/version3.0/nasal_parse.h b/version3.0/nasal_parse.h index 3cb60e4..8b55779 100644 --- a/version3.0/nasal_parse.h +++ b/version3.0/nasal_parse.h @@ -3,6 +3,10 @@ class nasal_parse { +#ifndef error_line +#define error_line tok_list[ptr>=tok_list_size? tok_list_size-1:ptr].line +#endif + private: int tok_list_size; int ptr; @@ -10,6 +14,8 @@ private: nasal_ast root; std::vector tok_list; void reset(); + bool check_multi_definition(); + bool check_multi_scalar(); bool check_function_end(nasal_ast&); bool check_special_call(); nasal_ast nil_gen(); @@ -73,7 +79,7 @@ void nasal_parse::clear() void nasal_parse::set_toklist(std::vector& lex_token) { this->tok_list=lex_token; - tok_list_size=this->tok_list.size(); + this->tok_list_size=this->tok_list.size(); return; } @@ -90,7 +96,7 @@ void nasal_parse::main_process() else if(ptr> [parse] complete generation. "<=tok_list_size) return node; node.set_line(tok_list[ptr].line); node.set_type(ast_nil); return node; @@ -181,11 +228,16 @@ nasal_ast nasal_parse::vector_gen() if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_bracket) + { + ++error; + error_info(error_line,lack_right_bracket); + } return node; } nasal_ast nasal_parse::hash_gen() @@ -198,23 +250,27 @@ nasal_ast nasal_parse::hash_gen() { node.add_child(hash_member_gen()); ++ptr; - if(ptr>=tok_list_size) break; if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_brace) + { + ++error; + error_info(error_line,lack_right_brace); + } return node; } nasal_ast nasal_parse::hash_member_gen() { nasal_ast node; - if(tok_list[ptr].type!=tok_identifier) + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) { - error_info(tok_list[ptr].line,lack_identifier); + error_info(error_line,lack_identifier); return node; } node.set_line(tok_list[ptr].line); @@ -223,7 +279,7 @@ nasal_ast nasal_parse::hash_member_gen() ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_colon) { - error_info(ptr>=tok_list_size?node.get_line():tok_list[ptr].line,lack_colon); + error_info(error_line,lack_colon); ++error; return node; } @@ -231,7 +287,7 @@ nasal_ast nasal_parse::hash_member_gen() if(ptr=tok_list_size) { - error_info(node.get_line(),lack_left_curve); + error_info(error_line,lack_left_curve); ++error; return node; } @@ -255,7 +311,7 @@ nasal_ast nasal_parse::func_gen() } if(ptr>=tok_list_size) { - error_info(node.get_line(),lack_left_brace); + error_info(error_line,lack_left_brace); ++error; return node; } @@ -264,7 +320,6 @@ nasal_ast nasal_parse::func_gen() } nasal_ast nasal_parse::args_list_gen() { - int last_id_line; nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_args); @@ -279,7 +334,6 @@ nasal_ast nasal_parse::args_list_gen() return node; } tmp=id_gen(); - last_id_line=tmp.get_line(); ++ptr; if(ptr=tok_list_size || (tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_curve)) { ++error; - error_info(last_id_line,lack_comma); + error_info(error_line,lack_comma); --ptr; } } @@ -329,6 +383,14 @@ nasal_ast nasal_parse::expr() case tok_sub: case tok_not: node=calculation(); break; case tok_var: node=definition(); break; + case tok_left_curve: + if(check_multi_definition()) + node=definition(); + else if(check_multi_scalar()) + node=multi_assgin(); + else + node=calculation(); + break; case tok_for: case tok_forindex: case tok_foreach: @@ -338,7 +400,7 @@ nasal_ast nasal_parse::expr() case tok_break: node=break_expr(); break; case tok_return: node=return_expr(); break; case tok_semi: --ptr; break; - default: error_info(tok_list[ptr].line,error_token,tok_list[ptr].str);++error;break; + default: error_info(error_line,error_token,tok_list[ptr].str);++error;break; } return node; } @@ -349,6 +411,7 @@ nasal_ast nasal_parse::exprs_gen() node.set_type(ast_block); if(tok_list[ptr].type==tok_left_brace) { + int left_brace_line=tok_list[ptr].line; while(ptr=tok_list_size) { - error_info(node.get_line(),lack_right_brace); + error_info(left_brace_line,exprs_lack_rbrace); ++error; } } @@ -388,17 +451,17 @@ nasal_ast nasal_parse::calculation() tmp.add_child(node); ++ptr; if(ptr=tok_list_size || tok_list[ptr].type!=tok_colon) { ++error; - error_info(tmp.get_line(),lack_colon); + error_info(error_line,lack_colon); return node; } ++ptr; if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_curve) { ++error; - error_info(curve_line,lack_right_curve); + error_info(error_line,lack_right_curve); } } else { ++error; - error_info(tok_list[ptr].line,lack_scalar); - return nil_gen(); + error_info(error_line,lack_scalar); + return node; } ++ptr; if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_bracket) { ++error; - error_info(node.get_line(),lack_comma); + error_info(error_line,lack_comma); break; } } + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_bracket) + { + ++error; + error_info(error_line,lack_right_bracket); + } return node; } nasal_ast nasal_parse::call_func() @@ -679,9 +757,14 @@ nasal_ast nasal_parse::call_func() else if(ptr>=tok_list_size || tok_list[ptr].type!=tok_comma || tok_list[ptr].type!=tok_right_curve) { ++error; - error_info(node.get_line(),lack_comma); + error_info(error_line,lack_comma); } } + 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::subvec() @@ -718,11 +801,63 @@ nasal_ast nasal_parse::subvec() nasal_ast nasal_parse::definition() { nasal_ast node; + node.set_line(tok_list[ptr].line); + node.set_type(ast_definition); + if(tok_list[ptr].type==tok_var) + { + ++ptr; + // unfinished + } + else if(tok_list[ptr].type==tok_left_curve) + { + ++ptr; + node.add_child(multi_id()); + ++ptr; + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) + { + ++error; + error_info(error_line,lack_right_curve); + return node; + } + ++ptr; + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_equal) + { + ++error; + error_info(error_line,lack_equal); + return node; + } + ++ptr; + // unfinished + } return node; } nasal_ast nasal_parse::multi_id() { nasal_ast node; + if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) + { + ++error; + error_info(error_line,lack_identifier); + return node; + } + node.set_line(tok_list[ptr].line); + node.set_type(ast_multi_id); + node.add_child(id_gen()); + ++ptr; + while(ptrtok_list_size) + if(ptr>=tok_list_size) { ++error; - error_info(tok_list.back().line,lack_token,"loop"); + error_info(error_line,lack_token,"loop"); return node; } switch(tok_list[ptr].type) @@ -767,13 +902,13 @@ nasal_ast nasal_parse::while_loop() else { ++error; - error_info(node.get_line(),lack_left_curve); + error_info(error_line,lack_left_curve); } ++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) { ++error; - error_info(node.get_line(),lack_right_curve); + error_info(error_line,lack_right_curve); } ++ptr; node.add_child(exprs_gen()); @@ -785,10 +920,10 @@ nasal_ast nasal_parse::for_loop() node.set_line(tok_list[ptr].line); node.set_type(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) { ++error; - error_info(node.get_line(),lack_left_curve); + error_info(error_line,lack_left_curve); } ++ptr; // unfinished @@ -804,10 +939,10 @@ nasal_ast nasal_parse::forei_loop() 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) { ++error; - error_info(node.get_line(),lack_left_curve); + error_info(error_line,lack_left_curve); } ++ptr; // unfinished