update
This commit is contained in:
parent
2b17f3d702
commit
40b690b67b
8
main.cpp
8
main.cpp
|
@ -20,6 +20,7 @@ void help_cmd()
|
||||||
<<" --code | view bytecode.\n"
|
<<" --code | view bytecode.\n"
|
||||||
<<" --exec | execute script file.\n"
|
<<" --exec | execute script file.\n"
|
||||||
<<" --time | execute and get the running time.\n"
|
<<" --time | execute and get the running time.\n"
|
||||||
|
<<" --opcnt | count operands while running.\n"
|
||||||
<<"file:\n"
|
<<"file:\n"
|
||||||
<<" input file name to execute script file.\n";
|
<<" input file name to execute script file.\n";
|
||||||
return;
|
return;
|
||||||
|
@ -101,12 +102,12 @@ void execute(std::string& file,std::string& command)
|
||||||
codegen.get_num_table(),
|
codegen.get_num_table(),
|
||||||
import.get_file()
|
import.get_file()
|
||||||
);
|
);
|
||||||
if(command=="--exec")
|
if(command=="--exec" || command=="--opcnt")
|
||||||
vm.run(codegen.get_exec_code());
|
vm.run(codegen.get_exec_code(),command=="--opcnt");
|
||||||
else if(command=="--time")
|
else if(command=="--time")
|
||||||
{
|
{
|
||||||
clock_t begin=clock();
|
clock_t begin=clock();
|
||||||
vm.run(codegen.get_exec_code());
|
vm.run(codegen.get_exec_code(),false);
|
||||||
std::cout<<"process exited after "<<((double)(clock()-begin))/CLOCKS_PER_SEC<<"s.\n";
|
std::cout<<"process exited after "<<((double)(clock()-begin))/CLOCKS_PER_SEC<<"s.\n";
|
||||||
}
|
}
|
||||||
vm.clear();
|
vm.clear();
|
||||||
|
@ -130,6 +131,7 @@ int main(int argc,const char* argv[])
|
||||||
!strcmp(argv[1],"--ast") ||
|
!strcmp(argv[1],"--ast") ||
|
||||||
!strcmp(argv[1],"--code") ||
|
!strcmp(argv[1],"--code") ||
|
||||||
!strcmp(argv[1],"--exec") ||
|
!strcmp(argv[1],"--exec") ||
|
||||||
|
!strcmp(argv[1],"--opcnt")||
|
||||||
!strcmp(argv[1],"--time")))
|
!strcmp(argv[1],"--time")))
|
||||||
{
|
{
|
||||||
file=argv[2];
|
file=argv[2];
|
||||||
|
|
|
@ -1276,13 +1276,17 @@ void nasal_codegen::print_op(int index)
|
||||||
|
|
||||||
void nasal_codegen::print_byte_code()
|
void nasal_codegen::print_byte_code()
|
||||||
{
|
{
|
||||||
|
if(num_res_table.size())
|
||||||
|
std::cout<<".number"<<std::endl;
|
||||||
for(auto& num:num_res_table)
|
for(auto& num:num_res_table)
|
||||||
std::cout<<".number "<<num<<'\n';
|
std::cout<<'\t'<<num<<'\n';
|
||||||
|
if(str_res_table.size())
|
||||||
|
std::cout<<".symbol"<<std::endl;
|
||||||
for(auto& str:str_res_table)
|
for(auto& str:str_res_table)
|
||||||
{
|
{
|
||||||
std::cout<<".symbol ";
|
std::cout<<'\t';
|
||||||
raw_string(str);
|
raw_string(str);
|
||||||
std::cout<<'\n';
|
std::cout<<std::endl;
|
||||||
}
|
}
|
||||||
for(int i=0;i<exec_code.size();++i)
|
for(int i=0;i<exec_code.size();++i)
|
||||||
print_op(i);
|
print_op(i);
|
||||||
|
|
|
@ -101,6 +101,7 @@ void nasal_vec::print()
|
||||||
std::cout<<"[]";
|
std::cout<<"[]";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
ssize_t iter=0;
|
||||||
std::cout<<'[';
|
std::cout<<'[';
|
||||||
for(auto i:elems)
|
for(auto i:elems)
|
||||||
{
|
{
|
||||||
|
@ -113,7 +114,7 @@ void nasal_vec::print()
|
||||||
case vm_hash: i->ptr.hash->print(); break;
|
case vm_hash: i->ptr.hash->print(); break;
|
||||||
case vm_func: std::cout<<"func(..){..}"; break;
|
case vm_func: std::cout<<"func(..){..}"; break;
|
||||||
}
|
}
|
||||||
std::cout<<",]"[i==elems.back()];
|
std::cout<<",]"[(++iter)==elems.size()];
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
54
nasal_vm.h
54
nasal_vm.h
|
@ -108,7 +108,7 @@ public:
|
||||||
std::vector<double>&,
|
std::vector<double>&,
|
||||||
std::vector<std::string>&);
|
std::vector<std::string>&);
|
||||||
void clear();
|
void clear();
|
||||||
void run(std::vector<opcode>&);
|
void run(std::vector<opcode>&,bool);
|
||||||
};
|
};
|
||||||
|
|
||||||
void nasal_vm::init(
|
void nasal_vm::init(
|
||||||
|
@ -180,7 +180,7 @@ void nasal_vm::stackinfo(int limit)
|
||||||
}
|
}
|
||||||
if(same_cnt)
|
if(same_cnt)
|
||||||
{
|
{
|
||||||
printf("\t%p %d same value(s) ...\n",last_ptr,same_cnt);
|
printf("\t%p ... | %d same value(s)\n",last_ptr,same_cnt);
|
||||||
same_cnt=0;
|
same_cnt=0;
|
||||||
}
|
}
|
||||||
last_ptr=stack_top[-i];
|
last_ptr=stack_top[-i];
|
||||||
|
@ -190,18 +190,18 @@ void nasal_vm::stackinfo(int limit)
|
||||||
else
|
else
|
||||||
switch(stack_top[-i]->type)
|
switch(stack_top[-i]->type)
|
||||||
{
|
{
|
||||||
case vm_nil: printf("nil");break;
|
case vm_nil: printf("nil | gc.nil_addr");break;
|
||||||
case vm_num: printf("num | %lf",stack_top[-i]->ptr.num);break;
|
case vm_num: printf("num | %lf",stack_top[-i]->ptr.num);break;
|
||||||
case vm_str: printf("str | ");raw_string(*stack_top[-i]->ptr.str);break;
|
case vm_str: printf("str | ");raw_string(*stack_top[-i]->ptr.str);break;
|
||||||
case vm_func: printf("func | func(..){..}");break;
|
case vm_func: printf("func | func(%lu para){..}",stack_top[-i]->ptr.func->key_table.size());break;
|
||||||
case vm_vec: printf("vec | ");stack_top[-i]->ptr.vec->print();break;
|
case vm_vec: printf("vec | [%lu val]",stack_top[-i]->ptr.vec->elems.size());break;
|
||||||
case vm_hash: printf("hash | ");stack_top[-i]->ptr.hash->print();break;
|
case vm_hash: printf("hash | {%lu member}",stack_top[-i]->ptr.hash->elems.size());break;
|
||||||
default: printf("unknown");break;
|
default: printf("unknown");break;
|
||||||
}
|
}
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
if(same_cnt)
|
if(same_cnt)
|
||||||
printf("\t%p %d same value(s) ...\n",last_ptr,same_cnt);
|
printf("\t%p ... | %d same value(s)\n",last_ptr,same_cnt);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void nasal_vm::die(std::string str)
|
void nasal_vm::die(std::string str)
|
||||||
|
@ -221,7 +221,7 @@ void nasal_vm::stackoverflow()
|
||||||
stackinfo(10);
|
stackinfo(10);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bool nasal_vm::condition(nasal_val* val_addr)
|
inline bool nasal_vm::condition(nasal_val* val_addr)
|
||||||
{
|
{
|
||||||
if(val_addr->type==vm_num)
|
if(val_addr->type==vm_num)
|
||||||
return val_addr->ptr.num;
|
return val_addr->ptr.num;
|
||||||
|
@ -965,9 +965,9 @@ inline void nasal_vm::opr_ret()
|
||||||
stack_top[0]=stack_top[1];// rewrite nasal_func with returned value
|
stack_top[0]=stack_top[1];// rewrite nasal_func with returned value
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void nasal_vm::run(std::vector<opcode>& exec)
|
void nasal_vm::run(std::vector<opcode>& exec,bool op_cnt)
|
||||||
{
|
{
|
||||||
//uint64_t count[op_ret+1]={0};
|
uint64_t count[op_ret+1]={0};
|
||||||
void* opr_table[]=
|
void* opr_table[]=
|
||||||
{
|
{
|
||||||
&&nop, &&intg, &&intl, &&offset,
|
&&nop, &&intg, &&intl, &&offset,
|
||||||
|
@ -1009,23 +1009,25 @@ nop:
|
||||||
if(canary && canary!=(nasal_val*)0xffff)
|
if(canary && canary!=(nasal_val*)0xffff)
|
||||||
stackoverflow();
|
stackoverflow();
|
||||||
// debug
|
// debug
|
||||||
// for(int i=0;i<15;++i)
|
if(op_cnt)
|
||||||
// {
|
{
|
||||||
// uint64_t maxnum=0,index=0;
|
std::cout<<std::endl;
|
||||||
// for(int j=0;j<op_ret+1;++j)
|
for(int i=0;i<15;++i)
|
||||||
// if(count[j]>maxnum)
|
{
|
||||||
// {
|
uint64_t maxnum=0,index=0;
|
||||||
// index=j;
|
for(int j=0;j<op_ret+1;++j)
|
||||||
// maxnum=count[j];
|
if(count[j]>maxnum)
|
||||||
// }
|
{
|
||||||
// std::cout<<code_table[index].name<<": "<<maxnum<<"\n";
|
index=j;
|
||||||
// count[index]=0;
|
maxnum=count[j];
|
||||||
// }
|
}
|
||||||
|
std::cout<<code_table[index].name<<": "<<maxnum<<"\n";
|
||||||
|
count[index]=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
//#define exec_operand(op,num) {op();++count[num];if(!canary[0])goto *code[++pc];goto nop;}
|
#define exec_operand(op,num) {op();++count[num];if(!canary)goto *code[++pc];goto nop;}
|
||||||
#define exec_operand(op,num) {op();if(!canary)goto *code[++pc];goto nop;}
|
#define exec_opnodie(op,num) {op();++count[num];goto *code[++pc];}
|
||||||
//#define exec_opnodie(op,num) {op();++count[num];goto *code[++pc];}
|
|
||||||
#define exec_opnodie(op,num) {op();goto *code[++pc];}
|
|
||||||
|
|
||||||
intg: exec_opnodie(opr_intg ,op_intg );
|
intg: exec_opnodie(opr_intg ,op_intg );
|
||||||
intl: exec_opnodie(opr_intl ,op_intl );
|
intl: exec_opnodie(opr_intl ,op_intl );
|
||||||
|
|
|
@ -63,14 +63,14 @@ props.Node=
|
||||||
foreach(var label;path)
|
foreach(var label;path)
|
||||||
tmp=tmp.val[label];
|
tmp=tmp.val[label];
|
||||||
tmp.val=val;
|
tmp.val=val;
|
||||||
if(typeof(val)=='string')
|
if(typeof(val)=='str')
|
||||||
{
|
{
|
||||||
if(val=='true' or val=='false')
|
if(val=='true' or val=='false')
|
||||||
tmp.type='BOOL';
|
tmp.type='BOOL';
|
||||||
else
|
else
|
||||||
tmp.type='STRING';
|
tmp.type='STRING';
|
||||||
}
|
}
|
||||||
elsif(typeof(val)=='number')
|
elsif(typeof(val)=='num')
|
||||||
tmp.type='DOUBLE';
|
tmp.type='DOUBLE';
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
|
@ -117,6 +117,11 @@ props.Node=
|
||||||
if(typeof(me.val)=='hash')
|
if(typeof(me.val)=='hash')
|
||||||
{
|
{
|
||||||
var key=keys(me.val);
|
var key=keys(me.val);
|
||||||
|
if(!size(key))
|
||||||
|
{
|
||||||
|
println("{}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
println('{');
|
println('{');
|
||||||
foreach(var k;key)
|
foreach(var k;key)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue