diff --git a/lib.nas b/lib.nas index c4ad15a..68c97ec 100644 --- a/lib.nas +++ b/lib.nas @@ -167,19 +167,32 @@ var io= SEEK_SET:0, SEEK_CUR:1, SEEK_END:2, + # get content of a file by filename. returns a string. fin: func(filename){return __builtin_fin(filename);}, + # input a string as the content of a file. fout: func(filename,str){return __builtin_fout(filename,str);}, + # same as C fopen. open file and get the FILE*. open: func(filename,mode="r"){return __builtin_open(filename,mode);}, + # same as C fclose. close file by FILE*. close: func(filehandle){return __builtin_close(filehandle);}, + # same as C fread. read file by FILE*. read: func(filehandle,buf,len){return __builtin_read(filehandle,buf,len);}, + # same as C fwrite. write file by FILE*. write: func(filehandle,str){return __builtin_write(filehandle,str);}, + # same as C fseek. seek place by FILE*. seek: func(filehandle,pos,whence){return __builtin_seek(filehandle,pos,whence);}, + # same as C ftell. tell: func(filehandle){return __builtin_tell(filehandle);}, + # read file by lines. use FILE*. + # get nil if EOF readln:func(filehandle){return __builtin_readln(filehandle);}, + # same as C stat. stat: func(filename){return __builtin_stat(filename);}, + # same as C feof. check if FILE* gets the end of file(EOF). eof: func(filehandle){return __builtin_eof(filehandle);} }; +# get file status. using data from io.stat var fstat=func(filename){ var s=io.stat(filename); return { @@ -197,19 +210,42 @@ var fstat=func(filename){ }; } +# functions that do bitwise calculation. +# carefully use it, all the calculations are based on integer. var bits= { + # xor bitxor: func(a,b){return __builtin_xor(a,b); }, + # and bitand: func(a,b){return __builtin_and(a,b); }, + # or bitor: func(a,b){return __builtin_or(a,b); }, + # nand bitnand: func(a,b){return __builtin_nand(a,b);}, + # not bitnot: func(a) {return __builtin_not(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;}, + # 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;}, + # 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;}, + # get a special string filled by '\0' to use in setfld. buf: func(len){return __builtin_buf;} }; +# mostly used math functions and special constants, you know. var math= { e: 2.7182818284590452354, @@ -264,15 +300,31 @@ var unix= getenv: func(envvar){return __builtin_getenv(envvar);} }; +# dylib is the core hashmap for developers to load their own library. var dylib= { + # open dynamic lib. dlopen: func(libname){return __builtin_dlopen;}, + # load symbol from an open dynamic lib. dlsym: func(lib,sym){return __builtin_dlsym; }, + # close dynamic lib, this operation will make all the symbols loaded from it invalid. dlclose: func(lib){return __builtin_dlclose; }, + # call the loaded symbol. dlcall: func(funcptr,args...){return __builtin_dlcall} }; +# os is used to use or get some os-related info/functions. +# windows/macOS/linux are supported. var os= { + # get a string that tell which os it runs on. platform: func(){return __builtin_platform;} +}; + +# runtime gives us some functions that we could manage it manually. +var runtime= +{ + # do garbage collection manually. + # carefully use it because using it frequently may make program running slower. + gc: func(){return __builtin_gc;} }; \ No newline at end of file diff --git a/nasal_builtin.h b/nasal_builtin.h index 310ec50..4dfaab7 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -84,6 +84,7 @@ nas_native(builtin_dlsym); nas_native(builtin_dlclose); nas_native(builtin_dlcall); nas_native(builtin_platform); +nas_native(builtin_gc); nasal_ref builtin_err(const char* func_name,std::string info) { @@ -166,6 +167,7 @@ struct {"__builtin_dlclose", builtin_dlclose }, {"__builtin_dlcall", builtin_dlcall }, {"__builtin_platform",builtin_platform}, + {"__builtin_gc", builtin_gc }, {nullptr, nullptr } }; @@ -889,7 +891,7 @@ nasal_ref builtin_sfld(std::vector& local,nasal_gc& gc) nasal_ref builtin_setfld(std::vector& local,nasal_gc& gc) { // bits.setfld(s,0,8,69); - // set 1000101(69) to string will get this: + // set 01000101(69) to string will get this: // 10100010(162) // so s[0]=162 nasal_ref str=local[1]; @@ -1071,4 +1073,10 @@ nasal_ref builtin_platform(std::vector& local,nasal_gc& gc) #endif return ret; } +nasal_ref builtin_gc(std::vector& local,nasal_gc& gc) +{ + gc.mark(); + gc.sweep(); + return nil; +} #endif \ No newline at end of file diff --git a/nasal_gc.h b/nasal_gc.h index 39eefb6..bc141fb 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -436,7 +436,7 @@ void nasal_gc::info() std::cout<b.second;} ); - std::cout<<'\n'; + uint64_t total=0; for(auto& i:opcall) { if(!i.second) break; - std::cout<