This commit is contained in:
Valk Richard Li
2019-11-12 23:23:42 +08:00
committed by GitHub
parent a61415fbed
commit 6aef19ce7a
7 changed files with 469 additions and 37 deletions

View File

@@ -1,63 +1,398 @@
#ifndef __ABSTRACT_SYNTAX_TREE_CPP__
#define __ABSTRACT_SYNTAX_TREE_CPP__
#include "abstract_syntax_tree.h"
int exit_type=0;
var abstract_syntax_tree::condition()
int exit_type=0;
std::stack<var> ret_stack;// for function ret use
var abstract_syntax_tree::calculation()
{
var cond;
cond.set_number(0);
if(this->type==__cmp_equal)
var temp;
temp.set_type(__null_type);
if(this->type==__number)
{
temp.set_type(__var_number);
temp.set_number(number);
return temp;
}
else if(this->type==__string)
{
temp.set_type(__var_string);
temp.set_string(str);
return temp;
}
else if(this->type==__id)
{
temp=this->call_identifier();
return temp;
}
if(this->type==__nor_operator)
{
temp.set_type(__var_number);
temp=children.front().calculation();
if(temp.get_type()!=__var_number)
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use a number to use \'!\' but use \'";
print_scalar(temp.get_type());
std::cout<<"\'."<<std::endl;
}
else
{
if(temp.get_number())
temp.set_number(0);
else
temp.set_number(1);
}
return temp;
}
else if(this->type==__add_operator)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==__var_number && right_child.get_type()==__var_number)
temp.set_number(left_child.get_number()+right_child.get_number());
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use a number to use \'+\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__sub_operator)
{
temp.set_type(__var_number);
std::list<abstract_syntax_tree>::iterator i=children.begin();
++i;
if(i==children.end())
{
temp=children.front().calculation();
if(temp.get_type()==__var_number)
temp.set_number(temp.get_number()*(-1));
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use a number to use \'-\' but use \'";
print_scalar(temp.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==__var_number && right_child.get_type()==__var_number)
temp.set_number(left_child.get_number()-right_child.get_number());
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use a number to use \'-\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
}
else if(this->type==__mul_operator)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==__var_number && right_child.get_type()==__var_number)
temp.set_number(left_child.get_number()*right_child.get_number());
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use a number to use \'*\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__div_operator)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==__var_number && right_child.get_type()==__var_number)
{
temp.set_number(left_child.get_number()/right_child.get_number());
if(std::isnan(temp.get_number()) || std::isinf(temp.get_number()))
{
exit_type=__sigfpe_arithmetic_exception;
std::cout<<">>[Runtime-error] get number \'NaN\' or \'Inf\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use a number to use \'/\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__link_operator)
{
temp.set_type(__var_string);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==__var_string && right_child.get_type()==__var_string)
temp.set_string(left_child.get_string()+right_child.get_string());
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use a string to use \'~\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__cmp_equal)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==right_child.get_type())
{
if(left_child.get_type()==__var_number)
temp.set_number((double)(left_child.get_number()==right_child.get_number()));
else if(left_child.get_type()==__var_string)
temp.set_number((double)(left_child.get_string()==right_child.get_string()));
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use number or string to use \'==\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'==\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__cmp_not_equal)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==right_child.get_type())
{
if(left_child.get_type()==__var_number)
temp.set_number((double)(left_child.get_number()!=right_child.get_number()));
else if(left_child.get_type()==__var_string)
temp.set_number((double)(left_child.get_string()!=right_child.get_string()));
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use number or string to use \'!=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'!=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__cmp_less)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==right_child.get_type())
{
if(left_child.get_type()==__var_number)
temp.set_number((double)(left_child.get_number()<right_child.get_number()));
else if(left_child.get_type()==__var_string)
temp.set_number((double)(left_child.get_string()<right_child.get_string()));
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use number or string to use \'<\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'<\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__cmp_more)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==right_child.get_type())
{
if(left_child.get_type()==__var_number)
temp.set_number((double)(left_child.get_number()>right_child.get_number()));
else if(left_child.get_type()==__var_string)
temp.set_number((double)(left_child.get_string()>right_child.get_string()));
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use number or string to use \'>\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'>\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__cmp_less_or_equal)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==right_child.get_type())
{
if(left_child.get_type()==__var_number)
temp.set_number((double)(left_child.get_number()<=right_child.get_number()));
else if(left_child.get_type()==__var_string)
temp.set_number((double)(left_child.get_string()<=right_child.get_string()));
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use number or string to use \'<=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'<=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__cmp_more_or_equal)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==right_child.get_type())
{
if(left_child.get_type()==__var_number)
temp.set_number((double)(left_child.get_number()>=right_child.get_number()));
else if(left_child.get_type()==__var_string)
temp.set_number((double)(left_child.get_string()>=right_child.get_string()));
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use number or string to use \'>=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'>=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__or_operator)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==__var_number && right_child.get_type()==__var_number)
temp.set_number((double)(left_child.get_number() || right_child.get_number()));
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'or\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__and_operator)
{
temp.set_type(__var_number);
var left_child=children.front().calculation();
var right_child=children.back().calculation();
if(left_child.get_type()==__var_number && right_child.get_type()==__var_number)
temp.set_number((double)(left_child.get_number() && right_child.get_number()));
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'and\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] error type occurred when doing calculation."<<std::endl;
}
return cond;
return temp;
}
bool abstract_syntax_tree::check()
bool abstract_syntax_tree::condition_check()
{
bool ret=false;
var cond=condition();
if(cond.get_number()==0)
ret=false;
var temp=calculation();
if(temp.get_type()==__var_number)
{
if(temp.get_number())
ret=true;
}
else
ret=true;
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use a number to make a choice but use \'";
print_scalar(temp.get_type());
std::cout<<"\'."<<std::endl;
}
return ret;
}
var abstract_syntax_tree::call_id()
var abstract_syntax_tree::call_identifier()
{
var temp;
if(scope.search_var(name))
@@ -70,7 +405,10 @@ var abstract_syntax_tree::call_id()
}
if(!children.empty())
{
;
for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
{
}
}
return temp;
}
@@ -80,7 +418,7 @@ var abstract_syntax_tree::get_value()
var temp;
temp.set_type(type);
if(type==__id)
temp=call_id();
temp=call_identifier();
else if(type==__number)
temp.set_number(number);
else if(type==__string)
@@ -115,6 +453,7 @@ var abstract_syntax_tree::get_value()
}
void abstract_syntax_tree::run_root()
{
while(!ret_stack.empty())ret_stack.pop();
scope.set_clear();
int beg_time,end_time;
exit_type=__process_exited_successfully;
@@ -141,12 +480,24 @@ void abstract_syntax_tree::run_root()
break;
}
}
else if(i->type==__equal || i->type==__add_equal || i->type==__sub_equal || i->type==__mul_equal || i->type==__div_equal || i->type==__link_equal)
{
;
}
else if(i->type==__add_operator || i->type==__sub_operator || i->type==__mul_operator || i->type==__div_operator || i->type==__link_operator || i->type==__or_operator || i->type==__and_operator || i->type==__nor_operator)
{
var t=i->calculation();
if(t.get_type()==__var_number)
std::cout<<t.get_number()<<std::endl;
else if(t.get_type()==__var_string)
std::cout<<t.get_string()<<std::endl;
}
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;
std::cout<<i->call_identifier().get_name()<<std::endl;
else if(i->type==__while)
{
scope.add_new_block_scope();
@@ -178,7 +529,7 @@ int abstract_syntax_tree::run_loop()
abstract_syntax_tree condition=children.front();
abstract_syntax_tree blk=children.back();
while(condition.check())
while(condition.condition_check())
{
int type=blk.run_block();
if(type==__break)
@@ -199,7 +550,12 @@ int abstract_syntax_tree::run_ifelse()
scope.add_new_local_scope();
for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
{
if(i->children.front().check())
if(i->type!=__else && i->children.front().condition_check())
{
ret=i->children.back().run_block();
break;
}
else
{
ret=i->children.back().run_block();
break;
@@ -209,13 +565,17 @@ int abstract_syntax_tree::run_ifelse()
return ret;
}
void abstract_syntax_tree::run_func()
var abstract_syntax_tree::run_func()
{
var ret;
scope.add_new_block_scope();
scope.add_new_local_scope();
//get return
scope.pop_last_block_scope();
return;
ret=ret_stack.top();
ret_stack.pop();
return ret;
}
int abstract_syntax_tree::run_block()
@@ -248,7 +608,19 @@ int abstract_syntax_tree::run_block()
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;
std::cout<<i->call_identifier().get_name()<<std::endl;
else if(i->type==__equal || i->type==__add_equal || i->type==__sub_equal || i->type==__mul_equal || i->type==__div_equal || i->type==__link_equal)
{
;
}
else if(i->type==__add_operator || i->type==__sub_operator || i->type==__mul_operator || i->type==__div_operator || i->type==__link_operator || i->type==__or_operator || i->type==__and_operator || i->type==__nor_operator)
{
var t=i->calculation();
if(t.get_type()==__var_number)
std::cout<<t.get_number()<<std::endl;
else if(t.get_type()==__var_string)
std::cout<<t.get_string()<<std::endl;
}
else if(i->type==__while)
{
int type=i->run_loop();
@@ -274,7 +646,16 @@ int abstract_syntax_tree::run_block()
else if(i->type==__break)
return __break;
else if(i->type==__return)
{
var temp;
temp.set_type(__null_type);
if(!(i->children.empty()))
{
}
ret_stack.push(temp);
return __return;
}
if(exit_type!=__process_exited_successfully)
break;
}