diff --git a/README.md b/README.md index dfdd4d9..fcbde33 100644 --- a/README.md +++ b/README.md @@ -1068,7 +1068,7 @@ Linux(`.so`): `clang++ -shared -o libfib.so fib.o` -Mac: same as Linux, but remember to generate `.dylib` +Mac(`.so` & `.dylib`): same as Linux. Windows(`.dll`): diff --git a/lib.nas b/lib.nas index 3aa7e8e..2863929 100644 --- a/lib.nas +++ b/lib.nas @@ -48,6 +48,23 @@ var io= eof: func(filehandle){return __builtin_eof(filehandle);} }; +var fstat=func(filename){ + var s=io.stat(filename); + return { + st_dev: s[0], + st_ino: s[1], + st_mode: s[2], + st_nlink:s[3], + st_uid: s[4], + st_gid: s[5], + st_rdev: s[6], + st_size: s[7], + st_atime:s[8], + st_mtime:s[9], + st_ctime:s[10] + }; +} + var bits= { bitxor: func(a,b){return __builtin_xor(a,b); }, @@ -120,4 +137,9 @@ var dylib= dlsym: func(lib,sym){return __builtin_dlsym; }, dlclose: func(lib){return __builtin_dlclose; }, dlcall: func(funcptr,args...){return __builtin_dlcall} +}; + +var os= +{ + platform: func(){return __builtin_platform;} }; \ No newline at end of file diff --git a/makefile b/makefile index 873443d..3da4ecf 100644 --- a/makefile +++ b/makefile @@ -22,6 +22,7 @@ test:nasal ./nasal -t test/loop.nas ./nasal -c test/mandel.nas ./nasal -t test/mandelbrot.nas + # ./nasal -t test/module_test.nas ./nasal test/nasal_test.nas ./nasal -t test/pi.nas ./nasal -t test/prime.nas diff --git a/module/fib.cpp b/module/fib.cpp index 83b71db..0ccc8c0 100644 --- a/module/fib.cpp +++ b/module/fib.cpp @@ -10,4 +10,18 @@ extern "C" nasal_ref fib(std::vector& args,nasal_gc& gc){ if(num.type!=vm_num) return builtin_err("extern_fib","\"num\" must be number"); return {vm_num,fibonaci(num.to_number())}; +} +extern "C" nasal_ref quick_fib(std::vector& args,nasal_gc& gc){ + nasal_ref num=args[0]; + if(num.type!=vm_num) + return builtin_err("extern_fib","\"num\" must be number"); + if(num.num()<2) + return num; + double a=1,b=1,res=0; + for(double i=1;i& local,nasal_gc& gc) nasal_ref builtin_stat(std::vector& local,nasal_gc& gc) { nasal_ref filename=local[1]; - if(filename!=vm_str) + if(filename.type!=vm_str) return builtin_err("stat","\"filename\" must be string"); struct stat buf; if(stat(filename.str()->c_str(),&buf)<0) @@ -1013,11 +1015,10 @@ nasal_ref builtin_dlsym(std::vector& local,nasal_gc& gc) return builtin_err("dlsym","\"lib\" is not a correct dynamic lib entry"); if(sym.type!=vm_str) return builtin_err("dlsym","\"sym\" must be string"); - void* func=nullptr; #ifdef _WIN32 - func=(void*)GetProcAddress((HMODULE)libptr.obj()->ptr,sym.str()->c_str()); + void* func=(void*)GetProcAddress((HMODULE)libptr.obj()->ptr,sym.str()->c_str()); #else - func=dlsym(libptr.obj()->ptr,sym.str()->c_str()); + void* func=dlsym(libptr.obj()->ptr,sym.str()->c_str()); #endif if(!func) return builtin_err("dlsym","cannot find symbol \""+*sym.str()+"\""); @@ -1048,4 +1049,17 @@ nasal_ref builtin_dlcall(std::vector& local,nasal_gc& gc) extern_func func=(extern_func)funcptr.obj()->ptr; return func(args.vec()->elems,gc); } +nasal_ref builtin_platform(std::vector& local,nasal_gc& gc) +{ + nasal_ref ret=gc.alloc(vm_str); +#if defined _WIN32 || defined _WIN64 + *ret.str()="windows"; +#elif defined __linux__ + *ret.str()="linux"; +#elif defined __APPLE__ + *ret.str()="macOS"; +#endif + + return ret; +} #endif \ No newline at end of file diff --git a/nasal_gc.h b/nasal_gc.h index a87b068..a68eb26 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -125,7 +125,7 @@ struct nasal_val { uint8_t mark; uint8_t type; - uint8_t unmut; + uint8_t unmut; // used toe mark if a string is mutable union { std::string* str; @@ -407,7 +407,7 @@ void nasal_gc::init(const std::vector& s) zero={vm_num,(double)0}; // init constant 0 one ={vm_num,(double)1}; // init constant 1 - nil ={vm_nil,(double)0}; // init constant nil + nil ={vm_nil,nullptr}; // init constant nil // init constant strings strs.resize(s.size()); diff --git a/nasal_vm.h b/nasal_vm.h index 34875ea..95d9324 100644 --- a/nasal_vm.h +++ b/nasal_vm.h @@ -139,7 +139,7 @@ void nasal_vm::valinfo(nasal_ref& val) case vm_nil: printf("\t| nil |\n");break; case vm_num: printf("\t| num | %lf\n",val.num());break; case vm_str: printf("\t| str | <0x%lx> %s\n",(uint64_t)p,rawstr(*val.str()).c_str());break; - case vm_func: printf("\t| func | <0x%lx> func{entry=0x%x}\n",(uint64_t)p,val.func()->entry);break; + case vm_func: printf("\t| func | <0x%lx> entry=0x%x\n",(uint64_t)p,val.func()->entry);break; case vm_vec: printf("\t| vec | <0x%lx> [%lu val]\n",(uint64_t)p,val.vec()->elems.size());break; case vm_hash: printf("\t| hash | <0x%lx> {%lu member}\n",(uint64_t)p,val.hash()->elems.size());break; case vm_obj: printf("\t| obj | <0x%lx>\n",(uint64_t)p);break; @@ -174,10 +174,8 @@ void nasal_vm::traceback() continue; } if(same) - { printf("\t0x%.8x: %d same call(s)\n",last,same); - same=0; - } + same=0; bytecodeinfo(point); } if(same) diff --git a/stl/lib.nas b/stl/lib.nas index 3aa7e8e..e907fe1 100644 --- a/stl/lib.nas +++ b/stl/lib.nas @@ -48,6 +48,23 @@ var io= eof: func(filehandle){return __builtin_eof(filehandle);} }; +var fstat=func(filename){ + var s=io.stat(filename); + return { + st_dev: s[0], + st_ino: s[1], + st_mode: s[2], + st_nlink:s[3], + st_uid: s[4], + st_gid: s[5], + st_rdev: s[6], + st_size: s[7], + st_atime:s[8], + st_mtime:s[9], + st_ctime:s[10] + }; +} + var bits= { bitxor: func(a,b){return __builtin_xor(a,b); }, diff --git a/test/module_test.nas b/test/module_test.nas new file mode 100644 index 0000000..224f4b3 --- /dev/null +++ b/test/module_test.nas @@ -0,0 +1,17 @@ +import("lib.nas"); + +var libfib=func(){ + var dl=dylib.dlopen("./module/libfib.so"); + var fib=dylib.dlsym(dl,"fib"); + var qfib=dylib.dlsym(dl,"quick_fib"); + var call=dylib.dlcall; + return + { + fib: func(x){return call(fib,x)}, + qfib:func(x){return call(qfib,x)} + }; +}(); + +println(libfib); +println(libfib.fib(29)); +println(libfib.qfib(29)); \ No newline at end of file