This commit is contained in:
Valk Richard Li 2019-11-14 23:21:47 +08:00 committed by GitHub
parent 6081c643d0
commit 8502ef20cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 10 deletions

View File

@ -468,10 +468,14 @@ var abstract_syntax_tree::call_identifier()
}
else if(i->type==__call_function && temp.get_type()==__var_function)
{
var self;
self.set_type(__var_function);
self.set_name(temp.get_name());
self.set_function(temp.get_function());
std::list<var> parameter;
for(std::list<abstract_syntax_tree>::iterator j=i->children.begin();j!=i->children.end();++j)
parameter.push_back(j->calculation());
temp=temp.get_function().run_func(parameter);
temp=temp.get_function().run_func(parameter,self);
}
else
{
@ -737,11 +741,23 @@ int abstract_syntax_tree::run_ifelse()
return ret;
}
var abstract_syntax_tree::run_func(std::list<var> parameter)
var abstract_syntax_tree::run_func(std::list<var> 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 "<<line<<":[SIGSEGV] too deep recursion."<<std::endl;
return error_var;
}
var ret;
scope.add_new_block_scope();
scope.add_new_local_scope();
scope.add_new_var(self_func);
abstract_syntax_tree para=children.front();
abstract_syntax_tree blk=children.back();
std::list<abstract_syntax_tree>::iterator para_name=para.children.begin();
@ -763,10 +779,11 @@ var abstract_syntax_tree::run_func(std::list<var> parameter)
if(exit_type==__process_exited_successfully)
{
int _t=blk.run_block();
// in case the exit_type is not __process_exited_successfully in run_block
// or the function does not have return value;
if(_t!=__return)
ret_stack.push(error_var);
}
else
ret_stack.push(error_var);
@ -774,7 +791,7 @@ var abstract_syntax_tree::run_func(std::list<var> parameter)
scope.pop_last_block_scope();
ret=ret_stack.top();
ret_stack.pop();
--recursion_depth;
return ret;
}
@ -793,6 +810,11 @@ int abstract_syntax_tree::run_block()
++j;
if(j!=i->children.end())
new_var=j->calculation();
if(new_var.get_type()==__var_function)
{
exit_type=__bad_definition;
std::cout<<">>[Runtime-error] line "<<line<<": cannot declare a function in any blocks."<<std::endl;
}
new_var.set_name(_name);
scope.add_new_var(new_var);
}

View File

@ -206,7 +206,7 @@ class abstract_syntax_tree
void run_root();
int run_loop();
int run_ifelse();
var run_func(std::list<var>);
var run_func(std::list<var>,var);
int run_block();
};

View File

@ -175,6 +175,7 @@ enum runtime_error_type
__find_var_failure,
__error_value_type,
__error_command_use,
__bad_definition,
__sigfpe_arithmetic_exception,
__sigsegv_segmentation_error,
__terminal_interrupt,
@ -192,6 +193,7 @@ void print_exit_type(int type)
case __find_var_failure: context="find_var_failure";break;
case __error_value_type: context="value_type_error";break;
case __error_command_use: context="command_use_error(continue/break/return)";break;
case __bad_definition: context="bad_definition(func in block)";break;
case __sigfpe_arithmetic_exception: context="SIGFPE_arithmetic_exception";break;
case __sigsegv_segmentation_error: context="SIGSEGV_segmentation_error";break;
case __terminal_interrupt: context="interrupt";break;

View File

@ -59,7 +59,7 @@ class var
void set_name(std::string);
void set_number(double);
void set_string(std::string);
void set_function(abstract_syntax_tree&);
void set_function(abstract_syntax_tree);
void append_array(var);
void append_hash(var);
int get_type();
@ -70,7 +70,7 @@ class var
var* get_array_member_addr(int);
var get_hash_member(std::string);
var* get_hash_member_addr(std::string);
abstract_syntax_tree& get_function();
abstract_syntax_tree get_function();
};
var error_var;
@ -99,7 +99,7 @@ void var::set_string(std::string _str)
return;
}
void var::set_function(abstract_syntax_tree& p)
void var::set_function(abstract_syntax_tree p)
{
function=p;
return;
@ -193,7 +193,7 @@ var* var::get_hash_member_addr(std::string _name)
return &error_var;
}
abstract_syntax_tree& var::get_function()
abstract_syntax_tree var::get_function()
{
return function;
}

View File

@ -55,7 +55,7 @@ var cosh=func(__x)
var tanh=func(__x)
{
__x=exp(__x);
return 1-2/(x*x+1);
return 1-2/(__x*__x+1);
};
var relu=func(__x)

View File

@ -26,6 +26,7 @@ int main()
std::cout<<">> 8. [run ] |run the programme in stack. (-lexer -parser)"<<std::endl;
std::cout<<">> 9. [rs ] |check the source program."<<std::endl;
std::cout<<">>10. [lib ] |add lib into resource codes."<<std::endl;
std::cout<<">>11. [sound ] |make noise(?)."<<std::endl;
}
else if(command=="cls")
{
@ -111,6 +112,8 @@ int main()
std::cout<<">>[Lexer] error(s) found,stop."<<std::endl;
}
}
else if(command=="sound")
alert_sound();
else
prog.input_file(command);
}