From 4952ea90029aeaef8cf760449734e3267a615602 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Tue, 26 May 2020 07:22:27 -0700 Subject: [PATCH] update --- version2.0/nasal.h | 1 + version2.0/nasal_gc.h | 8 ++++++ version2.0/nasal_runtime.h | 56 ++++++++++++++++++-------------------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/version2.0/nasal.h b/version2.0/nasal.h index 0588935..18bfb3f 100644 --- a/version2.0/nasal.h +++ b/version2.0/nasal.h @@ -11,6 +11,7 @@ // #include #include #include +#include #include #include diff --git a/version2.0/nasal_gc.h b/version2.0/nasal_gc.h index 5031457..e026310 100644 --- a/version2.0/nasal_gc.h +++ b/version2.0/nasal_gc.h @@ -89,6 +89,7 @@ public: void set_clear(); int* get_hash_member_addr(std::string); int get_hash_member(std::string); + std::vector get_elem(); void hash_push(std::string,int); void hash_pop(std::string); void deep_copy(nasal_hash&); @@ -607,6 +608,13 @@ int nasal_hash::get_hash_member(std::string member_name) return nasal_gc.get_scalar(nas_hash["parents"]).get_vector().get_parent_hash_member(member_name); return -1; } +std::vector nasal_hash::get_elem() +{ + std::vector vec; + for(std::map::iterator i=nas_hash.begin();i!=nas_hash.end();++i) + vec.push_back(i->second); + return vec; +} void nasal_hash::hash_push(std::string member_name,int addr) { nas_hash[member_name]=addr; diff --git a/version2.0/nasal_runtime.h b/version2.0/nasal_runtime.h index 782a62f..eb9d9d8 100644 --- a/version2.0/nasal_runtime.h +++ b/version2.0/nasal_runtime.h @@ -106,7 +106,7 @@ class nasal_runtime int hash_generation (std::list >&,abstract_syntax_tree&); int function_generation(std::list >&,abstract_syntax_tree&); - void update_closure (std::list >&,int); + void update_closure (std::vector&,int); bool check_condition (std::list >&,abstract_syntax_tree&); int calculation (std::list >&,abstract_syntax_tree&); int assignment (std::list >&,abstract_syntax_tree&,int); @@ -966,28 +966,17 @@ int nasal_runtime::function_generation(std::list >& lo } return addr; } -void nasal_runtime::update_closure(std::list >& local_scope,int local_scope_addr) +void nasal_runtime::update_closure(std::vector& update_list,int closure_addr) { // update_closure // each new function will be updated only once, after updating closure,functions' closure_updated flag will be set true - // but this has a bug, if this new function is a member of vector or hash, it will not be updated - if(!local_scope.size()) - return; - for(std::map::iterator i=local_scope.back().begin();i!=local_scope.back().end();++i) - if(nasal_gc.get_scalar(i->second).get_type()==scalar_function && - !nasal_gc.get_scalar(i->second).get_function().get_closure_update_state()) + int size=update_list.size(); + for(int i=0;isecond).get_function().set_local_scope(local_scope_addr); - nasal_gc.reference_add(local_scope_addr); - nasal_gc.get_scalar(i->second).get_function().set_closure_update_state(true); - } - for(std::map::iterator i=global_scope.begin();i!=global_scope.end();++i) - if(nasal_gc.get_scalar(i->second).get_type()==scalar_function && - !nasal_gc.get_scalar(i->second).get_function().get_closure_update_state()) - { - nasal_gc.get_scalar(i->second).get_function().set_local_scope(local_scope_addr); - nasal_gc.reference_add(local_scope_addr); - nasal_gc.get_scalar(i->second).get_function().set_closure_update_state(true); + nasal_gc.get_scalar(update_list[i]).get_function().set_local_scope(closure_addr); + nasal_gc.reference_add(closure_addr); + nasal_gc.get_scalar(update_list[i]).get_function().set_closure_update_state(true); } return; } @@ -2267,13 +2256,13 @@ int nasal_runtime::assignment(std::list >& local_scope return -1; } /* - assigned_addr=find_address() + assigned_addr=find_address(); while(children.size()) { - assigned_addr=new_addr() + assigned_addr=new_addr(); } - *assigned_addr->refcnt-- - *assigned_addr=new_value_addr + *assigned_addr->refcnt--; + *assigned_addr=new_value_addr; */ // data_addr is only a parameter here,and it's refcnt has not been changed when using it here nasal_gc.reference_add(*assigned_addr); @@ -2352,7 +2341,6 @@ int nasal_runtime::call_identifier(std::list >& local_ // call function identifier(...) else if(iter->get_node_type()==__call_function) addr=call_function(local_scope,iter,addr,last_hash_addr); - if(addr<0) break; } @@ -2726,6 +2714,9 @@ int nasal_runtime::conditional(std::list >& local_scop int nasal_runtime::block_proc(std::list >& local_scope,abstract_syntax_tree& node) { int state=__state_no_operation; + int closure_addr=nasal_gc.gc_alloc(); + nasal_gc.get_scalar(closure_addr).set_type(scalar_closure); + std::vector assignedfunc_addrs; for(std::list::iterator iter=node.get_children().begin();iter!=node.get_children().end();++iter) { // use local value node_type to avoid calling function too many times. @@ -2763,7 +2754,10 @@ int nasal_runtime::block_proc(std::list >& local_scope { int addr=this->calculation(local_scope,*iter); if(addr>=0) + { + if(nasal_gc.get_scalar(addr).get_type()==scalar_function) assignedfunc_addrs.push_back(addr); nasal_gc.reference_delete(addr); + } } else if(node_type==__definition) this->definition(local_scope,local_scope.back(),*iter); @@ -2791,10 +2785,8 @@ int nasal_runtime::block_proc(std::list >& local_scope break; } // update_closure - int closure_addr=nasal_gc.gc_alloc(); - nasal_gc.get_scalar(closure_addr).set_type(scalar_closure); nasal_gc.get_scalar(closure_addr).get_closure().set_local_scope(local_scope); - update_closure(local_scope,closure_addr); + update_closure(assignedfunc_addrs,closure_addr); nasal_gc.reference_delete(closure_addr); return state; } @@ -2909,6 +2901,9 @@ int nasal_runtime::func_proc( local_scope.push_back(new_scope); // process int state=__state_no_operation; + int closure_addr=nasal_gc.gc_alloc(); + nasal_gc.get_scalar(closure_addr).set_type(scalar_closure); + std::vector assignedfunc_addrs; for(std::list::iterator iter=func_root.get_children().begin();iter!=func_root.get_children().end();++iter) { // use local value node_type to avoid calling function too many times. @@ -2946,7 +2941,10 @@ int nasal_runtime::func_proc( { int addr=this->calculation(local_scope,*iter); if(addr>=0) + { + if(nasal_gc.get_scalar(addr).get_type()==scalar_function) assignedfunc_addrs.push_back(addr); nasal_gc.reference_delete(addr); + } } else if(node_type==__definition) this->definition(local_scope,local_scope.back(),*iter); @@ -2990,10 +2988,8 @@ int nasal_runtime::func_proc( nasal_gc.get_scalar(function_returned_addr).set_type(scalar_nil); } // update closure - int closure_addr=nasal_gc.gc_alloc(); - nasal_gc.get_scalar(closure_addr).set_type(scalar_closure); nasal_gc.get_scalar(closure_addr).get_closure().set_local_scope(local_scope); - update_closure(local_scope,closure_addr); + update_closure(assignedfunc_addrs,closure_addr); nasal_gc.reference_delete(closure_addr); for(std::map::iterator i=local_scope.back().begin();i!=local_scope.back().end();++i) nasal_gc.reference_delete(i->second);