bug fixed
This commit is contained in:
parent
14852bfc2e
commit
7f6a521ad7
105
nasal_codegen.h
105
nasal_codegen.h
|
@ -112,12 +112,10 @@ struct
|
|||
struct opcode
|
||||
{
|
||||
unsigned char op;
|
||||
unsigned int scope;
|
||||
unsigned int index;
|
||||
opcode()
|
||||
{
|
||||
op=op_nop;
|
||||
scope=0;
|
||||
index=0;
|
||||
return;
|
||||
}
|
||||
|
@ -136,7 +134,6 @@ private:
|
|||
std::vector<opcode> exec_code;
|
||||
std::vector<int> continue_ptr;
|
||||
std::vector<int> break_ptr;
|
||||
int scope_depth;
|
||||
int error;
|
||||
void regist_number(double);
|
||||
void regist_string(std::string);
|
||||
|
@ -206,7 +203,6 @@ void nasal_codegen::pop_gen()
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_pop;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -216,7 +212,6 @@ void nasal_codegen::nil_gen()
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_pushnil;
|
||||
op.scope=scope_depth;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
}
|
||||
|
@ -235,7 +230,6 @@ 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;
|
||||
}
|
||||
|
@ -246,7 +240,6 @@ 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;
|
||||
|
@ -257,7 +250,6 @@ 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<size;++i)
|
||||
|
@ -276,7 +268,6 @@ void nasal_codegen::hash_gen(nasal_ast& ast)
|
|||
int size=ast.get_children().size();
|
||||
opcode op;
|
||||
op.op=op_newhash;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
for(int i=0;i<size;++i)
|
||||
|
@ -284,7 +275,6 @@ void nasal_codegen::hash_gen(nasal_ast& ast)
|
|||
string_gen(ast.get_children()[i].get_children()[0]);
|
||||
calculation_gen(ast.get_children()[i].get_children()[1]);
|
||||
op.op=op_hashapp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -295,7 +285,6 @@ void nasal_codegen::function_gen(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_newfunc;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
|
||||
|
@ -335,11 +324,9 @@ void nasal_codegen::function_gen(nasal_ast& ast)
|
|||
}
|
||||
|
||||
op.op=op_entry;
|
||||
op.scope=scope_depth;
|
||||
op.index=exec_code.size()+2;
|
||||
exec_code.push_back(op);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
@ -349,11 +336,9 @@ void nasal_codegen::function_gen(nasal_ast& ast)
|
|||
if(!block.get_children().size() || block.get_children().back().get_type()!=ast_return)
|
||||
{
|
||||
op.op=op_pushnil;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
op.op=op_return;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -393,7 +378,6 @@ void nasal_codegen::call_id(nasal_ast& ast)
|
|||
break;
|
||||
}
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -405,7 +389,6 @@ void nasal_codegen::call_hash(nasal_ast& ast)
|
|||
op.op=op_callh;
|
||||
std::string str=ast.get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -418,14 +401,12 @@ void nasal_codegen::call_vec(nasal_ast& ast)
|
|||
calculation_gen(ast.get_children()[0]);
|
||||
opcode op;
|
||||
op.op=op_callv;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
}
|
||||
opcode op;
|
||||
op.op=op_slicebegin;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
int size=ast.get_children().size();
|
||||
|
@ -436,7 +417,6 @@ void nasal_codegen::call_vec(nasal_ast& ast)
|
|||
{
|
||||
calculation_gen(tmp);
|
||||
op.op=op_slice;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -445,13 +425,11 @@ void nasal_codegen::call_vec(nasal_ast& ast)
|
|||
calculation_gen(tmp.get_children()[0]);
|
||||
calculation_gen(tmp.get_children()[1]);
|
||||
op.op=op_slice2;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
}
|
||||
op.op=op_sliceend;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -463,12 +441,10 @@ void nasal_codegen::call_func(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_newvec;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
|
||||
op.op=op_callf;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -477,7 +453,6 @@ void nasal_codegen::call_func(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_newhash;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
int size=ast.get_children().size();
|
||||
|
@ -487,12 +462,10 @@ void nasal_codegen::call_func(nasal_ast& ast)
|
|||
string_gen(tmp.get_children()[0]);
|
||||
calculation_gen(tmp.get_children()[1]);
|
||||
op.op=op_hashapp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
op.op=op_callf;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -500,7 +473,6 @@ void nasal_codegen::call_func(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_newvec;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
int size=ast.get_children().size();
|
||||
|
@ -509,12 +481,10 @@ void nasal_codegen::call_func(nasal_ast& ast)
|
|||
nasal_ast& tmp=ast.get_children()[i];
|
||||
calculation_gen(tmp);
|
||||
op.op=op_vecapp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
op.op=op_callf;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -545,7 +515,6 @@ void nasal_codegen::mem_call_id(nasal_ast& ast)
|
|||
regist_string(str);
|
||||
opcode op;
|
||||
op.op=op_mcall;
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -556,7 +525,6 @@ void nasal_codegen::mem_call_vec(nasal_ast& ast)
|
|||
calculation_gen(ast.get_children()[0]);
|
||||
opcode op;
|
||||
op.op=op_mcallv;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -568,7 +536,6 @@ void nasal_codegen::mem_call_hash(nasal_ast& ast)
|
|||
regist_string(str);
|
||||
opcode op;
|
||||
op.op=op_mcallh;
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -581,7 +548,6 @@ void nasal_codegen::single_def(nasal_ast& ast)
|
|||
op.op=op_load;
|
||||
std::string str=ast.get_children()[0].get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -598,7 +564,6 @@ void nasal_codegen::multi_def(nasal_ast& ast)
|
|||
op.op=op_load;
|
||||
std::string str=ast.get_children()[0].get_children()[i].get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -610,13 +575,11 @@ void nasal_codegen::multi_def(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_callvi;
|
||||
op.scope=scope_depth;
|
||||
op.index=i;
|
||||
exec_code.push_back(op);
|
||||
op.op=op_load;
|
||||
std::string str=ast.get_children()[0].get_children()[i].get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -645,7 +608,6 @@ void nasal_codegen::multi_assignment_gen(nasal_ast& ast)
|
|||
mem_call(ast.get_children()[0].get_children()[i]);
|
||||
opcode op;
|
||||
op.op=op_meq;
|
||||
op.scope=scope_depth;
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
}
|
||||
|
@ -657,12 +619,10 @@ void nasal_codegen::multi_assignment_gen(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_callvi;
|
||||
op.scope=scope_depth;
|
||||
op.index=i;
|
||||
exec_code.push_back(op);
|
||||
mem_call(ast.get_children()[0].get_children()[i]);
|
||||
op.op=op_meq;
|
||||
op.scope=scope_depth;
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
}
|
||||
|
@ -682,13 +642,11 @@ void nasal_codegen::conditional_gen(nasal_ast& ast)
|
|||
{
|
||||
calculation_gen(tmp.get_children()[0]);
|
||||
op.op=op_jfp;
|
||||
op.scope=scope_depth;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
block_gen(tmp.get_children()[1]);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
jmp_label.push_back(exec_code.size());
|
||||
exec_code.push_back(op);
|
||||
exec_code[ptr].index=exec_code.size();
|
||||
|
@ -733,13 +691,11 @@ void nasal_codegen::while_gen(nasal_ast& ast)
|
|||
int loop_ptr=exec_code.size();
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
op.op=op_jfp;
|
||||
op.scope=scope_depth;
|
||||
int condition_ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
block_gen(ast.get_children()[1]);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=loop_ptr;
|
||||
exec_code.push_back(op);
|
||||
exec_code[condition_ptr].index=exec_code.size();
|
||||
|
@ -750,7 +706,6 @@ void nasal_codegen::while_gen(nasal_ast& ast)
|
|||
void nasal_codegen::for_gen(nasal_ast& ast)
|
||||
{
|
||||
opcode op;
|
||||
++scope_depth;
|
||||
switch(ast.get_children()[0].get_type())
|
||||
{
|
||||
case ast_null:break;
|
||||
|
@ -769,14 +724,12 @@ void nasal_codegen::for_gen(nasal_ast& ast)
|
|||
if(ast.get_children()[1].get_type()==ast_null)
|
||||
{
|
||||
op.op=op_pushone;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
else
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
op.op=op_jfp;
|
||||
op.scope=scope_depth;
|
||||
int label_exit=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
|
@ -796,12 +749,10 @@ void nasal_codegen::for_gen(nasal_ast& ast)
|
|||
case ast_trinocular:calculation_gen(ast.get_children()[2]);pop_gen();break;
|
||||
}
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=jmp_place;
|
||||
exec_code.push_back(op);
|
||||
exec_code[label_exit].index=exec_code.size();
|
||||
load_continue_break();
|
||||
--scope_depth;
|
||||
return;
|
||||
}
|
||||
void nasal_codegen::forindex_gen(nasal_ast& ast)
|
||||
|
@ -810,7 +761,6 @@ void nasal_codegen::forindex_gen(nasal_ast& ast)
|
|||
calculation_gen(ast.get_children()[1]);
|
||||
|
||||
op.op=op_forindex;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
@ -819,7 +769,6 @@ void nasal_codegen::forindex_gen(nasal_ast& ast)
|
|||
op.op=op_load;
|
||||
std::string str=ast.get_children()[0].get_children()[0].get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -827,13 +776,11 @@ void nasal_codegen::forindex_gen(nasal_ast& ast)
|
|||
{
|
||||
mem_call(ast.get_children()[0]);
|
||||
op.op=op_meq;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
block_gen(ast.get_children()[2]);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=ptr;
|
||||
exec_code.push_back(op);
|
||||
exec_code[ptr].index=exec_code.size();
|
||||
|
@ -846,7 +793,6 @@ void nasal_codegen::foreach_gen(nasal_ast& ast)
|
|||
calculation_gen(ast.get_children()[1]);
|
||||
|
||||
op.op=op_foreach;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
@ -855,7 +801,6 @@ void nasal_codegen::foreach_gen(nasal_ast& ast)
|
|||
op.op=op_load;
|
||||
std::string str=ast.get_children()[0].get_children()[0].get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -863,13 +808,11 @@ void nasal_codegen::foreach_gen(nasal_ast& ast)
|
|||
{
|
||||
mem_call(ast.get_children()[0]);
|
||||
op.op=op_meq;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
block_gen(ast.get_children()[2]);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=ptr;
|
||||
exec_code.push_back(op);
|
||||
exec_code[ptr].index=exec_code.size();
|
||||
|
@ -890,7 +833,6 @@ void nasal_codegen::or_gen(nasal_ast& ast)
|
|||
int l1,l2;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
op.op=op_jmptrue;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
l1=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
@ -898,7 +840,6 @@ void nasal_codegen::or_gen(nasal_ast& ast)
|
|||
pop_gen();
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
op.op=op_jmptrue;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
l2=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
@ -924,19 +865,16 @@ void nasal_codegen::and_gen(nasal_ast& ast)
|
|||
opcode op;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
op.op=op_jmptrue;
|
||||
op.scope=scope_depth;
|
||||
op.index=exec_code.size()+2;
|
||||
exec_code.push_back(op);
|
||||
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
||||
pop_gen();
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
op.op=op_jmptrue;
|
||||
op.scope=scope_depth;
|
||||
op.index=exec_code.size()+3;
|
||||
exec_code.push_back(op);
|
||||
|
||||
|
@ -952,13 +890,11 @@ void nasal_codegen::trino_gen(nasal_ast& ast)
|
|||
opcode op;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
op.op=op_jmpfalse;
|
||||
op.scope=scope_depth;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
int ptr_exit=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
exec_code[ptr].index=exec_code.size();
|
||||
|
@ -984,42 +920,36 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
|
|||
case ast_call:call_gen(ast);break;
|
||||
case ast_equal:
|
||||
op.op=op_meq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_add_equal:
|
||||
op.op=op_addeq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_sub_equal:
|
||||
op.op=op_subeq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_mult_equal:
|
||||
op.op=op_muleq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_div_equal:
|
||||
op.op=op_diveq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_link_equal:
|
||||
op.op=op_lnkeq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
|
@ -1028,77 +958,66 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
|
|||
case ast_and:and_gen(ast);break;
|
||||
case ast_add:
|
||||
op.op=op_add;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_sub:
|
||||
op.op=op_sub;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_mult:
|
||||
op.op=op_mul;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_div:
|
||||
op.op=op_div;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_link:
|
||||
op.op=op_lnk;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_cmp_equal:
|
||||
op.op=op_eq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_cmp_not_equal:
|
||||
op.op=op_neq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_less_equal:
|
||||
op.op=op_leq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_less_than:
|
||||
op.op=op_less;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_greater_equal:
|
||||
op.op=op_geq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_greater_than:
|
||||
op.op=op_grt;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
|
@ -1106,13 +1025,11 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
|
|||
case ast_trinocular:trino_gen(ast);break;
|
||||
case ast_unary_sub:
|
||||
op.op=op_usub;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_unary_not:
|
||||
op.op=op_unot;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
|
@ -1123,8 +1040,6 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
|
|||
void nasal_codegen::block_gen(nasal_ast& ast)
|
||||
{
|
||||
opcode op;
|
||||
++scope_depth;
|
||||
|
||||
int size=ast.get_children().size();
|
||||
for(int i=0;i<size;++i)
|
||||
{
|
||||
|
@ -1141,14 +1056,12 @@ void nasal_codegen::block_gen(nasal_ast& ast)
|
|||
case ast_conditional:conditional_gen(tmp);break;
|
||||
case ast_continue:
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
continue_ptr.push_back(exec_code.size());
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_break:
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
break_ptr.push_back(exec_code.size());
|
||||
exec_code.push_back(op);
|
||||
|
@ -1186,8 +1099,6 @@ void nasal_codegen::block_gen(nasal_ast& ast)
|
|||
case ast_return:return_gen(tmp);break;
|
||||
}
|
||||
}
|
||||
|
||||
--scope_depth;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1197,7 +1108,6 @@ void nasal_codegen::return_gen(nasal_ast& ast)
|
|||
calculation_gen(ast.get_children()[0]);
|
||||
opcode op;
|
||||
op.op=op_return;
|
||||
op.scope=scope_depth;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
}
|
||||
|
@ -1205,7 +1115,6 @@ void nasal_codegen::return_gen(nasal_ast& ast)
|
|||
void nasal_codegen::main_progress(nasal_ast& ast)
|
||||
{
|
||||
error=0;
|
||||
scope_depth=0;
|
||||
number_table.clear();
|
||||
string_table.clear();
|
||||
exec_code.clear();
|
||||
|
@ -1258,7 +1167,6 @@ void nasal_codegen::main_progress(nasal_ast& ast)
|
|||
}
|
||||
opcode op;
|
||||
op.op=op_nop;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
|
||||
|
@ -1273,6 +1181,7 @@ void nasal_codegen::main_progress(nasal_ast& ast)
|
|||
|
||||
void nasal_codegen::print_op(int index)
|
||||
{
|
||||
// print opcode ptr
|
||||
std::string numinfo="";
|
||||
int num=index;
|
||||
for(int i=0;i<8;++i)
|
||||
|
@ -1282,21 +1191,14 @@ void nasal_codegen::print_op(int index)
|
|||
num>>=4;
|
||||
}
|
||||
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<<"]";
|
||||
// print opcode name
|
||||
for(int i=0;code_table[i].name;++i)
|
||||
if(exec_code[index].op==code_table[i].type)
|
||||
{
|
||||
std::cout<<code_table[i].name<<" ";
|
||||
break;
|
||||
}
|
||||
// print opcode index
|
||||
numinfo="";
|
||||
num=exec_code[index].index;
|
||||
for(int i=0;i<8;++i)
|
||||
|
@ -1306,6 +1208,7 @@ void nasal_codegen::print_op(int index)
|
|||
num>>=4;
|
||||
}
|
||||
std::cout<<"0x"<<numinfo<<" ";
|
||||
// print detail info
|
||||
switch(exec_code[index].op)
|
||||
{
|
||||
case op_pushnum:std::cout<<'('<<number_result_table[exec_code[index].index]<<')';break;
|
||||
|
|
75
nasal_gc.h
75
nasal_gc.h
|
@ -31,7 +31,6 @@ private:
|
|||
public:
|
||||
nasal_vector(nasal_virtual_machine&);
|
||||
~nasal_vector();
|
||||
void set_vm(nasal_virtual_machine&);
|
||||
void add_elem(int);
|
||||
int del_elem();
|
||||
int size();
|
||||
|
@ -49,7 +48,6 @@ private:
|
|||
public:
|
||||
nasal_hash(nasal_virtual_machine&);
|
||||
~nasal_hash();
|
||||
void set_vm(nasal_virtual_machine&);
|
||||
void add_elem(std::string,int);
|
||||
void del_elem(std::string);
|
||||
int size();
|
||||
|
@ -71,7 +69,6 @@ private:
|
|||
public:
|
||||
nasal_function(nasal_virtual_machine&);
|
||||
~nasal_function();
|
||||
void set_vm(nasal_virtual_machine&);
|
||||
void set_closure_addr(int);
|
||||
int get_closure_addr();
|
||||
void set_arguments(nasal_ast&);
|
||||
|
@ -87,11 +84,12 @@ 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::map<std::string,int> elems;
|
||||
std::list<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);
|
||||
|
@ -167,11 +165,6 @@ nasal_vector::~nasal_vector()
|
|||
elems.clear();
|
||||
return;
|
||||
}
|
||||
void nasal_vector::set_vm(nasal_virtual_machine& nvm)
|
||||
{
|
||||
vm=nvm;
|
||||
return;
|
||||
}
|
||||
void nasal_vector::add_elem(int value_address)
|
||||
{
|
||||
int memory_address=vm.mem_alloc(value_address);
|
||||
|
@ -252,11 +245,6 @@ nasal_hash::~nasal_hash()
|
|||
elems.clear();
|
||||
return;
|
||||
}
|
||||
void nasal_hash::set_vm(nasal_virtual_machine& nvm)
|
||||
{
|
||||
vm=nvm;
|
||||
return;
|
||||
}
|
||||
void nasal_hash::add_elem(std::string key,int value_address)
|
||||
{
|
||||
if(elems.find(key)==elems.end())
|
||||
|
@ -442,52 +430,77 @@ nasal_ast& nasal_function::get_run_block()
|
|||
/*functions of nasal_closure*/
|
||||
nasal_closure::nasal_closure(nasal_virtual_machine& nvm):vm(nvm)
|
||||
{
|
||||
std::map<std::string,int> new_scope;
|
||||
elems.push_back(new_scope);
|
||||
return;
|
||||
}
|
||||
nasal_closure::~nasal_closure()
|
||||
{
|
||||
for(std::map<std::string,int>::iterator i=elems.begin();i!=elems.end();++i)
|
||||
vm.mem_free(i->second);
|
||||
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()
|
||||
{
|
||||
std::map<std::string,int>& last_scope=elems.back();
|
||||
for(std::map<std::string,int>::iterator i=last_scope.begin();i!=last_scope.end();++i)
|
||||
vm.mem_free(i->second);
|
||||
elems.pop_back();
|
||||
return;
|
||||
}
|
||||
void nasal_closure::add_new_value(std::string key,int value_address)
|
||||
{
|
||||
int new_mem_address=vm.mem_alloc(value_address);
|
||||
if(elems.find(key)!=elems.end())
|
||||
if(elems.back().find(key)!=elems.back().end())
|
||||
{
|
||||
// if this value already exists,delete the old value and update a new value
|
||||
int old_mem_address=elems[key];
|
||||
int old_mem_address=elems.back()[key];
|
||||
vm.mem_free(old_mem_address);
|
||||
}
|
||||
elems[key]=new_mem_address;
|
||||
elems.back()[key]=new_mem_address;
|
||||
return;
|
||||
}
|
||||
int nasal_closure::get_value_address(std::string key)
|
||||
{
|
||||
int ret_address=-1;
|
||||
if(elems.find(key)!=elems.end())
|
||||
ret_address=vm.mem_get(elems[key]);
|
||||
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]);
|
||||
return ret_address;
|
||||
}
|
||||
int nasal_closure::get_mem_address(std::string key)
|
||||
{
|
||||
int ret_address=-1;
|
||||
if(elems.find(key)!=elems.end())
|
||||
ret_address=elems[key];
|
||||
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];
|
||||
return ret_address;
|
||||
}
|
||||
void nasal_closure::set_closure(nasal_closure& tmp)
|
||||
{
|
||||
for(std::map<std::string,int>::iterator i=elems.begin();i!=elems.end();++i)
|
||||
vm.mem_free(i->second);
|
||||
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();
|
||||
for(std::map<std::string,int>::iterator i=tmp.elems.begin();i!=tmp.elems.end();++i)
|
||||
for(std::list<std::map<std::string,int> >::iterator i=tmp.elems.begin();i!=tmp.elems.end();++i)
|
||||
{
|
||||
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;
|
||||
std::map<std::string,int> new_scope;
|
||||
elems.push_back(new_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;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -746,6 +746,7 @@ 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();
|
||||
int run_closure_addr=reference_of_func.get_closure_addr();
|
||||
nasal_closure& run_closure=nasal_vm.gc_get(run_closure_addr).get_closure();
|
||||
run_closure.add_scope();
|
||||
if(last_call_hash_addr>=0)
|
||||
{
|
||||
// set hash.me
|
||||
|
@ -908,7 +909,7 @@ 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)
|
||||
{
|
||||
ret_value_addr=function_returned_address;
|
||||
|
|
Loading…
Reference in New Issue