📝 change identifiers' name and test/coroutine.nas, test/filesystem.nas

This commit is contained in:
ValKmjolnir 2022-07-06 16:04:21 +08:00
parent a04ed2a4aa
commit b638708722
8 changed files with 83 additions and 73 deletions

View File

@ -763,7 +763,7 @@ extern "C" nasal_ref fib(std::vector<nasal_ref>& 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())};
}
```

View File

@ -717,7 +717,7 @@ extern "C" nasal_ref fib(std::vector<nasal_ref>& 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())};
}
```

View File

@ -13,7 +13,7 @@ extern "C" nasal_ref fib(std::vector<nasal_ref>& 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<nasal_ref>& args,nasal_gc& gc){
std::cout<<"[mod] this is the first test module of nasal\n";

View File

@ -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

View File

@ -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();

View File

@ -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<nasal_ref>& 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
{

View File

@ -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));
}();
}();
# 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()<total)
consumer();
println("\nexecute ",counter," tasks during ",total," ms, avg ",counter/(total)," tasks/ms.")

View File

@ -4,11 +4,6 @@ while((var line=io.readln(fd))!=nil)
io.close(fd);
println(io.stat("test/filesystem.nas"));
var dd=unix.opendir("test");
while(var name=unix.readdir(dd))
println(name);
unix.closedir(dd);
var files=func(dir){
var dd=unix.opendir(dir);
var res=[];
@ -18,17 +13,26 @@ var files=func(dir){
return res;
}
var prt=func(s,path){
foreach(var i;files(path)){
print(s,i);
if(unix.isdir(path~'/'~i)){
print(' <dir>\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(" <file>\n");
else
print(' <unknown>\n');
}
}
prt('',".");
println("\e[36mNasal-Interpreter >\e[0m");
prt([""],".");