update
This commit is contained in:
parent
a61415fbed
commit
6aef19ce7a
|
@ -1,63 +1,398 @@
|
||||||
#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"
|
#include "abstract_syntax_tree.h"
|
||||||
|
|
||||||
|
|
||||||
int exit_type=0;
|
int exit_type=0;
|
||||||
|
std::stack<var> ret_stack;// for function ret use
|
||||||
|
|
||||||
var abstract_syntax_tree::condition()
|
var abstract_syntax_tree::calculation()
|
||||||
{
|
{
|
||||||
var cond;
|
var temp;
|
||||||
cond.set_number(0);
|
temp.set_type(__null_type);
|
||||||
if(this->type==__cmp_equal)
|
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)
|
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)
|
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)
|
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)
|
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)
|
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)
|
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)
|
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
|
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;
|
bool ret=false;
|
||||||
var cond=condition();
|
var temp=calculation();
|
||||||
if(cond.get_number()==0)
|
if(temp.get_type()==__var_number)
|
||||||
ret=false;
|
{
|
||||||
|
if(temp.get_number())
|
||||||
|
ret=true;
|
||||||
|
}
|
||||||
else
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
var abstract_syntax_tree::call_id()
|
var abstract_syntax_tree::call_identifier()
|
||||||
{
|
{
|
||||||
var temp;
|
var temp;
|
||||||
if(scope.search_var(name))
|
if(scope.search_var(name))
|
||||||
|
@ -70,7 +405,10 @@ var abstract_syntax_tree::call_id()
|
||||||
}
|
}
|
||||||
if(!children.empty())
|
if(!children.empty())
|
||||||
{
|
{
|
||||||
;
|
for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
@ -80,7 +418,7 @@ var abstract_syntax_tree::get_value()
|
||||||
var temp;
|
var temp;
|
||||||
temp.set_type(type);
|
temp.set_type(type);
|
||||||
if(type==__id)
|
if(type==__id)
|
||||||
temp=call_id();
|
temp=call_identifier();
|
||||||
else if(type==__number)
|
else if(type==__number)
|
||||||
temp.set_number(number);
|
temp.set_number(number);
|
||||||
else if(type==__string)
|
else if(type==__string)
|
||||||
|
@ -115,6 +453,7 @@ var abstract_syntax_tree::get_value()
|
||||||
}
|
}
|
||||||
void abstract_syntax_tree::run_root()
|
void abstract_syntax_tree::run_root()
|
||||||
{
|
{
|
||||||
|
while(!ret_stack.empty())ret_stack.pop();
|
||||||
scope.set_clear();
|
scope.set_clear();
|
||||||
int beg_time,end_time;
|
int beg_time,end_time;
|
||||||
exit_type=__process_exited_successfully;
|
exit_type=__process_exited_successfully;
|
||||||
|
@ -141,12 +480,24 @@ void abstract_syntax_tree::run_root()
|
||||||
break;
|
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)
|
else if(i->type==__number)
|
||||||
std::cout<<i->number<<std::endl;
|
std::cout<<i->number<<std::endl;
|
||||||
else if(i->type==__string)
|
else if(i->type==__string)
|
||||||
std::cout<<i->str<<std::endl;
|
std::cout<<i->str<<std::endl;
|
||||||
else if(i->type==__id)
|
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)
|
else if(i->type==__while)
|
||||||
{
|
{
|
||||||
scope.add_new_block_scope();
|
scope.add_new_block_scope();
|
||||||
|
@ -178,7 +529,7 @@ int 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.condition_check())
|
||||||
{
|
{
|
||||||
int type=blk.run_block();
|
int type=blk.run_block();
|
||||||
if(type==__break)
|
if(type==__break)
|
||||||
|
@ -199,7 +550,12 @@ int abstract_syntax_tree::run_ifelse()
|
||||||
scope.add_new_local_scope();
|
scope.add_new_local_scope();
|
||||||
for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
|
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();
|
ret=i->children.back().run_block();
|
||||||
break;
|
break;
|
||||||
|
@ -209,13 +565,17 @@ int abstract_syntax_tree::run_ifelse()
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void abstract_syntax_tree::run_func()
|
var abstract_syntax_tree::run_func()
|
||||||
{
|
{
|
||||||
|
var ret;
|
||||||
scope.add_new_block_scope();
|
scope.add_new_block_scope();
|
||||||
scope.add_new_local_scope();
|
scope.add_new_local_scope();
|
||||||
|
|
||||||
|
//get return
|
||||||
scope.pop_last_block_scope();
|
scope.pop_last_block_scope();
|
||||||
return;
|
ret=ret_stack.top();
|
||||||
|
ret_stack.pop();
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int abstract_syntax_tree::run_block()
|
int abstract_syntax_tree::run_block()
|
||||||
|
@ -248,7 +608,19 @@ int abstract_syntax_tree::run_block()
|
||||||
else if(i->type==__string)
|
else if(i->type==__string)
|
||||||
std::cout<<i->str<<std::endl;
|
std::cout<<i->str<<std::endl;
|
||||||
else if(i->type==__id)
|
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)
|
else if(i->type==__while)
|
||||||
{
|
{
|
||||||
int type=i->run_loop();
|
int type=i->run_loop();
|
||||||
|
@ -274,7 +646,16 @@ int abstract_syntax_tree::run_block()
|
||||||
else if(i->type==__break)
|
else if(i->type==__break)
|
||||||
return __break;
|
return __break;
|
||||||
else if(i->type==__return)
|
else if(i->type==__return)
|
||||||
|
{
|
||||||
|
var temp;
|
||||||
|
temp.set_type(__null_type);
|
||||||
|
if(!(i->children.empty()))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
ret_stack.push(temp);
|
||||||
return __return;
|
return __return;
|
||||||
|
}
|
||||||
if(exit_type!=__process_exited_successfully)
|
if(exit_type!=__process_exited_successfully)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -184,14 +184,14 @@ class abstract_syntax_tree
|
||||||
{
|
{
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
var condition();
|
var calculation();
|
||||||
bool check();
|
bool condition_check();
|
||||||
var call_id();
|
var call_identifier();
|
||||||
var get_value();
|
var get_value();
|
||||||
void run_root();
|
void run_root();
|
||||||
int run_loop();
|
int run_loop();
|
||||||
int run_ifelse();
|
int run_ifelse();
|
||||||
void run_func();
|
var run_func();
|
||||||
int run_block();
|
int run_block();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,10 @@
|
||||||
#define __BALLOON_H__
|
#define __BALLOON_H__
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <cmath>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
|
@ -654,7 +654,7 @@ abstract_syntax_tree balloon_parse::call_identifier()
|
||||||
parse.push(this_token);
|
parse.push(this_token);
|
||||||
temp=new_node;
|
temp=new_node;
|
||||||
new_node=assignment();
|
new_node=assignment();
|
||||||
new_node.add_child(temp);
|
new_node.get_children().push_front(temp);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
parse.push(this_token);
|
parse.push(this_token);
|
||||||
|
|
|
@ -145,7 +145,9 @@ enum runtime_error_type
|
||||||
__get_value_failure,
|
__get_value_failure,
|
||||||
__find_var_failure,
|
__find_var_failure,
|
||||||
__error_value_type,
|
__error_value_type,
|
||||||
__error_command_use
|
__error_command_use,
|
||||||
|
__sigfpe_arithmetic_exception,
|
||||||
|
__sigsegv_segmentation_error
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_exit_type(int type)
|
void print_exit_type(int type)
|
||||||
|
@ -159,6 +161,8 @@ void print_exit_type(int type)
|
||||||
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_value_type: context="value_type_error";break;
|
||||||
case __error_command_use: context="command_use_error(continue/break/return)";break;
|
case __error_command_use: context="command_use_error(continue/break/return)";break;
|
||||||
|
case __sigfpe_arithmetic_exception: context="SIGFPE";break;
|
||||||
|
case __sigsegv_segmentation_error: context="SIGSEGV";break;
|
||||||
default: context="unknown";break;
|
default: context="unknown";break;
|
||||||
}
|
}
|
||||||
std::cout<<context;
|
std::cout<<context;
|
||||||
|
|
|
@ -7,9 +7,27 @@ enum var_type
|
||||||
__var_number,
|
__var_number,
|
||||||
__var_string,
|
__var_string,
|
||||||
__var_array,
|
__var_array,
|
||||||
__var_hash
|
__var_hash,
|
||||||
|
__var_function
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void print_scalar(int type)
|
||||||
|
{
|
||||||
|
std::string str="";
|
||||||
|
switch(type)
|
||||||
|
{
|
||||||
|
case __null_type: str="null";break;
|
||||||
|
case __var_number: str="number";break;
|
||||||
|
case __var_string: str="string";break;
|
||||||
|
case __var_array: str="array";break;
|
||||||
|
case __var_hash: str="hash";break;
|
||||||
|
case __var_function:str="function";break;
|
||||||
|
default: str="unknown";break;
|
||||||
|
}
|
||||||
|
std::cout<<str;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
class var
|
class var
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
@ -28,6 +46,7 @@ class var
|
||||||
name="";
|
name="";
|
||||||
balloon_array.clear();
|
balloon_array.clear();
|
||||||
balloon_hash.clear();
|
balloon_hash.clear();
|
||||||
|
function.set_clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var(const var& p)
|
var(const var& p)
|
||||||
|
@ -38,6 +57,7 @@ class var
|
||||||
name=p.name;
|
name=p.name;
|
||||||
balloon_array=p.balloon_array;
|
balloon_array=p.balloon_array;
|
||||||
balloon_hash=p.balloon_hash;
|
balloon_hash=p.balloon_hash;
|
||||||
|
function=p.function;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var& operator=(const var& p)
|
var& operator=(const var& p)
|
||||||
|
@ -48,8 +68,10 @@ class var
|
||||||
name=p.name;
|
name=p.name;
|
||||||
balloon_array.clear();
|
balloon_array.clear();
|
||||||
balloon_hash.clear();
|
balloon_hash.clear();
|
||||||
|
function.set_clear();
|
||||||
balloon_array=p.balloon_array;
|
balloon_array=p.balloon_array;
|
||||||
balloon_hash=p.balloon_hash;
|
balloon_hash=p.balloon_hash;
|
||||||
|
function=p.function;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
void set_type(int);
|
void set_type(int);
|
||||||
|
@ -59,6 +81,7 @@ class var
|
||||||
void set_function(abstract_syntax_tree&);
|
void set_function(abstract_syntax_tree&);
|
||||||
void append_array(var);
|
void append_array(var);
|
||||||
void append_hash(var);
|
void append_hash(var);
|
||||||
|
int get_type();
|
||||||
std::string get_name();
|
std::string get_name();
|
||||||
double get_number();
|
double get_number();
|
||||||
std::string get_string();
|
std::string get_string();
|
||||||
|
@ -74,48 +97,63 @@ void var::set_type(int _type)
|
||||||
type=_type;
|
type=_type;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void var::set_name(std::string _name)
|
void var::set_name(std::string _name)
|
||||||
{
|
{
|
||||||
name=_name;
|
name=_name;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void var::set_number(double _num)
|
void var::set_number(double _num)
|
||||||
{
|
{
|
||||||
number=_num;
|
number=_num;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void var::set_string(std::string _str)
|
void var::set_string(std::string _str)
|
||||||
{
|
{
|
||||||
str=_str;
|
str=_str;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void var::set_function(abstract_syntax_tree& p)
|
void var::set_function(abstract_syntax_tree& p)
|
||||||
{
|
{
|
||||||
function=p;
|
function=p;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void var::append_array(var _new_var)
|
void var::append_array(var _new_var)
|
||||||
{
|
{
|
||||||
balloon_array.push_back(_new_var);
|
balloon_array.push_back(_new_var);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void var::append_hash(var _new_var)
|
void var::append_hash(var _new_var)
|
||||||
{
|
{
|
||||||
balloon_hash.push_back(_new_var);
|
balloon_hash.push_back(_new_var);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int var::get_type()
|
||||||
|
{
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
std::string var::get_name()
|
std::string var::get_name()
|
||||||
{
|
{
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
double var::get_number()
|
double var::get_number()
|
||||||
{
|
{
|
||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string var::get_string()
|
std::string var::get_string()
|
||||||
{
|
{
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
var& var::get_array_member(int _place)
|
var& var::get_array_member(int _place)
|
||||||
{
|
{
|
||||||
if(balloon_array.empty())
|
if(balloon_array.empty())
|
||||||
|
@ -130,6 +168,7 @@ var& var::get_array_member(int _place)
|
||||||
std::cout<<">>[Runtime-error] overflow when searching member "<<_place<<" but the max size of array \'"<<name<<"\' is "<<cnt<<"(this means 0-"<<cnt-1<<")."<<std::endl;
|
std::cout<<">>[Runtime-error] overflow when searching member "<<_place<<" but the max size of array \'"<<name<<"\' is "<<cnt<<"(this means 0-"<<cnt-1<<")."<<std::endl;
|
||||||
return error_var;
|
return error_var;
|
||||||
}
|
}
|
||||||
|
|
||||||
var& var::get_hash_member(std::string _name)
|
var& var::get_hash_member(std::string _name)
|
||||||
{
|
{
|
||||||
for(std::list<var>::iterator i=balloon_hash.begin();i!=balloon_hash.end();++i)
|
for(std::list<var>::iterator i=balloon_hash.begin();i!=balloon_hash.end();++i)
|
||||||
|
@ -138,6 +177,7 @@ var& var::get_hash_member(std::string _name)
|
||||||
std::cout<<">>[Runtime-error] hash \'"<<name<<"\' does not have a member named \'"<<_name<<"\'"<<std::endl;
|
std::cout<<">>[Runtime-error] hash \'"<<name<<"\' does not have a member named \'"<<_name<<"\'"<<std::endl;
|
||||||
return error_var;
|
return error_var;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract_syntax_tree& var::get_function()
|
abstract_syntax_tree& var::get_function()
|
||||||
{
|
{
|
||||||
return function;
|
return function;
|
||||||
|
|
|
@ -17,3 +17,8 @@ var i=func(v1,v2,v3){return [0,0,0];};
|
||||||
1073741824;
|
1073741824;
|
||||||
"str";
|
"str";
|
||||||
a;
|
a;
|
||||||
|
var m=[0,[0,1]];
|
||||||
|
1+1;
|
||||||
|
"hello "~"world!";
|
||||||
|
#10/0;
|
||||||
|
1+"h";
|
Loading…
Reference in New Issue