From 5d4cff0aa8283bc278977f24e9f6c4ae9a68a771 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Fri, 8 Jul 2022 00:42:56 +0800 Subject: [PATCH] :rocket: shrink code size & now import module can search lib file from local dir and dirs in PATH --- lib.nas | 3 ++- nasal.h | 16 +++++-------- nasal_builtin.h | 10 ++++----- nasal_codegen.h | 11 +++------ nasal_import.h | 60 ++++++++++++++++++++++++++++++++++++------------- nasal_lexer.h | 2 +- nasal_vm.h | 24 ++++++-------------- stl/file.nas | 2 +- stl/lib.nas | 3 ++- 9 files changed, 70 insertions(+), 61 deletions(-) diff --git a/lib.nas b/lib.nas index a24fef1..b09f175 100644 --- a/lib.nas +++ b/lib.nas @@ -412,7 +412,8 @@ var unix= chdir: func(path){return __builtin_chdir(path);}, environ: func(){return __builtin_environ();}, getcwd: func(){return __builtin_getcwd();}, - getenv: func(envvar){return __builtin_getenv(envvar);} + getenv: func(envvar){return __builtin_getenv(envvar);}, + getpath: func(){return split(os.platform()=="windows"?";":":",unix.getenv("PATH"));} }; # dylib is the core hashmap for developers to load their own library. diff --git a/nasal.h b/nasal.h index d8537cf..2363f42 100644 --- a/nasal.h +++ b/nasal.h @@ -129,7 +129,7 @@ double str2num(const char* str) int utf8_hdchk(char head) { - // RFC-2279 but in fact now we use RFC-3629 so nbytes is less than 4 + // RFC-2279 but now we use RFC-3629 so nbytes is less than 4 uint8_t c=(uint8_t)head; uint32_t nbytes=0; if((c>>5)==0x06) // 110x xxxx (10xx xxxx)^1 @@ -138,13 +138,6 @@ int utf8_hdchk(char head) nbytes=2; if((c>>3)==0x1e) // 1111 0xxx (10xx xxxx)^3 nbytes=3; - // these should not be true - if((c>>2)==0x3e) // 1111 10xx (10xx xxxx)^4 - nbytes=4; - if((c>>1)==0x7e) // 1111 110x (10xx xxxx)^5 - nbytes=5; - if(c==0xfe) // 1111 1110 (10xx xxxx)^6 - nbytes=6; return nbytes; } @@ -153,7 +146,7 @@ std::string chrhex(const char c) return {"0123456789abcdef"[(c&0xf0)>>4],"0123456789abcdef"[c&0x0f]}; } -std::string rawstr(const std::string& str) +std::string rawstr(const std::string& str,const size_t maxlen=0) { std::string ret(""); for(auto i:str) @@ -163,8 +156,7 @@ std::string rawstr(const std::string& str) // if 'chcp65001' is not enabled, we output the hex if(i<=0) { - ret+="\\x"; - ret+=chrhex(i); + ret+="\\x"+chrhex(i); continue; } #endif @@ -185,6 +177,8 @@ std::string rawstr(const std::string& str) default: ret+=i; break; } } + if(maxlen && ret.length()>maxlen) + ret=ret.substr(0,maxlen)+"..."; return ret; } #include "nasal_err.h" diff --git a/nasal_builtin.h b/nasal_builtin.h index 1af35b9..27b3698 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -341,7 +341,8 @@ nasal_ref builtin_split(nasal_ref* local,nasal_gc& gc) size_t pos=s.find(deli,last); while(pos!=std::string::npos) { - vec.push_back(gc.newstr(s.substr(last,pos-last))); + if(pos>last) + vec.push_back(gc.newstr(s.substr(last,pos-last))); last=pos+deli.length(); pos=s.find(deli,last); } @@ -1209,7 +1210,7 @@ std::string tohex(uint32_t num) } return str; } -std::string md5(std::string& source) +std::string md5(const std::string& source) { std::vector buff; uint32_t num=((source.length()+8)>>6)+1; @@ -1367,9 +1368,8 @@ nasal_ref builtin_corun(nasal_ref* local,nasal_gc& gc) } nasal_ref builtin_millisec(nasal_ref* local,nasal_gc& gc) { - timeb now; - ftime(&now); - return {vm_num,(double)(now.time*1000+now.millitm)}; + double res=std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); + return {vm_num,res}; } nasal_ref builtin_sysargv(nasal_ref* local,nasal_gc& gc) { diff --git a/nasal_codegen.h b/nasal_codegen.h index b600844..3879b97 100644 --- a/nasal_codegen.h +++ b/nasal_codegen.h @@ -1280,10 +1280,8 @@ void nasal_codegen::print_op(uint32_t index) std::cout<>31)<<"\n"; break; case op_lnkeqc: - { - std::string tmp=rawstr(str_res[c.num&0x7fffffff]); - printf("0x%x (\"%.16s%s\") sp-%u\n",c.num&0x7fffffff,tmp.c_str(),tmp.length()>16?"...":"",c.num>>31); - }break; + printf("0x%x (\"%s\") sp-%u\n",c.num&0x7fffffff,rawstr(str_res[c.num&0x7fffffff],16).c_str(),c.num>>31); + break; case op_addc: case op_subc: case op_mulc: case op_divc: case op_lessc: case op_leqc: case op_grtc: case op_geqc: case op_pnum: @@ -1302,10 +1300,7 @@ void nasal_codegen::print_op(uint32_t index) case op_lnkc: case op_callh: case op_mcallh: case op_para: case op_defpara:case op_dynpara: - { - std::string tmp=rawstr(str_res[c.num]); - printf("0x%x (\"%.16s%s\")\n",c.num,tmp.c_str(),tmp.length()>16?"...":""); - }break; + printf("0x%x (\"%s\")\n",c.num,rawstr(str_res[c.num],16).c_str());break; default:printf("\n");break; } } diff --git a/nasal_import.h b/nasal_import.h index 94d4277..926e62c 100644 --- a/nasal_import.h +++ b/nasal_import.h @@ -7,6 +7,7 @@ private: bool lib_loaded; nasal_err& nerr; std::vector files; + std::vector envpath; bool imptchk(const nasal_ast&); bool exist(const std::string&); void linker(nasal_ast&,nasal_ast&&); @@ -15,11 +16,32 @@ private: nasal_ast libimpt(); nasal_ast load(nasal_ast&,uint16_t); public: - nasal_import(nasal_err& e):lib_loaded(false),nerr(e){} + nasal_import(nasal_err&); void link(nasal_parse&,const std::string&); const std::vector& get_file() const {return files;} }; +nasal_import::nasal_import(nasal_err& e):lib_loaded(false),nerr(e){ +#ifdef _WIN32 + char sep=';'; +#else + char sep=':'; +#endif + std::string PATH=getenv("PATH"); + size_t last=0; + size_t pos=PATH.find(sep,last); + while(pos!=std::string::npos) + { + std::string dirpath=PATH.substr(last,pos-last); + if(dirpath.length()) + envpath.push_back(dirpath); + last=pos+1; + pos=PATH.find(sep,last); + } + if(last!=PATH.length()) + envpath.push_back(PATH.substr(last,pos-last)); +} + std::string nasal_import::path(const nasal_ast& node) { if(node[1].type()==ast_callf) @@ -31,8 +53,7 @@ std::string nasal_import::path(const nasal_ast& node) #else fpath+="\\"+node[i].str(); #endif - fpath+=".nas"; - return fpath; + return fpath+".nas"; } bool nasal_import::imptchk(const nasal_ast& node) @@ -44,7 +65,7 @@ bool nasal_import::imptchk(const nasal_ast& node) |_callh:stl |_callh:file */ - if(node.type()==ast_call && node.size()>=2 && node[0].str()=="import" && node[1].type()==ast_callh) + if(node.type()==ast_call && node[0].str()=="import" && node.size()>=2 && node[1].type()==ast_callh) { for(size_t i=1;i libpath={nalib,nastllib}; + for(auto& p:envpath) + { + libpath.push_back(p+path_nalib); + libpath.push_back(p+path_stllib); + } + nasal_lexer lex(nerr); nasal_parse par(nerr); - - const std::vector libpath= - { -#ifdef __WIN32 - ".\\lib.nas", - ".\\stl\\lib.nas" -#else - "./lib.nas", - "./stl/lib.nas" -#endif - }; std::string filename=""; for(auto& i:libpath) if(access(i.c_str(),F_OK)!=-1) diff --git a/nasal_lexer.h b/nasal_lexer.h index 94a6f82..0822ef3 100644 --- a/nasal_lexer.h +++ b/nasal_lexer.h @@ -401,7 +401,7 @@ void nasal_lexer::scan(const std::string& file) void nasal_lexer::print() { for(auto& tok:tokens) - std::cout<<"("< %.16s%s\n",(uint64_t)p,tmp.c_str(),tmp.length()>16?"...":""); - }break; + case vm_str: printf("| str | <0x" PRTHEX64 "> %s\n",(uint64_t)p,rawstr(val.str(),16).c_str());break; case vm_func: printf("| func | <0x" PRTHEX64 "> entry:0x%x\n",(uint64_t)p,val.func().entry);break; case vm_upval:printf("| upval| <0x" PRTHEX64 "> [%u val]\n",(uint64_t)p,val.upval().size);break; case vm_vec: printf("| vec | <0x" PRTHEX64 "> [%zu val]\n",(uint64_t)p,val.vec().size());break; @@ -204,10 +198,8 @@ void nasal_vm::bytecodeinfo(const char* header,const uint32_t p) std::cout<>31); break; case op_lnkeqc: - { - std::string tmp=rawstr(str_table[c.num&0x7fffffff]); - printf("0x%x (\"%.16s%s\") sp-%u",c.num&0x7fffffff,tmp.c_str(),tmp.length()>16?"...":"",c.num>>31); - }break; + printf("0x%x (\"%s\") sp-%u",c.num&0x7fffffff,rawstr(str_table[c.num&0x7fffffff],16).c_str(),c.num>>31); + break; case op_addc: case op_subc: case op_mulc: case op_divc: case op_lessc: case op_leqc: case op_grtc: case op_geqc: case op_pnum: @@ -226,10 +218,8 @@ void nasal_vm::bytecodeinfo(const char* header,const uint32_t p) case op_lnkc: case op_callh: case op_mcallh: case op_para: case op_defpara:case op_dynpara: - { - std::string tmp=rawstr(str_table[c.num]); - printf("0x%x (\"%.16s%s\")",c.num,tmp.c_str(),tmp.length()>16?"...":""); - }break; + printf("0x%x (\"%s\")",c.num,rawstr(str_table[c.num],16).c_str()); + break; default:printf("0x%x",c.num);break; } printf(" (%s:%u)\n",files[c.fidx].c_str(),c.line); diff --git a/stl/file.nas b/stl/file.nas index b284f77..ddd7de5 100644 --- a/stl/file.nas +++ b/stl/file.nas @@ -31,4 +31,4 @@ var find_all_files=func(path){ append(res,n); unix.closedir(dd); return res; -} \ No newline at end of file +} diff --git a/stl/lib.nas b/stl/lib.nas index a24fef1..b09f175 100644 --- a/stl/lib.nas +++ b/stl/lib.nas @@ -412,7 +412,8 @@ var unix= chdir: func(path){return __builtin_chdir(path);}, environ: func(){return __builtin_environ();}, getcwd: func(){return __builtin_getcwd();}, - getenv: func(envvar){return __builtin_getenv(envvar);} + getenv: func(envvar){return __builtin_getenv(envvar);}, + getpath: func(){return split(os.platform()=="windows"?";":":",unix.getenv("PATH"));} }; # dylib is the core hashmap for developers to load their own library.