fixed bug when meeting }}}}}}}}}}}}

This commit is contained in:
Valk Richard Li 2019-11-10 23:44:36 +08:00 committed by GitHub
parent ed2910f708
commit 5134f4eea2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 28 deletions

View File

@ -1,6 +1,6 @@
#ifndef __ABSTRACT_SYNTAX_TREE_CPP__ #ifndef __ABSTRACT_SYNTAX_TREE_CPP__
#define __ABSTRACT_SYNTAX_TREE_CPP__ #define __ABSTRACT_SYNTAX_TREE_CPP__
#include "abstract_syntax_tree.h"
int exit_type=0; int exit_type=0;
bool abstract_syntax_tree::check() bool abstract_syntax_tree::check()
@ -12,15 +12,17 @@ bool abstract_syntax_tree::check()
var abstract_syntax_tree::call_id() var abstract_syntax_tree::call_id()
{ {
var temp; var temp;
if(children.empty()) if(scope.search_var(name))
temp=scope.get_var(name);
else
{ {
if(scope.search_var(name)) std::cout<<">>[Runtime-error] cannot find a var named \'"<<name<<"\'."<<std::endl;
temp=scope.get_var(name); exit_type=__find_var_failure;
else return temp;
{ }
std::cout<<">>[Runtime-error] cannot find a var named \'"<<name<<"\'."<<std::endl; if(!children.empty())
exit_type=__find_var_failure; {
} ;
} }
return temp; return temp;
} }
@ -113,7 +115,7 @@ void abstract_syntax_tree::run_root()
break; break;
} }
end_time=time(NULL); end_time=time(NULL);
std::cout<<"-------------------------------------------------------------------------------------------"<<std::endl; std::cout<<"--------------------------------------------------------------------------------------"<<std::endl;
std::cout<<">>[Runtime] process exited after "<<end_time-beg_time<<" sec(s) with returned state \'"; std::cout<<">>[Runtime] process exited after "<<end_time-beg_time<<" sec(s) with returned state \'";
print_exit_type(exit_type); print_exit_type(exit_type);
std::cout<<"\'."<<std::endl; std::cout<<"\'."<<std::endl;
@ -128,7 +130,7 @@ void abstract_syntax_tree::run_loop()
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(__loop); blk.run_block();
scope.pop_last_local_scope(); scope.pop_last_local_scope();
return; return;
} }
@ -150,12 +152,68 @@ void abstract_syntax_tree::run_func()
return; return;
} }
void abstract_syntax_tree::run_block(int block_type) int abstract_syntax_tree::run_block()
{ {
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->type==__definition)
{
var new_var;
std::list<abstract_syntax_tree>::iterator j=i->children.begin();
std::string _name=j->name;
if(!scope.search_var(_name))
{
++j;
if(j!=i->children.end())
new_var=j->get_value();
new_var.set_name(_name);
scope.add_new_var(new_var);
}
else
{
std::cout<<">>[Runtime-error] redeclaration of \'"<<_name<<"\'."<<std::endl;
exit_type=__redeclaration;
break;
}
}
else if(i->type==__number)
std::cout<<i->number<<std::endl;
else if(i->type==__string)
std::cout<<i->str<<std::endl;
else if(i->type==__id)
std::cout<<i->call_id().get_name()<<std::endl;
else if(i->type==__while)
i->run_loop();
else if(i->type==__ifelse)
i->run_ifelse();
else if(i->type==__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)
{
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)
{
break;
}
if(exit_type!=__process_exited_successfully)
break;
}
scope.pop_last_local_scope(); scope.pop_last_local_scope();
return; return 0;
} }

View File

@ -97,10 +97,9 @@ class abstract_syntax_tree
} }
if((int)_str.length()>2 && (_str[1]=='x' || _str[1]=='o')) if((int)_str.length()>2 && (_str[1]=='x' || _str[1]=='o'))
{ {
double num=0;
double pw=1;
if(_str[1]=='x') if(_str[1]=='x')
{
double num=0;
double pw=1;
for(int i=(int)_str.length()-1;i>1;--i) for(int i=(int)_str.length()-1;i>1;--i)
{ {
if('0'<=_str[i] && _str[i]<='9') if('0'<=_str[i] && _str[i]<='9')
@ -111,19 +110,13 @@ class abstract_syntax_tree
num+=(10+_str[i]-'A')*pw; num+=(10+_str[i]-'A')*pw;
pw*=16; pw*=16;
} }
number=num;
}
else else
{
double num=0;
double pw=1;
for(int i=(int)_str.length()-1;i>1;--i) for(int i=(int)_str.length()-1;i>1;--i)
{ {
num+=(_str[i]-'0')*pw; num+=(_str[i]-'0')*pw;
pw*=8; pw*=8;
} }
number=num; number=num;
}
return; return;
} }
int dot_place=-1; int dot_place=-1;
@ -198,7 +191,7 @@ class abstract_syntax_tree
void run_loop(); void run_loop();
void run_ifelse(); void run_ifelse();
void run_func(); void run_func();
void run_block(int); int run_block();
}; };
#endif #endif

View File

@ -162,7 +162,10 @@ abstract_syntax_tree balloon_parse::choose()
temp.set_clear(); temp.set_clear();
get_token(); get_token();
if(this_token.type!=__elsif && this_token.type!=__else) if(this_token.type!=__elsif && this_token.type!=__else)
{
parse.push(this_token);
break; break;
}
if(this_token.type==__else) if(this_token.type==__else)
{ {
get_token(); get_token();
@ -478,7 +481,7 @@ abstract_syntax_tree balloon_parse::block()
std::cout<<">>[Parse-error] line "<<this_token.line<<": expect a \'{\' ."<<std::endl; std::cout<<">>[Parse-error] line "<<this_token.line<<": expect a \'{\' ."<<std::endl;
return new_node; return new_node;
} }
while(this_token.type!=__right_brace) while(1)
{ {
get_token(); get_token();
switch(this_token.type) switch(this_token.type)
@ -501,12 +504,14 @@ 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:break; case __right_brace:return new_node;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<<": \'";
print_detail_token(this_token.type); print_detail_token(this_token.type);
std::cout<<"\' should not appear in this scope."<<std::endl; std::cout<<"\' should not appear in this scope."<<std::endl;
return new_node;
break; break;
} }
} }

View File

@ -143,7 +143,9 @@ enum runtime_error_type
__process_exited_successfully, __process_exited_successfully,
__redeclaration, __redeclaration,
__get_value_failure, __get_value_failure,
__find_var_failure __find_var_failure,
__error_value_type,
__error_command_use
}; };
void print_exit_type(int type) void print_exit_type(int type)
@ -155,6 +157,8 @@ void print_exit_type(int type)
case __redeclaration: context="redeclaration";break; case __redeclaration: context="redeclaration";break;
case __get_value_failure: context="get_value_failure";break; case __get_value_failure: context="get_value_failure";break;
case __find_var_failure: context="find_var_failure";break; case __find_var_failure: context="find_var_failure";break;
case __error_value_type: context="value_type_error";break;
case __error_command_use: context="command_use_error(continue/break/return)";break;
default: context="unknown";break; default: context="unknown";break;
} }
std::cout<<context; std::cout<<context;