Add props lib & bug fixed(op_ret in foreach/index got SIGSEGV)

This commit is contained in:
Valk Richard Li 2021-02-18 23:49:29 +08:00
parent 9c9bb52818
commit 64961877de
3 changed files with 159 additions and 4 deletions

View File

@ -136,6 +136,8 @@ private:
std::list<std::vector<int> > continue_ptr;
std::list<std::vector<int> > break_ptr;
int error;
int in_forindex;
int in_foreach;
void regist_number(double);
void regist_string(std::string);
void gen(unsigned char,unsigned int);
@ -185,6 +187,8 @@ public:
nasal_codegen::nasal_codegen()
{
error=0;
in_foreach=0;
in_forindex=0;
return;
}
@ -548,8 +552,8 @@ void nasal_codegen::loop_gen(nasal_ast& ast)
{
case ast_while: while_gen(ast); break;
case ast_for: for_gen(ast); break;
case ast_forindex: forindex_gen(ast); break;
case ast_foreach: foreach_gen(ast); break;
case ast_forindex: ++in_forindex;forindex_gen(ast);--in_forindex; break;
case ast_foreach: ++in_foreach; foreach_gen(ast); --in_foreach; break;
}
return;
}
@ -844,6 +848,16 @@ void nasal_codegen::block_gen(nasal_ast& ast)
void nasal_codegen::return_gen(nasal_ast& ast)
{
for(int i=0;i<in_foreach;++i)
{
gen(op_pop,0);
gen(op_cntpop,0);
}
for(int i=0;i<in_forindex;++i)
{
gen(op_pop,0);
gen(op_cntpop,0);
}
if(ast.get_children().size())
calculation_gen(ast.get_children()[0]);
else

View File

@ -1083,9 +1083,8 @@ void nasal_vm::opr_mcallh()
}
void nasal_vm::opr_return()
{
nasal_val* closure_addr=local_scope_stack.top();
gc.del_reference(local_scope_stack.top());
local_scope_stack.pop();
gc.del_reference(closure_addr);
ptr=call_stack.top();
call_stack.pop();
nasal_val* tmp=*val_stack_top--;

142
props.nas Normal file
View File

@ -0,0 +1,142 @@
import("lib.nas");
var props=
{
globals:nil,
Node:nil,
getNode:func(path,index)
{
path=split('/',path);
var tmp=me.globals;
var path_size=size(path);
for(var i=0;i<path_size-1;i+=1)
tmp=tmp.val[path[i]];
if(path_size>0)
{
if(contains(tmp.val,path[i]~'['~index~']'))
return tmp.val[path[i]~'['~index~']'];
else
return tmp.val[path[i]];
}
return tmp;
}
};
props.Node=
{
new:func(values=nil)
{
var result={
parents:[props.Node],
val:{},
type:'GHOST',
parent:nil
};
if(typeof(values)=="hash")
result.val=values;
return result;
},
addChild:func(name)
{
if(!contains(me.val,name))
{
me.val[name]=props.Node.new();
me.val[name].parent=me;
return 1;
}
return 0;
},
addChildren:func(name,cnt=0)
{
for(var i=0;i<cnt;i+=1)
{
var label=name~'['~i~']';
me.val[label]=props.Node.new();
me.val[label].parent=me;
}
return;
},
setValue:func(path,val)
{
path=split('/',path);
var tmp=me;
foreach(var label;path)
tmp=tmp.val[label];
tmp.val=val;
if(typeof(val)=='string')
{
if(val=='true' or val=='false')
tmp.type='BOOL';
else
tmp.type='STRING';
}
elsif(typeof(val)=='number')
tmp.type='DOUBLE';
return;
},
setIntValue:func(num)
{
me.val=num;
me.type='INT';
return;
},
setBoolValue:func(state)
{
me.val=state;
me.type='BOOL';
return;
},
setDoubleValue:func(num)
{
me.val=num;
me.type='DOUBLE';
return;
},
getValue:func(){return me.val;},
getName:func()
{
var val=me.parent.val;
var key=keys(val);
foreach(var k;key)
if(val[k]==me)
return k;
return '';
},
equals:func(node){return me==node;},
debug:func(s='')
{
if(typeof(me.val)=='hash')
{
var key=keys(me.val);
println('{');
foreach(var k;key)
{
print(s~' ',k,':');
me.val[k].debug(s~' ');
}
println(s,'}');
}
else
println(me.val,' (',me.type,')');
return;
}
};
props.globals=props.Node.new();
var c=['aircraft','ai','models','position','orientation','controls','sim'];
foreach(var i;c)
props.getNode('/',1).addChild(i);
props.getNode('/ai',1).addChildren('ai',4);
props.getNode('/aircraft',1).setValue('/','IDG MD-11');
for(var i=0;i<4;i+=1)
props.getNode('/ai/ai['~i~']',1).setBoolValue('true');
props.getNode('/models',1).addChildren('building',4);
for(var i=0;i<4;i+=1)
props.getNode('/models/building['~i~']',1).setIntValue(i);
props.getNode('/',1).addChild('test');
props.getNode('/test',1).addChildren('in',4);
props.getNode('/test/in',0).setValue('/','true');
props.getNode('/test/in',1).setValue('/','false');
props.getNode('/test/in',2).setValue('/','welcome aboard,need help? use help->tutorial');
props.getNode('/test/in',3).setValue('/',2147483648);
props.globals.debug();