Fixed bugs in multive_calc(endless loop)

This commit is contained in:
Valk Richard Li 2019-10-22 23:43:51 +08:00 committed by GitHub
parent 5b2c10ca46
commit 1a74e67abd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 63 deletions

View File

@ -34,6 +34,7 @@ class abstract_syntax_tree
var_number=p.var_number; var_number=p.var_number;
var_string=p.var_string; var_string=p.var_string;
var_name=p.var_name; var_name=p.var_name;
children.clear();
children=p.children; children=p.children;
return *this; return *this;
} }
@ -53,6 +54,15 @@ class abstract_syntax_tree
str+="| "; str+="| ";
std::cout<<str; std::cout<<str;
print_token(ast_node_type); print_token(ast_node_type);
switch(ast_node_type)
{
case __number:std::cout<<": "<<var_number;break;
case __string:std::cout<<": "<<var_string;break;
case __id:
case __list_search:
case __hash_search:
case __call_function:std::cout<<": "<<var_name;break;
}
std::cout<<std::endl; std::cout<<std::endl;
if(!children.empty()) if(!children.empty())
{ {

View File

@ -1223,8 +1223,7 @@ abstract_syntax_tree nasal_parser::mul_div_operator_expr()
node=identifier_call_expr(); node=identifier_call_expr();
break; break;
case __left_curve: case __left_curve:
parse.push(this_token); node=in_curve_calc_expr();
node=calculation_expr();
break; break;
default: default:
++error; ++error;
@ -1277,8 +1276,7 @@ abstract_syntax_tree nasal_parser::mul_div_operator_expr()
node=temp; node=temp;
break; break;
case __left_curve: case __left_curve:
parse.push(this_token); temp.add_child(in_curve_calc_expr());
temp.add_child(calculation_expr());
node=temp; node=temp;
break; break;
default: default:
@ -1294,7 +1292,7 @@ abstract_syntax_tree nasal_parser::unary_operator_expr()
{ {
abstract_syntax_tree node; abstract_syntax_tree node;
abstract_syntax_tree temp; abstract_syntax_tree temp;
node.set_node_type(__unary_operation); node.set_node_type(this_token.type);
get_token(); get_token();
switch(this_token.type) switch(this_token.type)
{ {
@ -1315,8 +1313,7 @@ abstract_syntax_tree nasal_parser::unary_operator_expr()
node.add_child(temp); node.add_child(temp);
break; break;
case __left_curve: case __left_curve:
parse.push(this_token); node.add_child(in_curve_calc_expr());
node.add_child(calculation_expr());
break; break;
default: default:
++error; ++error;
@ -1539,42 +1536,14 @@ abstract_syntax_tree nasal_parser::call_function_expr()
abstract_syntax_tree node; abstract_syntax_tree node;
abstract_syntax_tree temp; abstract_syntax_tree temp;
node.set_node_type(__call_function); node.set_node_type(__call_function);
while(this_token.type!=__right_curve) get_token();
if(this_token.type!=__right_curve)
{ {
get_token(); parse.push(this_token);
temp.set_clear(); while(this_token.type!=__right_curve)
switch(this_token.type)
{
case __left_curve:
case __nor_operator:
case __sub_operator:
case __number:
case __string:
case __id:
parse.push(this_token);
temp=calculation_expr();
break;
case __left_bracket:
temp=list_generate_expr();
break;
case __left_brace:
temp=hash_generate_expr();
break;
case __func:
temp=parameter_function_expr();
break;
default:
++error;
std::cout<<">>[Error] line "<<this_token.line<<": incorrect token '";
print_token(this_token.type);
std::cout<<"' when calling a function."<<std::endl;
return node;
break;
}
get_token();
if(this_token.type==__colon)
{ {
get_token(); get_token();
temp.set_clear();
switch(this_token.type) switch(this_token.type)
{ {
case __left_curve: case __left_curve:
@ -1584,16 +1553,16 @@ abstract_syntax_tree nasal_parser::call_function_expr()
case __string: case __string:
case __id: case __id:
parse.push(this_token); parse.push(this_token);
temp.add_child(calculation_expr()); temp=calculation_expr();
break; break;
case __left_bracket: case __left_bracket:
temp.add_child(list_generate_expr()); temp=list_generate_expr();
break; break;
case __left_brace: case __left_brace:
temp.add_child(hash_generate_expr()); temp=hash_generate_expr();
break; break;
case __func: case __func:
temp.add_child(parameter_function_expr()); temp=parameter_function_expr();
break; break;
default: default:
++error; ++error;
@ -1603,22 +1572,55 @@ abstract_syntax_tree nasal_parser::call_function_expr()
return node; return node;
break; break;
} }
}
else
parse.push(this_token);
node.add_child(temp);
get_token();
if(this_token.type!=__comma && this_token.type!=__right_curve)
{
++error;
std::cout<<">>[Error] line "<<this_token.line<<": expect a ',' after parameter or ')' to end the call."<<std::endl;
return node;
}
if(this_token.type==__comma)
{
get_token(); get_token();
if(this_token.type!=__right_curve) if(this_token.type==__colon)
{
get_token();
switch(this_token.type)
{
case __left_curve:
case __nor_operator:
case __sub_operator:
case __number:
case __string:
case __id:
parse.push(this_token);
temp.add_child(calculation_expr());
break;
case __left_bracket:
temp.add_child(list_generate_expr());
break;
case __left_brace:
temp.add_child(hash_generate_expr());
break;
case __func:
temp.add_child(parameter_function_expr());
break;
default:
++error;
std::cout<<">>[Error] line "<<this_token.line<<": incorrect token '";
print_token(this_token.type);
std::cout<<"' when calling a function."<<std::endl;
return node;
break;
}
}
else
parse.push(this_token); parse.push(this_token);
node.add_child(temp);
get_token();
if(this_token.type!=__comma && this_token.type!=__right_curve)
{
++error;
std::cout<<">>[Error] line "<<this_token.line<<": expect a ',' after parameter or ')' to end the call."<<std::endl;
return node;
}
if(this_token.type==__comma)
{
get_token();
if(this_token.type!=__right_curve)
parse.push(this_token);
}
} }
} }
get_token(); get_token();
@ -1675,6 +1677,7 @@ abstract_syntax_tree nasal_parser::identifier_call_expr()
} }
node.set_var_name(temp_name); node.set_var_name(temp_name);
abstract_syntax_tree temp; abstract_syntax_tree temp;
abstract_syntax_tree assign;
get_token(); get_token();
switch(this_token.type) switch(this_token.type)
{ {
@ -1684,11 +1687,13 @@ abstract_syntax_tree nasal_parser::identifier_call_expr()
case __mul_equal: case __mul_equal:
case __div_equal: case __div_equal:
case __link_equal: case __link_equal:
temp.set_node_type(__assignment); assign.set_node_type(__assignment);
temp.set_node_type(this_token.type);
temp.add_child(node); temp.add_child(node);
temp.add_child(assignment_expr()); temp.add_child(assignment_expr());
assign.add_child(temp);
node.set_clear(); node.set_clear();
node=temp; node=assign;
break; break;
default:parse.push(this_token);break; default:parse.push(this_token);break;
} }

View File

@ -32,7 +32,6 @@ enum token_type
__root, __root,
__list,__hash, __list,__hash,
__hash_member, __hash_member,
__unary_operation,
__call_function,__list_search,__hash_search, __call_function,__list_search,__hash_search,
__normal_statement_block, __normal_statement_block,
__definition,__assignment, __definition,__assignment,
@ -98,7 +97,6 @@ void print_token(int type)
case __list: context="list";break; case __list: context="list";break;
case __hash: context="hash";break; case __hash: context="hash";break;
case __hash_member: context="hash_member";break; case __hash_member: context="hash_member";break;
case __unary_operation: context="unary_calc";break;
case __call_function: context="call_func";break; case __call_function: context="call_func";break;
case __list_search: context="call_list";break; case __list_search: context="call_list";break;
case __hash_search: context="call_hash";break; case __hash_search: context="call_hash";break;
@ -108,7 +106,7 @@ void print_token(int type)
case __function: context="function";break; case __function: context="function";break;
case __loop: context="loop";break; case __loop: context="loop";break;
case __ifelse: context="if-else";break; case __ifelse: context="if-else";break;
default: context="unknown_token";break; default: context="undefined_token";break;
} }
std::cout<<context; std::cout<<context;
return; return;