From 3384190e57b2d7282f649a478156e574a3f92bb1 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Tue, 31 Mar 2020 21:10:14 +0800 Subject: [PATCH] update --- version2.0/nasal_gc.h | 3 +- version2.0/nasal_runtime.h | 69 ++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/version2.0/nasal_gc.h b/version2.0/nasal_gc.h index 9419c72..2792cb9 100644 --- a/version2.0/nasal_gc.h +++ b/version2.0/nasal_gc.h @@ -478,8 +478,7 @@ int nasal_hash::get_hash_member(std::string member_name) } void nasal_hash::hash_push(std::string member_name,int addr) { - if(nas_hash.find(member_name)==nas_hash.end()) - nas_hash[member_name]=addr; + nas_hash[member_name]=addr; return; } void nasal_hash::hash_pop(std::string member_name) diff --git a/version2.0/nasal_runtime.h b/version2.0/nasal_runtime.h index d4ac039..7fa0a70 100644 --- a/version2.0/nasal_runtime.h +++ b/version2.0/nasal_runtime.h @@ -106,7 +106,7 @@ class nasal_runtime int loop_expr (std::list >&,abstract_syntax_tree&);// checked int conditional (std::list >&,abstract_syntax_tree&);// checked int block_proc (std::list >&,abstract_syntax_tree&);// checked - int func_proc (std::list >&,std::list >&,abstract_syntax_tree&,abstract_syntax_tree&,abstract_syntax_tree&,int);// checked + int func_proc (std::list >&,std::list >,abstract_syntax_tree&,abstract_syntax_tree&,abstract_syntax_tree&,int);// checked int inline_function (std::list >&,std::string); public: nasal_runtime() @@ -519,7 +519,7 @@ int nasal_runtime::hash_generation(std::list >& local_ // note:how does nasal_function change the address of me when a hash calls it? // because before calling this function,hash gives the address if itself as a parameter to the function, // and this parameter's name is 'me' - std::string member_name=i->get_children().front().get_var_string(); + std::string member_name=i->get_children().front().get_var_name(); int var_type=i->get_children().back().get_node_type(); if(member_name=="parents") { @@ -2141,7 +2141,6 @@ int nasal_runtime::assignment(std::list >& local_scope error_interrupt(__subvec_value_be_assigned,node.get_children().back().get_node_line()); return -1; } - /* assigned_addr=find_address() while(children.size()) @@ -2168,6 +2167,19 @@ int nasal_runtime::call_identifier(std::list >& local_ addr=(*iter)[tmp_id_name]; if(addr<0) { + /* + left for test: + for(std::list >::iterator i=local_scope.begin();i!=local_scope.end();++i) + { + for(std::map::iterator j=i->begin();j!=i->end();++j) + { + std::cout<first<<": "; + prt_hex(j->second); + std::cout< >& local_scop for(std::map::iterator i=local_scope.back().begin();i!=local_scope.back().end();++i) nasal_gc.reference_delete(i->second); local_scope.pop_back(); - return __state_no_operation; + return ret_state; } int nasal_runtime::block_proc(std::list >& local_scope,abstract_syntax_tree& node) { @@ -2882,30 +2894,33 @@ int nasal_runtime::block_proc(std::list >& local_scope } int nasal_runtime::func_proc( std::list >& parameters_assist_scope,// scope that used to generate parameters - std::list >& local_scope,// running scope,often gets the scope that calls it + std::list > local_scope,// running scope,often gets the scope that calls it abstract_syntax_tree& parameter_list, // parameter list format of nasal function abstract_syntax_tree& func_root, // main runnning block of nasal function abstract_syntax_tree& input_parameters, // input parameters when calling this nasal function int called_hash_addr // if called a hash before calling this nasal function,this address will be given to 'me' ) { + // local scope should not used as reference + // because this will change the elements in scalar_function value itself + // if you call the same function in the function,the scope will be in a mess function_returned_addr=-1; std::map new_scope; - local_scope.push_back(new_scope); + // new_scope will be used in parameters initialling process + // if push back this scope to local_scope + // there may be an error if an identifier has the same name as one identifier in the local_scope before if(called_hash_addr>=0) - local_scope.back()["me"]=called_hash_addr; + new_scope["me"]=called_hash_addr; // loading parameters std::vector para_name_list; - bool has_dynamic_id=false; - int dynamic_args=nasal_gc.gc_alloc(); - nasal_gc.get_scalar(dynamic_args).set_type(scalar_vector); + int dynamic_args=-1; for(std::list::iterator iter=parameter_list.get_children().begin();iter!=parameter_list.get_children().end();++iter) { if(iter->get_node_type()==__id) { para_name_list.push_back(iter->get_var_name()); - local_scope.back()[para_name_list.back()]=-1; + new_scope[para_name_list.back()]=-1; } else if(iter->get_node_type()==__default_parameter) { @@ -2913,23 +2928,24 @@ int nasal_runtime::func_proc( int default_val_addr=calculation(parameters_assist_scope,iter->get_children().back()); if(default_val_addr<0) return -1; - local_scope.back()[para_name_list.back()]=default_val_addr; + new_scope[para_name_list.back()]=default_val_addr; } else if(iter->get_node_type()==__dynamic_id) { - has_dynamic_id=true; - local_scope.back()[iter->get_var_name()]=dynamic_args; + dynamic_args=nasal_gc.gc_alloc(); + nasal_gc.get_scalar(dynamic_args).set_type(scalar_vector); + new_scope[iter->get_var_name()]=dynamic_args; break; } } - if(input_parameters.get_children().front().get_node_type()!=__special_parameter) + if(!input_parameters.get_children().empty() && input_parameters.get_children().front().get_node_type()!=__special_parameter) { int tmp_ptr=0; for(std::list::iterator iter=input_parameters.get_children().begin();iter!=input_parameters.get_children().end();++iter) { if(tmp_ptr>=para_name_list.size()) { - if(has_dynamic_id) + if(dynamic_args>=0) { int val_addr=calculation(parameters_assist_scope,*iter); if(val_addr<0) @@ -2944,12 +2960,12 @@ int nasal_runtime::func_proc( int val_addr=calculation(parameters_assist_scope,*iter); if(val_addr<0) return -1; - if(local_scope.back()[para_name_list[tmp_ptr]]<0) - local_scope.back()[para_name_list[tmp_ptr]]=val_addr; + if(new_scope[para_name_list[tmp_ptr]]<0) + new_scope[para_name_list[tmp_ptr]]=val_addr; else { - nasal_gc.reference_delete(local_scope.back()[para_name_list[tmp_ptr]]); - local_scope.back()[para_name_list[tmp_ptr]]=val_addr; + nasal_gc.reference_delete(new_scope[para_name_list[tmp_ptr]]); + new_scope[para_name_list[tmp_ptr]]=val_addr; } } ++tmp_ptr; @@ -2960,27 +2976,28 @@ int nasal_runtime::func_proc( for(std::list::iterator iter=input_parameters.get_children().begin();iter!=input_parameters.get_children().end();++iter) { std::string tmp_para_name=iter->get_children().front().get_var_name(); - if(local_scope.back().find(tmp_para_name)!=local_scope.back().end()) + if(new_scope.find(tmp_para_name)!=new_scope.end()) { int val_addr=calculation(parameters_assist_scope,iter->get_children().back()); if(val_addr<0) return -1; - if(local_scope.back()[tmp_para_name]<0) - local_scope.back()[tmp_para_name]=val_addr; + if(new_scope[tmp_para_name]<0) + new_scope[tmp_para_name]=val_addr; else { - nasal_gc.reference_delete(local_scope.back()[tmp_para_name]); - local_scope.back()[tmp_para_name]=val_addr; + nasal_gc.reference_delete(new_scope[tmp_para_name]); + new_scope[tmp_para_name]=val_addr; } } } } for(int i=0;i::iterator iter=func_root.get_children().begin();iter!=func_root.get_children().end();++iter)