📝 change some identifiers' name.
This commit is contained in:
parent
e8bd4664b3
commit
452bb4a5d8
32
nasal_gc.h
32
nasal_gc.h
|
@ -22,7 +22,7 @@ enum vm_type:std::uint32_t{
|
|||
const uint32_t gc_obj_size=vm_type_size-vm_str;
|
||||
// change parameters here to make your own efficient gc
|
||||
// better set bigger number on vm_vec
|
||||
const uint32_t initialize[gc_obj_size]=
|
||||
const uint32_t ini[gc_obj_size]=
|
||||
{
|
||||
128, // vm_str
|
||||
512, // vm_func
|
||||
|
@ -32,7 +32,7 @@ const uint32_t initialize[gc_obj_size]=
|
|||
0, // vm_obj
|
||||
0 // vm_co
|
||||
};
|
||||
const uint32_t increment[gc_obj_size]=
|
||||
const uint32_t incr[gc_obj_size]=
|
||||
{
|
||||
256, // vm_str
|
||||
512, // vm_func
|
||||
|
@ -274,11 +274,12 @@ void nasal_vec::print()
|
|||
}
|
||||
printed=true;
|
||||
size_t iter=0;
|
||||
size_t size=elems.size();
|
||||
std::cout<<'[';
|
||||
for(auto& i:elems)
|
||||
{
|
||||
i.print();
|
||||
std::cout<<",]"[(++iter)==elems.size()];
|
||||
std::cout<<",]"[(++iter)==size];
|
||||
}
|
||||
printed=false;
|
||||
}
|
||||
|
@ -330,12 +331,13 @@ void nasal_hash::print()
|
|||
}
|
||||
printed=true;
|
||||
size_t iter=0;
|
||||
size_t size=elems.size();
|
||||
std::cout<<'{';
|
||||
for(auto& i:elems)
|
||||
{
|
||||
std::cout<<i.first<<':';
|
||||
i.second.print();
|
||||
std::cout<<",}"[(++iter)==elems.size()];
|
||||
std::cout<<",}"[(++iter)==size];
|
||||
}
|
||||
printed=false;
|
||||
}
|
||||
|
@ -464,6 +466,7 @@ struct nasal_gc
|
|||
/* values for analysis */
|
||||
uint64_t size[gc_obj_size];
|
||||
uint64_t count[gc_obj_size];
|
||||
uint64_t allocc[gc_obj_size];
|
||||
nasal_gc(
|
||||
uint32_t& _pc,
|
||||
nasal_ref*& _localr,
|
||||
|
@ -577,9 +580,9 @@ void nasal_gc::init(const std::vector<std::string>& s,const std::vector<std::str
|
|||
funcr=nil;
|
||||
|
||||
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(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);
|
||||
memory.push_back(tmp);
|
||||
|
@ -618,20 +621,18 @@ void nasal_gc::clear()
|
|||
}
|
||||
void nasal_gc::info()
|
||||
{
|
||||
const char* name[]={
|
||||
"str ","func ","vec ",
|
||||
"hash ","upval","obj ",
|
||||
"co "
|
||||
};
|
||||
const char* name[]={"str ","func ","vec ","hash ","upval","obj ","co "};
|
||||
std::cout<<"\ngarbage collector info\n";
|
||||
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";
|
||||
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)
|
||||
{
|
||||
++allocc[type-vm_str];
|
||||
if(free_list[type].empty())
|
||||
{
|
||||
++count[type-vm_str];
|
||||
|
@ -641,7 +642,7 @@ nasal_ref nasal_gc::alloc(uint8_t type)
|
|||
if(free_list[type].empty())
|
||||
{
|
||||
++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);
|
||||
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
|
||||
// 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
|
||||
++allocc[type-vm_str];
|
||||
if(free_list[type].empty())
|
||||
{
|
||||
++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);
|
||||
memory.push_back(tmp);
|
||||
|
|
|
@ -37,8 +37,8 @@ enum tok:std::uint32_t{
|
|||
|
||||
struct{
|
||||
const char* str;
|
||||
const uint32_t tok_type;
|
||||
}token_table[]={
|
||||
const uint32_t type;
|
||||
}tok_table[]={
|
||||
{"for" ,tok_for },
|
||||
{"forindex",tok_forindex },
|
||||
{"foreach" ,tok_foreach },
|
||||
|
@ -123,10 +123,11 @@ public:
|
|||
line(1),
|
||||
column(0),
|
||||
ptr(0),
|
||||
nerr(e){}
|
||||
nerr(e),
|
||||
res(""){}
|
||||
void scan(const std::string&);
|
||||
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)
|
||||
|
@ -135,8 +136,7 @@ void nasal_lexer::open(const std::string& file)
|
|||
if(stat(file.c_str(),&buffer)==0 && !S_ISREG(buffer.st_mode))
|
||||
{
|
||||
nerr.err("lexer","<"+file+"> is not a regular file.");
|
||||
res="";
|
||||
return;
|
||||
nerr.chkerr();
|
||||
}
|
||||
std::ifstream fin(file,std::ios::binary);
|
||||
if(fin.fail())
|
||||
|
@ -148,11 +148,11 @@ void nasal_lexer::open(const std::string& file)
|
|||
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)
|
||||
if(tk_str==token_table[i].str)
|
||||
return token_table[i].tok_type;
|
||||
for(uint32_t i=0;tok_table[i].str;++i)
|
||||
if(str==tok_table[i].str)
|
||||
return tok_table[i].type;
|
||||
return tok_null;
|
||||
}
|
||||
|
||||
|
@ -166,19 +166,20 @@ std::string nasal_lexer::utf8_gen()
|
|||
if(nbytes)
|
||||
{
|
||||
tmp+=res[ptr++];
|
||||
++column;
|
||||
for(uint32_t i=0;i<nbytes;++i,++ptr,++column)
|
||||
for(uint32_t i=0;i<nbytes;++i,++ptr)
|
||||
if(ptr<res.size() && (res[ptr]&0xc0)==0x80)
|
||||
tmp+=res[ptr];
|
||||
if(tmp.length()!=1+nbytes)
|
||||
{
|
||||
++column;
|
||||
std::string utf_info="0x"+chrhex(tmp[0]);
|
||||
for(uint32_t i=1;i<tmp.size();++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);
|
||||
}
|
||||
str+=tmp;
|
||||
column+=2; // may have some problems because not all the unicode takes 2 space
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -271,7 +272,7 @@ std::string nasal_lexer::num_gen()
|
|||
std::string nasal_lexer::str_gen()
|
||||
{
|
||||
std::string str="";
|
||||
char begin=res[ptr];
|
||||
const char begin=res[ptr];
|
||||
++column;
|
||||
while(++ptr<res.size() && res[ptr]!=begin)
|
||||
{
|
||||
|
|
|
@ -106,7 +106,7 @@ public:
|
|||
};
|
||||
void nasal_parse::compile(const nasal_lexer& lexer)
|
||||
{
|
||||
tokens=lexer.get_tokens().data();
|
||||
tokens=lexer.result().data();
|
||||
ptr=in_func=in_loop=0;
|
||||
|
||||
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_str:die(error_line,"expected string"); 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;
|
||||
}
|
||||
|
@ -437,12 +437,12 @@ nasal_ast nasal_parse::lcurve_expr()
|
|||
}
|
||||
nasal_ast nasal_parse::expr()
|
||||
{
|
||||
uint32_t tok_type=tokens[ptr].type;
|
||||
if((tok_type==tok_break || tok_type==tok_continue) && !in_loop)
|
||||
uint32_t type=tokens[ptr].type;
|
||||
if((type==tok_break || type==tok_continue) && !in_loop)
|
||||
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");
|
||||
switch(tok_type)
|
||||
switch(type)
|
||||
{
|
||||
case tok_nil:
|
||||
case tok_num:
|
||||
|
|
|
@ -147,8 +147,11 @@ var mandelbrot=
|
|||
|
||||
var paper=[];
|
||||
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);
|
||||
setsize(char,256);
|
||||
forindex(var i;char)
|
||||
char[i]=chr(i);
|
||||
|
||||
var funcs=[
|
||||
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{paper[ptr]=input()[0];},
|
||||
func{print(chr(paper[ptr]));}
|
||||
func{print(char[paper[ptr]]);}
|
||||
];
|
||||
|
||||
var bf=func(program){
|
||||
|
|
|
@ -147,18 +147,18 @@ var mandelbrot=
|
|||
|
||||
var paper=[];
|
||||
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);
|
||||
setsize(char,256);
|
||||
|
||||
var color=[
|
||||
"\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"
|
||||
];
|
||||
var table=[];
|
||||
func(){
|
||||
var cnt=0;
|
||||
for(var i=0;i<256;i+=1){
|
||||
append(table,color[cnt]~chr(i)~"\e[0m");
|
||||
forindex(var i;char){
|
||||
char[i]=color[cnt]~chr(i)~"\e[0m";
|
||||
cnt+=1;
|
||||
if(cnt>12)
|
||||
cnt=0;
|
||||
|
@ -171,7 +171,7 @@ var funcs=[
|
|||
func{if(paper[ptr])pc=inum[pc];},
|
||||
func{if(!paper[ptr])pc=inum[pc];},
|
||||
func{paper[ptr]=input()[0];},
|
||||
func{print(table[paper[ptr]]);}
|
||||
func{print(char[paper[ptr]]);}
|
||||
];
|
||||
|
||||
var bf=func(program){
|
||||
|
|
Loading…
Reference in New Issue