This commit is contained in:
Valk Richard Li 2020-12-09 15:33:33 +08:00
parent d0206abb8d
commit 14852bfc2e
3 changed files with 139 additions and 133 deletions

View File

@ -18,16 +18,12 @@ enum op_code
op_addeq,op_subeq,op_muleq,op_diveq,op_lnkeq,op_meq, 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_eq,op_neq,op_less,op_leq,op_grt,op_geq,
op_pop, op_pop,
op_newscope,
op_delscope,
op_jmp, op_jmp,
op_jmptrue, op_jmptrue,
op_jmpfalse, op_jmpfalse,
op_jp, op_jp,
op_jtp, op_jtp,
op_jfp, op_jfp,
op_continue,
op_break,
op_forindex, // index counter on the top of forindex_stack plus 1 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_foreach, // index counter on the top of forindex_stack plus 1 and get the value in vector
op_call, // call identifier op_call, // call identifier
@ -88,16 +84,12 @@ struct
{op_grt, "g "}, {op_grt, "g "},
{op_geq, "geq "}, {op_geq, "geq "},
{op_pop, "pop "}, {op_pop, "pop "},
{op_newscope, "nscp "},
{op_delscope, "dscp "},
{op_jmp, "jmp "}, {op_jmp, "jmp "},
{op_jmptrue, "jt "}, {op_jmptrue, "jt "},
{op_jmpfalse, "jf "}, {op_jmpfalse, "jf "},
{op_jp, "jp "}, {op_jp, "jp "},
{op_jtp, "jtp "}, {op_jtp, "jtp "},
{op_jfp, "jfp "}, {op_jfp, "jfp "},
{op_continue, "contn "},
{op_break, "break "},
{op_forindex, "findx "}, {op_forindex, "findx "},
{op_foreach, "feach "}, {op_foreach, "feach "},
{op_call, "call "}, {op_call, "call "},
@ -120,8 +112,15 @@ struct
struct opcode struct opcode
{ {
unsigned char op; unsigned char op;
unsigned int scope;
unsigned int index; unsigned int index;
opcode(){op=op_nop;index=0;} opcode()
{
op=op_nop;
scope=0;
index=0;
return;
}
}; };
// unfinished // unfinished
@ -137,6 +136,7 @@ private:
std::vector<opcode> exec_code; std::vector<opcode> exec_code;
std::vector<int> continue_ptr; std::vector<int> continue_ptr;
std::vector<int> break_ptr; std::vector<int> break_ptr;
int scope_depth;
int error; int error;
void regist_number(double); void regist_number(double);
void regist_string(std::string); void regist_string(std::string);
@ -206,6 +206,7 @@ void nasal_codegen::pop_gen()
{ {
opcode op; opcode op;
op.op=op_pop; op.op=op_pop;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
return; return;
@ -215,6 +216,7 @@ void nasal_codegen::nil_gen()
{ {
opcode op; opcode op;
op.op=op_pushnil; op.op=op_pushnil;
op.scope=scope_depth;
exec_code.push_back(op); exec_code.push_back(op);
return; return;
} }
@ -233,6 +235,7 @@ void nasal_codegen::number_gen(nasal_ast& ast)
op.op=op_pushnum; op.op=op_pushnum;
op.index=number_table[num]; op.index=number_table[num];
} }
op.scope=scope_depth;
exec_code.push_back(op); exec_code.push_back(op);
return; return;
} }
@ -243,6 +246,7 @@ void nasal_codegen::string_gen(nasal_ast& ast)
regist_string(str); regist_string(str);
opcode op; opcode op;
op.op=op_pushstr; op.op=op_pushstr;
op.scope=scope_depth;
op.index=string_table[str]; op.index=string_table[str];
exec_code.push_back(op); exec_code.push_back(op);
return; return;
@ -253,6 +257,7 @@ void nasal_codegen::vector_gen(nasal_ast& ast)
int size=ast.get_children().size(); int size=ast.get_children().size();
opcode op; opcode op;
op.op=op_newvec; op.op=op_newvec;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
for(int i=0;i<size;++i) for(int i=0;i<size;++i)
@ -271,6 +276,7 @@ void nasal_codegen::hash_gen(nasal_ast& ast)
int size=ast.get_children().size(); int size=ast.get_children().size();
opcode op; opcode op;
op.op=op_newhash; op.op=op_newhash;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
for(int i=0;i<size;++i) for(int i=0;i<size;++i)
@ -278,6 +284,7 @@ void nasal_codegen::hash_gen(nasal_ast& ast)
string_gen(ast.get_children()[i].get_children()[0]); string_gen(ast.get_children()[i].get_children()[0]);
calculation_gen(ast.get_children()[i].get_children()[1]); calculation_gen(ast.get_children()[i].get_children()[1]);
op.op=op_hashapp; op.op=op_hashapp;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
@ -288,6 +295,7 @@ void nasal_codegen::function_gen(nasal_ast& ast)
{ {
opcode op; opcode op;
op.op=op_newfunc; op.op=op_newfunc;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
@ -327,9 +335,11 @@ void nasal_codegen::function_gen(nasal_ast& ast)
} }
op.op=op_entry; op.op=op_entry;
op.scope=scope_depth;
op.index=exec_code.size()+2; op.index=exec_code.size()+2;
exec_code.push_back(op); exec_code.push_back(op);
op.op=op_jmp; op.op=op_jmp;
op.scope=scope_depth;
op.index=0; op.index=0;
int ptr=exec_code.size(); int ptr=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
@ -339,9 +349,11 @@ void nasal_codegen::function_gen(nasal_ast& ast)
if(!block.get_children().size() || block.get_children().back().get_type()!=ast_return) if(!block.get_children().size() || block.get_children().back().get_type()!=ast_return)
{ {
op.op=op_pushnil; op.op=op_pushnil;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
op.op=op_return; op.op=op_return;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
@ -381,6 +393,7 @@ void nasal_codegen::call_id(nasal_ast& ast)
break; break;
} }
regist_string(str); regist_string(str);
op.scope=scope_depth;
op.index=string_table[str]; op.index=string_table[str];
exec_code.push_back(op); exec_code.push_back(op);
return; return;
@ -392,6 +405,7 @@ void nasal_codegen::call_hash(nasal_ast& ast)
op.op=op_callh; op.op=op_callh;
std::string str=ast.get_str(); std::string str=ast.get_str();
regist_string(str); regist_string(str);
op.scope=scope_depth;
op.index=string_table[str]; op.index=string_table[str];
exec_code.push_back(op); exec_code.push_back(op);
return; return;
@ -404,12 +418,14 @@ void nasal_codegen::call_vec(nasal_ast& ast)
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
opcode op; opcode op;
op.op=op_callv; op.op=op_callv;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
return; return;
} }
opcode op; opcode op;
op.op=op_slicebegin; op.op=op_slicebegin;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
int size=ast.get_children().size(); int size=ast.get_children().size();
@ -420,6 +436,7 @@ void nasal_codegen::call_vec(nasal_ast& ast)
{ {
calculation_gen(tmp); calculation_gen(tmp);
op.op=op_slice; op.op=op_slice;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
@ -428,11 +445,13 @@ void nasal_codegen::call_vec(nasal_ast& ast)
calculation_gen(tmp.get_children()[0]); calculation_gen(tmp.get_children()[0]);
calculation_gen(tmp.get_children()[1]); calculation_gen(tmp.get_children()[1]);
op.op=op_slice2; op.op=op_slice2;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
} }
op.op=op_sliceend; op.op=op_sliceend;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
return; return;
@ -444,10 +463,12 @@ void nasal_codegen::call_func(nasal_ast& ast)
{ {
opcode op; opcode op;
op.op=op_newvec; op.op=op_newvec;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
op.op=op_callf; op.op=op_callf;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
return; return;
@ -456,6 +477,7 @@ void nasal_codegen::call_func(nasal_ast& ast)
{ {
opcode op; opcode op;
op.op=op_newhash; op.op=op_newhash;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
int size=ast.get_children().size(); int size=ast.get_children().size();
@ -465,10 +487,12 @@ void nasal_codegen::call_func(nasal_ast& ast)
string_gen(tmp.get_children()[0]); string_gen(tmp.get_children()[0]);
calculation_gen(tmp.get_children()[1]); calculation_gen(tmp.get_children()[1]);
op.op=op_hashapp; op.op=op_hashapp;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
op.op=op_callf; op.op=op_callf;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
@ -476,6 +500,7 @@ void nasal_codegen::call_func(nasal_ast& ast)
{ {
opcode op; opcode op;
op.op=op_newvec; op.op=op_newvec;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
int size=ast.get_children().size(); int size=ast.get_children().size();
@ -484,10 +509,12 @@ void nasal_codegen::call_func(nasal_ast& ast)
nasal_ast& tmp=ast.get_children()[i]; nasal_ast& tmp=ast.get_children()[i];
calculation_gen(tmp); calculation_gen(tmp);
op.op=op_vecapp; op.op=op_vecapp;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
op.op=op_callf; op.op=op_callf;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
@ -518,6 +545,7 @@ void nasal_codegen::mem_call_id(nasal_ast& ast)
regist_string(str); regist_string(str);
opcode op; opcode op;
op.op=op_mcall; op.op=op_mcall;
op.scope=scope_depth;
op.index=string_table[str]; op.index=string_table[str];
exec_code.push_back(op); exec_code.push_back(op);
return; return;
@ -528,6 +556,7 @@ void nasal_codegen::mem_call_vec(nasal_ast& ast)
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
opcode op; opcode op;
op.op=op_mcallv; op.op=op_mcallv;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
return; return;
@ -539,6 +568,7 @@ void nasal_codegen::mem_call_hash(nasal_ast& ast)
regist_string(str); regist_string(str);
opcode op; opcode op;
op.op=op_mcallh; op.op=op_mcallh;
op.scope=scope_depth;
op.index=string_table[str]; op.index=string_table[str];
exec_code.push_back(op); exec_code.push_back(op);
return; return;
@ -551,6 +581,7 @@ void nasal_codegen::single_def(nasal_ast& ast)
op.op=op_load; op.op=op_load;
std::string str=ast.get_children()[0].get_str(); std::string str=ast.get_children()[0].get_str();
regist_string(str); regist_string(str);
op.scope=scope_depth;
op.index=string_table[str]; op.index=string_table[str];
exec_code.push_back(op); exec_code.push_back(op);
return; return;
@ -567,6 +598,7 @@ void nasal_codegen::multi_def(nasal_ast& ast)
op.op=op_load; op.op=op_load;
std::string str=ast.get_children()[0].get_children()[i].get_str(); std::string str=ast.get_children()[0].get_children()[i].get_str();
regist_string(str); regist_string(str);
op.scope=scope_depth;
op.index=string_table[str]; op.index=string_table[str];
exec_code.push_back(op); exec_code.push_back(op);
} }
@ -578,11 +610,13 @@ void nasal_codegen::multi_def(nasal_ast& ast)
{ {
opcode op; opcode op;
op.op=op_callvi; op.op=op_callvi;
op.scope=scope_depth;
op.index=i; op.index=i;
exec_code.push_back(op); exec_code.push_back(op);
op.op=op_load; op.op=op_load;
std::string str=ast.get_children()[0].get_children()[i].get_str(); std::string str=ast.get_children()[0].get_children()[i].get_str();
regist_string(str); regist_string(str);
op.scope=scope_depth;
op.index=string_table[str]; op.index=string_table[str];
exec_code.push_back(op); exec_code.push_back(op);
} }
@ -611,6 +645,7 @@ void nasal_codegen::multi_assignment_gen(nasal_ast& ast)
mem_call(ast.get_children()[0].get_children()[i]); mem_call(ast.get_children()[0].get_children()[i]);
opcode op; opcode op;
op.op=op_meq; op.op=op_meq;
op.scope=scope_depth;
exec_code.push_back(op); exec_code.push_back(op);
pop_gen(); pop_gen();
} }
@ -622,10 +657,12 @@ void nasal_codegen::multi_assignment_gen(nasal_ast& ast)
{ {
opcode op; opcode op;
op.op=op_callvi; op.op=op_callvi;
op.scope=scope_depth;
op.index=i; op.index=i;
exec_code.push_back(op); exec_code.push_back(op);
mem_call(ast.get_children()[0].get_children()[i]); mem_call(ast.get_children()[0].get_children()[i]);
op.op=op_meq; op.op=op_meq;
op.scope=scope_depth;
exec_code.push_back(op); exec_code.push_back(op);
pop_gen(); pop_gen();
} }
@ -645,11 +682,13 @@ void nasal_codegen::conditional_gen(nasal_ast& ast)
{ {
calculation_gen(tmp.get_children()[0]); calculation_gen(tmp.get_children()[0]);
op.op=op_jfp; op.op=op_jfp;
op.scope=scope_depth;
int ptr=exec_code.size(); int ptr=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
pop_gen(); pop_gen();
block_gen(tmp.get_children()[1]); block_gen(tmp.get_children()[1]);
op.op=op_jmp; op.op=op_jmp;
op.scope=scope_depth;
jmp_label.push_back(exec_code.size()); jmp_label.push_back(exec_code.size());
exec_code.push_back(op); exec_code.push_back(op);
exec_code[ptr].index=exec_code.size(); exec_code[ptr].index=exec_code.size();
@ -694,11 +733,13 @@ void nasal_codegen::while_gen(nasal_ast& ast)
int loop_ptr=exec_code.size(); int loop_ptr=exec_code.size();
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
op.op=op_jfp; op.op=op_jfp;
op.scope=scope_depth;
int condition_ptr=exec_code.size(); int condition_ptr=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
pop_gen(); pop_gen();
block_gen(ast.get_children()[1]); block_gen(ast.get_children()[1]);
op.op=op_jmp; op.op=op_jmp;
op.scope=scope_depth;
op.index=loop_ptr; op.index=loop_ptr;
exec_code.push_back(op); exec_code.push_back(op);
exec_code[condition_ptr].index=exec_code.size(); exec_code[condition_ptr].index=exec_code.size();
@ -709,6 +750,7 @@ void nasal_codegen::while_gen(nasal_ast& ast)
void nasal_codegen::for_gen(nasal_ast& ast) void nasal_codegen::for_gen(nasal_ast& ast)
{ {
opcode op; opcode op;
++scope_depth;
switch(ast.get_children()[0].get_type()) switch(ast.get_children()[0].get_type())
{ {
case ast_null:break; case ast_null:break;
@ -727,12 +769,14 @@ void nasal_codegen::for_gen(nasal_ast& ast)
if(ast.get_children()[1].get_type()==ast_null) if(ast.get_children()[1].get_type()==ast_null)
{ {
op.op=op_pushone; op.op=op_pushone;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
else else
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
op.op=op_jfp; op.op=op_jfp;
op.scope=scope_depth;
int label_exit=exec_code.size(); int label_exit=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
pop_gen(); pop_gen();
@ -752,10 +796,12 @@ void nasal_codegen::for_gen(nasal_ast& ast)
case ast_trinocular:calculation_gen(ast.get_children()[2]);pop_gen();break; case ast_trinocular:calculation_gen(ast.get_children()[2]);pop_gen();break;
} }
op.op=op_jmp; op.op=op_jmp;
op.scope=scope_depth;
op.index=jmp_place; op.index=jmp_place;
exec_code.push_back(op); exec_code.push_back(op);
exec_code[label_exit].index=exec_code.size(); exec_code[label_exit].index=exec_code.size();
load_continue_break(); load_continue_break();
--scope_depth;
return; return;
} }
void nasal_codegen::forindex_gen(nasal_ast& ast) void nasal_codegen::forindex_gen(nasal_ast& ast)
@ -764,6 +810,7 @@ void nasal_codegen::forindex_gen(nasal_ast& ast)
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
op.op=op_forindex; op.op=op_forindex;
op.scope=scope_depth;
op.index=0; op.index=0;
int ptr=exec_code.size(); int ptr=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
@ -772,6 +819,7 @@ void nasal_codegen::forindex_gen(nasal_ast& ast)
op.op=op_load; op.op=op_load;
std::string str=ast.get_children()[0].get_children()[0].get_str(); std::string str=ast.get_children()[0].get_children()[0].get_str();
regist_string(str); regist_string(str);
op.scope=scope_depth;
op.index=string_table[str]; op.index=string_table[str];
exec_code.push_back(op); exec_code.push_back(op);
} }
@ -779,11 +827,13 @@ void nasal_codegen::forindex_gen(nasal_ast& ast)
{ {
mem_call(ast.get_children()[0]); mem_call(ast.get_children()[0]);
op.op=op_meq; op.op=op_meq;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
block_gen(ast.get_children()[2]); block_gen(ast.get_children()[2]);
op.op=op_jmp; op.op=op_jmp;
op.scope=scope_depth;
op.index=ptr; op.index=ptr;
exec_code.push_back(op); exec_code.push_back(op);
exec_code[ptr].index=exec_code.size(); exec_code[ptr].index=exec_code.size();
@ -796,6 +846,7 @@ void nasal_codegen::foreach_gen(nasal_ast& ast)
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
op.op=op_foreach; op.op=op_foreach;
op.scope=scope_depth;
op.index=0; op.index=0;
int ptr=exec_code.size(); int ptr=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
@ -804,6 +855,7 @@ void nasal_codegen::foreach_gen(nasal_ast& ast)
op.op=op_load; op.op=op_load;
std::string str=ast.get_children()[0].get_children()[0].get_str(); std::string str=ast.get_children()[0].get_children()[0].get_str();
regist_string(str); regist_string(str);
op.scope=scope_depth;
op.index=string_table[str]; op.index=string_table[str];
exec_code.push_back(op); exec_code.push_back(op);
} }
@ -811,11 +863,13 @@ void nasal_codegen::foreach_gen(nasal_ast& ast)
{ {
mem_call(ast.get_children()[0]); mem_call(ast.get_children()[0]);
op.op=op_meq; op.op=op_meq;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
} }
block_gen(ast.get_children()[2]); block_gen(ast.get_children()[2]);
op.op=op_jmp; op.op=op_jmp;
op.scope=scope_depth;
op.index=ptr; op.index=ptr;
exec_code.push_back(op); exec_code.push_back(op);
exec_code[ptr].index=exec_code.size(); exec_code[ptr].index=exec_code.size();
@ -836,6 +890,7 @@ void nasal_codegen::or_gen(nasal_ast& ast)
int l1,l2; int l1,l2;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
op.op=op_jmptrue; op.op=op_jmptrue;
op.scope=scope_depth;
op.index=0; op.index=0;
l1=exec_code.size(); l1=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
@ -843,6 +898,7 @@ void nasal_codegen::or_gen(nasal_ast& ast)
pop_gen(); pop_gen();
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
op.op=op_jmptrue; op.op=op_jmptrue;
op.scope=scope_depth;
op.index=0; op.index=0;
l2=exec_code.size(); l2=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
@ -868,16 +924,19 @@ void nasal_codegen::and_gen(nasal_ast& ast)
opcode op; opcode op;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
op.op=op_jmptrue; op.op=op_jmptrue;
op.scope=scope_depth;
op.index=exec_code.size()+2; op.index=exec_code.size()+2;
exec_code.push_back(op); exec_code.push_back(op);
op.op=op_jmp; op.op=op_jmp;
op.scope=scope_depth;
int ptr=exec_code.size(); int ptr=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
pop_gen(); pop_gen();
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
op.op=op_jmptrue; op.op=op_jmptrue;
op.scope=scope_depth;
op.index=exec_code.size()+3; op.index=exec_code.size()+3;
exec_code.push_back(op); exec_code.push_back(op);
@ -893,11 +952,13 @@ void nasal_codegen::trino_gen(nasal_ast& ast)
opcode op; opcode op;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
op.op=op_jmpfalse; op.op=op_jmpfalse;
op.scope=scope_depth;
int ptr=exec_code.size(); int ptr=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
pop_gen(); pop_gen();
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
op.op=op_jmp; op.op=op_jmp;
op.scope=scope_depth;
int ptr_exit=exec_code.size(); int ptr_exit=exec_code.size();
exec_code.push_back(op); exec_code.push_back(op);
exec_code[ptr].index=exec_code.size(); exec_code[ptr].index=exec_code.size();
@ -923,36 +984,42 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
case ast_call:call_gen(ast);break; case ast_call:call_gen(ast);break;
case ast_equal: case ast_equal:
op.op=op_meq; op.op=op_meq;
op.scope=scope_depth;
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]); mem_call(ast.get_children()[0]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_add_equal: case ast_add_equal:
op.op=op_addeq; op.op=op_addeq;
op.scope=scope_depth;
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]); mem_call(ast.get_children()[0]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_sub_equal: case ast_sub_equal:
op.op=op_subeq; op.op=op_subeq;
op.scope=scope_depth;
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]); mem_call(ast.get_children()[0]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_mult_equal: case ast_mult_equal:
op.op=op_muleq; op.op=op_muleq;
op.scope=scope_depth;
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]); mem_call(ast.get_children()[0]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_div_equal: case ast_div_equal:
op.op=op_diveq; op.op=op_diveq;
op.scope=scope_depth;
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]); mem_call(ast.get_children()[0]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_link_equal: case ast_link_equal:
op.op=op_lnkeq; op.op=op_lnkeq;
op.scope=scope_depth;
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]); mem_call(ast.get_children()[0]);
exec_code.push_back(op); exec_code.push_back(op);
@ -961,66 +1028,77 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
case ast_and:and_gen(ast);break; case ast_and:and_gen(ast);break;
case ast_add: case ast_add:
op.op=op_add; op.op=op_add;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_sub: case ast_sub:
op.op=op_sub; op.op=op_sub;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_mult: case ast_mult:
op.op=op_mul; op.op=op_mul;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_div: case ast_div:
op.op=op_div; op.op=op_div;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_link: case ast_link:
op.op=op_lnk; op.op=op_lnk;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_cmp_equal: case ast_cmp_equal:
op.op=op_eq; op.op=op_eq;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_cmp_not_equal: case ast_cmp_not_equal:
op.op=op_neq; op.op=op_neq;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_less_equal: case ast_less_equal:
op.op=op_leq; op.op=op_leq;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_less_than: case ast_less_than:
op.op=op_less; op.op=op_less;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_greater_equal: case ast_greater_equal:
op.op=op_geq; op.op=op_geq;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_greater_than: case ast_greater_than:
op.op=op_grt; op.op=op_grt;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]); calculation_gen(ast.get_children()[1]);
exec_code.push_back(op); exec_code.push_back(op);
@ -1028,11 +1106,13 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
case ast_trinocular:trino_gen(ast);break; case ast_trinocular:trino_gen(ast);break;
case ast_unary_sub: case ast_unary_sub:
op.op=op_usub; op.op=op_usub;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_unary_not: case ast_unary_not:
op.op=op_unot; op.op=op_unot;
op.scope=scope_depth;
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
exec_code.push_back(op); exec_code.push_back(op);
break; break;
@ -1043,9 +1123,7 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
void nasal_codegen::block_gen(nasal_ast& ast) void nasal_codegen::block_gen(nasal_ast& ast)
{ {
opcode op; opcode op;
op.op=op_newscope; ++scope_depth;
op.index=0;
exec_code.push_back(op);
int size=ast.get_children().size(); int size=ast.get_children().size();
for(int i=0;i<size;++i) for(int i=0;i<size;++i)
@ -1062,13 +1140,15 @@ void nasal_codegen::block_gen(nasal_ast& ast)
case ast_multi_assign:multi_assignment_gen(tmp);break; case ast_multi_assign:multi_assignment_gen(tmp);break;
case ast_conditional:conditional_gen(tmp);break; case ast_conditional:conditional_gen(tmp);break;
case ast_continue: case ast_continue:
op.op=op_continue; op.op=op_jmp;
op.scope=scope_depth;
op.index=0; op.index=0;
continue_ptr.push_back(exec_code.size()); continue_ptr.push_back(exec_code.size());
exec_code.push_back(op); exec_code.push_back(op);
break; break;
case ast_break: case ast_break:
op.op=op_break; op.op=op_jmp;
op.scope=scope_depth;
op.index=0; op.index=0;
break_ptr.push_back(exec_code.size()); break_ptr.push_back(exec_code.size());
exec_code.push_back(op); exec_code.push_back(op);
@ -1107,9 +1187,7 @@ void nasal_codegen::block_gen(nasal_ast& ast)
} }
} }
op.op=op_delscope; --scope_depth;
op.index=0;
exec_code.push_back(op);
return; return;
} }
@ -1119,6 +1197,7 @@ void nasal_codegen::return_gen(nasal_ast& ast)
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
opcode op; opcode op;
op.op=op_return; op.op=op_return;
op.scope=scope_depth;
exec_code.push_back(op); exec_code.push_back(op);
return; return;
} }
@ -1126,6 +1205,7 @@ void nasal_codegen::return_gen(nasal_ast& ast)
void nasal_codegen::main_progress(nasal_ast& ast) void nasal_codegen::main_progress(nasal_ast& ast)
{ {
error=0; error=0;
scope_depth=0;
number_table.clear(); number_table.clear();
string_table.clear(); string_table.clear();
exec_code.clear(); exec_code.clear();
@ -1178,6 +1258,7 @@ void nasal_codegen::main_progress(nasal_ast& ast)
} }
opcode op; opcode op;
op.op=op_nop; op.op=op_nop;
op.scope=scope_depth;
op.index=0; op.index=0;
exec_code.push_back(op); exec_code.push_back(op);
@ -1201,6 +1282,15 @@ void nasal_codegen::print_op(int index)
num>>=4; num>>=4;
} }
std::cout<<"0x"<<numinfo<<": "; std::cout<<"0x"<<numinfo<<": ";
numinfo="";
num=exec_code[index].scope;
for(int i=0;i<8;++i)
{
int tmp=num&0x0f;
numinfo=(char)(tmp>9? 'a'+tmp-10:'0'+tmp)+numinfo;
num>>=4;
}
std::cout<<"[0x"<<numinfo<<"]";
for(int i=0;code_table[i].name;++i) for(int i=0;code_table[i].name;++i)
if(exec_code[index].op==code_table[i].type) if(exec_code[index].op==code_table[i].type)
{ {
@ -1215,7 +1305,7 @@ void nasal_codegen::print_op(int index)
numinfo=(char)(tmp>9? 'a'+tmp-10:'0'+tmp)+numinfo; numinfo=(char)(tmp>9? 'a'+tmp-10:'0'+tmp)+numinfo;
num>>=4; num>>=4;
} }
std::cout<<"0x"<<numinfo; std::cout<<"0x"<<numinfo<<" ";
switch(exec_code[index].op) switch(exec_code[index].op)
{ {
case op_pushnum:std::cout<<'('<<number_result_table[exec_code[index].index]<<')';break; case op_pushnum:std::cout<<'('<<number_result_table[exec_code[index].index]<<')';break;
@ -1236,6 +1326,10 @@ void nasal_codegen::print_op(int index)
void nasal_codegen::print_byte_code() void nasal_codegen::print_byte_code()
{ {
for(int i=0;i<number_result_table.size();++i)
std::cout<<".number "<<number_result_table[i]<<'\n';
for(int i=0;i<string_result_table.size();++i)
std::cout<<".symbol "<<string_result_table[i]<<'\n';
int size=exec_code.size(); int size=exec_code.size();
for(int i=0;i<size;++i) for(int i=0;i<size;++i)
print_op(i); print_op(i);

View File

@ -87,13 +87,11 @@ private:
// and this memory_manager_memory space stores an address to garbage_collector_memory // and this memory_manager_memory space stores an address to garbage_collector_memory
// and this address points to an nasal_hash // and this address points to an nasal_hash
nasal_virtual_machine& vm; nasal_virtual_machine& vm;
std::list<std::map<std::string,int> > elems; std::map<std::string,int> elems;
public: public:
nasal_closure(nasal_virtual_machine&); nasal_closure(nasal_virtual_machine&);
~nasal_closure(); ~nasal_closure();
void set_vm(nasal_virtual_machine&); void set_vm(nasal_virtual_machine&);
void add_scope();
void del_scope();
void add_new_value(std::string,int); void add_new_value(std::string,int);
int get_value_address(std::string); int get_value_address(std::string);
int get_mem_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() 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 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)
vm.mem_free(i->second); vm.mem_free(i->second);
this->elems.pop_back(); elems.clear();
return; return;
} }
void nasal_closure::add_new_value(std::string key,int value_address) void nasal_closure::add_new_value(std::string key,int value_address)
{ {
int new_mem_address=vm.mem_alloc(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 // 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); vm.mem_free(old_mem_address);
} }
elems.back()[key]=new_mem_address; elems[key]=new_mem_address;
return; return;
} }
int nasal_closure::get_value_address(std::string key) int nasal_closure::get_value_address(std::string key)
{ {
int ret_address=-1; int ret_address=-1;
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i) if(elems.find(key)!=elems.end())
{ ret_address=vm.mem_get(elems[key]);
if(i->find(key)!=i->end())
ret_address=vm.mem_get((*i)[key]);
}
return ret_address; return ret_address;
} }
int nasal_closure::get_mem_address(std::string key) int nasal_closure::get_mem_address(std::string key)
{ {
int ret_address=-1; int ret_address=-1;
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i) if(elems.find(key)!=elems.end())
{ ret_address=elems[key];
if(i->find(key)!=i->end())
ret_address=(*i)[key];
}
return ret_address; return ret_address;
} }
void nasal_closure::set_closure(nasal_closure& tmp) 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 i=elems.begin();i!=elems.end();++i)
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j) vm.mem_free(i->second);
vm.mem_free(j->second);
elems.clear(); 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(); int value_addr=vm.mem_get(i->second);
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j) int new_mem_addr=vm.mem_alloc(value_addr);
{ vm.add_reference(value_addr);
int value_addr=vm.mem_get(j->second); elems[i->first]=new_mem_addr;
int new_mem_addr=vm.mem_alloc(value_addr);
vm.add_reference(value_addr);
elems.back()[j->first]=new_mem_addr;
}
} }
return; return;
} }

