update
This commit is contained in:
parent
c186d3b030
commit
5899c6e378
|
@ -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;
|
||||
|
|
|
@ -10,16 +10,18 @@
|
|||
|
||||
class nasal_function
|
||||
{
|
||||
private:
|
||||
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:
|
||||
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();
|
||||
|
@ -30,9 +32,9 @@ class nasal_function
|
|||
|
||||
class nasal_number
|
||||
{
|
||||
private:
|
||||
private:
|
||||
double nas_number;
|
||||
public:
|
||||
public:
|
||||
void set_clear();
|
||||
void set_number(double);
|
||||
double get_number();
|
||||
|
@ -41,9 +43,9 @@ class nasal_number
|
|||
|
||||
class nasal_string
|
||||
{
|
||||
private:
|
||||
private:
|
||||
std::string nas_string;
|
||||
public:
|
||||
public:
|
||||
void set_clear();
|
||||
void set_string(std::string);
|
||||
std::string get_string();
|
||||
|
@ -52,9 +54,9 @@ class nasal_string
|
|||
|
||||
class nasal_vector
|
||||
{
|
||||
private:
|
||||
private:
|
||||
std::vector<int> nas_array;
|
||||
public:
|
||||
public:
|
||||
void set_clear();
|
||||
void vec_push(int);
|
||||
int* get_elem_addr(int);
|
||||
|
@ -69,9 +71,9 @@ class nasal_vector
|
|||
|
||||
class nasal_hash
|
||||
{
|
||||
private:
|
||||
private:
|
||||
std::map<std::string,int> nas_hash;
|
||||
public:
|
||||
public:
|
||||
void set_clear();
|
||||
int* get_hash_member_addr(std::string);
|
||||
int get_hash_member(std::string);
|
||||
|
@ -82,14 +84,14 @@ class nasal_hash
|
|||
|
||||
class nasal_scalar
|
||||
{
|
||||
private:
|
||||
private:
|
||||
int type;
|
||||
nasal_string var_string;
|
||||
nasal_number var_number;
|
||||
nasal_vector var_vector;
|
||||
nasal_hash var_hash;
|
||||
nasal_function var_func;
|
||||
public:
|
||||
public:
|
||||
nasal_scalar();
|
||||
void set_type(int);
|
||||
int get_type();
|
||||
|
@ -126,11 +128,11 @@ struct memory_block
|
|||
|
||||
class memory_block_list
|
||||
{
|
||||
private:
|
||||
private:
|
||||
memory_block* head;
|
||||
int mem_size;
|
||||
int blk_size;
|
||||
public:
|
||||
public:
|
||||
memory_block_list()
|
||||
{
|
||||
mem_size=0;
|
||||
|
@ -203,7 +205,7 @@ class memory_block_list
|
|||
|
||||
class gc_manager
|
||||
{
|
||||
private:
|
||||
private:
|
||||
// free_space list is used to store space that is not in use.
|
||||
std::list<int> free_space;
|
||||
/*
|
||||
|
@ -214,7 +216,7 @@ class gc_manager
|
|||
*/
|
||||
memory_block_list memory;
|
||||
bool error_occurred;
|
||||
public:
|
||||
public:
|
||||
void gc_init()
|
||||
{
|
||||
// this function must be called in class nasal_runtime before running any codes
|
||||
|
@ -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;
|
||||
|
|
|
@ -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,11 +4419,13 @@ 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(
|
||||
std::list<std::map<std::string,int> >& parameters_assist_scope,// scope that used to generate parameters
|
||||
std::list<std::map<std::string,int> > local_scope,// running scope,often gets the scope that calls it
|
||||
std::list<std::map<std::string,int> > 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
|
||||
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue