diff --git a/lib.nas b/lib.nas index 65a8882..65a3f47 100644 --- a/lib.nas +++ b/lib.nas @@ -211,7 +211,7 @@ var isnum=func(x){ var isscalar=func(s){ var t=typeof(s); - return t=="num" or t=="str"; + return (t=="num" or t=="str")?1:0; } var isstr=func(s){ @@ -223,7 +223,9 @@ var isvec=func(v){ } var vecindex=func(vec,val){ - # unfinished + forindex(var i;vec) + if(val==vec[i]) + return i; return nil; } diff --git a/nasal_vm.h b/nasal_vm.h index 556c996..8336b7d 100644 --- a/nasal_vm.h +++ b/nasal_vm.h @@ -521,7 +521,8 @@ inline void nasal_vm::opr_eq() gc.top[0]=one; else if(val1.type==vm_str && val2.type==vm_str) gc.top[0]=(val1.str()==val2.str())?one:zero; - else if(val1.type==vm_num || val2.type==vm_num) + else if((val1.type==vm_num || val2.type==vm_num) + && val1.type!=vm_nil && val2.type!=vm_nil) gc.top[0]=(val1.to_number()==val2.to_number())?one:zero; else gc.top[0]=(val1==val2)?one:zero; @@ -534,7 +535,8 @@ inline void nasal_vm::opr_neq() gc.top[0]=zero; else if(val1.type==vm_str && val2.type==vm_str) gc.top[0]=(val1.str()!=val2.str())?one:zero; - else if(val1.type==vm_num || val2.type==vm_num) + else if((val1.type==vm_num || val2.type==vm_num) + && val1.type!=vm_nil && val2.type!=vm_nil) gc.top[0]=(val1.to_number()!=val2.to_number())?one:zero; else gc.top[0]=(val1!=val2)?one:zero; diff --git a/stl/lib.nas b/stl/lib.nas index 65a8882..65a3f47 100644 --- a/stl/lib.nas +++ b/stl/lib.nas @@ -211,7 +211,7 @@ var isnum=func(x){ var isscalar=func(s){ var t=typeof(s); - return t=="num" or t=="str"; + return (t=="num" or t=="str")?1:0; } var isstr=func(s){ @@ -223,7 +223,9 @@ var isvec=func(v){ } var vecindex=func(vec,val){ - # unfinished + forindex(var i;vec) + if(val==vec[i]) + return i; return nil; } diff --git a/test/scalar.nas b/test/scalar.nas index 7cccca2..c23a594 100644 --- a/test/scalar.nas +++ b/test/scalar.nas @@ -149,4 +149,21 @@ f1() or f2(); # print '1' # this means that when using 'or' or 'and', # if the result is clear when calculating, -# objects behind will not be calculated \ No newline at end of file +# objects behind will not be calculated + +print( + subvec([0,1,2,3],2),'\n', + subvec([0,1,2,3],2,1),'\n', + abs(1),'\n', + abs(-1),'\n', + systime(),'\n', + isfunc(func{}),' ',isfunc([]),'\n', + ishash({}),' ',ishash([]),'\n', + isint(114.514),' ',isint(114514),'\n', + isnum("0xaa55"),' ',isnum("?"),'\n', + isscalar(0.618),' ',isscalar("hello"),' ',isscalar([]),'\n', + isstr("hello"),' ',isstr(func{}),'\n', + isvec([]),' ',isvec("[]"),'\n', + vecindex([0,1,2,3,4],1),'\n', + vecindex(["apple","banana"],"apple")!=nil,'\n' +); \ No newline at end of file