diff --git a/version2.0/nasal_gc.h b/version2.0/nasal_gc.h index e8a4cac..4f621e2 100644 --- a/version2.0/nasal_gc.h +++ b/version2.0/nasal_gc.h @@ -35,13 +35,16 @@ class nasal_scalar void set_string(std::string&); void vector_push(int); int vector_pop(); + int vector_get_member(int); void hash_add_new_member(std::string,int); void hash_delete_member(std::string); int hash_get_member(std::string); - int get_type(); - double get_number(); - std::string get_string(); - nasal_function& get_function(); + int get_type(); + double get_number(); + std::string get_string(); + std::vector& get_vector(); + std::map& get_hash(); + nasal_function& get_function(); }; struct gc_unit @@ -105,22 +108,6 @@ class gc_manager free_space.clear(); return; } - void gc_scanner() - { - int memory_size=memory.size(); - for(int i=0;i> [Gc] collected "; - prt_hex(i); - std::cout<<"."<::iterator begin_node=memory[place].elem.get_hash().begin(); + std::map::iterator end_node=memory[place].elem.get_hash().end(); + for(std::map::iterator iter=begin_node;iter!=end_node;++iter) + reference_delete(iter->second); + } + else if(memory[place].elem.get_type()==scalar_function) + { + std::list >::iterator begin_scope=memory[place].elem.get_function().get_local_scope().begin(); + std::list >::iterator end_scope=memory[place].elem.get_function().get_local_scope().end(); + for(std::list >::iterator scope_iter=begin_scope;scope_iter!=end_scope;++scope_iter) + for(std::map::iterator i=scope_iter->begin();i!=scope_iter->end();++i) + reference_delete(i->second); + } + memory[place].elem.set_clear(); + } + } else { std::cout<<">> [Gc] fatal error: unexpected memory place "; @@ -264,7 +279,8 @@ void nasal_scalar::set_clear() type=scalar_nil; var_string.clear(); var_number=0; - var_array.clear(); + std::vector tmp_vec; + var_array.swap(tmp_vec); var_hash.clear(); var_func.set_clear(); return; @@ -296,6 +312,12 @@ int nasal_scalar::vector_pop() var_array.pop_back(); return ret; } +int nasal_scalar::vector_get_member(int place) +{ + if(place<0 || place>=var_array.size()) + return -1; + return var_array[place]; +} void nasal_scalar::hash_add_new_member(std::string member_name,int addr) { // if hash has a new function member @@ -334,6 +356,14 @@ std::string nasal_scalar::get_string() { return var_string; } +std::vector& nasal_scalar::get_vector() +{ + return var_array; +} +std::map& nasal_scalar::get_hash() +{ + return var_hash; +} nasal_function& nasal_scalar::get_function() { return var_func; diff --git a/version2.0/nasal_runtime.h b/version2.0/nasal_runtime.h index 309b24f..53a5776 100644 --- a/version2.0/nasal_runtime.h +++ b/version2.0/nasal_runtime.h @@ -4,8 +4,11 @@ class nasal_runtime { private: - // local hash_map will be used when running - std::list > global_scope; + // global scope is a hash_map + // null_local_scope is used when function need reference of local scope + // but this function is called from global scope + std::map global_scope; + std::list > null_local_scope; // see detail of each enum type in function error_interrupt(const int) enum runtime_error_type @@ -17,10 +20,10 @@ class nasal_runtime void error_interrupt (const int); void vector_generation (abstract_syntax_tree&); void hash_generation (abstract_syntax_tree&); - void call_identifier (abstract_syntax_tree&); void calculation (abstract_syntax_tree&); - void assignment (abstract_syntax_tree&); - void definition (abstract_syntax_tree&); + void call_identifier (std::list >&,abstract_syntax_tree&); + void assignment (std::list >&,abstract_syntax_tree&); + void definition (std::list >&,abstract_syntax_tree&); void loop_expr (abstract_syntax_tree&); void conditional (abstract_syntax_tree&); void func_proc (std::list >&,abstract_syntax_tree&); @@ -64,28 +67,42 @@ void nasal_runtime::hash_generation(abstract_syntax_tree& node) { return; } -void nasal_runtime::call_identifier(abstract_syntax_tree& node) -{ - return; -} void nasal_runtime::calculation(abstract_syntax_tree& node) { return; } -void nasal_runtime::assignment(abstract_syntax_tree& node) +void nasal_runtime::call_identifier(std::list >& local_scope,abstract_syntax_tree& node) { + if(local_scope.empty()) + ; return; } -void nasal_runtime::definition(abstract_syntax_tree& node) +void nasal_runtime::assignment(std::list >& local_scope,abstract_syntax_tree& node) { + if(local_scope.empty()) + ; + return; +} +void nasal_runtime::definition(std::list >& local_scope,abstract_syntax_tree& node) +{ + if(local_scope.empty()) + ; return; } void nasal_runtime::loop_expr(abstract_syntax_tree& node) { + std::list > local_scope; + std::map new_scope; + local_scope.push_back(new_scope); + return; } void nasal_runtime::conditional(abstract_syntax_tree& node) { + std::list > local_scope; + std::map new_scope; + local_scope.push_back(new_scope); + return; } @@ -104,7 +121,7 @@ void nasal_runtime::func_proc(std::list >& local_scope ; // only number or string else if(node_type==__id) - this->call_identifier(*iter); + this->call_identifier(local_scope,*iter); else if(node_type==__vector) this->vector_generation(*iter); else if(node_type==__hash) @@ -150,7 +167,7 @@ void nasal_runtime::main_proc(abstract_syntax_tree& root) if(node_type==__number || node_type==__string) ; else if(node_type==__id) - this->call_identifier(*iter); + this->call_identifier(null_local_scope,*iter); else if(node_type==__vector) this->vector_generation(*iter); else if(node_type==__hash)