diff --git a/nasal_codegen.h b/nasal_codegen.h index 1e8a28a..fbb95ff 100644 --- a/nasal_codegen.h +++ b/nasal_codegen.h @@ -18,16 +18,12 @@ enum op_code op_addeq,op_subeq,op_muleq,op_diveq,op_lnkeq,op_meq, op_eq,op_neq,op_less,op_leq,op_grt,op_geq, op_pop, - op_newscope, - op_delscope, op_jmp, op_jmptrue, op_jmpfalse, op_jp, op_jtp, op_jfp, - op_continue, - op_break, op_forindex, // index counter on the top of forindex_stack plus 1 op_foreach, // index counter on the top of forindex_stack plus 1 and get the value in vector op_call, // call identifier @@ -88,16 +84,12 @@ struct {op_grt, "g "}, {op_geq, "geq "}, {op_pop, "pop "}, - {op_newscope, "nscp "}, - {op_delscope, "dscp "}, {op_jmp, "jmp "}, {op_jmptrue, "jt "}, {op_jmpfalse, "jf "}, {op_jp, "jp "}, {op_jtp, "jtp "}, {op_jfp, "jfp "}, - {op_continue, "contn "}, - {op_break, "break "}, {op_forindex, "findx "}, {op_foreach, "feach "}, {op_call, "call "}, @@ -120,8 +112,15 @@ struct struct opcode { unsigned char op; + unsigned int scope; unsigned int index; - opcode(){op=op_nop;index=0;} + opcode() + { + op=op_nop; + scope=0; + index=0; + return; + } }; // unfinished @@ -137,6 +136,7 @@ private: std::vector exec_code; std::vector continue_ptr; std::vector break_ptr; + int scope_depth; int error; void regist_number(double); void regist_string(std::string); @@ -206,6 +206,7 @@ void nasal_codegen::pop_gen() { opcode op; op.op=op_pop; + op.scope=scope_depth; op.index=0; exec_code.push_back(op); return; @@ -215,6 +216,7 @@ void nasal_codegen::nil_gen() { opcode op; op.op=op_pushnil; + op.scope=scope_depth; exec_code.push_back(op); return; } @@ -233,6 +235,7 @@ void nasal_codegen::number_gen(nasal_ast& ast) op.op=op_pushnum; op.index=number_table[num]; } + op.scope=scope_depth; exec_code.push_back(op); return; } @@ -243,6 +246,7 @@ void nasal_codegen::string_gen(nasal_ast& ast) regist_string(str); opcode op; op.op=op_pushstr; + op.scope=scope_depth; op.index=string_table[str]; exec_code.push_back(op); return; @@ -253,6 +257,7 @@ void nasal_codegen::vector_gen(nasal_ast& ast) int size=ast.get_children().size(); opcode op; op.op=op_newvec; + op.scope=scope_depth; op.index=0; exec_code.push_back(op); for(int i=0;i>=4; } std::cout<<"0x"<9? 'a'+tmp-10:'0'+tmp)+numinfo; + num>>=4; + } + std::cout<<"[0x"<9? 'a'+tmp-10:'0'+tmp)+numinfo; num>>=4; } - std::cout<<"0x"< > elems; + std::map 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 >::iterator i=elems.begin();i!=elems.end();++i) - for(std::map::iterator j=i->begin();j!=i->end();++j) - vm.mem_free(j->second); - elems.clear(); - return; -} -void nasal_closure::add_scope() -{ - std::map new_scope; - elems.push_back(new_scope); - return; -} -void nasal_closure::del_scope() -{ - if(this->elems.empty()) - return; - for(std::map::iterator i=elems.back().begin();i!=elems.back().end();++i) + for(std::map::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 >::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 >::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 >::iterator i=elems.begin();i!=elems.end();++i) - for(std::map::iterator j=i->begin();j!=i->end();++j) - vm.mem_free(j->second); + for(std::map::iterator i=elems.begin();i!=elems.end();++i) + vm.mem_free(i->second); elems.clear(); - for(std::list >::iterator i=tmp.elems.begin();i!=tmp.elems.end();++i) + for(std::map::iterator i=tmp.elems.begin();i!=tmp.elems.end();++i) { - this->add_scope(); - for(std::map::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; } diff --git a/nasal_runtime.h b/nasal_runtime.h index 3edb119..16e99ef 100644 --- a/nasal_runtime.h +++ b/nasal_runtime.h @@ -128,13 +128,11 @@ void nasal_runtime::run() this->function_returned_address=-1; this->global_scope_address=nasal_vm.gc_alloc(vm_closure); - nasal_vm.gc_get(global_scope_address).get_closure().add_scope(); time_t begin_time=std::time(NULL); main_progress(); time_t end_time=std::time(NULL); - nasal_vm.gc_get(global_scope_address).get_closure().del_scope(); nasal_vm.del_reference(global_scope_address); nasal_vm.clear(); @@ -224,17 +222,6 @@ void nasal_runtime::main_progress() int nasal_runtime::block_progress(nasal_ast& node,int local_scope_addr) { int ret_state=rt_exit_without_error; - // if local_scope does not exist,create a new one. - if(local_scope_addr<0) - { - local_scope_addr=nasal_vm.gc_alloc(vm_closure); - nasal_vm.gc_get(local_scope_addr).get_closure().add_scope(); - } - else - { - nasal_vm.add_reference(local_scope_addr); - nasal_vm.gc_get(local_scope_addr).get_closure().add_scope(); - } int expr_number=node.get_children().size(); int process_returned_value_addr=-1; for(int i=0;i=0) { // set hash.me @@ -966,7 +908,6 @@ int nasal_runtime::call_function(nasal_ast& node,std::string func_name,int base_ } } block_progress(reference_of_func.get_run_block(),run_closure_addr); - run_closure.del_scope(); if(function_returned_address>=0) {