forked from xxq250/Nasal-Interpreter
update
This commit is contained in:
+17
-46
@@ -87,13 +87,11 @@ private:
|
||||
// and this memory_manager_memory space stores an address to garbage_collector_memory
|
||||
// and this address points to an nasal_hash
|
||||
nasal_virtual_machine& vm;
|
||||
std::list<std::map<std::string,int> > elems;
|
||||
std::map<std::string,int> elems;
|
||||
public:
|
||||
nasal_closure(nasal_virtual_machine&);
|
||||
~nasal_closure();
|
||||
void set_vm(nasal_virtual_machine&);
|
||||
void add_scope();
|
||||
void del_scope();
|
||||
void add_new_value(std::string,int);
|
||||
int get_value_address(std::string);
|
||||
int get_mem_address(std::string);
|
||||
@@ -448,75 +446,48 @@ nasal_closure::nasal_closure(nasal_virtual_machine& nvm):vm(nvm)
|
||||
}
|
||||
nasal_closure::~nasal_closure()
|
||||
{
|
||||
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j)
|
||||
vm.mem_free(j->second);
|
||||
elems.clear();
|
||||
return;
|
||||
}
|
||||
void nasal_closure::add_scope()
|
||||
{
|
||||
std::map<std::string,int> new_scope;
|
||||
elems.push_back(new_scope);
|
||||
return;
|
||||
}
|
||||
void nasal_closure::del_scope()
|
||||
{
|
||||
if(this->elems.empty())
|
||||
return;
|
||||
for(std::map<std::string,int>::iterator i=elems.back().begin();i!=elems.back().end();++i)
|
||||
for(std::map<std::string,int>::iterator i=elems.begin();i!=elems.end();++i)
|
||||
vm.mem_free(i->second);
|
||||
this->elems.pop_back();
|
||||
elems.clear();
|
||||
return;
|
||||
}
|
||||
void nasal_closure::add_new_value(std::string key,int value_address)
|
||||
{
|
||||
int new_mem_address=vm.mem_alloc(value_address);
|
||||
if(elems.back().find(key)!=elems.back().end())
|
||||
if(elems.find(key)!=elems.end())
|
||||
{
|
||||
// if this value already exists,delete the old value and update a new value
|
||||
int old_mem_address=elems.back()[key];
|
||||
int old_mem_address=elems[key];
|
||||
vm.mem_free(old_mem_address);
|
||||
}
|
||||
elems.back()[key]=new_mem_address;
|
||||
elems[key]=new_mem_address;
|
||||
return;
|
||||
}
|
||||
int nasal_closure::get_value_address(std::string key)
|
||||
{
|
||||
int ret_address=-1;
|
||||
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||
{
|
||||
if(i->find(key)!=i->end())
|
||||
ret_address=vm.mem_get((*i)[key]);
|
||||
}
|
||||
if(elems.find(key)!=elems.end())
|
||||
ret_address=vm.mem_get(elems[key]);
|
||||
return ret_address;
|
||||
}
|
||||
int nasal_closure::get_mem_address(std::string key)
|
||||
{
|
||||
int ret_address=-1;
|
||||
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||
{
|
||||
if(i->find(key)!=i->end())
|
||||
ret_address=(*i)[key];
|
||||
}
|
||||
if(elems.find(key)!=elems.end())
|
||||
ret_address=elems[key];
|
||||
return ret_address;
|
||||
}
|
||||
void nasal_closure::set_closure(nasal_closure& tmp)
|
||||
{
|
||||
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j)
|
||||
vm.mem_free(j->second);
|
||||
for(std::map<std::string,int>::iterator i=elems.begin();i!=elems.end();++i)
|
||||
vm.mem_free(i->second);
|
||||
elems.clear();
|
||||
for(std::list<std::map<std::string,int> >::iterator i=tmp.elems.begin();i!=tmp.elems.end();++i)
|
||||
for(std::map<std::string,int>::iterator i=tmp.elems.begin();i!=tmp.elems.end();++i)
|
||||
{
|
||||
this->add_scope();
|
||||
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j)
|
||||
{
|
||||
int value_addr=vm.mem_get(j->second);
|
||||
int new_mem_addr=vm.mem_alloc(value_addr);
|
||||
vm.add_reference(value_addr);
|
||||
elems.back()[j->first]=new_mem_addr;
|
||||
}
|
||||
int value_addr=vm.mem_get(i->second);
|
||||
int new_mem_addr=vm.mem_alloc(value_addr);
|
||||
vm.add_reference(value_addr);
|
||||
elems[i->first]=new_mem_addr;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user