forked from xxq250/Nasal-Interpreter
bug fixed
This commit is contained in:
+4
-101
@@ -112,12 +112,10 @@ struct
|
|||||||
struct opcode
|
struct opcode
|
||||||
{
|
{
|
||||||
unsigned char op;
|
unsigned char op;
|
||||||
unsigned int scope;
|
|
||||||
unsigned int index;
|
unsigned int index;
|
||||||
opcode()
|
opcode()
|
||||||
{
|
{
|
||||||
op=op_nop;
|
op=op_nop;
|
||||||
scope=0;
|
|
||||||
index=0;
|
index=0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -136,7 +134,6 @@ 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,7 +203,6 @@ 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;
|
||||||
@@ -216,7 +212,6 @@ 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;
|
||||||
}
|
}
|
||||||
@@ -235,7 +230,6 @@ 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;
|
||||||
}
|
}
|
||||||
@@ -246,7 +240,6 @@ 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;
|
||||||
@@ -257,7 +250,6 @@ 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)
|
||||||
@@ -276,7 +268,6 @@ 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)
|
||||||
@@ -284,7 +275,6 @@ 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);
|
||||||
}
|
}
|
||||||
@@ -295,7 +285,6 @@ 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);
|
||||||
|
|
||||||
@@ -335,11 +324,9 @@ 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);
|
||||||
@@ -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)
|
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);
|
||||||
}
|
}
|
||||||
@@ -393,7 +378,6 @@ 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;
|
||||||
@@ -405,7 +389,6 @@ 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;
|
||||||
@@ -418,14 +401,12 @@ 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();
|
||||||
@@ -436,7 +417,6 @@ 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);
|
||||||
}
|
}
|
||||||
@@ -445,13 +425,11 @@ 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;
|
||||||
@@ -463,12 +441,10 @@ 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;
|
||||||
@@ -477,7 +453,6 @@ 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();
|
||||||
@@ -487,12 +462,10 @@ 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);
|
||||||
}
|
}
|
||||||
@@ -500,7 +473,6 @@ 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();
|
||||||
@@ -509,12 +481,10 @@ 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);
|
||||||
}
|
}
|
||||||
@@ -545,7 +515,6 @@ 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;
|
||||||
@@ -556,7 +525,6 @@ 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;
|
||||||
@@ -568,7 +536,6 @@ 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;
|
||||||
@@ -581,7 +548,6 @@ 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;
|
||||||
@@ -598,7 +564,6 @@ 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);
|
||||||
}
|
}
|
||||||
@@ -610,13 +575,11 @@ 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);
|
||||||
}
|
}
|
||||||
@@ -645,7 +608,6 @@ 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();
|
||||||
}
|
}
|
||||||
@@ -657,12 +619,10 @@ 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();
|
||||||
}
|
}
|
||||||
@@ -682,13 +642,11 @@ 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();
|
||||||
@@ -733,13 +691,11 @@ 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();
|
||||||
@@ -750,7 +706,6 @@ 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;
|
||||||
@@ -769,14 +724,12 @@ 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();
|
||||||
@@ -796,12 +749,10 @@ 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)
|
||||||
@@ -810,7 +761,6 @@ 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);
|
||||||
@@ -819,7 +769,6 @@ 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);
|
||||||
}
|
}
|
||||||
@@ -827,13 +776,11 @@ 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();
|
||||||
@@ -846,7 +793,6 @@ 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);
|
||||||
@@ -855,7 +801,6 @@ 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);
|
||||||
}
|
}
|
||||||
@@ -863,13 +808,11 @@ 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();
|
||||||
@@ -890,7 +833,6 @@ 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);
|
||||||
@@ -898,7 +840,6 @@ 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);
|
||||||
@@ -924,19 +865,16 @@ 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);
|
||||||
|
|
||||||
@@ -952,13 +890,11 @@ 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();
|
||||||
@@ -984,42 +920,36 @@ 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);
|
||||||
@@ -1028,77 +958,66 @@ 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);
|
||||||
@@ -1106,13 +1025,11 @@ 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;
|
||||||
@@ -1123,8 +1040,6 @@ 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;
|
||||||
++scope_depth;
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@@ -1141,14 +1056,12 @@ void nasal_codegen::block_gen(nasal_ast& ast)
|
|||||||
case ast_conditional:conditional_gen(tmp);break;
|
case ast_conditional:conditional_gen(tmp);break;
|
||||||
case ast_continue:
|
case ast_continue:
|
||||||
op.op=op_jmp;
|
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_jmp;
|
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);
|
||||||
@@ -1186,8 +1099,6 @@ void nasal_codegen::block_gen(nasal_ast& ast)
|
|||||||
case ast_return:return_gen(tmp);break;
|
case ast_return:return_gen(tmp);break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
--scope_depth;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1197,7 +1108,6 @@ 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;
|
||||||
}
|
}
|
||||||
@@ -1205,7 +1115,6 @@ 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();
|
||||||
@@ -1258,7 +1167,6 @@ 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);
|
||||||
|
|
||||||
@@ -1273,6 +1181,7 @@ void nasal_codegen::main_progress(nasal_ast& ast)
|
|||||||
|
|
||||||
void nasal_codegen::print_op(int index)
|
void nasal_codegen::print_op(int index)
|
||||||
{
|
{
|
||||||
|
// print opcode ptr
|
||||||
std::string numinfo="";
|
std::string numinfo="";
|
||||||
int num=index;
|
int num=index;
|
||||||
for(int i=0;i<8;++i)
|
for(int i=0;i<8;++i)
|
||||||
@@ -1282,21 +1191,14 @@ void nasal_codegen::print_op(int index)
|
|||||||
num>>=4;
|
num>>=4;
|
||||||
}
|
}
|
||||||
std::cout<<"0x"<<numinfo<<": ";
|
std::cout<<"0x"<<numinfo<<": ";
|
||||||
numinfo="";
|
// print opcode name
|
||||||
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)
|
||||||
{
|
{
|
||||||
std::cout<<code_table[i].name<<" ";
|
std::cout<<code_table[i].name<<" ";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// print opcode index
|
||||||
numinfo="";
|
numinfo="";
|
||||||
num=exec_code[index].index;
|
num=exec_code[index].index;
|
||||||
for(int i=0;i<8;++i)
|
for(int i=0;i<8;++i)
|
||||||
@@ -1306,6 +1208,7 @@ void nasal_codegen::print_op(int index)
|
|||||||
num>>=4;
|
num>>=4;
|
||||||
}
|
}
|
||||||
std::cout<<"0x"<<numinfo<<" ";
|
std::cout<<"0x"<<numinfo<<" ";
|
||||||
|
// print detail info
|
||||||
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;
|
||||||
|
|||||||
+42
-29
@@ -31,7 +31,6 @@ private:
|
|||||||
public:
|
public:
|
||||||
nasal_vector(nasal_virtual_machine&);
|
nasal_vector(nasal_virtual_machine&);
|
||||||
~nasal_vector();
|
~nasal_vector();
|
||||||
void set_vm(nasal_virtual_machine&);
|
|
||||||
void add_elem(int);
|
void add_elem(int);
|
||||||
int del_elem();
|
int del_elem();
|
||||||
int size();
|
int size();
|
||||||
@@ -49,7 +48,6 @@ private:
|
|||||||
public:
|
public:
|
||||||
nasal_hash(nasal_virtual_machine&);
|
nasal_hash(nasal_virtual_machine&);
|
||||||
~nasal_hash();
|
~nasal_hash();
|
||||||
void set_vm(nasal_virtual_machine&);
|
|
||||||
void add_elem(std::string,int);
|
void add_elem(std::string,int);
|
||||||
void del_elem(std::string);
|
void del_elem(std::string);
|
||||||
int size();
|
int size();
|
||||||
@@ -71,7 +69,6 @@ private:
|
|||||||
public:
|
public:
|
||||||
nasal_function(nasal_virtual_machine&);
|
nasal_function(nasal_virtual_machine&);
|
||||||
~nasal_function();
|
~nasal_function();
|
||||||
void set_vm(nasal_virtual_machine&);
|
|
||||||
void set_closure_addr(int);
|
void set_closure_addr(int);
|
||||||
int get_closure_addr();
|
int get_closure_addr();
|
||||||
void set_arguments(nasal_ast&);
|
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 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::map<std::string,int> elems;
|
std::list<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 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);
|
||||||
@@ -167,11 +165,6 @@ nasal_vector::~nasal_vector()
|
|||||||
elems.clear();
|
elems.clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void nasal_vector::set_vm(nasal_virtual_machine& nvm)
|
|
||||||
{
|
|
||||||
vm=nvm;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
void nasal_vector::add_elem(int value_address)
|
void nasal_vector::add_elem(int value_address)
|
||||||
{
|
{
|
||||||
int memory_address=vm.mem_alloc(value_address);
|
int memory_address=vm.mem_alloc(value_address);
|
||||||
@@ -252,11 +245,6 @@ nasal_hash::~nasal_hash()
|
|||||||
elems.clear();
|
elems.clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void nasal_hash::set_vm(nasal_virtual_machine& nvm)
|
|
||||||
{
|
|
||||||
vm=nvm;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
void nasal_hash::add_elem(std::string key,int value_address)
|
void nasal_hash::add_elem(std::string key,int value_address)
|
||||||
{
|
{
|
||||||
if(elems.find(key)==elems.end())
|
if(elems.find(key)==elems.end())
|
||||||
@@ -442,52 +430,77 @@ nasal_ast& nasal_function::get_run_block()
|
|||||||
/*functions of nasal_closure*/
|
/*functions of nasal_closure*/
|
||||||
nasal_closure::nasal_closure(nasal_virtual_machine& nvm):vm(nvm)
|
nasal_closure::nasal_closure(nasal_virtual_machine& nvm):vm(nvm)
|
||||||
{
|
{
|
||||||
|
std::map<std::string,int> new_scope;
|
||||||
|
elems.push_back(new_scope);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
nasal_closure::~nasal_closure()
|
nasal_closure::~nasal_closure()
|
||||||
{
|
{
|
||||||
for(std::map<std::string,int>::iterator i=elems.begin();i!=elems.end();++i)
|
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||||
vm.mem_free(i->second);
|
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j)
|
||||||
|
vm.mem_free(j->second);
|
||||||
elems.clear();
|
elems.clear();
|
||||||
return;
|
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)
|
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.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
|
// 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);
|
vm.mem_free(old_mem_address);
|
||||||
}
|
}
|
||||||
elems[key]=new_mem_address;
|
elems.back()[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;
|
||||||
if(elems.find(key)!=elems.end())
|
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||||
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;
|
||||||
if(elems.find(key)!=elems.end())
|
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||||
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::map<std::string,int>::iterator i=elems.begin();i!=elems.end();++i)
|
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||||
vm.mem_free(i->second);
|
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j)
|
||||||
|
vm.mem_free(j->second);
|
||||||
elems.clear();
|
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);
|
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);
|
int new_mem_addr=vm.mem_alloc(value_addr);
|
||||||
vm.add_reference(value_addr);
|
vm.add_reference(value_addr);
|
||||||
elems[i->first]=new_mem_addr;
|
elems.back()[j->first]=new_mem_addr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -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();
|
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
|
||||||
@@ -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);
|
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)
|
||||||
{
|
{
|
||||||
ret_value_addr=function_returned_address;
|
ret_value_addr=function_returned_address;
|
||||||
|
|||||||
Reference in New Issue
Block a user