update&bugs fixed

This commit is contained in:
Valk Richard Li 2019-11-16 00:34:30 +08:00 committed by GitHub
parent d695b0e9ca
commit b4d7805f06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 136 additions and 11 deletions

View File

@ -5,27 +5,37 @@
int exit_type=0; int exit_type=0;
std::stack<var> ret_stack; // for function ret use std::stack<var> ret_stack; // for function ret use
int recursion_depth=0; // avoid deep recursion to sigsegv
var abstract_syntax_tree::calculation() var abstract_syntax_tree::calculation()
{ {
++recursion_depth;
if(recursion_depth>512)
{
exit_type=__sigsegv_segmentation_error;
std::cout<<">>[Runtime-error] line "<<line<<":[SIGSEGV] too deep recursion(calc)."<<std::endl;
return error_var;
}
var temp; var temp;
temp.set_type(__null_type); temp.set_type(__null_type);
if(this->type==__number) if(this->type==__number)
{ {
temp.set_type(__var_number); temp.set_type(__var_number);
temp.set_number(number); temp.set_number(number);
--recursion_depth;
return temp; return temp;
} }
else if(this->type==__string) else if(this->type==__string)
{ {
temp.set_type(__var_string); temp.set_type(__var_string);
temp.set_string(str); temp.set_string(str);
--recursion_depth;
return temp; return temp;
} }
else if(this->type==__id) else if(this->type==__id)
{ {
temp=this->call_identifier(); temp=this->call_identifier();
--recursion_depth;
return temp; return temp;
} }
else if(this->type==__array) else if(this->type==__array)
@ -34,6 +44,7 @@ var abstract_syntax_tree::calculation()
if(!children.empty()) if(!children.empty())
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)
temp.append_array(i->calculation()); temp.append_array(i->calculation());
--recursion_depth;
return temp; return temp;
} }
else if(this->type==__hash) else if(this->type==__hash)
@ -47,12 +58,14 @@ var abstract_syntax_tree::calculation()
t.set_name(i->name); t.set_name(i->name);
temp.append_hash(t); temp.append_hash(t);
} }
--recursion_depth;
return temp; return temp;
} }
else if(this->type==__function) else if(this->type==__function)
{ {
temp.set_type(__var_function); temp.set_type(__var_function);
temp.set_function(*this); temp.set_function(*this);
--recursion_depth;
return temp; return temp;
} }
if(this->type==__nor_operator) if(this->type==__nor_operator)
@ -73,6 +86,7 @@ var abstract_syntax_tree::calculation()
else else
temp.set_number(1); temp.set_number(1);
} }
--recursion_depth;
return temp; return temp;
} }
else if(this->type==__add_operator) else if(this->type==__add_operator)
@ -400,11 +414,19 @@ var abstract_syntax_tree::calculation()
print_detail_token(this->type); print_detail_token(this->type);
std::cout<<"\' occurred when doing calculation."<<std::endl; std::cout<<"\' occurred when doing calculation."<<std::endl;
} }
--recursion_depth;
return temp; return temp;
} }
bool abstract_syntax_tree::condition_check() bool abstract_syntax_tree::condition_check()
{ {
++recursion_depth;
if(recursion_depth>512)
{
exit_type=__sigsegv_segmentation_error;
std::cout<<">>[Runtime-error] line "<<line<<":[SIGSEGV] too deep recursion(check)."<<std::endl;
return false;
}
bool ret=false; bool ret=false;
var temp=calculation(); var temp=calculation();
if(temp.get_type()==__var_number) if(temp.get_type()==__var_number)
@ -419,11 +441,19 @@ bool abstract_syntax_tree::condition_check()
print_scalar(temp.get_type()); print_scalar(temp.get_type());
std::cout<<"\'."<<std::endl; std::cout<<"\'."<<std::endl;
} }
--recursion_depth;
return ret; return ret;
} }
var abstract_syntax_tree::call_identifier() var abstract_syntax_tree::call_identifier()
{ {
++recursion_depth;
if(recursion_depth>512)
{
exit_type=__sigsegv_segmentation_error;
std::cout<<">>[Runtime-error] line "<<line<<":[SIGSEGV] too deep recursion(call)."<<std::endl;
return error_var;
}
var temp; var temp;
temp.set_type(__null_type); temp.set_type(__null_type);
if(scope.search_var(name)) if(scope.search_var(name))
@ -489,11 +519,19 @@ var abstract_syntax_tree::call_identifier()
} }
} }
} }
--recursion_depth;
return temp; return temp;
} }
var* abstract_syntax_tree::get_var_addr() var* abstract_syntax_tree::get_var_addr()
{ {
++recursion_depth;
if(recursion_depth>512)
{
exit_type=__sigsegv_segmentation_error;
std::cout<<">>[Runtime-error] line "<<line<<":[SIGSEGV] too deep recursion(addr)."<<std::endl;
return &error_var;
}
var* addr=&error_var; var* addr=&error_var;
if(scope.search_var(name)) if(scope.search_var(name))
addr=scope.get_addr(name); addr=scope.get_addr(name);
@ -551,11 +589,19 @@ var* abstract_syntax_tree::get_var_addr()
} }
} }
} }
--recursion_depth;
return addr; return addr;
} }
var abstract_syntax_tree::assignment() var abstract_syntax_tree::assignment()
{ {
++recursion_depth;
if(recursion_depth>512)
{
exit_type=__sigsegv_segmentation_error;
std::cout<<">>[Runtime-error] line "<<line<<":[SIGSEGV] too deep recursion(assignment)."<<std::endl;
return error_var;
}
var ret,temp; var ret,temp;
abstract_syntax_tree id=children.front(); abstract_syntax_tree id=children.front();
abstract_syntax_tree value=children.back(); abstract_syntax_tree value=children.back();
@ -615,11 +661,14 @@ var abstract_syntax_tree::assignment()
} }
ret=*addr; ret=*addr;
--recursion_depth;
return ret; return ret;
} }
void abstract_syntax_tree::run_root() void abstract_syntax_tree::run_root()
{ {
recursion_depth=0;
++recursion_depth;
while(!ret_stack.empty())ret_stack.pop(); while(!ret_stack.empty())ret_stack.pop();
scope.set_clear(); scope.set_clear();
int beg_time,end_time; int beg_time,end_time;
@ -708,6 +757,13 @@ void abstract_syntax_tree::run_root()
int abstract_syntax_tree::run_loop() int abstract_syntax_tree::run_loop()
{ {
++recursion_depth;
if(recursion_depth>512)
{
exit_type=__sigsegv_segmentation_error;
std::cout<<">>[Runtime-error] line "<<line<<":[SIGSEGV] too deep recursion(loop)."<<std::endl;
return 0;
}
int ret=0; int ret=0;
scope.add_new_local_scope(); scope.add_new_local_scope();
@ -727,13 +783,22 @@ int abstract_syntax_tree::run_loop()
break; break;
} }
scope.pop_last_local_scope(); scope.pop_last_local_scope();
--recursion_depth;
return ret; return ret;
} }
int abstract_syntax_tree::run_ifelse() int abstract_syntax_tree::run_ifelse()
{ {
++recursion_depth;
if(recursion_depth>512)
{
exit_type=__sigsegv_segmentation_error;
std::cout<<">>[Runtime-error] line "<<line<<":[SIGSEGV] too deep recursion(choose)."<<std::endl;
return 0;
}
int ret=0; 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) for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
{ {
if((i->type==__if || i->type==__elsif) && i->children.front().condition_check()) if((i->type==__if || i->type==__elsif) && i->children.front().condition_check())
@ -748,17 +813,17 @@ int abstract_syntax_tree::run_ifelse()
} }
} }
scope.pop_last_local_scope(); scope.pop_last_local_scope();
--recursion_depth;
return ret; return ret;
} }
var abstract_syntax_tree::run_func(std::list<var> parameter,var self_func) var abstract_syntax_tree::run_func(std::list<var> parameter,var self_func)
{ {
static int recursion_depth=0;
++recursion_depth; ++recursion_depth;
if(recursion_depth>512) if(recursion_depth>512)
{ {
exit_type=__sigsegv_segmentation_error; exit_type=__sigsegv_segmentation_error;
std::cout<<">>[Runtime-error] line "<<line<<":[SIGSEGV] too deep recursion."<<std::endl; std::cout<<">>[Runtime-error] line "<<line<<":[SIGSEGV] too deep recursion(function)."<<std::endl;
return error_var; return error_var;
} }
@ -807,6 +872,13 @@ var abstract_syntax_tree::run_func(std::list<var> parameter,var self_func)
int abstract_syntax_tree::run_block() int abstract_syntax_tree::run_block()
{ {
++recursion_depth;
if(recursion_depth>512)
{
exit_type=__sigsegv_segmentation_error;
std::cout<<">>[Runtime-error] line "<<line<<":[SIGSEGV] too deep recursion(block)."<<std::endl;
return 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) for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
{ {
@ -862,7 +934,10 @@ int abstract_syntax_tree::run_block()
if(type) if(type)
{ {
if(type==__return) if(type==__return)
{
--recursion_depth;
return type; return type;
}
else else
{ {
std::cout<<"[Runtime-error] line "<<line<<": incorrect use of break/continue."<<std::endl; std::cout<<"[Runtime-error] line "<<line<<": incorrect use of break/continue."<<std::endl;
@ -874,12 +949,21 @@ int abstract_syntax_tree::run_block()
{ {
int type=i->run_ifelse(); int type=i->run_ifelse();
if(type) if(type)
{
--recursion_depth;
return type; return type;
}
} }
else if(i->type==__continue) else if(i->type==__continue)
{
--recursion_depth;
return __continue; return __continue;
}
else if(i->type==__break) else if(i->type==__break)
{
--recursion_depth;
return __break; return __break;
}
else if(i->type==__return) else if(i->type==__return)
{ {
var temp; var temp;
@ -887,12 +971,14 @@ int abstract_syntax_tree::run_block()
if(!(i->children.empty())) if(!(i->children.empty()))
temp=i->children.front().calculation(); temp=i->children.front().calculation();
ret_stack.push(temp); ret_stack.push(temp);
--recursion_depth;
return __return; return __return;
} }
if(exit_type!=__process_exited_successfully) if(exit_type!=__process_exited_successfully)
break; break;
} }
scope.pop_last_local_scope(); scope.pop_last_local_scope();
--recursion_depth;
return 0; return 0;
} }

View File

@ -20,6 +20,8 @@
#include <stack> #include <stack>
#include <ctime> #include <ctime>
#include <unistd.h>
void alert_sound() void alert_sound()
{ {
printf("\a"); printf("\a");
@ -48,5 +50,6 @@ void alert_sound()
/* global varia in abstract_syntax_tree.cpp : /* global varia in abstract_syntax_tree.cpp :
int exit_type; // record the state of runtime int exit_type; // record the state of runtime
std::stack<var> ret_stack; // for function ret use std::stack<var> ret_stack; // for function ret use
int recursion_depth; // avoid too deep recursion
*/ */
#endif #endif

View File

@ -92,7 +92,10 @@ class resource_file
{ {
std::string lib_name; std::string lib_name;
lib_name="lib/math.nas"; lib_name="lib/math.nas";
input_file(lib_name); if(access("lib/math.nas",0))
std::cout<<">>[Resource] lack lib file: lib/math.nas ."<<std::endl;
else
input_file(lib_name);
return; return;
} }
void input_file(std::string filename) void input_file(std::string filename)
@ -115,8 +118,6 @@ class resource_file
resource.push_back(' '); resource.push_back(' ');
} }
resource.push_back('\n'); resource.push_back('\n');
resource.push_back(';');
resource.pudh_back('\n');
return; return;
} }
std::list<char>& get_resource() std::list<char>& get_resource()

View File

@ -1007,6 +1007,7 @@ void balloon_parse::parse_main()
case __foreach: case __foreach:
case __while:parse.push(this_token);root.add_child(loop());break; case __while:parse.push(this_token);root.add_child(loop());break;
case __semi:break; case __semi:break;
case 0:break;// avoid some cases in which the parse has been empty but because of push() the zero token is pushed in.
default: default:
++error; ++error;
std::cout<<">>[Parse-error] line "<<this_token.line<<": \'"; std::cout<<">>[Parse-error] line "<<this_token.line<<": \'";

View File

@ -28,7 +28,7 @@ void print_token(int type)
enum parse_type enum parse_type
{ {
__equal, // = __equal=1, // =
__cmp_equal,__cmp_not_equal, // == != __cmp_equal,__cmp_not_equal, // == !=
__cmp_less,__cmp_less_or_equal, // < <= __cmp_less,__cmp_less_or_equal, // < <=
__cmp_more,__cmp_more_or_equal, // > >= __cmp_more,__cmp_more_or_equal, // > >=
@ -169,7 +169,7 @@ void print_scalar(int type)
enum runtime_error_type enum runtime_error_type
{ {
__process_exited_successfully, __process_exited_successfully=1,
__redeclaration, __redeclaration,
__get_value_failure, __get_value_failure,
__find_var_failure, __find_var_failure,

View File

@ -1,18 +1,21 @@
var pi=3.14159265358979;
var abs=func(__x) var abs=func(__x)
{ {
if(__x>0){return __x;} if(__x>0){return __x;}
else{return -__x;} else{return -__x;}
}; };
var asr=func(__x,__y) var __balloon_lib_ln_asr=func(__x,__y)
{ {
if(abs(__y-__x)<=0.1){return (1/__x+8/(__x+__y)+1/__y)*(__y-__x)/6;} if(abs(__y-__x)<=0.1){return (1/__x+8/(__x+__y)+1/__y)*(__y-__x)/6;}
var __mid=(__x+__y)/2; var __mid=(__x+__y)/2;
return asr(__x,__mid)+asr(__mid,__y); return __balloon_lib_ln_asr(__x,__mid)+__balloon_lib_ln_asr(__mid,__y);
}; };
var ln=func(_x) var ln=func(_x)
{ {
return asr(1,_x); return __balloon_lib_ln_asr(1,_x);
}; };
var log=func(__a,__x) var log=func(__a,__x)
@ -63,3 +66,34 @@ var relu=func(__x)
if(x>=0){return __x;} if(x>=0){return __x;}
else{return 0;} else{return 0;}
}; };
var sin=func(__x)
{
if(__x<0){return -1*sin(-__x);}
var fl=1;
while(__x>2*pi){__x-=2*pi;}
if(__x>pi){__x-=2*pi;}
elsif(__x<-pi){__x+=2*pi;}
if(__x>pi/2){__x-=pi;fl*=-1;}
elsif(__x<-pi/2){__x+=pi;fl*=-1;}
if(__x>pi/4){return cos(pi/2-__x);}
else{return fl*(__x*(1-__x*__x*(1/6+__x*__x*(1/120-__x*__x*(1/5040+__x*__x/362880)))));}
};
var cos=func(__x)
{
__x=abs(__x);
var fl=1;
while(__x>2*pi){__x-=2*pi;}
if(__x>pi){__x-=2*pi;}
elsif(__x<-pi){__x+=2*pi;}
if(__x>pi/2){__x-=pi;fl*=-1;}
elsif(__x<-pi/2){__x+=pi;fl*=-1;}
if(__x>pi/4){return sin(pi/2-__x);}
else{return fl*(1-__x*__x*(1/2+__x*__x*(1/24-__x*__x*(1/720+__x*__x/40320))));}
};
var tan=func(__x)
{
return sin(__x)/cos(__x);
};