From b4d7805f066ce102a35b05f968383640997fdc4c Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Sat, 16 Nov 2019 00:34:30 +0800 Subject: [PATCH] update&bugs fixed --- balloon/abstract_syntax_tree.cpp | 92 ++++++++++++++++++++++++++++++-- balloon/balloon.h | 3 ++ balloon/balloon_lexer.h | 7 +-- balloon/balloon_parse.h | 1 + balloon/balloon_type.h | 4 +- balloon/lib/math.nas | 40 ++++++++++++-- 6 files changed, 136 insertions(+), 11 deletions(-) diff --git a/balloon/abstract_syntax_tree.cpp b/balloon/abstract_syntax_tree.cpp index 019a1e9..6b360be 100644 --- a/balloon/abstract_syntax_tree.cpp +++ b/balloon/abstract_syntax_tree.cpp @@ -5,27 +5,37 @@ int exit_type=0; std::stack ret_stack; // for function ret use - +int recursion_depth=0; // avoid deep recursion to sigsegv var abstract_syntax_tree::calculation() { + ++recursion_depth; + if(recursion_depth>512) + { + exit_type=__sigsegv_segmentation_error; + std::cout<<">>[Runtime-error] line "<type==__number) { temp.set_type(__var_number); temp.set_number(number); + --recursion_depth; return temp; } else if(this->type==__string) { temp.set_type(__var_string); temp.set_string(str); + --recursion_depth; return temp; } else if(this->type==__id) { temp=this->call_identifier(); + --recursion_depth; return temp; } else if(this->type==__array) @@ -34,6 +44,7 @@ var abstract_syntax_tree::calculation() if(!children.empty()) for(std::list::iterator i=children.begin();i!=children.end();++i) temp.append_array(i->calculation()); + --recursion_depth; return temp; } else if(this->type==__hash) @@ -47,12 +58,14 @@ var abstract_syntax_tree::calculation() t.set_name(i->name); temp.append_hash(t); } + --recursion_depth; return temp; } else if(this->type==__function) { temp.set_type(__var_function); temp.set_function(*this); + --recursion_depth; return temp; } if(this->type==__nor_operator) @@ -73,6 +86,7 @@ var abstract_syntax_tree::calculation() else temp.set_number(1); } + --recursion_depth; return temp; } else if(this->type==__add_operator) @@ -400,11 +414,19 @@ var abstract_syntax_tree::calculation() print_detail_token(this->type); std::cout<<"\' occurred when doing calculation."<512) + { + exit_type=__sigsegv_segmentation_error; + std::cout<<">>[Runtime-error] line "<512) + { + exit_type=__sigsegv_segmentation_error; + std::cout<<">>[Runtime-error] line "<512) + { + exit_type=__sigsegv_segmentation_error; + std::cout<<">>[Runtime-error] line "<512) + { + exit_type=__sigsegv_segmentation_error; + std::cout<<">>[Runtime-error] line "<512) + { + exit_type=__sigsegv_segmentation_error; + std::cout<<">>[Runtime-error] line "<512) + { + exit_type=__sigsegv_segmentation_error; + std::cout<<">>[Runtime-error] line "<::iterator i=children.begin();i!=children.end();++i) { 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(); + --recursion_depth; return ret; } var abstract_syntax_tree::run_func(std::list parameter,var self_func) { - static int recursion_depth=0; ++recursion_depth; if(recursion_depth>512) { exit_type=__sigsegv_segmentation_error; - std::cout<<">>[Runtime-error] line "<>[Runtime-error] line "< parameter,var self_func) int abstract_syntax_tree::run_block() { + ++recursion_depth; + if(recursion_depth>512) + { + exit_type=__sigsegv_segmentation_error; + std::cout<<">>[Runtime-error] line "<::iterator i=children.begin();i!=children.end();++i) { @@ -862,7 +934,10 @@ int abstract_syntax_tree::run_block() if(type) { if(type==__return) + { + --recursion_depth; return type; + } else { std::cout<<"[Runtime-error] line "<run_ifelse(); if(type) + { + --recursion_depth; return type; + } } else if(i->type==__continue) + { + --recursion_depth; return __continue; + } else if(i->type==__break) + { + --recursion_depth; return __break; + } else if(i->type==__return) { var temp; @@ -887,12 +971,14 @@ int abstract_syntax_tree::run_block() if(!(i->children.empty())) temp=i->children.front().calculation(); ret_stack.push(temp); + --recursion_depth; return __return; } if(exit_type!=__process_exited_successfully) break; } scope.pop_last_local_scope(); + --recursion_depth; return 0; } diff --git a/balloon/balloon.h b/balloon/balloon.h index 561d908..ff407d2 100644 --- a/balloon/balloon.h +++ b/balloon/balloon.h @@ -20,6 +20,8 @@ #include #include +#include + void alert_sound() { printf("\a"); @@ -48,5 +50,6 @@ void alert_sound() /* global varia in abstract_syntax_tree.cpp : int exit_type; // record the state of runtime std::stack ret_stack; // for function ret use + int recursion_depth; // avoid too deep recursion */ #endif diff --git a/balloon/balloon_lexer.h b/balloon/balloon_lexer.h index 231d8fa..45b5cf7 100644 --- a/balloon/balloon_lexer.h +++ b/balloon/balloon_lexer.h @@ -92,7 +92,10 @@ class resource_file { std::string lib_name; lib_name="lib/math.nas"; - input_file(lib_name); + if(access("lib/math.nas",0)) + std::cout<<">>[Resource] lack lib file: lib/math.nas ."<& get_resource() diff --git a/balloon/balloon_parse.h b/balloon/balloon_parse.h index 5e5e09d..5a75ac1 100644 --- a/balloon/balloon_parse.h +++ b/balloon/balloon_parse.h @@ -1007,6 +1007,7 @@ void balloon_parse::parse_main() case __foreach: case __while:parse.push(this_token);root.add_child(loop());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: ++error; std::cout<<">>[Parse-error] line "< >= @@ -169,7 +169,7 @@ void print_scalar(int type) enum runtime_error_type { - __process_exited_successfully, + __process_exited_successfully=1, __redeclaration, __get_value_failure, __find_var_failure, diff --git a/balloon/lib/math.nas b/balloon/lib/math.nas index b8fe28a..e8092d4 100644 --- a/balloon/lib/math.nas +++ b/balloon/lib/math.nas @@ -1,18 +1,21 @@ +var pi=3.14159265358979; + var abs=func(__x) { if(__x>0){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;} 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) { - return asr(1,_x); + return __balloon_lib_ln_asr(1,_x); }; var log=func(__a,__x) @@ -63,3 +66,34 @@ var relu=func(__x) if(x>=0){return __x;} 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); +}; \ No newline at end of file