📝 change some identifiers' name.

This commit is contained in:
ValKmjolnir 2022-07-03 22:46:28 +08:00
parent e8bd4664b3
commit 452bb4a5d8
5 changed files with 48 additions and 42 deletions

View File

@ -22,7 +22,7 @@ enum vm_type:std::uint32_t{
const uint32_t gc_obj_size=vm_type_size-vm_str; const uint32_t gc_obj_size=vm_type_size-vm_str;
// change parameters here to make your own efficient gc // change parameters here to make your own efficient gc
// better set bigger number on vm_vec // better set bigger number on vm_vec
const uint32_t initialize[gc_obj_size]= const uint32_t ini[gc_obj_size]=
{ {
128, // vm_str 128, // vm_str
512, // vm_func 512, // vm_func
@ -32,7 +32,7 @@ const uint32_t initialize[gc_obj_size]=
0, // vm_obj 0, // vm_obj
0 // vm_co 0 // vm_co
}; };
const uint32_t increment[gc_obj_size]= const uint32_t incr[gc_obj_size]=
{ {
256, // vm_str 256, // vm_str
512, // vm_func 512, // vm_func
@ -274,11 +274,12 @@ void nasal_vec::print()
} }
printed=true; printed=true;
size_t iter=0; size_t iter=0;
size_t size=elems.size();
std::cout<<'['; std::cout<<'[';
for(auto& i:elems) for(auto& i:elems)
{ {
i.print(); i.print();
std::cout<<",]"[(++iter)==elems.size()]; std::cout<<",]"[(++iter)==size];
} }
printed=false; printed=false;
} }
@ -330,12 +331,13 @@ void nasal_hash::print()
} }
printed=true; printed=true;
size_t iter=0; size_t iter=0;
size_t size=elems.size();
std::cout<<'{'; std::cout<<'{';
for(auto& i:elems) for(auto& i:elems)
{ {
std::cout<<i.first<<':'; std::cout<<i.first<<':';
i.second.print(); i.second.print();
std::cout<<",}"[(++iter)==elems.size()]; std::cout<<",}"[(++iter)==size];
} }
printed=false; printed=false;
} }
@ -464,6 +466,7 @@ struct nasal_gc
/* values for analysis */ /* values for analysis */
uint64_t size[gc_obj_size]; uint64_t size[gc_obj_size];
uint64_t count[gc_obj_size]; uint64_t count[gc_obj_size];
uint64_t allocc[gc_obj_size];
nasal_gc( nasal_gc(
uint32_t& _pc, uint32_t& _pc,
nasal_ref*& _localr, nasal_ref*& _localr,
@ -577,9 +580,9 @@ void nasal_gc::init(const std::vector<std::string>& s,const std::vector<std::str
funcr=nil; funcr=nil;
for(uint8_t i=0;i<gc_obj_size;++i) for(uint8_t i=0;i<gc_obj_size;++i)
size[i]=count[i]=0; size[i]=count[i]=allocc[i]=0;
for(uint8_t i=vm_str;i<vm_type_size;++i) for(uint8_t i=vm_str;i<vm_type_size;++i)
for(uint32_t j=0;j<initialize[i-vm_str];++j) for(uint32_t j=0;j<ini[i-vm_str];++j)
{ {
nasal_val* tmp=new nasal_val(i); nasal_val* tmp=new nasal_val(i);
memory.push_back(tmp); memory.push_back(tmp);
@ -618,20 +621,18 @@ void nasal_gc::clear()
} }
void nasal_gc::info() void nasal_gc::info()
{ {
const char* name[]={ const char* name[]={"str ","func ","vec ","hash ","upval","obj ","co "};
"str ","func ","vec ",
"hash ","upval","obj ",
"co "
};
std::cout<<"\ngarbage collector info\n"; std::cout<<"\ngarbage collector info\n";
for(uint8_t i=0;i<gc_obj_size;++i) for(uint8_t i=0;i<gc_obj_size;++i)
std::cout<<" "<<name[i]<<" | "<<count[i]<<"\n"; std::cout<<" "<<name[i]<<" | mark-sweep | "<<count[i]<<"\n"
<<" | mem-alloc | "<<allocc[i]<<"\n";
std::cout<<"\nmemory allocator info(max size)\n"; std::cout<<"\nmemory allocator info(max size)\n";
for(uint8_t i=0;i<gc_obj_size;++i) for(uint8_t i=0;i<gc_obj_size;++i)
std::cout<<" "<<name[i]<<" | "<<initialize[i]+size[i]*increment[i]<<"(+"<<size[i]<<")\n"; std::cout<<" "<<name[i]<<" | "<<ini[i]+size[i]*incr[i]<<" (+"<<size[i]<<")\n";
} }
nasal_ref nasal_gc::alloc(uint8_t type) nasal_ref nasal_gc::alloc(uint8_t type)
{ {
++allocc[type-vm_str];
if(free_list[type].empty()) if(free_list[type].empty())
{ {
++count[type-vm_str]; ++count[type-vm_str];
@ -641,7 +642,7 @@ nasal_ref nasal_gc::alloc(uint8_t type)
if(free_list[type].empty()) if(free_list[type].empty())
{ {
++size[type-vm_str]; ++size[type-vm_str];
for(uint32_t i=0;i<increment[type-vm_str];++i) for(uint32_t i=0;i<incr[type-vm_str];++i)
{ {
nasal_val* tmp=new nasal_val(type); nasal_val* tmp=new nasal_val(type);
memory.push_back(tmp); memory.push_back(tmp);
@ -659,10 +660,11 @@ nasal_ref nasal_gc::builtin_alloc(uint8_t type)
// this may cause mark-sweep in gc::alloc // this may cause mark-sweep in gc::alloc
// and the value got before will be collected,this is a fatal error // and the value got before will be collected,this is a fatal error
// so use builtin_alloc in builtin functions if this function uses alloc more then one time // so use builtin_alloc in builtin functions if this function uses alloc more then one time
++allocc[type-vm_str];
if(free_list[type].empty()) if(free_list[type].empty())
{ {
++size[type-vm_str]; ++size[type-vm_str];
for(uint32_t i=0;i<increment[type-vm_str];++i) for(uint32_t i=0;i<incr[type-vm_str];++i)
{ {
nasal_val* tmp=new nasal_val(type); nasal_val* tmp=new nasal_val(type);
memory.push_back(tmp); memory.push_back(tmp);

View File

@ -37,8 +37,8 @@ enum tok:std::uint32_t{
struct{ struct{
const char* str; const char* str;
const uint32_t tok_type; const uint32_t type;
}token_table[]={ }tok_table[]={
{"for" ,tok_for }, {"for" ,tok_for },
{"forindex",tok_forindex }, {"forindex",tok_forindex },
{"foreach" ,tok_foreach }, {"foreach" ,tok_foreach },
@ -123,10 +123,11 @@ public:
line(1), line(1),
column(0), column(0),
ptr(0), ptr(0),
nerr(e){} nerr(e),
res(""){}
void scan(const std::string&); void scan(const std::string&);
void print(); void print();
const std::vector<token>& get_tokens() const {return tokens;} const std::vector<token>& result() const {return tokens;}
}; };
void nasal_lexer::open(const std::string& file) void nasal_lexer::open(const std::string& file)
@ -135,8 +136,7 @@ void nasal_lexer::open(const std::string& file)
if(stat(file.c_str(),&buffer)==0 && !S_ISREG(buffer.st_mode)) if(stat(file.c_str(),&buffer)==0 && !S_ISREG(buffer.st_mode))
{ {
nerr.err("lexer","<"+file+"> is not a regular file."); nerr.err("lexer","<"+file+"> is not a regular file.");
res=""; nerr.chkerr();
return;
} }
std::ifstream fin(file,std::ios::binary); std::ifstream fin(file,std::ios::binary);
if(fin.fail()) if(fin.fail())
@ -148,11 +148,11 @@ void nasal_lexer::open(const std::string& file)
res=ss.str(); res=ss.str();
} }
uint32_t nasal_lexer::get_type(const std::string& tk_str) uint32_t nasal_lexer::get_type(const std::string& str)
{ {
for(int i=0;token_table[i].str;++i) for(uint32_t i=0;tok_table[i].str;++i)
if(tk_str==token_table[i].str) if(str==tok_table[i].str)
return token_table[i].tok_type; return tok_table[i].type;
return tok_null; return tok_null;
} }
@ -166,19 +166,20 @@ std::string nasal_lexer::utf8_gen()
if(nbytes) if(nbytes)
{ {
tmp+=res[ptr++]; tmp+=res[ptr++];
++column; for(uint32_t i=0;i<nbytes;++i,++ptr)
for(uint32_t i=0;i<nbytes;++i,++ptr,++column)
if(ptr<res.size() && (res[ptr]&0xc0)==0x80) if(ptr<res.size() && (res[ptr]&0xc0)==0x80)
tmp+=res[ptr]; tmp+=res[ptr];
if(tmp.length()!=1+nbytes) if(tmp.length()!=1+nbytes)
{ {
++column;
std::string utf_info="0x"+chrhex(tmp[0]); std::string utf_info="0x"+chrhex(tmp[0]);
for(uint32_t i=1;i<tmp.size();++i) for(uint32_t i=1;i<tmp.size();++i)
utf_info+=" 0x"+chrhex(tmp[i]); utf_info+=" 0x"+chrhex(tmp[i]);
die("invalid utf-8 character `"+utf_info+"`, make sure this is a text file."); die("invalid utf-8 character `"+utf_info+"`, make sure it is utf8-text file.");
std::exit(1); std::exit(1);
} }
str+=tmp; str+=tmp;
column+=2; // may have some problems because not all the unicode takes 2 space
} }
else else
{ {
@ -271,7 +272,7 @@ std::string nasal_lexer::num_gen()
std::string nasal_lexer::str_gen() std::string nasal_lexer::str_gen()
{ {
std::string str=""; std::string str="";
char begin=res[ptr]; const char begin=res[ptr];
++column; ++column;
while(++ptr<res.size() && res[ptr]!=begin) while(++ptr<res.size() && res[ptr]!=begin)
{ {

View File

@ -106,7 +106,7 @@ public:
}; };
void nasal_parse::compile(const nasal_lexer& lexer) void nasal_parse::compile(const nasal_lexer& lexer)
{ {
tokens=lexer.get_tokens().data(); tokens=lexer.result().data();
ptr=in_func=in_loop=0; ptr=in_func=in_loop=0;
root={1,ast_root}; root={1,ast_root};
@ -147,7 +147,7 @@ void nasal_parse::match(uint32_t type,const char* info)
case tok_num:die(error_line,"expected number"); break; case tok_num:die(error_line,"expected number"); break;
case tok_str:die(error_line,"expected string"); break; case tok_str:die(error_line,"expected string"); break;
case tok_id: die(error_line,"expected identifier");break; case tok_id: die(error_line,"expected identifier");break;
default: die(error_line,"expected \'"+std::string(token_table[type-tok_for].str)+"\'"); break; default: die(error_line,"expected \'"+std::string(tok_table[type-tok_for].str)+"\'"); break;
} }
return; return;
} }
@ -437,12 +437,12 @@ nasal_ast nasal_parse::lcurve_expr()
} }
nasal_ast nasal_parse::expr() nasal_ast nasal_parse::expr()
{ {
uint32_t tok_type=tokens[ptr].type; uint32_t type=tokens[ptr].type;
if((tok_type==tok_break || tok_type==tok_continue) && !in_loop) if((type==tok_break || type==tok_continue) && !in_loop)
die(error_line,"should use break/continue in loops"); die(error_line,"should use break/continue in loops");
if(tok_type==tok_ret && !in_func) if(type==tok_ret && !in_func)
die(error_line,"should use return in functions"); die(error_line,"should use return in functions");
switch(tok_type) switch(type)
{ {
case tok_nil: case tok_nil:
case tok_num: case tok_num:

View File

@ -147,8 +147,11 @@ var mandelbrot=
var paper=[]; var paper=[];
var (ptr,pc)=(0,0); var (ptr,pc)=(0,0);
var (code,inum,stack)=([],[],[]); var (code,inum,stack,char)=([],[],[],[]);
var (add,mov,jt,jf,in,out)=(0,1,2,3,4,5); var (add,mov,jt,jf,in,out)=(0,1,2,3,4,5);
setsize(char,256);
forindex(var i;char)
char[i]=chr(i);
var funcs=[ var funcs=[
func{paper[ptr]+=inum[pc];}, func{paper[ptr]+=inum[pc];},
@ -156,7 +159,7 @@ var funcs=[
func{if(paper[ptr])pc=inum[pc];}, func{if(paper[ptr])pc=inum[pc];},
func{if(!paper[ptr])pc=inum[pc];}, func{if(!paper[ptr])pc=inum[pc];},
func{paper[ptr]=input()[0];}, func{paper[ptr]=input()[0];},
func{print(chr(paper[ptr]));} func{print(char[paper[ptr]]);}
]; ];
var bf=func(program){ var bf=func(program){

View File

@ -147,18 +147,18 @@ var mandelbrot=
var paper=[]; var paper=[];
var (ptr,pc)=(0,0); var (ptr,pc)=(0,0);
var (code,inum,stack)=([],[],[]); var (code,inum,stack,char)=([],[],[],[]);
var (add,mov,jt,jf,in,out)=(0,1,2,3,4,5); var (add,mov,jt,jf,in,out)=(0,1,2,3,4,5);
setsize(char,256);
var color=[ var color=[
"\e[31m","\e[32m","\e[33m","\e[34m","\e[35m","\e[36m", "\e[31m","\e[32m","\e[33m","\e[34m","\e[35m","\e[36m",
"\e[90m","\e[91m","\e[92m","\e[93m","\e[94m","\e[95m","\e[96m" "\e[90m","\e[91m","\e[92m","\e[93m","\e[94m","\e[95m","\e[96m"
]; ];
var table=[];
func(){ func(){
var cnt=0; var cnt=0;
for(var i=0;i<256;i+=1){ forindex(var i;char){
append(table,color[cnt]~chr(i)~"\e[0m"); char[i]=color[cnt]~chr(i)~"\e[0m";
cnt+=1; cnt+=1;
if(cnt>12) if(cnt>12)
cnt=0; cnt=0;
@ -171,7 +171,7 @@ var funcs=[
func{if(paper[ptr])pc=inum[pc];}, func{if(paper[ptr])pc=inum[pc];},
func{if(!paper[ptr])pc=inum[pc];}, func{if(!paper[ptr])pc=inum[pc];},
func{paper[ptr]=input()[0];}, func{paper[ptr]=input()[0];},
func{print(table[paper[ptr]]);} func{print(char[paper[ptr]]);}
]; ];
var bf=func(program){ var bf=func(program){