diff --git a/README.md b/README.md index 9ca6eda..81dae69 100644 --- a/README.md +++ b/README.md @@ -763,7 +763,7 @@ extern "C" nasal_ref fib(std::vector& args,nasal_gc& gc){ // ok, you must know that vm_num now is not managed by gc // if want to return a gc object, use gc.alloc(type) // usage of gc is the same as adding a native function - return {vm_num,fibonaci(num.to_number())}; + return {vm_num,fibonaci(num.tonum())}; } ``` diff --git a/doc/README_zh.md b/doc/README_zh.md index 593ae81..f79f746 100644 --- a/doc/README_zh.md +++ b/doc/README_zh.md @@ -717,7 +717,7 @@ extern "C" nasal_ref fib(std::vector& args,nasal_gc& gc){ return builtin_err("extern_fib","\"num\" must be number"); // vm_num作为普通的数字类型,不是内存管理的对象,所以无需申请 // 如果需要返回内存管理的对象,请使用gc.alloc(type) - return {vm_num,fibonaci(num.to_number())}; + return {vm_num,fibonaci(num.tonum())}; } ``` diff --git a/module/fib.cpp b/module/fib.cpp index a3e2b53..71eb3bc 100644 --- a/module/fib.cpp +++ b/module/fib.cpp @@ -13,7 +13,7 @@ extern "C" nasal_ref fib(std::vector& args,nasal_gc& gc){ nasal_ref num=args[0]; if(num.type!=vm_num) return builtin_err("extern_fib","\"num\" must be number"); - return {vm_num,fibonaci(num.to_number())}; + return {vm_num,fibonaci(num.tonum())}; } extern "C" nasal_ref quick_fib(std::vector& args,nasal_gc& gc){ std::cout<<"[mod] this is the first test module of nasal\n"; diff --git a/nasal_builtin.h b/nasal_builtin.h index 778b916..4340462 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -430,7 +430,7 @@ nasal_ref builtin_num(nasal_ref* local,nasal_gc& gc) return val; if(val.type!=vm_str) return nil; - double res=val.to_number(); + double res=val.tonum(); if(std::isnan(res)) return nil; return {vm_num,res}; @@ -1418,7 +1418,7 @@ nasal_ref builtin_coresume(nasal_ref* local,nasal_gc& gc) nasal_ref builtin_coyield(nasal_ref* local,nasal_gc& gc) { if(!gc.coroutine) - return builtin_err("coroutine::yield","cannot yield, no coroutine is running"); + return builtin_err("coroutine::yield","no coroutine is running"); gc.ctxreserve(); // this will set to main stack top // then builtin_coresume will return it diff --git a/nasal_gc.h b/nasal_gc.h index 0eed8a1..63a8025 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -85,8 +85,8 @@ struct nasal_ref bool operator==(const nasal_ref& nr){return type==nr.type && value.gcobj==nr.value.gcobj;} bool operator!=(const nasal_ref& nr){return type!=nr.type || value.gcobj!=nr.value.gcobj;} // number and string can be translated to each other - double to_number(); - std::string to_string(); + double tonum(); + std::string tostr(); void print(); bool objchk(uint32_t); inline nasal_ref* addr(); @@ -380,11 +380,11 @@ nasal_val::~nasal_val() } type=vm_nil; } -double nasal_ref::to_number() +double nasal_ref::tonum() { return type!=vm_str?value.num:str2num(str().c_str()); } -std::string nasal_ref::to_string() +std::string nasal_ref::tostr() { if(type==vm_str) return str(); diff --git a/nasal_vm.h b/nasal_vm.h index 5f1ab3f..9d3f8fd 100644 --- a/nasal_vm.h +++ b/nasal_vm.h @@ -513,11 +513,11 @@ inline void nasal_vm::opr_unot() } inline void nasal_vm::opr_usub() { - top[0]={vm_num,-top[0].to_number()}; + top[0]={vm_num,-top[0].tonum()}; } #define op_calc(type)\ - nasal_ref val(vm_num,top[-1].to_number() type top[0].to_number());\ + nasal_ref val(vm_num,top[-1].tonum() type top[0].tonum());\ (--top)[0]=val; inline void nasal_vm::opr_add(){op_calc(+);} @@ -527,12 +527,12 @@ inline void nasal_vm::opr_div(){op_calc(/);} inline void nasal_vm::opr_lnk() { nasal_ref val=gc.alloc(vm_str); - val.str()=top[-1].to_string()+top[0].to_string(); + val.str()=top[-1].tostr()+top[0].tostr(); (--top)[0]=val; } #define op_calc_const(type)\ - nasal_ref val(vm_num,top[0].to_number() type num_table[imm[pc]]);\ + nasal_ref val(vm_num,top[0].tonum() type num_table[imm[pc]]);\ top[0]=val; inline void nasal_vm::opr_addc(){op_calc_const(+);} @@ -542,12 +542,12 @@ inline void nasal_vm::opr_divc(){op_calc_const(/);} inline void nasal_vm::opr_lnkc() { nasal_ref val=gc.alloc(vm_str); - val.str()=top[0].to_string()+str_table[imm[pc]]; + val.str()=top[0].tostr()+str_table[imm[pc]]; top[0]=val; } #define op_calc_eq(type)\ - nasal_ref val(vm_num,memr[0].to_number() type top[-1].to_number());\ + nasal_ref val(vm_num,memr[0].tonum() type top[-1].tonum());\ (--top)[0]=memr[0]=val;\ memr=nullptr;\ top-=imm[pc]; @@ -559,14 +559,14 @@ inline void nasal_vm::opr_diveq(){op_calc_eq(/);} inline void nasal_vm::opr_lnkeq() { nasal_ref val=gc.alloc(vm_str); - val.str()=memr[0].to_string()+top[-1].to_string(); + val.str()=memr[0].tostr()+top[-1].tostr(); (--top)[0]=memr[0]=val; memr=nullptr; top-=imm[pc]; } #define op_calc_eq_const(type)\ - nasal_ref val(vm_num,memr[0].to_number() type num_table[imm[pc]&0x7fffffff]);\ + nasal_ref val(vm_num,memr[0].tonum() type num_table[imm[pc]&0x7fffffff]);\ top[0]=memr[0]=val;\ memr=nullptr;\ top-=(imm[pc]>>31); @@ -578,7 +578,7 @@ inline void nasal_vm::opr_diveqc(){op_calc_eq_const(/);} inline void nasal_vm::opr_lnkeqc() { nasal_ref val=gc.alloc(vm_str); - val.str()=memr[0].to_string()+str_table[imm[pc]&0x7fffffff]; + val.str()=memr[0].tostr()+str_table[imm[pc]&0x7fffffff]; top[0]=memr[0]=val; memr=nullptr; top-=(imm[pc]>>31); @@ -605,7 +605,7 @@ inline void nasal_vm::opr_eq() top[0]=(val1.str()==val2.str())?one:zero; else if((val1.type==vm_num || val2.type==vm_num) && val1.type!=vm_nil && val2.type!=vm_nil) - top[0]=(val1.to_number()==val2.to_number())?one:zero; + top[0]=(val1.tonum()==val2.tonum())?one:zero; else top[0]=(val1==val2)?one:zero; } @@ -619,14 +619,14 @@ inline void nasal_vm::opr_neq() top[0]=(val1.str()!=val2.str())?one:zero; else if((val1.type==vm_num || val2.type==vm_num) && val1.type!=vm_nil && val2.type!=vm_nil) - top[0]=(val1.to_number()!=val2.to_number())?one:zero; + top[0]=(val1.tonum()!=val2.tonum())?one:zero; else top[0]=(val1!=val2)?one:zero; } #define op_cmp(type)\ --top;\ - top[0]=(top[0].to_number() type top[1].to_number())?one:zero; + top[0]=(top[0].tonum() type top[1].tonum())?one:zero; inline void nasal_vm::opr_less(){op_cmp(<);} inline void nasal_vm::opr_leq(){op_cmp(<=);} @@ -634,7 +634,7 @@ inline void nasal_vm::opr_grt(){op_cmp(>);} inline void nasal_vm::opr_geq(){op_cmp(>=);} #define op_cmp_const(type)\ - top[0]=(top[0].to_number() type num_table[imm[pc]])?one:zero; + top[0]=(top[0].tonum() type num_table[imm[pc]])?one:zero; inline void nasal_vm::opr_lessc(){op_cmp_const(<);} inline void nasal_vm::opr_leqc(){op_cmp_const(<=);} @@ -705,9 +705,9 @@ inline void nasal_vm::opr_callv() nasal_ref vec=(--top)[0]; if(vec.type==vm_vec) { - top[0]=vec.vec().get_val(val.to_number()); + top[0]=vec.vec().get_val(val.tonum()); if(top[0].type==vm_none) - die("callv: index out of range:"+std::to_string(val.to_number())); + die("callv: index out of range:"+std::to_string(val.tonum())); } else if(vec.type==vm_hash) { @@ -722,10 +722,10 @@ inline void nasal_vm::opr_callv() else if(vec.type==vm_str) { std::string& str=vec.str(); - int num=val.to_number(); + int num=val.tonum(); int str_size=str.length(); if(num<-str_size || num>=str_size) - die("callv: index out of range:"+std::to_string(val.to_number())); + die("callv: index out of range:"+std::to_string(val.tonum())); top[0]={vm_num,double((uint8_t)str[num>=0? num:num+str_size])}; } else @@ -862,9 +862,9 @@ inline void nasal_vm::opr_slcend() inline void nasal_vm::opr_slc() { nasal_ref val=(top--)[0]; - nasal_ref res=top[-1].vec().get_val(val.to_number()); + nasal_ref res=top[-1].vec().get_val(val.tonum()); if(res.type==vm_none) - die("slc: index out of range:"+std::to_string(val.to_number())); + die("slc: index out of range:"+std::to_string(val.tonum())); top[0].vec().elems.push_back(res); } inline void nasal_vm::opr_slc2() @@ -875,8 +875,8 @@ inline void nasal_vm::opr_slc2() std::vector& aim=top[0].vec().elems; uint8_t type1=val1.type,type2=val2.type; - int num1=val1.to_number(); - int num2=val2.to_number(); + int num1=val1.tonum(); + int num2=val2.tonum(); int size=ref.size(); if(type1==vm_nil && type2==vm_nil) { @@ -925,9 +925,9 @@ inline void nasal_vm::opr_mcallv() nasal_ref vec=(--top)[0]; // mcall vector, reserved on stack to avoid gc if(vec.type==vm_vec) { - memr=vec.vec().get_mem(val.to_number()); + memr=vec.vec().get_mem(val.tonum()); if(!memr) - die("mcallv: index out of range:"+std::to_string(val.to_number())); + die("mcallv: index out of range:"+std::to_string(val.tonum())); } else if(vec.type==vm_hash) // special call of hash, this do mcallh but use the mcallv way { diff --git a/test/coroutine.nas b/test/coroutine.nas index 0858604..3db0181 100644 --- a/test/coroutine.nas +++ b/test/coroutine.nas @@ -1,5 +1,6 @@ # coroutine.nas by ValKmjolnir # 2022/5/19 +import("stl/process_bar.nas"); var fib=func(){ var (a,b)=(1,1); coroutine.yield(a); @@ -10,41 +11,46 @@ var fib=func(){ } return; } - +# different coroutines don't share the same local scope var co=[coroutine.create(fib),coroutine.create(fib)]; for(var i=0;i<45;i+=1){ var res=[coroutine.resume(co[0]),coroutine.resume(co[1])]; - println('coroutine[0]:',res[0]==nil?nil:res[0][0],'\ncoroutine[1]:',res[1]==nil?nil:res[1][0]); + println('co[0]: ',res[0]==nil?nil:res[0][0],'\nco[1]: ',res[1]==nil?nil:res[1][0]); } -var productor=func(){ - for(var i=0;;i+=1) - coroutine.yield(i); -} -var counter=0; -var consumer=func(){ - counter+=1; - print('[',counter,']: '); - for(var i=0;i<5;i+=1){ - print('[',i,']',coroutine.resume(co)[0],' '); - } - print('\n'); -} - -var co=coroutine.create(productor); -var tm=maketimestamp(); -tm.stamp(); -while(tm.elapsedMSec()<1000) - consumer(); - +# test if coroutine can get upvalues func(){ var x=1; var co=coroutine.create(func(){ - for(var j=0;j<1024;j+=1){ + for(var j=0;j<128;j+=1){ coroutine.yield(x,i,j); x+=1; } }); - for(var i=0;i<256;i+=1) + for(var i=0;i<16;i+=1) println(coroutine.resume(co)); -}(); \ No newline at end of file +}(); + +# pressure test +var productor=func(){ + for(var i=0;;i+=1) + coroutine.yield(i); +} +var total=10000; # ms +var co=coroutine.create(productor); +var tm=maketimestamp(); + +var counter=0; +var bar=process_bar.bar("block","point","line",40); +var consumer=func(){ + counter+=1; + for(var i=0;i<5;i+=1) + coroutine.resume(co); + var rate=(tm.elapsedMSec()+1)/total; + print(bar.bar(rate)," ",rate*100,"% \r"); +} + +tm.stamp(); +while(tm.elapsedMSec()\n'); - if(i!='.' and i!='..') - prt(s~' |',path~'/'~i); + var vec=files(path); + var last=size(vec)-1; + forindex(var i;vec){ + var f=vec[i]; + if(f=="." or f=="..") + continue; + foreach(var j;s) + print("\e[34m",j,"\e[0m"); + if(unix.isdir(path~"/"~f)){ + println("\e[34m",i==last?"└─":"├─","\e[0m\e[36m",f," >\e[0m"); + append(s,i==last?" ":"│ "); + prt(s,path~"/"~f); + pop(s); + }elsif(unix.isfile(path~"/"~f)){ + println("\e[34m",i==last?"└─":"├─","\e[0m\e[32m",f,"\e[0m"); + }else{ + println("\e[34m",i==last?"└─":"├─","\e[0m\e[91m",f,"\e[0m"); } - elsif(unix.isfile(path~'/'~i)) - print(" \n"); - else - print(' \n'); } } -prt('',"."); \ No newline at end of file + +println("\e[36mNasal-Interpreter >\e[0m"); +prt([""],"."); \ No newline at end of file