diff --git a/balloon/abstract_syntax_tree.cpp b/balloon/abstract_syntax_tree.cpp index cf853b1..4f4b467 100644 --- a/balloon/abstract_syntax_tree.cpp +++ b/balloon/abstract_syntax_tree.cpp @@ -5,7 +5,6 @@ int exit_type=0; std::stack ret_stack; // for function ret use -std::list parameter; // for function call use var abstract_syntax_tree::calculation() @@ -469,10 +468,10 @@ var abstract_syntax_tree::call_identifier() } else if(i->type==__call_function && temp.get_type()==__var_function) { - parameter.clear(); + std::list parameter; for(std::list::iterator j=i->children.begin();j!=i->children.end();++j) parameter.push_back(j->calculation()); - temp=temp.get_function().run_func(); + temp=temp.get_function().run_func(parameter); } else { @@ -738,7 +737,7 @@ int abstract_syntax_tree::run_ifelse() return ret; } -var abstract_syntax_tree::run_func() +var abstract_syntax_tree::run_func(std::list parameter) { var ret; scope.add_new_block_scope(); diff --git a/balloon/abstract_syntax_tree.h b/balloon/abstract_syntax_tree.h index da057b0..eb9238e 100644 --- a/balloon/abstract_syntax_tree.h +++ b/balloon/abstract_syntax_tree.h @@ -206,7 +206,7 @@ class abstract_syntax_tree void run_root(); int run_loop(); int run_ifelse(); - var run_func(); + var run_func(std::list); int run_block(); }; diff --git a/balloon/balloon.h b/balloon/balloon.h index 24bda53..561d908 100644 --- a/balloon/balloon.h +++ b/balloon/balloon.h @@ -48,6 +48,5 @@ 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 - std::list parameter; // for function call use */ #endif diff --git a/balloon/balloon_lexer.h b/balloon/balloon_lexer.h index 79b124f..0733b85 100644 --- a/balloon/balloon_lexer.h +++ b/balloon/balloon_lexer.h @@ -88,6 +88,13 @@ class resource_file resource.clear(); return; } + void add_lib() + { + std::string lib_name; + lib_name="lib/math.nas"; + input_file(lib_name); + return; + } void input_file(std::string filename) { std::ifstream fin(filename,std::ios::binary); diff --git a/balloon/lib/math.nas b/balloon/lib/math.nas index 8cca27b..0013f20 100644 --- a/balloon/lib/math.nas +++ b/balloon/lib/math.nas @@ -1,12 +1,18 @@ -var fabs=func(__x) +var abs=func(__x) { - if(x>0){return __x;} + if(__x>0){return __x;} else{return -__x;} }; -var ln=func(__x) +var asr=func(__x,__y) { - return (1+8/(1+__x)+1/__x)*(__x-1)/6; + 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); +}; +var ln=func(_x) +{ + return asr(1,_x); }; var log=func(__a,__x) @@ -26,7 +32,7 @@ var pow=func(__x,__num) { if(__num==0){return 1;} else if(__num<0){return 1/pow(__x,-__num);} - else{return exp(__x*ln(__num));} + else{return exp(__num*ln(__x));} }; var sigmoid=func(__x) diff --git a/balloon/main.cpp b/balloon/main.cpp index c497aad..9910c39 100644 --- a/balloon/main.cpp +++ b/balloon/main.cpp @@ -25,6 +25,7 @@ int main() std::cout<<">> 7. [del ] |delete program in memory."<> 8. [run ] |run the programme in stack. (-lexer -parser)"<> 9. [rs ] |check the source program."<>10. [lib ] |add lib into resource codes."<