📝 change some identifiers' name.

This commit is contained in:
ValKmjolnir 2022-07-09 16:24:58 +08:00
parent 9890b46f02
commit 2ed20f6362
11 changed files with 453 additions and 460 deletions

View File

@ -331,7 +331,7 @@ You could see how to write your own native-functions below.
```javascript
var my_new_obj=func(){
return __builtin_my_obj();
return __my_obj();
}
var obj=my_new_obj();
```
@ -652,25 +652,25 @@ struct func
nasal_ref (*func)(nasal_ref*,nasal_gc&);
} builtin[]=
{
{"__builtin_print",builtin_print},
{"__print",builtin_print},
{nullptr, nullptr }
};
```
At last,warp the `__builtin_print` in a nasal file:
At last,warp the `__print` in a nasal file:
```javascript
var print=func(elems...){
return __builtin_print(elems);
return __print(elems);
};
```
In fact the arguments that `__builtin_print` uses are not necessary.
In fact the arguments that `__print` uses are not necessary.
So writting it like this is also right:
```javascript
var print=func(elems...){
return __builtin_print;
return __print;
};
```
@ -719,10 +719,10 @@ After 2021/12/3, there are some new functions added to `lib.nas`:
```javascript
var dylib=
{
dlopen: func(libname){return __builtin_dlopen;},
dlsym: func(lib,sym){return __builtin_dlsym; },
dlclose: func(lib){return __builtin_dlclose; },
dlcall: func(funcptr,args...){return __builtin_dlcall}
dlopen: func(libname){return __dlopen;},
dlsym: func(lib,sym){return __dlsym; },
dlclose: func(lib){return __dlclose; },
dlcall: func(funcptr,args...){return __dlcall}
};
```
@ -1334,7 +1334,7 @@ func <0x29c>:
0x0000029d: 02 00 00 00 02 intl 0x2
0x0000029e: 0d 00 00 00 66 para 0x66 ("libname")
0x0000029f: 32 00 00 02 a2 jmp 0x2a2
0x000002a0: 40 00 00 00 42 callb 0x42 <__builtin_dlopen@0x41dc40>
0x000002a0: 40 00 00 00 42 callb 0x42 <__dlopen@0x41dc40>
0x000002a1: 4a 00 00 00 00 ret
<0x29c>;
@ -1346,7 +1346,7 @@ func <0x2a3>:
0x000002a5: 0d 00 00 00 68 para 0x68 ("lib")
0x000002a6: 0d 00 00 00 69 para 0x69 ("sym")
0x000002a7: 32 00 00 02 aa jmp 0x2aa
0x000002a8: 40 00 00 00 43 callb 0x43 <__builtin_dlsym@0x41df00>
0x000002a8: 40 00 00 00 43 callb 0x43 <__dlsym@0x41df00>
0x000002a9: 4a 00 00 00 00 ret
<0x2a3>;
@ -1357,7 +1357,7 @@ func <0x2ab>:
0x000002ac: 02 00 00 00 02 intl 0x2
0x000002ad: 0d 00 00 00 68 para 0x68 ("lib")
0x000002ae: 32 00 00 02 b1 jmp 0x2b1
0x000002af: 40 00 00 00 44 callb 0x44 <__builtin_dlclose@0x41e2a0>
0x000002af: 40 00 00 00 44 callb 0x44 <__dlclose@0x41e2a0>
0x000002b0: 4a 00 00 00 00 ret
<0x2ab>;
@ -1369,7 +1369,7 @@ func <0x2b2>:
0x000002b4: 0d 00 00 00 6c para 0x6c ("funcptr")
0x000002b5: 0f 00 00 00 6d dyn 0x6d ("args")
0x000002b6: 32 00 00 02 b9 jmp 0x2b9
0x000002b7: 40 00 00 00 45 callb 0x45 <__builtin_dlcall@0x41e3d0>
0x000002b7: 40 00 00 00 45 callb 0x45 <__dlcall@0x41e3d0>
0x000002b8: 4a 00 00 00 00 ret
<0x2b2>;
@ -1381,7 +1381,7 @@ func <0x2bc>:
0x000002bc: 0b 00 00 02 bf newf 0x2bf
0x000002bd: 02 00 00 00 01 intl 0x1
0x000002be: 32 00 00 02 c1 jmp 0x2c1
0x000002bf: 40 00 00 00 46 callb 0x46 <__builtin_platform@0x41e4f0>
0x000002bf: 40 00 00 00 46 callb 0x46 <__platform@0x41e4f0>
0x000002c0: 4a 00 00 00 00 ret
<0x2bc>;
@ -1393,7 +1393,7 @@ func <0x2c4>:
0x000002c4: 0b 00 00 02 c7 newf 0x2c7
0x000002c5: 02 00 00 00 01 intl 0x1
0x000002c6: 32 00 00 02 c9 jmp 0x2c9
0x000002c7: 40 00 00 00 47 callb 0x47 <__builtin_gc@0x41e530>
0x000002c7: 40 00 00 00 47 callb 0x47 <__gc@0x41e530>
0x000002c8: 4a 00 00 00 00 ret
<0x2c4>;
@ -1409,11 +1409,11 @@ Now we add coroutine in this runtime:
```javascript
var coroutine={
create: func(function){return __builtin_cocreate;},
resume: func(co) {return __builtin_coresume;},
yield: func(args...) {return __builtin_coyield; },
status: func(co) {return __builtin_costatus;},
running:func() {return __builtin_corun; }
create: func(function){return __cocreate;},
resume: func(co) {return __coresume;},
yield: func(args...) {return __coyield; },
status: func(co) {return __costatus;},
running:func() {return __corun; }
};
```
@ -1748,7 +1748,7 @@ hello
[vm] error: error occurred this line
[vm] native function error.
trace back:
0x000000ac: 40 00 00 00 25 callb 0x25 <__builtin_die@0x41afc0> (lib.nas:131)
0x000000ac: 40 00 00 00 25 callb 0x25 <__die@0x41afc0> (lib.nas:131)
0x000004f6: 3e 00 00 00 01 callfv 0x1 (a.nas:4)
0x000004fa: 3e 00 00 00 00 callfv 0x0 (a.nas:6)
vm stack(0x7fffcd21bc68<sp+80>, limit 10, total 12):
@ -1825,7 +1825,7 @@ hello
[vm] error: error occurred this line
[vm] native function error.
trace back:
0x000000ac: 40 00 00 00 25 callb 0x25 <__builtin_die@0x41afc0> (lib.nas:131)
0x000000ac: 40 00 00 00 25 callb 0x25 <__die@0x41afc0> (lib.nas:131)
0x000004f6: 3e 00 00 00 01 callfv 0x1 (a.nas:4)
0x000004fa: 3e 00 00 00 00 callfv 0x0 (a.nas:6)
vm stack(0x7ffff42f3d08<sp+80>, limit 10, total 12):
@ -1961,7 +1961,7 @@ next bytecode:
0x00000002: 02 00 00 00 02 intl 0x2 (lib.nas:5)
0x00000003: 0d 00 00 00 00 para 0x0 ("filename") (lib.nas:5)
0x00000004: 32 00 00 00 07 jmp 0x7 (lib.nas:5)
0x00000005: 40 00 00 00 24 callb 0x24 <__builtin_import@0x419b20> (lib.nas:6)
0x00000005: 40 00 00 00 24 callb 0x24 <__import@0x419b20> (lib.nas:6)
0x00000006: 4a 00 00 00 00 ret 0x0 (lib.nas:6)
0x00000007: 03 00 00 00 00 loadg 0x0 (lib.nas:5)
vm stack(0x7fffe05e3190<sp+79>, limit 5, total 0)

View File

@ -309,7 +309,7 @@ __`vm_obj`__ 是一种用来存储用户自定义数据的特别类型。这意
```javascript
var my_new_obj=func(){
return __builtin_my_obj();
return __my_obj();
}
var obj=my_new_obj();
```
@ -618,7 +618,7 @@ struct func
nasal_ref (*func)(nasal_ref*,nasal_gc&);
} builtin[]=
{
{"__builtin_print",builtin_print},
{"__print",builtin_print},
{nullptr, nullptr }
};
```
@ -627,15 +627,15 @@ struct func
```javascript
var print=func(elems...){
return __builtin_print(elems);
return __print(elems);
};
```
事实上`__builtin_print`后面跟着的传参列表不是必须要写的。所以这样写也对:
事实上`__print`后面跟着的传参列表不是必须要写的。所以这样写也对:
```javascript
var print=func(elems...){
return __builtin_print;
return __print;
};
```
@ -679,10 +679,10 @@ nasal_ref builtin_keys(nasal_ref* local,nasal_gc& gc)
```javascript
var dylib=
{
dlopen: func(libname){return __builtin_dlopen;},
dlsym: func(lib,sym){return __builtin_dlsym; },
dlclose: func(lib){return __builtin_dlclose; },
dlcall: func(funcptr,args...){return __builtin_dlcall}
dlopen: func(libname){return __dlopen;},
dlsym: func(lib,sym){return __dlsym; },
dlclose: func(lib){return __dlclose; },
dlcall: func(funcptr,args...){return __dlcall}
};
```
@ -1235,7 +1235,7 @@ func <0x29c>:
0x0000029d: 02 00 00 00 02 intl 0x2
0x0000029e: 0d 00 00 00 66 para 0x66 ("libname")
0x0000029f: 32 00 00 02 a2 jmp 0x2a2
0x000002a0: 40 00 00 00 42 callb 0x42 <__builtin_dlopen@0x41dc40>
0x000002a0: 40 00 00 00 42 callb 0x42 <__dlopen@0x41dc40>
0x000002a1: 4a 00 00 00 00 ret
<0x29c>;
@ -1247,7 +1247,7 @@ func <0x2a3>:
0x000002a5: 0d 00 00 00 68 para 0x68 ("lib")
0x000002a6: 0d 00 00 00 69 para 0x69 ("sym")
0x000002a7: 32 00 00 02 aa jmp 0x2aa
0x000002a8: 40 00 00 00 43 callb 0x43 <__builtin_dlsym@0x41df00>
0x000002a8: 40 00 00 00 43 callb 0x43 <__dlsym@0x41df00>
0x000002a9: 4a 00 00 00 00 ret
<0x2a3>;
@ -1258,7 +1258,7 @@ func <0x2ab>:
0x000002ac: 02 00 00 00 02 intl 0x2
0x000002ad: 0d 00 00 00 68 para 0x68 ("lib")
0x000002ae: 32 00 00 02 b1 jmp 0x2b1
0x000002af: 40 00 00 00 44 callb 0x44 <__builtin_dlclose@0x41e2a0>
0x000002af: 40 00 00 00 44 callb 0x44 <__dlclose@0x41e2a0>
0x000002b0: 4a 00 00 00 00 ret
<0x2ab>;
@ -1270,7 +1270,7 @@ func <0x2b2>:
0x000002b4: 0d 00 00 00 6c para 0x6c ("funcptr")
0x000002b5: 0f 00 00 00 6d dyn 0x6d ("args")
0x000002b6: 32 00 00 02 b9 jmp 0x2b9
0x000002b7: 40 00 00 00 45 callb 0x45 <__builtin_dlcall@0x41e3d0>
0x000002b7: 40 00 00 00 45 callb 0x45 <__dlcall@0x41e3d0>
0x000002b8: 4a 00 00 00 00 ret
<0x2b2>;
@ -1282,7 +1282,7 @@ func <0x2bc>:
0x000002bc: 0b 00 00 02 bf newf 0x2bf
0x000002bd: 02 00 00 00 01 intl 0x1
0x000002be: 32 00 00 02 c1 jmp 0x2c1
0x000002bf: 40 00 00 00 46 callb 0x46 <__builtin_platform@0x41e4f0>
0x000002bf: 40 00 00 00 46 callb 0x46 <__platform@0x41e4f0>
0x000002c0: 4a 00 00 00 00 ret
<0x2bc>;
@ -1294,7 +1294,7 @@ func <0x2c4>:
0x000002c4: 0b 00 00 02 c7 newf 0x2c7
0x000002c5: 02 00 00 00 01 intl 0x1
0x000002c6: 32 00 00 02 c9 jmp 0x2c9
0x000002c7: 40 00 00 00 47 callb 0x47 <__builtin_gc@0x41e530>
0x000002c7: 40 00 00 00 47 callb 0x47 <__gc@0x41e530>
0x000002c8: 4a 00 00 00 00 ret
<0x2c4>;
@ -1310,11 +1310,11 @@ func <0x2c4>:
```javascript
var coroutine={
create: func(function){return __builtin_cocreate;},
resume: func(co) {return __builtin_coresume;},
yield: func(args...) {return __builtin_coyield; },
status: func(co) {return __builtin_costatus;},
running:func() {return __builtin_corun; }
create: func(function){return __cocreate;},
resume: func(co) {return __coresume;},
yield: func(args...) {return __coyield; },
status: func(co) {return __costatus;},
running:func() {return __corun; }
};
```
@ -1609,7 +1609,7 @@ hello
[vm] error: error occurred this line
[vm] native function error.
trace back:
0x000000ac: 40 00 00 00 25 callb 0x25 <__builtin_die@0x41afc0> (lib.nas:131)
0x000000ac: 40 00 00 00 25 callb 0x25 <__die@0x41afc0> (lib.nas:131)
0x000004f6: 3e 00 00 00 01 callfv 0x1 (a.nas:4)
0x000004fa: 3e 00 00 00 00 callfv 0x0 (a.nas:6)
vm stack(0x7fffcd21bc68<sp+80>, limit 10, total 12):
@ -1686,7 +1686,7 @@ hello
[vm] error: error occurred this line
[vm] native function error.
trace back:
0x000000ac: 40 00 00 00 25 callb 0x25 <__builtin_die@0x41afc0> (lib.nas:131)
0x000000ac: 40 00 00 00 25 callb 0x25 <__die@0x41afc0> (lib.nas:131)
0x000004f6: 3e 00 00 00 01 callfv 0x1 (a.nas:4)
0x000004fa: 3e 00 00 00 00 callfv 0x0 (a.nas:6)
vm stack(0x7ffff42f3d08<sp+80>, limit 10, total 12):
@ -1820,7 +1820,7 @@ next bytecode:
0x00000002: 02 00 00 00 02 intl 0x2 (lib.nas:5)
0x00000003: 0d 00 00 00 00 para 0x0 ("filename") (lib.nas:5)
0x00000004: 32 00 00 00 07 jmp 0x7 (lib.nas:5)
0x00000005: 40 00 00 00 24 callb 0x24 <__builtin_import@0x419b20> (lib.nas:6)
0x00000005: 40 00 00 00 24 callb 0x24 <__import@0x419b20> (lib.nas:6)
0x00000006: 4a 00 00 00 00 ret 0x0 (lib.nas:6)
0x00000007: 03 00 00 00 00 loadg 0x0 (lib.nas:5)
vm stack(0x7fffe05e3190<sp+79>, limit 5, total 0)

