Add continue|break|return

This commit is contained in:
Valk Richard Li 2019-11-11 23:31:20 +08:00 committed by GitHub
parent 5134f4eea2
commit 2989f731e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 35 deletions

View File

@ -5,7 +5,39 @@ int exit_type=0;
bool abstract_syntax_tree::check() bool abstract_syntax_tree::check()
{ {
bool ret=false;
if(this->type==__cmp_equal)
{
}
else if(this->type==__cmp_not_equal)
{
}
else if(this->type==__cmp_less)
{
}
else if(this->type==__cmp_less_or_equal)
{
}
else if(this->type==__cmp_more)
{
}
else if(this->type==__cmp_more_or_equal)
{
}
else if(this->type==__or_operator)
{
}
else if(this->type==__and_operator)
{
}
return true; return true;
} }
@ -123,24 +155,42 @@ void abstract_syntax_tree::run_root()
return; return;
} }
void abstract_syntax_tree::run_loop() int abstract_syntax_tree::run_loop()
{ {
int ret=0;
scope.add_new_local_scope(); scope.add_new_local_scope();
abstract_syntax_tree condition=children.front(); abstract_syntax_tree condition=children.front();
abstract_syntax_tree blk=children.back(); abstract_syntax_tree blk=children.back();
while(condition.check()) while(condition.check())
blk.run_block(); {
int type=blk.run_block();
if(type==__break)
break;
else if(type==__return)
{
ret=__return;
break;
}
}
scope.pop_last_local_scope(); scope.pop_last_local_scope();
return; return ret;
} }
void abstract_syntax_tree::run_ifelse() int abstract_syntax_tree::run_ifelse()
{ {
int ret=0;
scope.add_new_local_scope(); scope.add_new_local_scope();
for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
{
if(i->children.front().check())
{
ret=i->children.back().run_block();
break;
}
}
scope.pop_last_local_scope(); scope.pop_last_local_scope();
return; return ret;
} }
void abstract_syntax_tree::run_func() void abstract_syntax_tree::run_func()
@ -184,31 +234,31 @@ int abstract_syntax_tree::run_block()
else if(i->type==__id) else if(i->type==__id)
std::cout<<i->call_id().get_name()<<std::endl; std::cout<<i->call_id().get_name()<<std::endl;
else if(i->type==__while) else if(i->type==__while)
i->run_loop(); {
int type=i->run_loop();
if(type)
{
if(type==__return)
return type;
else
{
std::cout<<"[Runtime-error] incorrect use of break/continue."<<std::endl;
exit_type=__error_command_use;
}
}
}
else if(i->type==__ifelse) else if(i->type==__ifelse)
i->run_ifelse(); {
int type=i->run_ifelse();
if(type)
return type;
}
else if(i->type==__continue) else if(i->type==__continue)
{
return __continue; return __continue;
// else
// {
// std::cout<<">>[Runtime-error] must use \'continue\' in a loop."<<std::endl;
// exit_type=__error_command_use;
// }
}
else if(i->type==__break) else if(i->type==__break)
{ return __break;
return __loop;
// else
// {
// std::cout<<">>[Runtime-error] must use \'continue\' in a loop."<<std::endl;
// exit_type=__error_command_use;
// }
}
else if(i->type==__return) else if(i->type==__return)
{ return __return;
break;
}
if(exit_type!=__process_exited_successfully) if(exit_type!=__process_exited_successfully)
break; break;
} }

View File

@ -188,8 +188,8 @@ class abstract_syntax_tree
var call_id(); var call_id();
var get_value(); var get_value();
void run_root(); void run_root();
void run_loop(); int run_loop();
void run_ifelse(); int run_ifelse();
void run_func(); void run_func();
int run_block(); int run_block();
}; };

View File

@ -315,10 +315,7 @@ abstract_syntax_tree balloon_parse::array_generate()
std::cout<<">>[Parse-error] line "<<this_token.line<<": expect a \'[\' here."<<std::endl; std::cout<<">>[Parse-error] line "<<this_token.line<<": expect a \'[\' here."<<std::endl;
return new_node; return new_node;
} }
get_token(); while(1)
if(this_token.type!=__right_bracket)
parse.push(this_token);
while(this_token.type!=__right_bracket)
{ {
get_token(); get_token();
switch(this_token.type) switch(this_token.type)
@ -346,6 +343,8 @@ abstract_syntax_tree balloon_parse::array_generate()
std::cout<<">>[Parse-error] line "<<this_token.line<<": expect a \',\' or \']\' here."<<std::endl; std::cout<<">>[Parse-error] line "<<this_token.line<<": expect a \',\' or \']\' here."<<std::endl;
return new_node; return new_node;
} }
else if(this_token.type==__right_bracket)
break;
} }
return new_node; return new_node;
} }
@ -504,8 +503,7 @@ abstract_syntax_tree balloon_parse::block()
case __continue: case __continue:
case __break:temp.set_clear();temp.set_type(this_token.type);new_node.add_child(temp);check_semi();break; case __break:temp.set_clear();temp.set_type(this_token.type);new_node.add_child(temp);check_semi();break;
case __return:parse.push(this_token);new_node.add_child(ret());check_semi();break; case __return:parse.push(this_token);new_node.add_child(ret());check_semi();break;
case __right_brace:return new_node;break; case __right_brace:parse.push(this_token);break;
// must ret at this place when meeting a lot of right braces here like }}} or some of the } will be missed
default: default:
++error; ++error;
std::cout<<">>[Parse-error] line "<<this_token.line<<": \'"; std::cout<<">>[Parse-error] line "<<this_token.line<<": \'";
@ -514,6 +512,11 @@ abstract_syntax_tree balloon_parse::block()
return new_node; return new_node;
break; break;
} }
get_token();
if(this_token.type==__right_brace)
break;
else
parse.push(this_token);
} }
return new_node; return new_node;
} }