fixed bug when meeting }}}}}}}}}}}}
This commit is contained in:
parent
ed2910f708
commit
5134f4eea2
|
@ -1,6 +1,6 @@
|
|||
#ifndef __ABSTRACT_SYNTAX_TREE_CPP__
|
||||
#define __ABSTRACT_SYNTAX_TREE_CPP__
|
||||
|
||||
#include "abstract_syntax_tree.h"
|
||||
int exit_type=0;
|
||||
|
||||
bool abstract_syntax_tree::check()
|
||||
|
@ -12,15 +12,17 @@ bool abstract_syntax_tree::check()
|
|||
var abstract_syntax_tree::call_id()
|
||||
{
|
||||
var temp;
|
||||
if(children.empty())
|
||||
if(scope.search_var(name))
|
||||
temp=scope.get_var(name);
|
||||
else
|
||||
{
|
||||
if(scope.search_var(name))
|
||||
temp=scope.get_var(name);
|
||||
else
|
||||
{
|
||||
std::cout<<">>[Runtime-error] cannot find a var named \'"<<name<<"\'."<<std::endl;
|
||||
exit_type=__find_var_failure;
|
||||
}
|
||||
std::cout<<">>[Runtime-error] cannot find a var named \'"<<name<<"\'."<<std::endl;
|
||||
exit_type=__find_var_failure;
|
||||
return temp;
|
||||
}
|
||||
if(!children.empty())
|
||||
{
|
||||
;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
@ -113,7 +115,7 @@ void abstract_syntax_tree::run_root()
|
|||
break;
|
||||
}
|
||||
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 \'";
|
||||
print_exit_type(exit_type);
|
||||
std::cout<<"\'."<<std::endl;
|
||||
|
@ -128,7 +130,7 @@ void abstract_syntax_tree::run_loop()
|
|||
abstract_syntax_tree condition=children.front();
|
||||
abstract_syntax_tree blk=children.back();
|
||||
while(condition.check())
|
||||
blk.run_block(__loop);
|
||||
blk.run_block();
|
||||
scope.pop_last_local_scope();
|
||||
return;
|
||||
}
|
||||
|
@ -150,12 +152,68 @@ void abstract_syntax_tree::run_func()
|
|||
return;
|
||||
}
|
||||
|
||||
void abstract_syntax_tree::run_block(int block_type)
|
||||
int abstract_syntax_tree::run_block()
|
||||
{
|
||||
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();
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -97,10 +97,9 @@ class abstract_syntax_tree
|
|||
}
|
||||
if((int)_str.length()>2 && (_str[1]=='x' || _str[1]=='o'))
|
||||
{
|
||||
double num=0;
|
||||
double pw=1;
|
||||
if(_str[1]=='x')
|
||||
{
|
||||
double num=0;
|
||||
double pw=1;
|
||||
for(int i=(int)_str.length()-1;i>1;--i)
|
||||
{
|
||||
if('0'<=_str[i] && _str[i]<='9')
|
||||
|
@ -111,19 +110,13 @@ class abstract_syntax_tree
|
|||
num+=(10+_str[i]-'A')*pw;
|
||||
pw*=16;
|
||||
}
|
||||
number=num;
|
||||
}
|
||||
else
|
||||
{
|
||||
double num=0;
|
||||
double pw=1;
|
||||
for(int i=(int)_str.length()-1;i>1;--i)
|
||||
{
|
||||
num+=(_str[i]-'0')*pw;
|
||||
pw*=8;
|
||||
}
|
||||
number=num;
|
||||
}
|
||||
number=num;
|
||||
return;
|
||||
}
|
||||
int dot_place=-1;
|
||||
|
@ -198,7 +191,7 @@ class abstract_syntax_tree
|
|||
void run_loop();
|
||||
void run_ifelse();
|
||||
void run_func();
|
||||
void run_block(int);
|
||||
int run_block();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -162,7 +162,10 @@ abstract_syntax_tree balloon_parse::choose()
|
|||
temp.set_clear();
|
||||
get_token();
|
||||
if(this_token.type!=__elsif && this_token.type!=__else)
|
||||
{
|
||||
parse.push(this_token);
|
||||
break;
|
||||
}
|
||||
if(this_token.type==__else)
|
||||
{
|
||||
get_token();
|
||||
|
@ -478,7 +481,7 @@ abstract_syntax_tree balloon_parse::block()
|
|||
std::cout<<">>[Parse-error] line "<<this_token.line<<": expect a \'{\' ."<<std::endl;
|
||||
return new_node;
|
||||
}
|
||||
while(this_token.type!=__right_brace)
|
||||
while(1)
|
||||
{
|
||||
get_token();
|
||||
switch(this_token.type)
|
||||
|
@ -501,12 +504,14 @@ abstract_syntax_tree balloon_parse::block()
|
|||
case __continue:
|
||||
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 __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:
|
||||
++error;
|
||||
std::cout<<">>[Parse-error] line "<<this_token.line<<": \'";
|
||||
print_detail_token(this_token.type);
|
||||
std::cout<<"\' should not appear in this scope."<<std::endl;
|
||||
return new_node;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -143,7 +143,9 @@ enum runtime_error_type
|
|||
__process_exited_successfully,
|
||||
__redeclaration,
|
||||
__get_value_failure,
|
||||
__find_var_failure
|
||||
__find_var_failure,
|
||||
__error_value_type,
|
||||
__error_command_use
|
||||
};
|
||||
|
||||
void print_exit_type(int type)
|
||||
|
@ -155,6 +157,8 @@ void print_exit_type(int type)
|
|||
case __redeclaration: context="redeclaration";break;
|
||||
case __get_value_failure: context="get_value_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;
|
||||
}
|
||||
std::cout<<context;
|
||||
|
|
Loading…
Reference in New Issue