diff --git a/lib.nas b/lib.nas index 65a3f47..55986f7 100644 --- a/lib.nas +++ b/lib.nas @@ -66,7 +66,7 @@ var floor=func(val){ return __builtin_floor(val); } -# abs gets absolute number +# abs gets absolute number. var abs=func(n){ return n>0?n:-n; } @@ -126,6 +126,11 @@ var die=func(str){ return __builtin_die(str); } +# find will give the first position of the needle in haystack +var find=func(needle,haystack){ + return __builtin_find(needle,haystack); +} + # typeof is used to get the type of an object. # this function returns a string. var typeof=func(object){ @@ -175,11 +180,20 @@ var chr=func(code){ # mut is used to change unmutable strings to mutable. var mut=func(str){ - if(typeof(str)!="str") - die("mut: \"str\" must be string."); return str~""; } +# srand wraps up rand, using time(0) as the seed. +var srand=func(){ + rand(time(0)); + return 0; +} + +# values() gets all values in a hash. +var values=func(hash){ + return __builtin_values(hash); +} + # println has the same function as print. # but it will output a '\n' after using print. var println=func(elems...){ @@ -193,7 +207,7 @@ var isfunc=func(f){ } var isghost=func(g){ - die("this runtime have no ghost object"); + die("this runtime has no ghost object"); return 0; } diff --git a/nasal_builtin.h b/nasal_builtin.h index 5d17c79..da5f205 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -46,6 +46,7 @@ nas_native(builtin_delete); nas_native(builtin_keys); nas_native(builtin_import); nas_native(builtin_die); +nas_native(builtin_find); nas_native(builtin_type); nas_native(builtin_substr); nas_native(builtin_streq); @@ -53,6 +54,7 @@ nas_native(builtin_left); nas_native(builtin_right); nas_native(builtin_cmp); nas_native(builtin_chr); +nas_native(builtin_values); nas_native(builtin_open); nas_native(builtin_close); nas_native(builtin_read); @@ -135,6 +137,7 @@ struct {"__builtin_keys", builtin_keys }, {"__builtin_import", builtin_import }, {"__builtin_die", builtin_die }, + {"__builtin_find", builtin_find }, {"__builtin_type", builtin_type }, {"__builtin_substr", builtin_substr }, {"__builtin_streq", builtin_streq }, @@ -142,6 +145,7 @@ struct {"__builtin_right", builtin_right }, {"__builtin_cmp", builtin_cmp }, {"__builtin_chr", builtin_chr }, + {"__builtin_values", builtin_values }, {"__builtin_open", builtin_open }, {"__builtin_close", builtin_close }, {"__builtin_read", builtin_read }, @@ -617,6 +621,19 @@ nasal_ref builtin_die(nasal_ref* local,nasal_gc& gc) std::cerr<<"[vm] error: "<0?n:-n; } @@ -126,6 +126,11 @@ var die=func(str){ return __builtin_die(str); } +# find will give the first position of the needle in haystack +var find=func(needle,haystack){ + return __builtin_find(needle,haystack); +} + # typeof is used to get the type of an object. # this function returns a string. var typeof=func(object){ @@ -175,11 +180,20 @@ var chr=func(code){ # mut is used to change unmutable strings to mutable. var mut=func(str){ - if(typeof(str)!="str") - die("mut: \"str\" must be string."); return str~""; } +# srand wraps up rand, using time(0) as the seed. +var srand=func(){ + rand(time(0)); + return 0; +} + +# values() gets all values in a hash. +var values=func(hash){ + return __builtin_values(hash); +} + # println has the same function as print. # but it will output a '\n' after using print. var println=func(elems...){ @@ -193,7 +207,7 @@ var isfunc=func(f){ } var isghost=func(g){ - die("this runtime have no ghost object"); + die("this runtime has no ghost object"); return 0; } diff --git a/test/scalar.nas b/test/scalar.nas index c23a594..266e43e 100644 --- a/test/scalar.nas +++ b/test/scalar.nas @@ -166,4 +166,13 @@ print( isvec([]),' ',isvec("[]"),'\n', vecindex([0,1,2,3,4],1),'\n', vecindex(["apple","banana"],"apple")!=nil,'\n' -); \ No newline at end of file +); + +println(values({ + a:1, + b:2, + c:3 +})); +println(find("cd", "abcdef")); # prints 2 +println(find("x", "abcdef")); # prints -1 +println(find("cd", "abcdef")); # prints 2 \ No newline at end of file