This commit is contained in:
Valk Richard Li 2020-04-12 01:58:30 -07:00 committed by GitHub
parent c186d3b030
commit 5899c6e378
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 284 additions and 250 deletions

View File

@ -49,7 +49,6 @@ int main()
std::cin>>command;
if(command=="help")
{
std::cout<<">> Be careful that this program does not support unicode(unicode will be set to \'?\')"<<std::endl;
std::cout<<">> [\'file\'] input a file."<<std::endl;
std::cout<<">> [cls ] clear the screen."<<std::endl;
std::cout<<">> [del ] clear the resource code."<<std::endl;

View File

@ -11,15 +11,17 @@
class nasal_function
{
private:
// closure_updated flag is used to mark if this function's closure is updated.
// to avoid some unexpected errors,closure of each function must be updated before blocks popping back the last scope
bool closure_updated;
std::list<std::map<std::string,int> > local_scope;
abstract_syntax_tree parameter_list;
abstract_syntax_tree function_root;
// parent_hash_addr is used to store the address of the hash which has this nasal_function
// because nasal_function needs this address to adjust the identifier called 'me' in local_scope
// 'me' is the identifier which points to the hash which has this nasal_function
public:
void set_clear();
void set_local_scope(std::list<std::map<std::string,int> >&);
bool get_closure_update_state();
void set_closure_update_state(bool);
void set_paramemter_list(abstract_syntax_tree&);
void set_statement_block(abstract_syntax_tree&);
std::list<std::map<std::string,int> >& get_local_scope();
@ -339,18 +341,31 @@ void nasal_function::set_clear()
for(std::list<std::map<std::string,int> >::iterator iter=local_scope.begin();iter!=local_scope.end();++iter)
for(std::map<std::string,int>::iterator i=iter->begin();i!=iter->end();++i)
nasal_gc.reference_delete(i->second);
closure_updated=false;
local_scope.clear();
function_root.set_clear();
return;
}
void nasal_function::set_local_scope(std::list<std::map<std::string,int> >& tmp_scope)
{
for(std::list<std::map<std::string,int> >::iterator iter=local_scope.begin();iter!=local_scope.end();++iter)
for(std::map<std::string,int>::iterator i=iter->begin();i!=iter->end();++i)
nasal_gc.reference_delete(i->second);
local_scope=tmp_scope;
for(std::list<std::map<std::string,int> >::iterator iter=local_scope.begin();iter!=local_scope.end();++iter)
for(std::map<std::string,int>::iterator i=iter->begin();i!=iter->end();++i)
nasal_gc.reference_add(i->second);
return;
}
bool nasal_function::get_closure_update_state()
{
return closure_updated;
}
void nasal_function::set_closure_update_state(bool _state)
{
closure_updated=_state;
return;
}
void nasal_function::set_paramemter_list(abstract_syntax_tree& para_list)
{
parameter_list=para_list;

View File

@ -100,6 +100,7 @@ class nasal_runtime
int vector_generation (std::list<std::map<std::string,int> >&,abstract_syntax_tree&);//checked
int hash_generation (std::list<std::map<std::string,int> >&,abstract_syntax_tree&);// checked
int function_generation(std::list<std::map<std::string,int> >&,abstract_syntax_tree&);// checked
void update_closure (std::list<std::map<std::string,int> >&);
bool check_condition (std::list<std::map<std::string,int> >&,abstract_syntax_tree&);// checked
int calculation (std::list<std::map<std::string,int> >&,abstract_syntax_tree&);// checked
int assignment (std::list<std::map<std::string,int> >&,abstract_syntax_tree&,int);
@ -1463,6 +1464,7 @@ int nasal_runtime::function_generation(std::list<std::map<std::string,int> >& lo
int addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(addr).set_type(scalar_function);
nasal_gc.get_scalar(addr).get_function().set_local_scope(local_scope);
nasal_gc.get_scalar(addr).get_function().set_closure_update_state(false);
std::list<abstract_syntax_tree>::iterator i=node.get_children().begin();
nasal_gc.get_scalar(addr).get_function().set_paramemter_list(*i);
@ -2054,6 +2056,20 @@ int nasal_runtime::function_generation(std::list<std::map<std::string,int> >& lo
}
return addr;
}
void nasal_runtime::update_closure(std::list<std::map<std::string,int> >& local_scope)
{
// update_closure
if(!local_scope.size())
return;
for(std::map<std::string,int>::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())
{
nasal_gc.get_scalar(i->second).get_function().set_local_scope(local_scope);
nasal_gc.get_scalar(i->second).get_function().set_closure_update_state(true);
}
return;
}
bool nasal_runtime::check_condition(std::list<std::map<std::string,int> >& local_scope,abstract_syntax_tree& node)
{
bool ret=false;
@ -4403,6 +4419,8 @@ int nasal_runtime::block_proc(std::list<std::map<std::string,int> >& local_scope
if(state==__state_break || state==__state_continue || state==__state_return || state==__state_error)
break;
}
// update_closure
update_closure(local_scope);
return state;
}
int nasal_runtime::func_proc(
@ -4596,6 +4614,8 @@ int nasal_runtime::func_proc(
function_returned_addr=nasal_gc.gc_alloc();
nasal_gc.get_scalar(function_returned_addr).set_type(scalar_nil);
}
// update closure
update_closure(local_scope);
for(std::map<std::string,int>::iterator i=local_scope.back().begin();i!=local_scope.back().end();++i)
nasal_gc.reference_delete(i->second);
local_scope.pop_back();