From 46516485b5ed93a2d815de8de136d950197f41e4 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sat, 23 Jul 2022 12:35:21 +0800 Subject: [PATCH] :memo: change name of used types --- README.md | 2 +- doc/README_zh.md | 2 +- main.cpp | 24 +++--- nasal.h | 32 +++++--- nasal_ast.h | 24 +++--- nasal_builtin.h | 190 +++++++++++++++++++++++------------------------ nasal_codegen.h | 74 +++++++++--------- nasal_dbg.h | 24 +++--- nasal_err.h | 10 +-- nasal_gc.h | 158 +++++++++++++++++++-------------------- nasal_import.h | 12 +-- nasal_lexer.h | 34 ++++----- nasal_opt.h | 4 +- nasal_parse.h | 40 +++++----- nasal_vm.h | 154 +++++++++++++++++++------------------- 15 files changed, 397 insertions(+), 387 deletions(-) diff --git a/README.md b/README.md index 1b6107a..0eb750b 100644 --- a/README.md +++ b/README.md @@ -196,7 +196,7 @@ __`vm_nil`__ is a null type. It means nothing. var spc=nil; ``` -__`vm_num`__ has 3 formats: `dec`, `hex` and `oct`. Using IEEE754 double to store. +__`vm_num`__ has 3 formats: `dec`, `hex` and `oct`. Using IEEE754 `double` to store. ```javascript # this language use '#' to write notes diff --git a/doc/README_zh.md b/doc/README_zh.md index 3bce648..7f2553d 100644 --- a/doc/README_zh.md +++ b/doc/README_zh.md @@ -180,7 +180,7 @@ __`vm_nil`__ 是空类型。类似于null。 var spc=nil; ``` -__`vm_num`__ 有三种形式:十进制,十六进制以及八进制。并且该类型使用IEEE754标准的浮点数double格式来存储。 +__`vm_num`__ 有三种形式:十进制,十六进制以及八进制。并且该类型使用IEEE754标准的浮点数`double`格式来存储。 ```javascript # this language use '#' to write notes diff --git a/main.cpp b/main.cpp index d5e3859..8dcc0c2 100644 --- a/main.cpp +++ b/main.cpp @@ -1,14 +1,14 @@ #include "nasal.h" -const uint32_t VM_LEXINFO =0x01; -const uint32_t VM_ASTINFO =0x02; -const uint32_t VM_CODEINFO =0x04; -const uint32_t VM_EXECTIME =0x08; -const uint32_t VM_OPCALLNUM=0x10; -const uint32_t VM_EXEC =0x20; -const uint32_t VM_DBGINFO =0x40; -const uint32_t VM_DEBUG =0x80; -const uint32_t VM_OPTIMIZE =0x100; +const u32 VM_LEXINFO =0x01; +const u32 VM_ASTINFO =0x02; +const u32 VM_CODEINFO =0x04; +const u32 VM_EXECTIME =0x08; +const u32 VM_OPCALLNUM=0x10; +const u32 VM_EXEC =0x20; +const u32 VM_DBGINFO =0x40; +const u32 VM_DEBUG =0x80; +const u32 VM_OPTIMIZE =0x100; void help() { @@ -70,7 +70,7 @@ void err() std::exit(1); } -void execute(const std::string& file,const std::vector& argv,const uint32_t cmd) +void execute(const std::string& file,const std::vector& argv,const u32 cmd) { // front end use the same error module nasal_err nerr; @@ -138,7 +138,7 @@ int main(int argc,const char* argv[]) err(); return 0; } - std::unordered_map cmdlst={ + std::unordered_map cmdlst={ {"--lex",VM_LEXINFO},{"-l",VM_LEXINFO}, {"--ast",VM_ASTINFO},{"-a",VM_ASTINFO}, {"--code",VM_CODEINFO},{"-c",VM_CODEINFO}, @@ -149,7 +149,7 @@ int main(int argc,const char* argv[]) {"--optimize",VM_OPTIMIZE},{"-op",VM_OPTIMIZE}, {"--debug",VM_DEBUG},{"-dbg",VM_DEBUG} }; - uint32_t cmd=0; + u32 cmd=0; std::string filename; std::vector vm_argv; for(int i=1;i #endif -const uint32_t STACK_DEPTH=2048; +using i32=std::int32_t; +using i64=std::int64_t; +using u8 =std::uint8_t; +using u16=std::uint16_t; +using u32=std::uint32_t; +using u64=std::uint64_t; +using usize=std::size_t; +using f64=double; -inline double hex_to_double(const char* str) + +const u32 STACK_DEPTH=2048; + +inline f64 hex_to_double(const char* str) { - double ret=0; + f64 ret=0; for(;*str;++str) { ret*=16; @@ -59,9 +69,9 @@ inline double hex_to_double(const char* str) } return ret; } -inline double oct_to_double(const char* str) +inline f64 oct_to_double(const char* str) { - double ret=0; + f64 ret=0; for(;*str;++str) { ret*=8; @@ -72,9 +82,9 @@ inline double oct_to_double(const char* str) } return ret; } -inline double dec_to_double(const char* str) +inline f64 dec_to_double(const char* str) { - double ret=0,negative=1,num_pow=0; + f64 ret=0,negative=1,num_pow=0; while('0'<=*str && *str<='9') ret=ret*10+(*str++-'0'); if(!*str) return ret; @@ -105,10 +115,10 @@ inline double dec_to_double(const char* str) } return ret*std::pow(10,negative*num_pow); } -double str2num(const char* str) +f64 str2num(const char* str) { bool negative=false; - double res=0; + f64 res=0; if(*str=='-' || *str=='+') negative=(*str++=='-'); if(!*str) @@ -125,7 +135,7 @@ double str2num(const char* str) int utf8_hdchk(const char head) { // RFC-2279 but now we use RFC-3629 so nbytes is less than 4 - const uint8_t c=(uint8_t)head; + const u8 c=(u8)head; if((c>>5)==0x06) // 110x xxxx (10xx xxxx)^1 return 1; if((c>>4)==0x0e) // 1110 xxxx (10xx xxxx)^2 @@ -140,7 +150,7 @@ std::string chrhex(const char c) return {"0123456789abcdef"[(c&0xf0)>>4],"0123456789abcdef"[c&0x0f]}; } -std::string rawstr(const std::string& str,const size_t maxlen=0) +std::string rawstr(const std::string& str,const usize maxlen=0) { std::string ret(""); for(auto i:str) diff --git a/nasal_ast.h b/nasal_ast.h index db152fa..f71dadf 100644 --- a/nasal_ast.h +++ b/nasal_ast.h @@ -129,13 +129,13 @@ const char* ast_name[]= class nasal_ast { private: - uint32_t _line; - uint32_t _type; - double _num; + u32 _line; + u32 _type; + f64 _num; std::string _str; std::vector _child; public: - nasal_ast(const uint32_t l=0,const uint32_t t=ast_null):_line(l),_type(t),_num(0){} + nasal_ast(const u32 l=0,const u32 t=ast_null):_line(l),_type(t),_num(0){} nasal_ast(const nasal_ast&); nasal_ast(nasal_ast&&); void print(int,bool); @@ -145,18 +145,18 @@ public: nasal_ast& operator=(nasal_ast&&); nasal_ast& operator[](const int index){return _child[index];} const nasal_ast& operator[](const int index) const {return _child[index];} - size_t size() const {return _child.size();} + usize size() const {return _child.size();} void add(nasal_ast&& ast){_child.push_back(std::move(ast));} void add(const nasal_ast& ast){_child.push_back(ast);} - void set_line(const uint32_t l){_line=l;} - void set_type(const uint32_t t){_type=t;} + void set_line(const u32 l){_line=l;} + void set_type(const u32 t){_type=t;} void set_str(const std::string& s){_str=s;} - void set_num(const double n){_num=n;} + void set_num(const f64 n){_num=n;} - inline uint32_t line() const {return _line;} - inline uint32_t type() const {return _type;} - inline double num() const {return _num;} + inline u32 line() const {return _line;} + inline u32 type() const {return _type;} + inline f64 num() const {return _num;} inline const std::string& str() const {return _str;} inline const std::vector& child() const {return _child;} inline std::vector& child(){return _child;} @@ -232,7 +232,7 @@ void nasal_ast::print(int depth,bool last=false) #else intentation.back()="│ "; #endif - for(uint32_t i=0;i<_child.size();++i) + for(u32 i=0;i<_child.size();++i) { #ifdef _WIN32 intentation.push_back(i==_child.size()-1?"`-":"|-"); diff --git a/nasal_builtin.h b/nasal_builtin.h index c29d5e3..2257418 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -262,7 +262,7 @@ nasal_ref builtin_setsize(nasal_ref* local,nasal_gc& gc) return builtin_err("setsize","\"vector\" must be vector"); if(size.type!=vm_num) return builtin_err("setsize","\"size\" is not a number"); - int64_t num=(int64_t)size.num(); + i64 num=(i64)size.num(); if(num<0) return builtin_err("setsize","\"size\" must be greater than -1"); vec.vec().elems.resize(num,nil); @@ -273,7 +273,7 @@ nasal_ref builtin_system(nasal_ref* local,nasal_gc& gc) nasal_ref str=local[1]; if(str.type!=vm_str) return builtin_err("system","\"str\" must be string"); - return {vm_num,(double)system(str.str().c_str())}; + return {vm_num,(f64)system(str.str().c_str())}; } nasal_ref builtin_input(nasal_ref* local,nasal_gc& gc) { @@ -331,8 +331,8 @@ nasal_ref builtin_split(nasal_ref* local,nasal_gc& gc) gc.temp=nil; return res; } - size_t last=0; - size_t pos=s.find(deli,last); + usize last=0; + usize pos=s.find(deli,last); while(pos!=std::string::npos) { if(pos>last) @@ -355,7 +355,7 @@ nasal_ref builtin_rand(nasal_ref* local,nasal_gc& gc) srand((unsigned int)val.num()); return nil; } - double num=0; + f64 num=0; for(int i=0;i<5;++i) num=(num+rand())*(1.0/(RAND_MAX+1.0)); return {vm_num,num}; @@ -365,7 +365,7 @@ nasal_ref builtin_id(nasal_ref* local,nasal_gc& gc) nasal_ref val=local[1]; std::stringstream ss; if(val.type>vm_num) - ss<<"0x"<=str.str().length() || begin+length>str.str().length()) return builtin_err("susbtr","index out of range"); return gc.newstr(str.str().substr(begin,length)); @@ -667,7 +667,7 @@ nasal_ref builtin_streq(nasal_ref* local,nasal_gc& gc) { nasal_ref a=local[1]; nasal_ref b=local[2]; - return {vm_num,double((a.type!=vm_str || b.type!=vm_str)?0:(a.str()==b.str()))}; + return {vm_num,f64((a.type!=vm_str || b.type!=vm_str)?0:(a.str()==b.str()))}; } nasal_ref builtin_left(nasal_ref* local,nasal_gc& gc) { @@ -706,7 +706,7 @@ nasal_ref builtin_cmp(nasal_ref* local,nasal_gc& gc) return builtin_err("cmp","\"a\" must be string"); if(b.type!=vm_str) return builtin_err("cmp","\"b\" must be string"); - return {vm_num,(double)strcmp(a.str().c_str(),b.str().c_str())}; + return {vm_num,(f64)strcmp(a.str().c_str(),b.str().c_str())}; } nasal_ref builtin_chr(nasal_ref* local,nasal_gc& gc) { @@ -792,10 +792,10 @@ nasal_ref builtin_read(nasal_ref* local,nasal_gc& gc) return builtin_err("read","\"len\" must be number"); if(len.num()<=0 || len.num()>=(1<<30)) return builtin_err("read","\"len\" less than 1 or too large"); - char* buff=new char[(size_t)len.num()+1]; + char* buff=new char[(usize)len.num()+1]; if(!buff) return builtin_err("read","memory allocation error"); - double res=fread(buff,1,len.num(),(FILE*)fd.obj().ptr); + f64 res=fread(buff,1,len.num(),(FILE*)fd.obj().ptr); buf.str()=buff; delete []buff; return {vm_num,res}; @@ -808,7 +808,7 @@ nasal_ref builtin_write(nasal_ref* local,nasal_gc& gc) return builtin_err("write","not a valid filehandle"); if(str.type!=vm_str) return builtin_err("write","\"str\" must be string"); - double res=(double)fwrite(str.str().c_str(),1,str.str().length(),(FILE*)fd.obj().ptr); + f64 res=(f64)fwrite(str.str().c_str(),1,str.str().length(),(FILE*)fd.obj().ptr); return {vm_num,res}; } nasal_ref builtin_seek(nasal_ref* local,nasal_gc& gc) @@ -822,7 +822,7 @@ nasal_ref builtin_seek(nasal_ref* local,nasal_gc& gc) return builtin_err("seek","\"pos\" must be number"); if(whence.type!=vm_num || whence.num()<0 || whence.num()>2) return builtin_err("seek","\"whence\" must be number between 0 and 2"); - double res=fseek((FILE*)fd.obj().ptr,pos.num(),whence.num()); + f64 res=fseek((FILE*)fd.obj().ptr,pos.num(),whence.num()); return {vm_num,res}; } nasal_ref builtin_tell(nasal_ref* local,nasal_gc& gc) @@ -830,7 +830,7 @@ nasal_ref builtin_tell(nasal_ref* local,nasal_gc& gc) nasal_ref fd=local[1]; if(!fd.objchk(nasal_obj::file)) return builtin_err("tell","not a valid filehandle"); - double res=ftell((FILE*)fd.obj().ptr); + f64 res=ftell((FILE*)fd.obj().ptr); return {vm_num,res}; } nasal_ref builtin_readln(nasal_ref* local,nasal_gc& gc) @@ -863,17 +863,17 @@ nasal_ref builtin_stat(nasal_ref* local,nasal_gc& gc) return builtin_err("stat","failed to open file <"+name.str()+">"); nasal_ref ret=gc.alloc(vm_vec); ret.vec().elems={ - {vm_num,(double)buf.st_dev}, - {vm_num,(double)buf.st_ino}, - {vm_num,(double)buf.st_mode}, - {vm_num,(double)buf.st_nlink}, - {vm_num,(double)buf.st_uid}, - {vm_num,(double)buf.st_gid}, - {vm_num,(double)buf.st_rdev}, - {vm_num,(double)buf.st_size}, - {vm_num,(double)buf.st_atime}, - {vm_num,(double)buf.st_mtime}, - {vm_num,(double)buf.st_ctime} + {vm_num,(f64)buf.st_dev}, + {vm_num,(f64)buf.st_ino}, + {vm_num,(f64)buf.st_mode}, + {vm_num,(f64)buf.st_nlink}, + {vm_num,(f64)buf.st_uid}, + {vm_num,(f64)buf.st_gid}, + {vm_num,(f64)buf.st_rdev}, + {vm_num,(f64)buf.st_size}, + {vm_num,(f64)buf.st_atime}, + {vm_num,(f64)buf.st_mtime}, + {vm_num,(f64)buf.st_ctime} }; return ret; } @@ -882,7 +882,7 @@ nasal_ref builtin_eof(nasal_ref* local,nasal_gc& gc) nasal_ref fd=local[1]; if(!fd.objchk(nasal_obj::file)) return builtin_err("readln","not a valid filehandle"); - return {vm_num,(double)feof((FILE*)fd.obj().ptr)}; + return {vm_num,(f64)feof((FILE*)fd.obj().ptr)}; } nasal_ref builtin_fld(nasal_ref* local,nasal_gc& gc) { @@ -896,16 +896,16 @@ nasal_ref builtin_fld(nasal_ref* local,nasal_gc& gc) return builtin_err("fld","\"str\" must be mutable string"); if(startbit.type!=vm_num || length.type!=vm_num) return builtin_err("fld","\"startbit\",\"len\" must be number"); - uint32_t bit=(uint32_t)startbit.num(); - uint32_t len=(uint32_t)length.num(); + u32 bit=(u32)startbit.num(); + u32 len=(u32)length.num(); if(bit+len>8*str.str().length()) return builtin_err("fld","bitfield out of bounds"); - uint32_t res=0; + u32 res=0; auto& s=str.str(); - for(uint32_t i=bit;i>3]&(1<<(7-(i&7)))) res|=1<<(bit+len-i-1); - return {vm_num,(double)res}; + return {vm_num,(f64)res}; } nasal_ref builtin_sfld(nasal_ref* local,nasal_gc& gc) { @@ -920,18 +920,18 @@ nasal_ref builtin_sfld(nasal_ref* local,nasal_gc& gc) return builtin_err("sfld","\"str\" must be mutable string"); if(startbit.type!=vm_num || length.type!=vm_num) return builtin_err("sfld","\"startbit\",\"len\" must be number"); - uint32_t bit=(uint32_t)startbit.num(); - uint32_t len=(uint32_t)length.num(); + u32 bit=(u32)startbit.num(); + u32 len=(u32)length.num(); if(bit+len>8*str.str().length()) return builtin_err("sfld","bitfield out of bounds"); - uint32_t res=0; + u32 res=0; auto& s=str.str(); - for(uint32_t i=bit;i>3]&(1<<(7-(i&7)))) res|=1<<(bit+len-i-1); if(res&(1<<(len-1))) res|=~((1<8*str.str().length()) return builtin_err("setfld","bitfield out of bounds"); auto& s=str.str(); - for(uint32_t i=bit;i>3]|=(1<<(7-(i&7))); @@ -977,7 +977,7 @@ nasal_ref builtin_sleep(nasal_ref* local,nasal_gc& gc) nasal_ref val=local[1]; if(val.type!=vm_num) return builtin_err("sleep","\"duration\" must be number"); - std::this_thread::sleep_for(std::chrono::microseconds(int64_t(val.num()*1e6))); + std::this_thread::sleep_for(std::chrono::microseconds(i64(val.num()*1e6))); return nil; } nasal_ref builtin_pipe(nasal_ref* local,nasal_gc& gc) @@ -987,8 +987,8 @@ nasal_ref builtin_pipe(nasal_ref* local,nasal_gc& gc) nasal_ref res=gc.alloc(vm_vec); if(pipe(fd)==-1) return builtin_err("pipe","failed to create pipe"); - res.vec().elems.push_back({vm_num,(double)fd[0]}); - res.vec().elems.push_back({vm_num,(double)fd[1]}); + res.vec().elems.push_back({vm_num,(f64)fd[0]}); + res.vec().elems.push_back({vm_num,(f64)fd[1]}); return res; #endif return builtin_err("pipe","not supported for windows"); @@ -996,10 +996,10 @@ nasal_ref builtin_pipe(nasal_ref* local,nasal_gc& gc) nasal_ref builtin_fork(nasal_ref* local,nasal_gc& gc) { #ifndef _WIN32 - double res=fork(); + f64 res=fork(); if(res<0) return builtin_err("fork","failed to fork a process"); - return {vm_num,(double)res}; + return {vm_num,(f64)res}; #endif return builtin_err("fork","not supported for windows"); } @@ -1014,8 +1014,8 @@ nasal_ref builtin_waitpid(nasal_ref* local,nasal_gc& gc) int status; ret_pid=waitpid(pid.num(),&status,nohang.num()==0?0:WNOHANG); nasal_ref vec=gc.alloc(vm_vec); - vec.vec().elems.push_back({vm_num,(double)ret_pid}); - vec.vec().elems.push_back({vm_num,(double)status}); + vec.vec().elems.push_back({vm_num,(f64)ret_pid}); + vec.vec().elems.push_back({vm_num,(f64)status}); return vec; #endif return builtin_err("waitpid","not supported for windows"); @@ -1206,14 +1206,14 @@ nasal_ref builtin_gc(nasal_ref* local,nasal_gc& gc) } // md5 related functions -std::string tohex(uint32_t num) +std::string tohex(u32 num) { const char str16[]="0123456789abcdef"; std::string str=""; - for(uint32_t i=0;i<4;i++,num>>=8) + for(u32 i=0;i<4;i++,num>>=8) { std::string tmp=""; - for(uint32_t j=0,b=num&0xff;j<2;j++,b>>=4) + for(u32 j=0,b=num&0xff;j<2;j++,b>>=4) tmp.insert(0,1,str16[b&0xf]); str+=tmp; } @@ -1221,18 +1221,18 @@ std::string tohex(uint32_t num) } std::string md5(const std::string& source) { - std::vector buff; - uint32_t num=((source.length()+8)>>6)+1; - uint32_t buffsize=num<<4; + std::vector buff; + u32 num=((source.length()+8)>>6)+1; + u32 buffsize=num<<4; buff.resize(buffsize,0); - for(uint32_t i=0;i>2]|=((unsigned char)source[i])<<((i&0x3)<<3); buff[source.length()>>2]|=0x80<<(((source.length()%4))<<3); buff[buffsize-2]=(source.length()<<3)&0xffffffff; buff[buffsize-1]=((source.length()<<3)>>32)&0xffffffff; - // uint32_t(abs(sin(i+1))*(2pow32)) - const uint32_t k[]={ + // u32(abs(sin(i+1))*(2pow32)) + const u32 k[]={ 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501, 0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,0xa679438e,0x49b40821, 0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8, @@ -1244,7 +1244,7 @@ std::string md5(const std::string& source) }; // left shift bits - const uint32_t s[]={ + const u32 s[]={ 7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22, 5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20, 4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23, @@ -1252,7 +1252,7 @@ std::string md5(const std::string& source) }; // index - const uint32_t idx[]={ + const u32 idx[]={ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, // g=i 1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12, // g=(5*i+1)%16; 5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2, // g=(3*i+5)%16; @@ -1265,18 +1265,18 @@ std::string md5(const std::string& source) #define md5h(x,y,z) ((x)^(y)^(z)) #define md5i(x,y,z) ((y)^((x)|(~z))) - uint32_t atmp=0x67452301,btmp=0xefcdab89; - uint32_t ctmp=0x98badcfe,dtmp=0x10325476; - for(uint32_t i=0;i - (std::chrono::high_resolution_clock::now().time_since_epoch()) - .count(); + f64 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 e1bdafc..ea03ac0 100644 --- a/nasal_codegen.h +++ b/nasal_codegen.h @@ -166,11 +166,11 @@ struct struct opcode { - uint8_t op; // opcode - uint16_t fidx;// source code file index - uint32_t num; // imm num - uint32_t line;// line of source code - opcode(uint8_t o=op_exit,uint16_t f=0,uint32_t n=0,uint32_t l=0): + u8 op; // opcode + u16 fidx;// source code file index + u32 num; // imm num + u32 line;// line of source code + opcode(u8 o=op_exit,u16 f=0,u32 n=0,u32 l=0): op(o),fidx(f),num(n),line(l){} opcode& operator=(const opcode& tmp) { @@ -181,20 +181,20 @@ struct opcode return *this; } void print(const char*, - const double*, + const f64*, const std::string*, - const uint32_t,bool) const; + const u32,bool) const; }; void opcode::print(const char* header, - const double* constnum, + const f64* constnum, const std::string* conststr, - const uint32_t index, + const u32 index, bool deftnum=false) const { std::cout<>24)&0xff)<<" " <>16)&0xff)<<" " <>8)&0xff)<<" " @@ -244,13 +244,13 @@ void opcode::print(const char* header, class nasal_codegen { private: - uint16_t fileindex; + u16 fileindex; nasal_err& nerr; const std::string* file; - std::stack in_iterloop; - std::unordered_map num_table; - std::unordered_map str_table; - std::vector num_res; + std::stack in_iterloop; + std::unordered_map num_table; + std::unordered_map str_table; + std::vector num_res; std::vector str_res; std::vector code; std::list> continue_ptr; @@ -261,18 +261,18 @@ private: std::list> local; // func end stack, reserved for code print - std::stack fbstk; - std::stack festk; + std::stack fbstk; + std::stack festk; - void die(const std::string&,const uint32_t); - void regist_num(const double); + void die(const std::string&,const u32); + void regist_num(const f64); void regist_str(const std::string&); void find_symbol(const nasal_ast&); void add_sym(const std::string&); int local_find(const std::string&); int global_find(const std::string&); int upvalue_find(const std::string&); - void gen(uint8_t,uint32_t,uint32_t); + void gen(u8,u32,u32); void num_gen(const nasal_ast&); void str_gen(const nasal_ast&); void vec_gen(const nasal_ast&); @@ -305,27 +305,27 @@ private: void block_gen(const nasal_ast&); void ret_gen(const nasal_ast&); - void singleop(const uint32_t); + void singleop(const u32); public: nasal_codegen(nasal_err& e):fileindex(0),nerr(e),file(nullptr){} void compile(const nasal_parse&,const nasal_import&); void print(); const std::vector& strs() const {return str_res;} - const std::vector& nums() const {return num_res;} + const std::vector& nums() const {return num_res;} const std::vector& codes() const {return code;} }; -void nasal_codegen::die(const std::string& info,const uint32_t line) +void nasal_codegen::die(const std::string& info,const u32 line) { nerr.load(file[fileindex]); nerr.err("code",line,info); } -void nasal_codegen::regist_num(const double num) +void nasal_codegen::regist_num(const f64 num) { if(!num_table.count(num)) { - uint32_t size=num_table.size(); + u32 size=num_table.size(); num_table[num]=size; num_res.push_back(num); } @@ -335,7 +335,7 @@ void nasal_codegen::regist_str(const std::string& str) { if(!str_table.count(str)) { - uint32_t size=str_table.size(); + u32 size=str_table.size(); str_table[str]=size; str_res.push_back(str); } @@ -398,24 +398,24 @@ int nasal_codegen::upvalue_find(const std::string& name) { // 32768 level 65536 upvalues int index=-1; - size_t size=local.size(); + usize size=local.size(); if(size<=1) return -1; auto iter=local.begin(); - for(uint32_t i=0;icount(name)) index=((i<<16)|(*iter)[name]); return index; } -void nasal_codegen::gen(uint8_t op,uint32_t num,uint32_t line) +void nasal_codegen::gen(u8 op,u32 num,u32 line) { code.push_back({op,fileindex,num,line}); } void nasal_codegen::num_gen(const nasal_ast& ast) { - double num=ast.num(); + f64 num=ast.num(); regist_num(num); gen(op_pnum,num_table[num],ast.line()); } @@ -523,7 +523,7 @@ void nasal_codegen::call_gen(const nasal_ast& ast) void nasal_codegen::call_id(const nasal_ast& ast) { const std::string& str=ast.str(); - for(uint32_t i=0;builtin[i].name;++i) + for(u32 i=0;builtin[i].name;++i) if(builtin[i].name==str) { gen(op_callb,i,ast.line()); @@ -621,7 +621,7 @@ void nasal_codegen::mcall(const nasal_ast& ast) return; } calc_gen(ast[0]); - for(size_t i=1;i:\n"; - for(uint32_t i=index;i parse(const std::string&); - uint16_t fileindex(const std::string&); + u16 fileindex(const std::string&); void err(); void help(); void stepinfo(); @@ -32,7 +32,7 @@ public: std::vector nasal_dbg::parse(const std::string& cmd) { std::vector res; - size_t last=0,pos=cmd.find(" ",0); + usize last=0,pos=cmd.find(" ",0); while(pos!=std::string::npos) { if(pos>last) @@ -45,9 +45,9 @@ std::vector nasal_dbg::parse(const std::string& cmd) return res; } -uint16_t nasal_dbg::fileindex(const std::string& filename) +u16 nasal_dbg::fileindex(const std::string& filename) { - for(uint16_t i=0;i>3)==0?0:((line>>3)<<3); end=(1+(line>>3))<<3; - for(uint32_t i=begin;i ":" ")<>3)==0?0:((pc>>3)<<3); end=(1+(pc>>3))<<3; - for(uint32_t i=begin;i ":" ",i); stackinfo(10); } @@ -131,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 tmp; res.swap(tmp); } - const std::string& operator[](const uint32_t line){return res[line];} + const std::string& operator[](const u32 line){return res[line];} const std::string& name(){return file;} - size_t size(){return res.size();} + usize size(){return res.size();} }; class nasal_err:public fstreamline { private: - uint32_t error; + u32 error; public: nasal_err():error(0){} void err(const char* stage,const std::string& info) @@ -51,7 +51,7 @@ public: ++error; std::cerr<<"["< local; // local scope with default value(nasal_ref) - std::vector upvalue; // closure - std::unordered_map keys; // parameter name table, size_t begins from 1 + i32 dynpara; // dynamic parameter name index in hash. + u32 entry; // pc will set to entry-1 to call this function + u32 psize; // used to load default parameters to a new function + u32 lsize; // used to expand memory space for local values on stack + std::vector local; // local scope with default value(nasal_ref) + std::vector upvalue; // closure + std::unordered_map keys; // parameter name table, u32 begins from 1 nasal_func():dynpara(-1),entry(0),psize(0),lsize(0){} void clear(); @@ -137,7 +137,7 @@ struct nasal_func struct nasal_upval { bool onstk; - uint32_t size; + u32 size; nasal_ref* stk; std::vector elems; @@ -148,7 +148,7 @@ struct nasal_upval struct nasal_obj { - enum obj_t:std::uint32_t + enum obj_t:u32 { null, file, @@ -158,7 +158,7 @@ struct nasal_obj }; /* RAII constructor */ /* new object is initialized when creating */ - uint32_t type; + u32 type; void* ptr; /* RAII destroyer */ /* default destroyer does nothing */ @@ -167,7 +167,7 @@ struct nasal_obj nasal_obj():type(obj_t::null),ptr(nullptr),dtor(nullptr){} ~nasal_obj(){clear();} - void set(uint32_t t=obj_t::null,void* p=nullptr,dest d=nullptr) + void set(u32 t=obj_t::null,void* p=nullptr,dest d=nullptr) { type=t; ptr=p; @@ -192,7 +192,7 @@ struct nasal_co }; nasal_ref stack[STACK_DEPTH]; - uint32_t pc; + u32 pc; nasal_ref* top; nasal_ref* canary; nasal_ref* localr; @@ -200,41 +200,41 @@ struct nasal_co nasal_ref funcr; nasal_ref upvalr; - uint32_t status; + u32 status; nasal_co(): pc(0), top(stack), canary(stack+STACK_DEPTH-1), localr(nullptr), memr(nullptr), - funcr({vm_nil,(double)0}), - upvalr({vm_nil,(double)0}), + funcr({vm_nil,(f64)0}), + upvalr({vm_nil,(f64)0}), status(nasal_co::suspended) { - for(uint32_t i=0;i"; break; } } -bool nasal_ref::objchk(uint32_t objtype) +bool nasal_ref::objchk(u32 objtype) { return type==vm_obj && obj().type==objtype && obj().ptr; } inline nasal_ref* nasal_ref::addr (){return val.addr; } -inline uint32_t nasal_ref::ret (){return val.ret; } -inline int64_t& nasal_ref::cnt (){return val.cnt; } -inline double nasal_ref::num (){return val.num; } +inline u32 nasal_ref::ret (){return val.ret; } +inline i64& nasal_ref::cnt (){return val.cnt; } +inline f64 nasal_ref::num (){return val.num; } inline std::string& nasal_ref::str (){return *val.gcobj->ptr.str; } inline nasal_vec& nasal_ref::vec (){return *val.gcobj->ptr.vec; } inline nasal_hash& nasal_ref::hash (){return *val.gcobj->ptr.hash; } @@ -427,15 +427,15 @@ inline nasal_upval& nasal_ref::upval(){return *val.gcobj->ptr.upval;} inline nasal_obj& nasal_ref::obj (){return *val.gcobj->ptr.obj; } inline nasal_co& nasal_ref::co (){return *val.gcobj->ptr.co; } -const nasal_ref zero={vm_num,(double)0}; -const nasal_ref one ={vm_num,(double)1}; -const nasal_ref nil ={vm_nil,(double)0}; +const nasal_ref zero={vm_num,(f64)0}; +const nasal_ref one ={vm_num,(f64)1}; +const nasal_ref nil ={vm_nil,(f64)0}; struct nasal_gc { struct { - uint32_t pc; + u32 pc; nasal_ref* top; nasal_ref* localr; nasal_ref* memr; @@ -446,7 +446,7 @@ struct nasal_gc } main_ctx; /* runtime context */ - uint32_t& pc; // program counter + u32& pc; // program counter nasal_ref*& localr; // local scope register nasal_ref*& memr; // used for mem_call nasal_ref& funcr; // function register @@ -464,11 +464,11 @@ struct nasal_gc std::queue unused[gc_tsize]; // gc free list /* values for analysis */ - uint64_t size[gc_tsize]; - uint64_t count[gc_tsize]; - uint64_t allocc[gc_tsize]; + u64 size[gc_tsize]; + u64 count[gc_tsize]; + u64 allocc[gc_tsize]; nasal_gc( - uint32_t& _pc, + u32& _pc, nasal_ref*& _localr, nasal_ref*& _memr, nasal_ref& _funcr, @@ -483,7 +483,7 @@ struct nasal_gc void init(const std::vector&,const std::vector&); void clear(); void info(); - nasal_ref alloc(const uint8_t); + nasal_ref alloc(const u8); nasal_ref newstr(char); nasal_ref newstr(const char*); nasal_ref newstr(const std::string&); @@ -575,10 +575,10 @@ void nasal_gc::init(const std::vector& s,const std::vector& s,const std::vectorunmut=1; @@ -595,7 +595,7 @@ void nasal_gc::init(const std::vector& s,const std::vectorunmut=1; @@ -607,7 +607,7 @@ void nasal_gc::clear() for(auto i:memory) delete i; memory.clear(); - for(uint8_t i=0;i=2 && node[1].type()==ast_callh) { - for(size_t i=1;i'||c=='~') #define NOTE(c) (c=='#') -enum tok:std::uint32_t{ +enum tok:u32{ tok_null=0, // null token (default token type) tok_num, // number basic token type tok_str, // string basic token type @@ -41,7 +41,7 @@ enum tok:std::uint32_t{ struct{ const char* str; - const uint32_t type; + const u32 type; }tok_table[]={ {"for" ,tok_for }, {"forindex",tok_forindex }, @@ -93,11 +93,11 @@ struct{ struct token { - uint32_t line; - uint32_t col; - uint32_t type; + u32 line; + u32 col; + u32 type; std::string str; - token(uint32_t l=0,uint32_t c=0,uint32_t t=tok_null,const std::string& s=""):str(s) + token(u32 l=0,u32 c=0,u32 t=tok_null,const std::string& s=""):str(s) { line=l; col=c; @@ -108,14 +108,14 @@ struct token class nasal_lexer { private: - uint32_t line; - uint32_t column; - size_t ptr; + u32 line; + u32 column; + usize ptr; nasal_err& nerr; std::string res; std::vector tokens; - uint32_t get_type(const std::string&); + u32 get_type(const std::string&); void die(const std::string& info){nerr.err("lexer",line,column,info);} void open(const std::string&); std::string utf8_gen(); @@ -152,9 +152,9 @@ void nasal_lexer::open(const std::string& file) res=ss.str(); } -uint32_t nasal_lexer::get_type(const std::string& str) +u32 nasal_lexer::get_type(const std::string& str) { - for(uint32_t i=0;tok_table[i].str;++i) + for(u32 i=0;tok_table[i].str;++i) if(str==tok_table[i].str) return tok_table[i].type; return tok_null; @@ -166,18 +166,18 @@ std::string nasal_lexer::utf8_gen() while(ptr imm; // immediate number + u32 pc; // program counter + nasal_ref* localr; // local scope register + nasal_ref* memr; // used for mem_call + nasal_ref funcr; // function register + nasal_ref upvalr; // upvalue register + nasal_ref* canary; // avoid stackoverflow + nasal_ref* top; // stack top + const f64* num_table;// const numbers, ref from nasal_codegen + const std::string* str_table;// const symbols, ref from nasal_codegen + std::vector imm; // immediate number /* garbage collector */ nasal_gc gc; @@ -27,22 +27,22 @@ protected: void init( const std::vector&, - const std::vector&, + const std::vector&, const std::vector&, const std::vector&, const std::vector&); /* debug functions */ bool detail_info; void valinfo(nasal_ref&); - void bytecodeinfo(const char*,const uint32_t); + void bytecodeinfo(const char*,const u32); void traceback(); - void stackinfo(const uint32_t); + void stackinfo(const u32); void register_info(); void global_state(); void local_state(); void upval_state(); void detail(); - void opcallsort(const uint64_t*); + void opcallsort(const u64*); void die(const std::string&); /* vm calculation functions*/ bool condition(nasal_ref); @@ -137,7 +137,7 @@ public: void nasal_vm::init( const std::vector& strs, - const std::vector& nums, + const std::vector& nums, const std::vector& code, const std::vector& filenames, const std::vector& argv) @@ -156,7 +156,7 @@ void nasal_vm::init( top=stack; /* clear main stack */ - for(uint32_t i=0;i "< entry:0x"< ["< ["< {"< obj:0x"<<(uint64_t)val.obj().ptr + case vm_obj: std::cout<<"| obj | <0x"< obj:0x"<<(u64)val.obj().ptr < coroutine";break; - default: std::cout<<"| err | <0x"< unknown object";break; } std::cout<<"\n"; } -void nasal_vm::bytecodeinfo(const char* header,const uint32_t p) +void nasal_vm::bytecodeinfo(const char* header,const u32 p) { const opcode& c=bytecode[p]; c.print(header,num_table,str_table,p,true); @@ -205,18 +205,18 @@ void nasal_vm::bytecodeinfo(const char* header,const uint32_t p) } void nasal_vm::traceback() { - const uint32_t global_size=bytecode[0].num; // bytecode[0] is op_intg + const u32 global_size=bytecode[0].num; // bytecode[0] is op_intg nasal_ref* t=top; nasal_ref* bottom=stack+global_size; - std::stack ret; + std::stack ret; for(nasal_ref* i=bottom;i<=t;++i) if(i->type==vm_ret) ret.push(i->ret()); // push pc to ret stack to store the position program crashed ret.push(pc); std::cout<<"trace back:\n"; - uint32_t same=0,last=0xffffffff; - for(uint32_t point=0;!ret.empty();last=point,ret.pop()) + u32 same=0,last=0xffffffff; + for(u32 point=0;!ret.empty();last=point,ret.pop()) { if((point=ret.top())==last) { @@ -233,20 +233,20 @@ void nasal_vm::traceback() std::cout<<" 0x"<, limit "<=bottom;++i,--t) + <<(t=bottom;++i,--t) { std::cout<<" 0x"<)\n"<)\n"<)\n"<)\n"< upval["< op; + typedef std::pair op; std::vector opcall; - uint64_t total=0; - for(uint32_t i=0;i=top[-1].vec().size()) + if((usize)(++top[0].cnt())>=top[-1].vec().size()) { pc=imm[pc]-1; return; } - top[1]={vm_num,(double)top[0].cnt()}; + top[1]={vm_num,(f64)top[0].cnt()}; ++top; } inline void nasal_vm::opr_feach() { std::vector& ref=top[-1].vec().elems; - if((size_t)(++top[0].cnt())>=ref.size()) + if((usize)(++top[0].cnt())>=ref.size()) { pc=imm[pc]-1; return; @@ -679,7 +679,7 @@ inline void nasal_vm::opr_callv() int len=str.length(); if(num<-len || num>=len) die("callv: index out of range:"+std::to_string(val.tonum())); - top[0]={vm_num,double((uint8_t)str[num>=0? num:num+len])}; + top[0]={vm_num,f64((u8)str[num>=0? num:num+len])}; } else die("callv: must call a vector/hash/string"); @@ -710,8 +710,8 @@ inline void nasal_vm::opr_callh() } inline void nasal_vm::opr_callfv() { - uint32_t argc=imm[pc]; // arguments counter - nasal_ref* local=top-argc+1;// arguments begin address + u32 argc=imm[pc]; // arguments counter + nasal_ref* local=top-argc+1; // arguments begin address if(local[-1].type!=vm_func) die("callfv: must call a function"); @@ -723,7 +723,7 @@ inline void nasal_vm::opr_callfv() if(top-argc+func.lsize+3>=canary) die("stack overflow"); // parameter size is func->psize-1, 1 is reserved for "me" - uint32_t psize=func.psize-1; + u32 psize=func.psize-1; if(argc=0)// load dynamic arguments { dynamic=gc.alloc(vm_vec); - for(uint32_t i=psize;i=1;--i)// load arguments + for(u32 i=min_size;i>=1;--i)// load arguments local[i]=local[i-1]; local[0]=func.local[0];// load "me" // load local scope & default arguments - for(uint32_t i=min_size+1;i=0) local[psize+1]=dynamic; @@ -774,7 +774,7 @@ inline void nasal_vm::opr_callfh() nasal_ref* local=top; top+=func.lsize; - for(uint32_t i=0;i& ref=top[-1].vec().elems; std::vector& aim=top[0].vec().elems; - uint8_t type1=val1.type,type2=val2.type; + u8 type1=val1.type,type2=val2.type; int num1=val1.tonum(); int num2=val2.tonum(); int size=ref.size(); @@ -957,7 +957,7 @@ inline void nasal_vm::opr_ret() auto& upval=up.upval(); auto size=func.func().lsize; upval.onstk=false; - for(uint32_t i=0;i code; + std::vector code; for(auto& i:gen.codes()) { code.push_back(i.op);