View File

@ -128,13 +128,11 @@ void nasal_runtime::run()
this->function_returned_address=-1; this->function_returned_address=-1;
this->global_scope_address=nasal_vm.gc_alloc(vm_closure); 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); time_t begin_time=std::time(NULL);
main_progress(); main_progress();
time_t end_time=std::time(NULL); 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.del_reference(global_scope_address);
nasal_vm.clear(); 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 nasal_runtime::block_progress(nasal_ast& node,int local_scope_addr)
{ {
int ret_state=rt_exit_without_error; 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 expr_number=node.get_children().size();
int process_returned_value_addr=-1; int process_returned_value_addr=-1;
for(int i=0;i<expr_number;++i) for(int i=0;i<expr_number;++i)
@ -275,8 +262,6 @@ int nasal_runtime::block_progress(nasal_ast& node,int local_scope_addr)
if(error || ret_state==rt_break || ret_state==rt_continue || ret_state==rt_return) if(error || ret_state==rt_break || ret_state==rt_continue || ret_state==rt_return)
break; break;
} }
nasal_vm.gc_get(local_scope_addr).get_closure().del_scope();
nasal_vm.del_reference(local_scope_addr);
return ret_state; return ret_state;
} }
void nasal_runtime::before_for_loop(nasal_ast& node,int local_scope_addr) void nasal_runtime::before_for_loop(nasal_ast& node,int local_scope_addr)
@ -324,20 +309,8 @@ int nasal_runtime::loop_progress(nasal_ast& node,int local_scope_addr)
{ {
nasal_ast& condition_node=node.get_children()[0]; nasal_ast& condition_node=node.get_children()[0];
nasal_ast& run_block_node=node.get_children()[1]; nasal_ast& run_block_node=node.get_children()[1];
// create a new local scope to store iterator if local_scope_addr=-1
int while_local_scope_addr=local_scope_addr;
if(while_local_scope_addr<0)
{
while_local_scope_addr=nasal_vm.gc_alloc(vm_closure);
nasal_vm.gc_get(while_local_scope_addr).get_closure().add_scope();
}
else
{
nasal_vm.add_reference(local_scope_addr);
nasal_vm.gc_get(while_local_scope_addr).get_closure().add_scope();
}
// check condition and begin loop // check condition and begin loop
int condition_value_addr=calculation(condition_node,while_local_scope_addr); int condition_value_addr=calculation(condition_node,local_scope_addr);
bool result=check_condition(condition_value_addr); bool result=check_condition(condition_value_addr);
nasal_vm.del_reference(condition_value_addr); nasal_vm.del_reference(condition_value_addr);
while(result) while(result)
@ -346,12 +319,10 @@ int nasal_runtime::loop_progress(nasal_ast& node,int local_scope_addr)
ret_state=block_progress(run_block_node,local_scope_addr); ret_state=block_progress(run_block_node,local_scope_addr);
if(ret_state==rt_break || ret_state==rt_return || error) if(ret_state==rt_break || ret_state==rt_return || error)
break; break;
condition_value_addr=calculation(condition_node,while_local_scope_addr); condition_value_addr=calculation(condition_node,local_scope_addr);
result=check_condition(condition_value_addr); result=check_condition(condition_value_addr);
nasal_vm.del_reference(condition_value_addr); nasal_vm.del_reference(condition_value_addr);
} }
nasal_vm.gc_get(while_local_scope_addr).get_closure().del_scope();
nasal_vm.del_reference(while_local_scope_addr);
} }
else if(loop_type==ast_forindex || loop_type==ast_foreach) else if(loop_type==ast_forindex || loop_type==ast_foreach)
{ {
@ -365,26 +336,14 @@ int nasal_runtime::loop_progress(nasal_ast& node,int local_scope_addr)
die(vector_node.get_line(),"must use vector in forindex/foreach"); die(vector_node.get_line(),"must use vector in forindex/foreach");
return rt_error; return rt_error;
} }
// create a new local scope to store iterator if local_scope_addr=-1
int forei_local_scope_addr=local_scope_addr;
if(forei_local_scope_addr<0)
{
forei_local_scope_addr=nasal_vm.gc_alloc(vm_closure);
nasal_vm.gc_get(forei_local_scope_addr).get_closure().add_scope();
}
else
{
nasal_vm.add_reference(local_scope_addr);
nasal_vm.gc_get(forei_local_scope_addr).get_closure().add_scope();
}
// begin loop progress // begin loop progress
int mem_addr=-1; int mem_addr=-1;
if(iter_node.get_type()==ast_new_iter) if(iter_node.get_type()==ast_new_iter)
{ {
int new_value_addr=nasal_vm.gc_alloc(vm_nil); int new_value_addr=nasal_vm.gc_alloc(vm_nil);
std::string val_name=iter_node.get_children()[0].get_str(); std::string val_name=iter_node.get_children()[0].get_str();
nasal_vm.gc_get(forei_local_scope_addr).get_closure().add_new_value(val_name,new_value_addr); nasal_vm.gc_get(local_scope_addr<0? global_scope_address:local_scope_addr).get_closure().add_new_value(val_name,new_value_addr);
mem_addr=nasal_vm.gc_get(forei_local_scope_addr).get_closure().get_mem_address(val_name); mem_addr=nasal_vm.gc_get(local_scope_addr<0? global_scope_address:local_scope_addr).get_closure().get_mem_address(val_name);
} }
else else
mem_addr=call_scalar_mem(iter_node,local_scope_addr); mem_addr=call_scalar_mem(iter_node,local_scope_addr);
@ -410,13 +369,11 @@ int nasal_runtime::loop_progress(nasal_ast& node,int local_scope_addr)
nasal_vm.add_reference(value_addr); nasal_vm.add_reference(value_addr);
nasal_vm.mem_change(mem_addr,value_addr); nasal_vm.mem_change(mem_addr,value_addr);
} }
ret_state=block_progress(run_block_node,forei_local_scope_addr); ret_state=block_progress(run_block_node,local_scope_addr);
if(ret_state==rt_break || ret_state==rt_return || error) if(ret_state==rt_break || ret_state==rt_return || error)
break; break;
} }
nasal_vm.del_reference(vector_value_addr); nasal_vm.del_reference(vector_value_addr);
nasal_vm.gc_get(forei_local_scope_addr).get_closure().del_scope();
nasal_vm.del_reference(forei_local_scope_addr);
} }
else if(loop_type==ast_for) else if(loop_type==ast_for)
{ {
@ -425,37 +382,23 @@ int nasal_runtime::loop_progress(nasal_ast& node,int local_scope_addr)
nasal_ast& each_loop_do_node=node.get_children()[2]; nasal_ast& each_loop_do_node=node.get_children()[2];
nasal_ast& run_block_node=node.get_children()[3]; nasal_ast& run_block_node=node.get_children()[3];
// set local scope if local_scope_addr=-1
int for_local_scope_addr=local_scope_addr;
if(for_local_scope_addr<0)
{
for_local_scope_addr=nasal_vm.gc_alloc(vm_closure);
nasal_vm.gc_get(for_local_scope_addr).get_closure().add_scope();
}
else
{
nasal_vm.add_reference(local_scope_addr);
nasal_vm.gc_get(for_local_scope_addr).get_closure().add_scope();
}
// for progress // for progress
before_for_loop(before_loop_node,for_local_scope_addr); before_for_loop(before_loop_node,local_scope_addr);
int condition_value_addr=calculation(condition_node,for_local_scope_addr); int condition_value_addr=calculation(condition_node,local_scope_addr);
bool result=check_condition(condition_value_addr); bool result=check_condition(condition_value_addr);
nasal_vm.del_reference(condition_value_addr); nasal_vm.del_reference(condition_value_addr);
while(result) while(result)
{ {
if(error) if(error)
break; break;
ret_state=block_progress(run_block_node,for_local_scope_addr); ret_state=block_progress(run_block_node,local_scope_addr);
if(ret_state==rt_return || ret_state==rt_break || error) if(ret_state==rt_return || ret_state==rt_break || error)
break; break;
after_each_for_loop(each_loop_do_node,for_local_scope_addr); after_each_for_loop(each_loop_do_node,local_scope_addr);
condition_value_addr=calculation(condition_node,for_local_scope_addr); condition_value_addr=calculation(condition_node,local_scope_addr);
result=check_condition(condition_value_addr); result=check_condition(condition_value_addr);
nasal_vm.del_reference(condition_value_addr); nasal_vm.del_reference(condition_value_addr);
} }
nasal_vm.gc_get(for_local_scope_addr).get_closure().del_scope();
nasal_vm.del_reference(for_local_scope_addr);
} }
if(ret_state==rt_break || ret_state==rt_continue) if(ret_state==rt_break || ret_state==rt_continue)
ret_state=rt_exit_without_error; ret_state=rt_exit_without_error;
@ -803,7 +746,6 @@ int nasal_runtime::call_function(nasal_ast& node,std::string func_name,int base_
nasal_function& reference_of_func=nasal_vm.gc_get(base_value_addr).get_func(); nasal_function& reference_of_func=nasal_vm.gc_get(base_value_addr).get_func();
int run_closure_addr=reference_of_func.get_closure_addr(); int run_closure_addr=reference_of_func.get_closure_addr();
nasal_closure& run_closure=nasal_vm.gc_get(run_closure_addr).get_closure(); nasal_closure& run_closure=nasal_vm.gc_get(run_closure_addr).get_closure();
run_closure.add_scope();
if(last_call_hash_addr>=0) if(last_call_hash_addr>=0)
{ {
// set hash.me // 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); block_progress(reference_of_func.get_run_block(),run_closure_addr);
run_closure.del_scope();
if(function_returned_address>=0) if(function_returned_address>=0)
{ {