184
lib.nas
View File

@ -3,66 +3,66 @@
# print is used to print all things in nasal, try and see how it works.
# this function uses std::cout/printf to output logs.
var print=func(elems...){
return __builtin_print(elems);
return __print(elems);
}
# append is used to add values into a vector.
var append=func(vec,elems...){
return __builtin_append(vec,elems);
return __append(vec,elems);
}
# setsize is used to change the size of vector.
# if the size is larger than before,
# this function will fill vm_nil into uninitialized space.
var setsize=func(vec,size){
return __builtin_setsize(vec,size);
return __setsize(vec,size);
}
# system has the same use in C.
var system=func(str){
return __builtin_system(str);
return __system(str);
}
# input uses std::cin and returns what we input.
var input=func(){
return __builtin_input();
return __input();
}
# split a string by separator for example:
# split("ll","hello world") -> ["he","o world"]
# this function will return a vector.
var split=func(separator,str){
return __builtin_split(separator,str);
return __split(separator,str);
}
# rand has the same function as the rand in C
# if seed is nil, it will return the random number.
# if seed is not nil, it will be initialized by this seed.
var rand=func(seed=nil){
return __builtin_rand(seed);
return __rand(seed);
}
# id will return the pointer of an gc-object.
# if this object is not managed by gc, it will return 0.
var id=func(object){
return __builtin_id(object);
return __id(object);
}
# int will get the integer of input number.
# but carefully use it, because int has range between -2147483648~2147483647
var int=func(val){
return __builtin_int(val);
return __int(val);
}
# floor will get the integral number of input argument
# which is less than or equal to this argument
var floor=func(val){
return __builtin_floor(val);
return __floor(val);
}
# abort using std::abort
var abort=func(){
__builtin_abort();
__abort();
}
# abs gets absolute number.
@ -73,47 +73,47 @@ var abs=func(n){
# num will change all the other types into number.
# mostly used to change a numerable string.
var num=func(val){
return __builtin_num(val);
return __num(val);
}
# pop used to pop the last element in a vector.
# this function will return the value that poped if vector has element(s).
# if the vector is empty, it will return nil.
var pop=func(vec){
return __builtin_pop(vec);
return __pop(vec);
}
# str is used to change number into string.
var str=func(num){
return __builtin_str(num);
return __str(num);
}
# size can get the size of a string/vector/hashmap.
# in fact it can also get the size of number, and the result is the number itself.
# so don't do useless things, though it really works.
var size=func(object){
return __builtin_size(object);
return __size(object);
}
# contains is used to check if a key exists in a hashmap/dict.
var contains=func(hash,key){
return __builtin_contains(hash,key);
return __contains(hash,key);
}
# delete is used to delete a pair in a hashmap/dict by key.
var delete=func(hash,key){
return __builtin_delete(hash,key);
return __delete(hash,key);
}
# keys is used to get all keys in a hashmap/dict.
# this function will return a vector.
var keys=func(hash){
return __builtin_keys(hash);
return __keys(hash);
}
# time has the same function in C.
var time=func(begin){
return __builtin_time(begin);
return __time(begin);
}
var systime=func(){
return time(0);
@ -122,18 +122,18 @@ var systime=func(){
# die is a special native function.
# use it at where you want the program to crash immediately.
var die=func(str){
return __builtin_die(str);
return __die(str);
}
# find will give the first position of the needle in haystack
var find=func(needle,haystack){
return __builtin_find(needle,haystack);
return __find(needle,haystack);
}
# typeof is used to get the type of an object.
# this function returns a string.
var typeof=func(object){
return __builtin_type(object);
return __type(object);
}
# subvec is used to get part of a vector
@ -144,37 +144,37 @@ var subvec=func(vec,begin,length=nil){
# substr will get the sub-string.
# it gets the string, the begin index and sub-string's length as arguments.
var substr=func(str,begin,len){
return __builtin_substr(str,begin,len);
return __substr(str,begin,len);
}
# streq is used to compare if two strings are the same.
var streq=func(a,b){
return __builtin_streq(a,b);
return __streq(a,b);
}
# left is used to get the sub-string like substr.
# but the begin index is 0.
var left=func(str,len){
return __builtin_left(str,len);
return __left(str,len);
}
# right i used to get the sub-string like substr.
# but the begin index is strlen-len.
var right=func(str,len){
return __builtin_right(str,len);
return __right(str,len);
}
# cmp is used to compare two strings.
# normal string will not be correctly compared by operators < > <= >=
# because these operators will turn strings into numbers then compare.
var cmp=func(a,b){
return __builtin_cmp(a,b);
return __cmp(a,b);
}
# chr is used to get the character by ascii-number.
# for example chr(65) -> 'A'
var chr=func(code){
return __builtin_chr(code);
return __chr(code);
}
# mut is used to change unmutable strings to mutable.
@ -190,13 +190,13 @@ var srand=func(){
# values() gets all values in a hash.
var values=func(hash){
return __builtin_values(hash);
return __values(hash);
}
# println has the same function as print.
# but it will output a '\n' after using print.
var println=func(elems...){
return __builtin_println(elems);
return __println(elems);
}
var isfunc=func(f){
@ -263,7 +263,7 @@ var assert=func(condition,message="assertion failed!"){
var maketimestamp=func(){
var t=0;
var millisec=func(){
return __builtin_millisec;
return __millisec;
}
return {
stamp:func(){t=millisec();},
@ -274,7 +274,7 @@ var maketimestamp=func(){
# md5
var md5=func(str){
return __builtin_md5(str);
return __md5(str);
}
var io=
@ -283,29 +283,29 @@ var io=
SEEK_CUR:1,
SEEK_END:2,
# get content of a file by filename. returns a string.
fin: func(filename){return __builtin_fin(filename);},
fin: func(filename){return __fin(filename);},
# input a string as the content of a file.
fout: func(filename,str){return __builtin_fout(filename,str);},
fout: func(filename,str){return __fout(filename,str);},
# same as C fopen. open file and get the FILE*.
open: func(filename,mode="r"){return __builtin_open(filename,mode);},
open: func(filename,mode="r"){return __open(filename,mode);},
# same as C fclose. close file by FILE*.
close: func(filehandle){return __builtin_close(filehandle);},
close: func(filehandle){return __close(filehandle);},
# same as C fread. read file by FILE*.
# caution: buf must be a mutable string.use mut("") to get an empty mutable string.
read: func(filehandle,buf,len){return __builtin_read(filehandle,buf,len);},
read: func(filehandle,buf,len){return __read(filehandle,buf,len);},
# same as C fwrite. write file by FILE*.
write: func(filehandle,str){return __builtin_write(filehandle,str);},
write: func(filehandle,str){return __write(filehandle,str);},
# same as C fseek. seek place by FILE*.
seek: func(filehandle,pos,whence){return __builtin_seek(filehandle,pos,whence);},
seek: func(filehandle,pos,whence){return __seek(filehandle,pos,whence);},
# same as C ftell.
tell: func(filehandle){return __builtin_tell(filehandle);},
tell: func(filehandle){return __tell(filehandle);},
# read file by lines. use FILE*.
# get nil if EOF
readln:func(filehandle){return __builtin_readln(filehandle);},
readln:func(filehandle){return __readln(filehandle);},
# same as C stat.
stat: func(filename){return __builtin_stat(filename);},
stat: func(filename){return __stat(filename);},
# same as C feof. check if FILE* gets the end of file(EOF).
eof: func(filehandle){return __builtin_eof(filehandle);}
eof: func(filehandle){return __eof(filehandle);}
};
# get file status. using data from io.stat
@ -331,45 +331,45 @@ var fstat=func(filename){
var bits=
{
# i32 xor
i32_xor: func(a,b){return __builtin_i32xor(a,b); },
i32_xor: func(a,b){return __i32xor(a,b); },
# i32 and
i32_and: func(a,b){return __builtin_i32and(a,b); },
i32_and: func(a,b){return __i32and(a,b); },
# i32 or
i32_or: func(a,b){return __builtin_i32or(a,b); },
i32_or: func(a,b){return __i32or(a,b); },
# i32 nand
i32_nand:func(a,b){return __builtin_i32nand(a,b);},
i32_nand:func(a,b){return __i32nand(a,b);},
# i32 not
i32_not: func(a) {return __builtin_i32not(a); },
i32_not: func(a) {return __i32not(a); },
# u32 xor
u32_xor: func(a,b){return __builtin_u32xor(a,b); },
u32_xor: func(a,b){return __u32xor(a,b); },
# u32 and
u32_and: func(a,b){return __builtin_u32and(a,b); },
u32_and: func(a,b){return __u32and(a,b); },
# u32 or
u32_or: func(a,b){return __builtin_u32or(a,b); },
u32_or: func(a,b){return __u32or(a,b); },
# u32 nand
u32_nand:func(a,b){return __builtin_u32nand(a,b);},
u32_nand:func(a,b){return __u32nand(a,b);},
# u32 not
u32_not: func(a) {return __builtin_u32not(a); },
u32_not: func(a) {return __u32not(a); },
# get bit data from a special string. for example:
# bits.fld(s,0,3);
# if s stores 10100010(162)
# will get 101(5).
fld: func(str,startbit,len){return __builtin_fld;},
fld: func(str,startbit,len){return __fld;},
# get sign-extended data from a special string. for example:
# bits.sfld(s,0,3);
# if s stores 10100010(162)
# will get 101(5) then this will be signed extended to
# 11111101(-3).
sfld: func(str,startbit,len){return __builtin_sfld;},
sfld: func(str,startbit,len){return __sfld;},
# set value into a special string to store it. little-endian, for example:
# bits.setfld(s,0,8,69);
# set 01000101(69) to string will get this:
# 10100010(162)
# so s[0]=162.
setfld: func(str,startbit,len,val){return __builtin_setfld;},
setfld: func(str,startbit,len,val){return __setfld;},
# get a special string filled by '\0' to use in setfld.
buf: func(len){return __builtin_buf;}
buf: func(len){return __buf;}
};
# mostly used math functions and special constants, you know.
@ -380,39 +380,39 @@ var math=
inf: 1/0,
nan: 0/0,
abs: func(x) {return x>0?x:-x; },
floor: func(x) {return __builtin_floor(x); },
pow: func(x,y){return __builtin_pow(x,y); },
sin: func(x) {return __builtin_sin(x); },
cos: func(x) {return __builtin_cos(x); },
tan: func(x) {return __builtin_tan(x); },
exp: func(x) {return __builtin_exp(x); },
lg: func(x) {return __builtin_lg(x); },
ln: func(x) {return __builtin_ln(x); },
sqrt: func(x) {return __builtin_sqrt(x); },
atan2: func(x,y){return __builtin_atan2(x,y);},
isnan: func(x) {return __builtin_isnan(x); },
floor: func(x) {return __floor(x); },
pow: func(x,y){return __pow(x,y); },
sin: func(x) {return __sin(x); },
cos: func(x) {return __cos(x); },
tan: func(x) {return __tan(x); },
exp: func(x) {return __exp(x); },
lg: func(x) {return __lg(x); },
ln: func(x) {return __ln(x); },
sqrt: func(x) {return __sqrt(x); },
atan2: func(x,y){return __atan2(x,y);},
isnan: func(x) {return __isnan(x); },
max: func(x,y){return x>y?x:y; },
min: func(x,y){return x<y?x:y; }
};
var unix=
{
pipe: func(){return __builtin_pipe;},
fork: func(){return __builtin_fork;},
pipe: func(){return __pipe;},
fork: func(){return __fork;},
dup2: func(fd0,fd1){die("not supported yet");},
exec: func(filename,argv,envp){die("not supported yet");},
waitpid: func(pid,nohang=0){return __builtin_waitpid;},
waitpid: func(pid,nohang=0){return __waitpid;},
isdir: func(path){return !!bits.u32_and(io.stat(path)[2],0x4000);}, # S_IFDIR 0x4000
isfile: func(path){return !!bits.u32_and(io.stat(path)[2],0x8000);}, # S_IFREG 0x8000
opendir: func(path){return __builtin_opendir;},
readdir: func(handle){return __builtin_readdir;},
closedir: func(handle){return __builtin_closedir;},
opendir: func(path){return __opendir;},
readdir: func(handle){return __readdir;},
closedir: func(handle){return __closedir;},
time: func(){return time(0);},
sleep: func(secs){return __builtin_sleep(secs);},
chdir: func(path){return __builtin_chdir(path);},
environ: func(){return __builtin_environ();},
getcwd: func(){return __builtin_getcwd();},
getenv: func(envvar){return __builtin_getenv(envvar);},
sleep: func(secs){return __sleep(secs);},
chdir: func(path){return __chdir(path);},
environ: func(){return __environ();},
getcwd: func(){return __getcwd();},
getenv: func(envvar){return __getenv(envvar);},
getpath: func(){return split(os.platform()=="windows"?";":":",unix.getenv("PATH"));}
};
@ -421,13 +421,13 @@ var unix=
var dylib=
{
# open dynamic lib.
dlopen: func(libname){return __builtin_dlopen;},
dlopen: func(libname){return __dlopen;},
# load symbol from an open dynamic lib.
dlsym: func(lib,sym){return __builtin_dlsym; },
dlsym: func(lib,sym){return __dlsym; },
# close dynamic lib, this operation will make all the symbols loaded from it invalid.
dlclose: func(lib){return __builtin_dlclose; },
dlclose: func(lib){return __dlclose; },
# call the loaded symbol.
dlcall: func(funcptr,args...){return __builtin_dlcall}
dlcall: func(funcptr,args...){return __dlcall}
};
# os is used to use or get some os-related info/functions.
@ -435,8 +435,8 @@ var dylib=
var os=
{
# get a string that tell which os it runs on.
platform: func(){return __builtin_platform;},
time: func(){return __builtin_logtime; }
platform: func(){return __platform;},
time: func(){return __logtime; }
};
# runtime gives us some functions that we could manage it manually.
@ -444,10 +444,10 @@ var runtime=
{
# do garbage collection manually.
# carefully use it because using it frequently may make program running slower.
gc: func(){return __builtin_gc;},
gc: func(){return __gc;},
# command line arguments
argv: func(){return __builtin_sysargv;}
argv: func(){return __sysargv;}
};
# important global constants
@ -490,9 +490,9 @@ var compile=func(code,filename="<compile>"){
}
var coroutine={
create: func(function){return __builtin_cocreate;},
resume: func(co) {return __builtin_coresume;},
yield: func(args...) {return __builtin_coyield; },
status: func(co) {return __builtin_costatus;},
running:func() {return __builtin_corun; }
create: func(function){return __cocreate;},
resume: func(co) {return __coresume;},
yield: func(args...) {return __coyield; },
status: func(co) {return __costatus;},
running:func() {return __corun; }
};

View File

@ -7,103 +7,102 @@
#endif
// declaration of builtin functions
// to add new builtin function, declare it here and write the definition below
#define nas_native(name) nasal_ref name(nasal_ref*,nasal_gc&)
nas_native(builtin_print);
nas_native(builtin_println);
nas_native(builtin_abort);
nas_native(builtin_append);
nas_native(builtin_setsize);
nas_native(builtin_system);
nas_native(builtin_input);
nas_native(builtin_fin);
nas_native(builtin_fout);
nas_native(builtin_split);
nas_native(builtin_rand);
nas_native(builtin_id);
nas_native(builtin_int);
nas_native(builtin_floor);
nas_native(builtin_num);
nas_native(builtin_pop);
nas_native(builtin_str);
nas_native(builtin_size);
nas_native(builtin_i32xor);
nas_native(builtin_i32and);
nas_native(builtin_i32or);
nas_native(builtin_i32nand);
nas_native(builtin_i32not);
nas_native(builtin_u32xor);
nas_native(builtin_u32and);
nas_native(builtin_u32or);
nas_native(builtin_u32nand);
nas_native(builtin_u32not);
nas_native(builtin_pow);
nas_native(builtin_sin);
nas_native(builtin_cos);
nas_native(builtin_tan);
nas_native(builtin_exp);
nas_native(builtin_lg);
nas_native(builtin_ln);
nas_native(builtin_sqrt);
nas_native(builtin_atan2);
nas_native(builtin_isnan);
nas_native(builtin_time);
nas_native(builtin_contains);
nas_native(builtin_delete);
nas_native(builtin_keys);
nas_native(builtin_die);
nas_native(builtin_find);
nas_native(builtin_type);
nas_native(builtin_substr);
nas_native(builtin_streq);
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);
nas_native(builtin_write);
nas_native(builtin_seek);
nas_native(builtin_tell);
nas_native(builtin_readln);
nas_native(builtin_stat);
nas_native(builtin_eof);
nas_native(builtin_fld);
nas_native(builtin_sfld);
nas_native(builtin_setfld);
nas_native(builtin_buf);
nas_native(builtin_sleep);
nas_native(builtin_pipe);
nas_native(builtin_fork);
nas_native(builtin_waitpid);
nas_native(builtin_opendir);
nas_native(builtin_readdir);
nas_native(builtin_closedir);
nas_native(builtin_chdir);
nas_native(builtin_environ);
nas_native(builtin_getcwd);
nas_native(builtin_getenv);
nas_native(builtin_dlopen);
nas_native(builtin_dlsym);
nas_native(builtin_dlclose);
nas_native(builtin_dlcall);
nas_native(builtin_platform);
nas_native(builtin_gc);
nas_native(builtin_md5);
nas_native(builtin_cocreate);
nas_native(builtin_coresume);
nas_native(builtin_coyield);
nas_native(builtin_costatus);
nas_native(builtin_corun);
nas_native(builtin_millisec);
nas_native(builtin_sysargv);
nas_native(builtin_logtime);
#define native(name) nasal_ref name(nasal_ref*,nasal_gc&)
native(builtin_print);
native(builtin_println);
native(builtin_abort);
native(builtin_append);
native(builtin_setsize);
native(builtin_system);
native(builtin_input);
native(builtin_fin);
native(builtin_fout);
native(builtin_split);
native(builtin_rand);
native(builtin_id);
native(builtin_int);
native(builtin_floor);
native(builtin_num);
native(builtin_pop);
native(builtin_str);
native(builtin_size);
native(builtin_i32xor);
native(builtin_i32and);
native(builtin_i32or);
native(builtin_i32nand);
native(builtin_i32not);
native(builtin_u32xor);
native(builtin_u32and);
native(builtin_u32or);
native(builtin_u32nand);
native(builtin_u32not);
native(builtin_pow);
native(builtin_sin);
native(builtin_cos);
native(builtin_tan);
native(builtin_exp);
native(builtin_lg);
native(builtin_ln);
native(builtin_sqrt);
native(builtin_atan2);
native(builtin_isnan);
native(builtin_time);
native(builtin_contains);
native(builtin_delete);
native(builtin_keys);
native(builtin_die);
native(builtin_find);
native(builtin_type);
native(builtin_substr);
native(builtin_streq);
native(builtin_left);
native(builtin_right);
native(builtin_cmp);
native(builtin_chr);
native(builtin_values);
native(builtin_open);
native(builtin_close);
native(builtin_read);
native(builtin_write);
native(builtin_seek);
native(builtin_tell);
native(builtin_readln);
native(builtin_stat);
native(builtin_eof);
native(builtin_fld);
native(builtin_sfld);
native(builtin_setfld);
native(builtin_buf);
native(builtin_sleep);
native(builtin_pipe);
native(builtin_fork);
native(builtin_waitpid);
native(builtin_opendir);
native(builtin_readdir);
native(builtin_closedir);
native(builtin_chdir);
native(builtin_environ);
native(builtin_getcwd);
native(builtin_getenv);
native(builtin_dlopen);
native(builtin_dlsym);
native(builtin_dlclose);
native(builtin_dlcall);
native(builtin_platform);
native(builtin_gc);
native(builtin_md5);
native(builtin_cocreate);
native(builtin_coresume);
native(builtin_coyield);
native(builtin_costatus);
native(builtin_corun);
native(builtin_millisec);
native(builtin_sysargv);
native(builtin_logtime);
nasal_ref builtin_err(const char* func_name,const std::string& info)
nasal_ref builtin_err(const std::string& errfunc,const std::string& info)
{
std::cerr<<"[vm] "<<func_name<<": "<<info<<".\n";
std::cerr<<"[vm] "<<errfunc<<": "<<info<<".\n";
return {vm_none};
}
@ -115,98 +114,98 @@ struct
nasal_ref (*func)(nasal_ref*,nasal_gc&);
} builtin[]=
{
{"__builtin_print", builtin_print },
{"__builtin_println", builtin_println },
{"__builtin_abort", builtin_abort },
{"__builtin_append", builtin_append },
{"__builtin_setsize", builtin_setsize },
{"__builtin_system", builtin_system },
{"__builtin_input", builtin_input },
{"__builtin_fin", builtin_fin },
{"__builtin_fout", builtin_fout },
{"__builtin_split", builtin_split },
{"__builtin_rand", builtin_rand },
{"__builtin_id", builtin_id },
{"__builtin_int", builtin_int },
{"__builtin_floor", builtin_floor },
{"__builtin_num", builtin_num },
{"__builtin_pop", builtin_pop },
{"__builtin_str", builtin_str },
{"__builtin_size", builtin_size },
{"__builtin_i32xor", builtin_i32xor },
{"__builtin_i32and", builtin_i32and },
{"__builtin_i32or", builtin_i32or },
{"__builtin_i32nand", builtin_i32nand },
{"__builtin_i32not", builtin_i32not },
{"__builtin_u32xor", builtin_u32xor },
{"__builtin_u32and", builtin_u32and },
{"__builtin_u32or", builtin_u32or },
{"__builtin_u32nand", builtin_u32nand },
{"__builtin_u32not", builtin_u32not },
{"__builtin_pow", builtin_pow },
{"__builtin_sin", builtin_sin },
{"__builtin_cos", builtin_cos },
{"__builtin_tan", builtin_tan },
{"__builtin_exp", builtin_exp },
{"__builtin_lg", builtin_lg },
{"__builtin_ln", builtin_ln },
{"__builtin_sqrt", builtin_sqrt },
{"__builtin_atan2", builtin_atan2 },
{"__builtin_isnan", builtin_isnan },
{"__builtin_time", builtin_time },
{"__builtin_contains",builtin_contains},
{"__builtin_delete", builtin_delete },
{"__builtin_keys", builtin_keys },
{"__builtin_die", builtin_die },
{"__builtin_find", builtin_find },
{"__builtin_type", builtin_type },
{"__builtin_substr", builtin_substr },
{"__builtin_streq", builtin_streq },
{"__builtin_left", builtin_left },
{"__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 },
{"__builtin_write", builtin_write },
{"__builtin_seek", builtin_seek },
{"__builtin_tell", builtin_tell },
{"__builtin_readln", builtin_readln },
{"__builtin_stat", builtin_stat },
{"__builtin_eof", builtin_eof },
{"__builtin_fld", builtin_fld },
{"__builtin_sfld", builtin_sfld },
{"__builtin_setfld", builtin_setfld },
{"__builtin_buf", builtin_buf },
{"__builtin_sleep", builtin_sleep },
{"__builtin_pipe", builtin_pipe },
{"__builtin_fork", builtin_fork },
{"__builtin_waitpid", builtin_waitpid },
{"__builtin_opendir", builtin_opendir },
{"__builtin_readdir", builtin_readdir },
{"__builtin_closedir",builtin_closedir},
{"__builtin_chdir", builtin_chdir },
{"__builtin_environ", builtin_environ },
{"__builtin_getcwd", builtin_getcwd },
{"__builtin_getenv", builtin_getenv },
{"__builtin_dlopen", builtin_dlopen },
{"__builtin_dlsym", builtin_dlsym },
{"__builtin_dlclose", builtin_dlclose },
{"__builtin_dlcall", builtin_dlcall },
{"__builtin_platform",builtin_platform},
{"__builtin_gc", builtin_gc },
{"__builtin_md5", builtin_md5 },
{"__builtin_cocreate",builtin_cocreate},
{"__builtin_coresume",builtin_coresume},
{"__builtin_coyield", builtin_coyield },
{"__builtin_costatus",builtin_costatus},
{"__builtin_corun" ,builtin_corun },
{"__builtin_millisec",builtin_millisec},
{"__builtin_sysargv", builtin_sysargv },
{"__builtin_logtime", builtin_logtime },
{nullptr, nullptr }
{"__print", builtin_print },
{"__println", builtin_println },
{"__abort", builtin_abort },
{"__append", builtin_append },
{"__setsize", builtin_setsize },
{"__system", builtin_system },
{"__input", builtin_input },
{"__fin", builtin_fin },
{"__fout", builtin_fout },
{"__split", builtin_split },
{"__rand", builtin_rand },
{"__id", builtin_id },
{"__int", builtin_int },
{"__floor", builtin_floor },
{"__num", builtin_num },
{"__pop", builtin_pop },
{"__str", builtin_str },
{"__size", builtin_size },
{"__i32xor", builtin_i32xor },
{"__i32and", builtin_i32and },
{"__i32or", builtin_i32or },
{"__i32nand", builtin_i32nand },
{"__i32not", builtin_i32not },
{"__u32xor", builtin_u32xor },
{"__u32and", builtin_u32and },
{"__u32or", builtin_u32or },
{"__u32nand", builtin_u32nand },
{"__u32not", builtin_u32not },
{"__pow", builtin_pow },
{"__sin", builtin_sin },
{"__cos", builtin_cos },
{"__tan", builtin_tan },
{"__exp", builtin_exp },
{"__lg", builtin_lg },
{"__ln", builtin_ln },
{"__sqrt", builtin_sqrt },
{"__atan2", builtin_atan2 },
{"__isnan", builtin_isnan },
{"__time", builtin_time },
{"__contains",builtin_contains},
{"__delete", builtin_delete },
{"__keys", builtin_keys },
{"__die", builtin_die },
{"__find", builtin_find },
{"__type", builtin_type },
{"__substr", builtin_substr },
{"__streq", builtin_streq },
{"__left", builtin_left },
{"__right", builtin_right },
{"__cmp", builtin_cmp },
{"__chr", builtin_chr },
{"__values", builtin_values },
{"__open", builtin_open },
{"__close", builtin_close },
{"__read", builtin_read },
{"__write", builtin_write },
{"__seek", builtin_seek },
{"__tell", builtin_tell },
{"__readln", builtin_readln },
{"__stat", builtin_stat },
{"__eof", builtin_eof },
{"__fld", builtin_fld },
{"__sfld", builtin_sfld },
{"__setfld", builtin_setfld },
{"__buf", builtin_buf },
{"__sleep", builtin_sleep },
{"__pipe", builtin_pipe },
{"__fork", builtin_fork },
{"__waitpid", builtin_waitpid },
{"__opendir", builtin_opendir },
{"__readdir", builtin_readdir },
{"__closedir",builtin_closedir},
{"__chdir", builtin_chdir },
{"__environ", builtin_environ },
{"__getcwd", builtin_getcwd },
{"__getenv", builtin_getenv },
{"__dlopen", builtin_dlopen },
{"__dlsym", builtin_dlsym },
{"__dlclose", builtin_dlclose },
{"__dlcall", builtin_dlcall },
{"__platform",builtin_platform},
{"__gc", builtin_gc },
{"__md5", builtin_md5 },
{"__cocreate",builtin_cocreate},
{"__coresume",builtin_coresume},
{"__coyield", builtin_coyield },
{"__costatus",builtin_costatus},
{"__corun" ,builtin_corun },
{"__millisec",builtin_millisec},
{"__sysargv", builtin_sysargv },
{"__logtime", builtin_logtime },
{nullptr, nullptr }
};
inline void print_core(std::vector<nasal_ref>& elems)
@ -347,7 +346,7 @@ nasal_ref builtin_split(nasal_ref* local,nasal_gc& gc)
pos=s.find(deli,last);
}
if(last!=s.length())
vec.push_back(gc.newstr(s.substr(last,s.length()-1)));
vec.push_back(gc.newstr(s.substr(last)));
gc.temp=nil;
return res;
}

View File

@ -7,12 +7,13 @@ class nasal_dbg:public nasal_vm
{
private:
bool next_step;
size_t fsize;
uint16_t bk_fidx;
uint32_t bk_line;
fstreamline src;
std::vector<std::string> parse(const std::string&);
uint16_t get_fileindex(const std::string&);
uint16_t fileindex(const std::string&);
void err();
void help();
void stepinfo();
@ -31,25 +32,22 @@ public:
std::vector<std::string> nasal_dbg::parse(const std::string& cmd)
{
std::vector<std::string> res;
std::string tmp="";
for(uint32_t i=0;i<cmd.length();++i)
size_t last=0,pos=cmd.find(" ",0);
while(pos!=std::string::npos)
{
if(cmd[i]==' ' && tmp.length())
{
res.push_back(tmp);
tmp="";
continue;
}
tmp+=cmd[i];
if(pos>last)
res.push_back(cmd.substr(last,pos-last));
last=pos+1;
pos=cmd.find(" ",last);
}
if(tmp.length())
res.push_back(tmp);
if(last<cmd.length())
res.push_back(cmd.substr(last));
return res;
}
uint16_t nasal_dbg::get_fileindex(const std::string& filename)
uint16_t nasal_dbg::fileindex(const std::string& filename)
{
for(uint16_t i=0;i<files_size;++i)
for(uint16_t i=0;i<fsize;++i)
if(filename==files[i])
return i;
return 65535;
@ -133,7 +131,7 @@ void nasal_dbg::interact()
else if(res[0]=="c" || res[0]=="continue")
return;
else if(res[0]=="f" || res[0]=="file")
for(size_t i=0;i<files_size;++i)
for(size_t i=0;i<fsize;++i)
printf("[%zu] %s\n",i,files[i].c_str());
else if(res[0]=="g" || res[0]=="global")
global_state();
@ -158,7 +156,7 @@ void nasal_dbg::interact()
}
else if(res.size()==3 && (res[0]=="bk" || res[0]=="break"))
{
bk_fidx=get_fileindex(res[1]);
bk_fidx=fileindex(res[1]);
if(bk_fidx==65535)
{
printf("cannot find file named \"%s\"\n",res[1].c_str());
@ -181,6 +179,7 @@ void nasal_dbg::run(
const std::vector<std::string>& argv)
{
detail_info=true;
fsize=linker.get_file().size();
init(gen.get_strs(),gen.get_nums(),gen.get_code(),linker.get_file(),argv);
const void* oprs[]=
{

View File

@ -39,7 +39,7 @@ nasal_import::nasal_import(nasal_err& e):lib_loaded(false),nerr(e){
pos=PATH.find(sep,last);
}
if(last!=PATH.length())
envpath.push_back(PATH.substr(last,pos-last));
envpath.push_back(PATH.substr(last));
}
std::string nasal_import::path(const nasal_ast& node)

View File

@ -47,19 +47,17 @@ void calc_const(nasal_ast& root)
}
if(vec.size()!=2)
return;
if(root.type()!=ast_add &&
root.type()!=ast_sub &&
root.type()!=ast_mult &&
root.type()!=ast_div &&
root.type()!=ast_link &&
root.type()!=ast_less &&
root.type()!=ast_leq &&
root.type()!=ast_grt &&
root.type()!=ast_geq)
if(root.type()!=ast_add && root.type()!=ast_sub &&
root.type()!=ast_mult && root.type()!=ast_div &&
root.type()!=ast_link && root.type()!=ast_less &&
root.type()!=ast_leq && root.type()!=ast_grt &&
root.type()!=ast_geq)
return;
if(root.type()==ast_link && vec[0].type()==ast_str && vec[1].type()==ast_str)
if(root.type()==ast_link &&
vec[0].type()==ast_str && vec[1].type()==ast_str)
const_str(root);
else if(root.type()!=ast_link && vec[0].type()==ast_num && vec[1].type()==ast_num)
else if(root.type()!=ast_link &&
vec[0].type()==ast_num && vec[1].type()==ast_num)
const_num(root);
}
void optimize(nasal_ast& root)

View File

@ -22,7 +22,6 @@ protected:
nasal_ref stack[STACK_DEPTH];
/* values used for debugger */
size_t files_size;
const std::string* files; // ref from nasal_import
const opcode* bytecode; // ref from nasal_codegen
@ -144,7 +143,6 @@ void nasal_vm::init(
str_table=strs.data();
bytecode=code.data();
files=filenames.data();
files_size=filenames.size();
/* set canary and program counter */
pc=0;
@ -340,18 +338,17 @@ void nasal_vm::opcallsort(const uint64_t* arr)
{
typedef std::pair<uint32_t,uint64_t> op;
std::vector<op> opcall;
for(uint32_t i=0;i<=op_ret;++i)
uint64_t total=0;
for(uint32_t i=0;i<op_ret+1;++i)
{
total+=arr[i];
if(arr[i])
opcall.push_back({i,arr[i]});
std::sort(
opcall.begin(),
opcall.end(),
[](op& a,op& b){return a.second>b.second;}
}
std::sort(opcall.begin(),opcall.end(),
[](const op& a,const op& b){return a.second>b.second;}
);
std::cout<<"\noperands call info";
uint64_t total=0;
for(auto& i:opcall)
total+=i.second;
for(auto& i:opcall)
{
uint64_t rate=i.second*100/total;

View File

@ -3,66 +3,66 @@
# print is used to print all things in nasal, try and see how it works.
# this function uses std::cout/printf to output logs.
var print=func(elems...){
return __builtin_print(elems);
return __print(elems);
}
# append is used to add values into a vector.
var append=func(vec,elems...){
return __builtin_append(vec,elems);
return __append(vec,elems);
}
# setsize is used to change the size of vector.
# if the size is larger than before,
# this function will fill vm_nil into uninitialized space.
var setsize=func(vec,size){
return __builtin_setsize(vec,size);
return __setsize(vec,size);
}
# system has the same use in C.
var system=func(str){
return __builtin_system(str);
return __system(str);
}
# input uses std::cin and returns what we input.
var input=func(){
return __builtin_input();
return __input();
}
# split a string by separator for example:
# split("ll","hello world") -> ["he","o world"]
# this function will return a vector.
var split=func(separator,str){
return __builtin_split(separator,str);
return __split(separator,str);
}
# rand has the same function as the rand in C
# if seed is nil, it will return the random number.
# if seed is not nil, it will be initialized by this seed.
var rand=func(seed=nil){
return __builtin_rand(seed);
return __rand(seed);
}
# id will return the pointer of an gc-object.
# if this object is not managed by gc, it will return 0.
var id=func(object){
return __builtin_id(object);
return __id(object);
}
# int will get the integer of input number.
# but carefully use it, because int has range between -2147483648~2147483647
var int=func(val){
return __builtin_int(val);
return __int(val);
}
# floor will get the integral number of input argument
# which is less than or equal to this argument
var floor=func(val){
return __builtin_floor(val);
return __floor(val);
}
# abort using std::abort
var abort=func(){
__builtin_abort();
__abort();
}
# abs gets absolute number.
@ -73,47 +73,47 @@ var abs=func(n){
# num will change all the other types into number.
# mostly used to change a numerable string.
var num=func(val){
return __builtin_num(val);
return __num(val);
}
# pop used to pop the last element in a vector.
# this function will return the value that poped if vector has element(s).
# if the vector is empty, it will return nil.
var pop=func(vec){
return __builtin_pop(vec);
return __pop(vec);
}
# str is used to change number into string.
var str=func(num){
return __builtin_str(num);
return __str(num);
}
# size can get the size of a string/vector/hashmap.
# in fact it can also get the size of number, and the result is the number itself.
# so don't do useless things, though it really works.
var size=func(object){
return __builtin_size(object);
return __size(object);
}
# contains is used to check if a key exists in a hashmap/dict.
var contains=func(hash,key){
return __builtin_contains(hash,key);
return __contains(hash,key);
}
# delete is used to delete a pair in a hashmap/dict by key.
var delete=func(hash,key){
return __builtin_delete(hash,key);
return __delete(hash,key);
}
# keys is used to get all keys in a hashmap/dict.
# this function will return a vector.
var keys=func(hash){
return __builtin_keys(hash);
return __keys(hash);
}
# time has the same function in C.
var time=func(begin){
return __builtin_time(begin);
return __time(begin);
}
var systime=func(){
return time(0);
@ -122,18 +122,18 @@ var systime=func(){
# die is a special native function.
# use it at where you want the program to crash immediately.
var die=func(str){
return __builtin_die(str);
return __die(str);
}
# find will give the first position of the needle in haystack
var find=func(needle,haystack){
return __builtin_find(needle,haystack);
return __find(needle,haystack);
}
# typeof is used to get the type of an object.
# this function returns a string.
var typeof=func(object){
return __builtin_type(object);
return __type(object);
}
# subvec is used to get part of a vector
@ -144,37 +144,37 @@ var subvec=func(vec,begin,length=nil){
# substr will get the sub-string.
# it gets the string, the begin index and sub-string's length as arguments.
var substr=func(str,begin,len){
return __builtin_substr(str,begin,len);
return __substr(str,begin,len);
}
# streq is used to compare if two strings are the same.
var streq=func(a,b){
return __builtin_streq(a,b);
return __streq(a,b);
}
# left is used to get the sub-string like substr.
# but the begin index is 0.
var left=func(str,len){
return __builtin_left(str,len);
return __left(str,len);
}
# right i used to get the sub-string like substr.
# but the begin index is strlen-len.
var right=func(str,len){
return __builtin_right(str,len);
return __right(str,len);
}
# cmp is used to compare two strings.
# normal string will not be correctly compared by operators < > <= >=
# because these operators will turn strings into numbers then compare.
var cmp=func(a,b){
return __builtin_cmp(a,b);
return __cmp(a,b);
}
# chr is used to get the character by ascii-number.
# for example chr(65) -> 'A'
var chr=func(code){
return __builtin_chr(code);
return __chr(code);
}
# mut is used to change unmutable strings to mutable.
@ -190,13 +190,13 @@ var srand=func(){
# values() gets all values in a hash.
var values=func(hash){
return __builtin_values(hash);
return __values(hash);
}
# println has the same function as print.
# but it will output a '\n' after using print.
var println=func(elems...){
return __builtin_println(elems);
return __println(elems);
}
var isfunc=func(f){
@ -263,7 +263,7 @@ var assert=func(condition,message="assertion failed!"){
var maketimestamp=func(){
var t=0;
var millisec=func(){
return __builtin_millisec;
return __millisec;
}
return {
stamp:func(){t=millisec();},
@ -274,7 +274,7 @@ var maketimestamp=func(){
# md5
var md5=func(str){
return __builtin_md5(str);
return __md5(str);
}
var io=
@ -283,29 +283,29 @@ var io=
SEEK_CUR:1,
SEEK_END:2,
# get content of a file by filename. returns a string.
fin: func(filename){return __builtin_fin(filename);},
fin: func(filename){return __fin(filename);},
# input a string as the content of a file.
fout: func(filename,str){return __builtin_fout(filename,str);},
fout: func(filename,str){return __fout(filename,str);},
# same as C fopen. open file and get the FILE*.
open: func(filename,mode="r"){return __builtin_open(filename,mode);},
open: func(filename,mode="r"){return __open(filename,mode);},
# same as C fclose. close file by FILE*.
close: func(filehandle){return __builtin_close(filehandle);},
close: func(filehandle){return __close(filehandle);},
# same as C fread. read file by FILE*.
# caution: buf must be a mutable string.use mut("") to get an empty mutable string.
read: func(filehandle,buf,len){return __builtin_read(filehandle,buf,len);},
read: func(filehandle,buf,len){return __read(filehandle,buf,len);},
# same as C fwrite. write file by FILE*.
write: func(filehandle,str){return __builtin_write(filehandle,str);},
write: func(filehandle,str){return __write(filehandle,str);},
# same as C fseek. seek place by FILE*.
seek: func(filehandle,pos,whence){return __builtin_seek(filehandle,pos,whence);},
seek: func(filehandle,pos,whence){return __seek(filehandle,pos,whence);},
# same as C ftell.
tell: func(filehandle){return __builtin_tell(filehandle);},
tell: func(filehandle){return __tell(filehandle);},
# read file by lines. use FILE*.
# get nil if EOF
readln:func(filehandle){return __builtin_readln(filehandle);},
readln:func(filehandle){return __readln(filehandle);},
# same as C stat.
stat: func(filename){return __builtin_stat(filename);},
stat: func(filename){return __stat(filename);},
# same as C feof. check if FILE* gets the end of file(EOF).
eof: func(filehandle){return __builtin_eof(filehandle);}
eof: func(filehandle){return __eof(filehandle);}
};
# get file status. using data from io.stat
@ -331,45 +331,45 @@ var fstat=func(filename){
var bits=
{
# i32 xor
i32_xor: func(a,b){return __builtin_i32xor(a,b); },
i32_xor: func(a,b){return __i32xor(a,b); },
# i32 and
i32_and: func(a,b){return __builtin_i32and(a,b); },
i32_and: func(a,b){return __i32and(a,b); },
# i32 or
i32_or: func(a,b){return __builtin_i32or(a,b); },
i32_or: func(a,b){return __i32or(a,b); },
# i32 nand
i32_nand:func(a,b){return __builtin_i32nand(a,b);},
i32_nand:func(a,b){return __i32nand(a,b);},
# i32 not
i32_not: func(a) {return __builtin_i32not(a); },
i32_not: func(a) {return __i32not(a); },
# u32 xor
u32_xor: func(a,b){return __builtin_u32xor(a,b); },
u32_xor: func(a,b){return __u32xor(a,b); },
# u32 and
u32_and: func(a,b){return __builtin_u32and(a,b); },
u32_and: func(a,b){return __u32and(a,b); },
# u32 or
u32_or: func(a,b){return __builtin_u32or(a,b); },
u32_or: func(a,b){return __u32or(a,b); },
# u32 nand
u32_nand:func(a,b){return __builtin_u32nand(a,b);},
u32_nand:func(a,b){return __u32nand(a,b);},
# u32 not
u32_not: func(a) {return __builtin_u32not(a); },
u32_not: func(a) {return __u32not(a); },
# get bit data from a special string. for example:
# bits.fld(s,0,3);
# if s stores 10100010(162)
# will get 101(5).
fld: func(str,startbit,len){return __builtin_fld;},
fld: func(str,startbit,len){return __fld;},
# get sign-extended data from a special string. for example:
# bits.sfld(s,0,3);
# if s stores 10100010(162)
# will get 101(5) then this will be signed extended to
# 11111101(-3).
sfld: func(str,startbit,len){return __builtin_sfld;},
sfld: func(str,startbit,len){return __sfld;},
# set value into a special string to store it. little-endian, for example:
# bits.setfld(s,0,8,69);
# set 01000101(69) to string will get this:
# 10100010(162)
# so s[0]=162.
setfld: func(str,startbit,len,val){return __builtin_setfld;},
setfld: func(str,startbit,len,val){return __setfld;},
# get a special string filled by '\0' to use in setfld.
buf: func(len){return __builtin_buf;}
buf: func(len){return __buf;}
};
# mostly used math functions and special constants, you know.
@ -380,39 +380,39 @@ var math=
inf: 1/0,
nan: 0/0,
abs: func(x) {return x>0?x:-x; },
floor: func(x) {return __builtin_floor(x); },
pow: func(x,y){return __builtin_pow(x,y); },
sin: func(x) {return __builtin_sin(x); },
cos: func(x) {return __builtin_cos(x); },
tan: func(x) {return __builtin_tan(x); },
exp: func(x) {return __builtin_exp(x); },
lg: func(x) {return __builtin_lg(x); },
ln: func(x) {return __builtin_ln(x); },
sqrt: func(x) {return __builtin_sqrt(x); },
atan2: func(x,y){return __builtin_atan2(x,y);},
isnan: func(x) {return __builtin_isnan(x); },
floor: func(x) {return __floor(x); },
pow: func(x,y){return __pow(x,y); },
sin: func(x) {return __sin(x); },
cos: func(x) {return __cos(x); },
tan: func(x) {return __tan(x); },
exp: func(x) {return __exp(x); },
lg: func(x) {return __lg(x); },
ln: func(x) {return __ln(x); },
sqrt: func(x) {return __sqrt(x); },
atan2: func(x,y){return __atan2(x,y);},
isnan: func(x) {return __isnan(x); },
max: func(x,y){return x>y?x:y; },
min: func(x,y){return x<y?x:y; }
};
var unix=
{
pipe: func(){return __builtin_pipe;},
fork: func(){return __builtin_fork;},
pipe: func(){return __pipe;},
fork: func(){return __fork;},
dup2: func(fd0,fd1){die("not supported yet");},
exec: func(filename,argv,envp){die("not supported yet");},
waitpid: func(pid,nohang=0){return __builtin_waitpid;},
waitpid: func(pid,nohang=0){return __waitpid;},
isdir: func(path){return !!bits.u32_and(io.stat(path)[2],0x4000);}, # S_IFDIR 0x4000
isfile: func(path){return !!bits.u32_and(io.stat(path)[2],0x8000);}, # S_IFREG 0x8000
opendir: func(path){return __builtin_opendir;},
readdir: func(handle){return __builtin_readdir;},
closedir: func(handle){return __builtin_closedir;},
opendir: func(path){return __opendir;},
readdir: func(handle){return __readdir;},
closedir: func(handle){return __closedir;},
time: func(){return time(0);},
sleep: func(secs){return __builtin_sleep(secs);},
chdir: func(path){return __builtin_chdir(path);},
environ: func(){return __builtin_environ();},
getcwd: func(){return __builtin_getcwd();},
getenv: func(envvar){return __builtin_getenv(envvar);},
sleep: func(secs){return __sleep(secs);},
chdir: func(path){return __chdir(path);},
environ: func(){return __environ();},
getcwd: func(){return __getcwd();},
getenv: func(envvar){return __getenv(envvar);},
getpath: func(){return split(os.platform()=="windows"?";":":",unix.getenv("PATH"));}
};
@ -421,13 +421,13 @@ var unix=
var dylib=
{
# open dynamic lib.
dlopen: func(libname){return __builtin_dlopen;},
dlopen: func(libname){return __dlopen;},
# load symbol from an open dynamic lib.
dlsym: func(lib,sym){return __builtin_dlsym; },
dlsym: func(lib,sym){return __dlsym; },
# close dynamic lib, this operation will make all the symbols loaded from it invalid.
dlclose: func(lib){return __builtin_dlclose; },
dlclose: func(lib){return __dlclose; },
# call the loaded symbol.
dlcall: func(funcptr,args...){return __builtin_dlcall}
dlcall: func(funcptr,args...){return __dlcall}
};
# os is used to use or get some os-related info/functions.
@ -435,8 +435,8 @@ var dylib=
var os=
{
# get a string that tell which os it runs on.
platform: func(){return __builtin_platform;},
time: func(){return __builtin_logtime; }
platform: func(){return __platform;},
time: func(){return __logtime; }
};
# runtime gives us some functions that we could manage it manually.
@ -444,10 +444,10 @@ var runtime=
{
# do garbage collection manually.
# carefully use it because using it frequently may make program running slower.
gc: func(){return __builtin_gc;},
gc: func(){return __gc;},
# command line arguments
argv: func(){return __builtin_sysargv;}
argv: func(){return __sysargv;}
};
# important global constants
@ -490,9 +490,9 @@ var compile=func(code,filename="<compile>"){
}
var coroutine={
create: func(function){return __builtin_cocreate;},
resume: func(co) {return __builtin_coresume;},
yield: func(args...) {return __builtin_coyield; },
status: func(co) {return __builtin_costatus;},
running:func() {return __builtin_corun; }
create: func(function){return __cocreate;},
resume: func(co) {return __coresume;},
yield: func(args...) {return __coyield; },
status: func(co) {return __costatus;},
running:func() {return __corun; }
};

View File

@ -6,7 +6,7 @@
# all the invalid functions cannot be called
var module_call_func=func(fptr,args){
return __builtin_dlcall;
return __dlcall;
}
var extern={
new: func(fptr){

View File

@ -37,7 +37,7 @@ var productor=func(){
for(var i=0;;i+=1)
coroutine.yield(i);
}
var total=10000; # ms
var total=4000; # ms
var co=coroutine.create(productor);
var tm=maketimestamp();
@ -54,4 +54,4 @@ var consumer=func(){
tm.stamp();
while(tm.elapsedMSec()<total)
consumer();
println("\nexecute ",counter," tasks during ",total," ms, avg ",counter/(total)," tasks/ms.")
println("\nexecute ",counter," tasks during ",total," ms, avg ",counter/total," tasks/ms.")