From 6b9150ab256ec57f11382b212aab73a37385da4a Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Sun, 2 Feb 2020 16:17:12 +0800 Subject: [PATCH] update --- version2.0/nasal_enum.h | 4 +++ version2.0/nasal_parse.h | 60 +++++++++++++++++++++++---------------- version2.0/test/scope.nas | 2 +- 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/version2.0/nasal_enum.h b/version2.0/nasal_enum.h index 595af9b..3e14869 100644 --- a/version2.0/nasal_enum.h +++ b/version2.0/nasal_enum.h @@ -165,6 +165,7 @@ enum parse_error_type definition_lack_id, // lack identifier definition_lack_equal, // lack '=' when not getting ';' + assignment_begin_error, // assignment begins with more than one identifier_call multi_definition_need_curve, // lack right curve when generating 'var (id,id,id)' multi_assignment_need_curve, // lack right curve when generating (scalar,scalar)=(scalar,scalar) @@ -226,6 +227,9 @@ void print_parse_error(int error_type,int line,int error_token_type=__stack_end) print_parse_token(error_token_type); std::cout<<"\' when generating definition."<get_token(); } + + // assignment must has only one identifier_call as its beginning + // if not it is a parse-error + // this block is only used for assignment check(not multi-assignment) + this->push_token(); + if(calc_node.get_node_type()==__id) + { + abstract_syntax_tree assignment_node; + this->get_token();// check if this token is '=' or '+=' or '-=' or '*=' or '/=' or '~=' + if((this_token.type==__equal) || (this_token.type==__add_equal) || (this_token.type==__sub_equal) || (this_token.type==__mul_equal) || (this_token.type==__div_equal) || (this_token.type==__link_equal)) + { + // ('=' | '+=' | '-=' | '*=' | '/=' | '~=') + assignment_node.set_node_line(this_token.line); + assignment_node.set_node_type(this_token.type); + assignment_node.add_children(calc_node); + assignment_node.add_children(calculation()); + calc_node=assignment_node; + } + else + this->push_token(); + } + else + { + this->get_token(); + if((this_token.type==__equal) || (this_token.type==__add_equal) || (this_token.type==__sub_equal) || (this_token.type==__mul_equal) || (this_token.type==__div_equal) || (this_token.type==__link_equal)) + { + ++error; + print_parse_error(assignment_begin_error,this_token.line); + } + this->push_token(); + } + + // check ternary_operator + this->get_token(); if(this_token.type==__ques_mark) { tmp_node.set_clear(); @@ -757,7 +790,7 @@ abstract_syntax_tree nasal_parse::multive_calculation() else { this->push_token(); - calc_node=assign_calculation(); + calc_node=scalar_generate(); } this->get_token(); while((this_token.type==__mul_operator) || (this_token.type==__div_operator)) @@ -782,7 +815,7 @@ abstract_syntax_tree nasal_parse::multive_calculation() else { this->push_token(); - calc_node=assign_calculation(); + calc_node=scalar_generate(); } tmp_node.add_children(calc_node); calc_node=tmp_node; @@ -792,27 +825,6 @@ abstract_syntax_tree nasal_parse::multive_calculation() return calc_node; } -abstract_syntax_tree nasal_parse::assign_calculation() -{ - abstract_syntax_tree scalar_node=scalar_generate(); - abstract_syntax_tree assignment_node; - this->get_token();// check if this token is '=' or '+=' or '-=' or '*=' or '/=' or '~=' - if((this_token.type==__equal) || (this_token.type==__add_equal) || (this_token.type==__sub_equal) || (this_token.type==__mul_equal) || (this_token.type==__div_equal) || (this_token.type==__link_equal)) - { - // ('=' | '+=' | '-=' | '*=' | '/=' | '~=') - assignment_node.set_node_line(this_token.line); - assignment_node.set_node_type(this_token.type); - assignment_node.add_children(scalar_node); - assignment_node.add_children(calculation()); - } - else - { - this->push_token(); - assignment_node=scalar_node; - } - return assignment_node; -} - abstract_syntax_tree nasal_parse::scalar_generate() { this->get_token(); diff --git a/version2.0/test/scope.nas b/version2.0/test/scope.nas index a8a1fab..c49f02c 100644 --- a/version2.0/test/scope.nas +++ b/version2.0/test/scope.nas @@ -26,7 +26,7 @@ var func2=func() print(temp_value,"< 10"); elsif(10<=temp_value and temp_value<50) print(temp_value,"< 50"); - 10=temp_value; + temp_value=10; } return; }