📝 change `int` in code to `i32`, optimize code.
This commit is contained in:
parent
7ad1d69c64
commit
068743aa4c
2
nasal.h
2
nasal.h
|
@ -132,7 +132,7 @@ f64 str2num(const char* str)
|
|||
return negative?-res:res;
|
||||
}
|
||||
|
||||
int utf8_hdchk(const char head)
|
||||
i32 utf8_hdchk(const char head)
|
||||
{
|
||||
// RFC-2279 but now we use RFC-3629 so nbytes is less than 4
|
||||
const u8 c=(u8)head;
|
||||
|
|
|
@ -356,7 +356,7 @@ nas_ref builtin_rand(nas_ref* local,nasal_gc& gc)
|
|||
return builtin_err("rand","\"seed\" must be nil or number");
|
||||
if(val.type==vm_num)
|
||||
{
|
||||
srand((unsigned int)val.num());
|
||||
srand((u32)val.num());
|
||||
return nil;
|
||||
}
|
||||
f64 num=0;
|
||||
|
@ -662,10 +662,9 @@ nas_ref builtin_left(nas_ref* local,nasal_gc& gc)
|
|||
return builtin_err("left","\"string\" must be string");
|
||||
if(len.type!=vm_num)
|
||||
return builtin_err("left","\"length\" must be number");
|
||||
int length=(int)len.num();
|
||||
if(length<0)
|
||||
length=0;
|
||||
return gc.newstr(str.str().substr(0,length));
|
||||
if(len.num()<0)
|
||||
return gc.newstr("");
|
||||
return gc.newstr(str.str().substr(0,len.num()));
|
||||
}
|
||||
nas_ref builtin_right(nas_ref* local,nasal_gc& gc)
|
||||
{
|
||||
|
@ -675,13 +674,13 @@ nas_ref builtin_right(nas_ref* local,nasal_gc& gc)
|
|||
return builtin_err("right","\"string\" must be string");
|
||||
if(len.type!=vm_num)
|
||||
return builtin_err("right","\"length\" must be number");
|
||||
int length=(int)len.num();
|
||||
int srclen=str.str().length();
|
||||
i32 length=(i32)len.num();
|
||||
i32 srclen=str.str().length();
|
||||
if(length>srclen)
|
||||
length=srclen;
|
||||
if(length<0)
|
||||
length=0;
|
||||
return gc.newstr(str.str().substr(srclen-length, srclen));
|
||||
return gc.newstr(str.str().substr(srclen-length,srclen));
|
||||
}
|
||||
nas_ref builtin_cmp(nas_ref* local,nasal_gc& gc)
|
||||
{
|
||||
|
@ -716,7 +715,7 @@ nas_ref builtin_chr(nas_ref* local,nasal_gc& gc)
|
|||
nas_ref code=local[1];
|
||||
if(code.type!=vm_num)
|
||||
return builtin_err("chr","\"code\" must be number");
|
||||
int num=code.num();
|
||||
i32 num=code.num();
|
||||
if(0<=num && num<128)
|
||||
return gc.newstr((char)num);
|
||||
else if(128<=num && num<256)
|
||||
|
@ -974,7 +973,7 @@ nas_ref builtin_sleep(nas_ref* local,nasal_gc& gc)
|
|||
nas_ref builtin_pipe(nas_ref* local,nasal_gc& gc)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
int fd[2];
|
||||
i32 fd[2];
|
||||
nas_ref res=gc.alloc(vm_vec);
|
||||
if(pipe(fd)==-1)
|
||||
return builtin_err("pipe","failed to create pipe");
|
||||
|
@ -1001,8 +1000,8 @@ nas_ref builtin_waitpid(nas_ref* local,nasal_gc& gc)
|
|||
if(pid.type!=vm_num || nohang.type!=vm_num)
|
||||
return builtin_err("waitpid","pid and nohang must be number");
|
||||
#ifndef _WIN32
|
||||
int ret_pid;
|
||||
int status;
|
||||
i32 ret_pid;
|
||||
i32 status;
|
||||
ret_pid=waitpid(pid.num(),&status,nohang.num()==0?0:WNOHANG);
|
||||
nas_ref vec=gc.alloc(vm_vec);
|
||||
vec.vec().elems.push_back({vm_num,(f64)ret_pid});
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef __NASAL_CODEGEN_H__
|
||||
#define __NASAL_CODEGEN_H__
|
||||
|
||||
enum op_code
|
||||
enum op_code:u8
|
||||
{
|
||||
op_exit, // stop the virtual machine
|
||||
op_intg, // global scope size
|
||||
|
@ -82,7 +82,7 @@ enum op_code
|
|||
|
||||
struct
|
||||
{
|
||||
int type;
|
||||
u8 type;
|
||||
const char* name;
|
||||
}code_table[]=
|
||||
{
|
||||
|
@ -160,8 +160,7 @@ struct
|
|||
{op_mupval,"mupval"},
|
||||
{op_mcallv,"mcallv"},
|
||||
{op_mcallh,"mcallh"},
|
||||
{op_ret, "ret "},
|
||||
{-1, nullptr },
|
||||
{op_ret, "ret "}
|
||||
};
|
||||
|
||||
struct opcode
|
||||
|
@ -253,12 +252,12 @@ private:
|
|||
std::vector<f64> num_res;
|
||||
std::vector<string> str_res;
|
||||
std::vector<opcode> code;
|
||||
std::list<std::vector<int>> continue_ptr;
|
||||
std::list<std::vector<int>> break_ptr;
|
||||
std::list<std::vector<i32>> continue_ptr;
|
||||
std::list<std::vector<i32>> break_ptr;
|
||||
// global : max 4095 values
|
||||
std::unordered_map<string,int> global;
|
||||
std::unordered_map<string,i32> global;
|
||||
// local : max 32768 upvalues 65536 values
|
||||
std::list<std::unordered_map<string,int>> local;
|
||||
std::list<std::unordered_map<string,i32>> local;
|
||||
|
||||
// func end stack, reserved for code print
|
||||
std::stack<u32> fbstk;
|
||||
|
@ -269,9 +268,9 @@ private:
|
|||
void regist_str(const string&);
|
||||
void find_symbol(const nasal_ast&);
|
||||
void add_sym(const string&);
|
||||
int local_find(const string&);
|
||||
int global_find(const string&);
|
||||
int upvalue_find(const string&);
|
||||
i32 local_find(const string&);
|
||||
i32 global_find(const string&);
|
||||
i32 upvalue_find(const string&);
|
||||
void gen(u8,u32,u32);
|
||||
void num_gen(const nasal_ast&);
|
||||
void str_gen(const nasal_ast&);
|
||||
|
@ -293,7 +292,7 @@ private:
|
|||
void multi_assign_gen(const nasal_ast&);
|
||||
void conditional_gen(const nasal_ast&);
|
||||
void loop_gen(const nasal_ast&);
|
||||
void load_continue_break(int,int);
|
||||
void load_continue_break(i32,i32);
|
||||
void while_gen(const nasal_ast&);
|
||||
void for_gen(const nasal_ast&);
|
||||
void forindex_gen(const nasal_ast&);
|
||||
|
@ -372,32 +371,32 @@ void nasal_codegen::add_sym(const string& name)
|
|||
{
|
||||
if(global.count(name))
|
||||
return;
|
||||
int index=global.size();
|
||||
i32 index=global.size();
|
||||
global[name]=index;
|
||||
return;
|
||||
}
|
||||
if(local.back().count(name))
|
||||
return;
|
||||
int index=local.back().size();
|
||||
i32 index=local.back().size();
|
||||
local.back()[name]=index;
|
||||
}
|
||||
|
||||
int nasal_codegen::local_find(const string& name)
|
||||
i32 nasal_codegen::local_find(const string& name)
|
||||
{
|
||||
if(local.empty())
|
||||
return -1;
|
||||
return local.back().count(name)?local.back()[name]:-1;
|
||||
}
|
||||
|
||||
int nasal_codegen::global_find(const string& name)
|
||||
i32 nasal_codegen::global_find(const string& name)
|
||||
{
|
||||
return global.count(name)?global[name]:-1;
|
||||
}
|
||||
|
||||
int nasal_codegen::upvalue_find(const string& name)
|
||||
i32 nasal_codegen::upvalue_find(const string& name)
|
||||
{
|
||||
// 32768 level 65536 upvalues
|
||||
int index=-1;
|
||||
i32 index=-1;
|
||||
usize size=local.size();
|
||||
if(size<=1)
|
||||
return -1;
|
||||
|
@ -530,7 +529,7 @@ void nasal_codegen::call_id(const nasal_ast& ast)
|
|||
die("builtin functions work in a local scope.",ast.line());
|
||||
return;
|
||||
}
|
||||
int index;
|
||||
i32 index;
|
||||
if((index=local_find(str))>=0)
|
||||
{
|
||||
gen(op_calll,index,ast.line());
|
||||
|
@ -646,7 +645,7 @@ void nasal_codegen::mcall_id(const nasal_ast& ast)
|
|||
die("cannot change builtin function.",ast.line());
|
||||
return;
|
||||
}
|
||||
int index;
|
||||
i32 index;
|
||||
if((index=local_find(str))>=0)
|
||||
{
|
||||
gen(op_mcalll,index,ast.line());
|
||||
|
@ -723,12 +722,12 @@ void nasal_codegen::def_gen(const nasal_ast& ast)
|
|||
|
||||
void nasal_codegen::multi_assign_gen(const nasal_ast& ast)
|
||||
{
|
||||
int size=ast[0].size();
|
||||
i32 size=ast[0].size();
|
||||
if(ast[1].type()==ast_multi_scalar)
|
||||
{
|
||||
for(int i=size-1;i>=0;--i)
|
||||
for(i32 i=size-1;i>=0;--i)
|
||||
calc_gen(ast[1][i]);
|
||||
for(int i=0;i<size;++i)
|
||||
for(i32 i=0;i<size;++i)
|
||||
{
|
||||
mcall(ast[0][i]);
|
||||
// multi assign user loadl and loadg to avoid meq's stack--
|
||||
|
@ -746,7 +745,7 @@ void nasal_codegen::multi_assign_gen(const nasal_ast& ast)
|
|||
else
|
||||
{
|
||||
calc_gen(ast[1]);
|
||||
for(int i=0;i<size;++i)
|
||||
for(i32 i=0;i<size;++i)
|
||||
{
|
||||
gen(op_callvi,i,ast[1].line());
|
||||
// multi assign user loadl and loadg to avoid meq's stack--
|
||||
|
@ -767,7 +766,7 @@ void nasal_codegen::multi_assign_gen(const nasal_ast& ast)
|
|||
|
||||
void nasal_codegen::conditional_gen(const nasal_ast& ast)
|
||||
{
|
||||
std::vector<int> jmp_label;
|
||||
std::vector<usize> jmp_label;
|
||||
for(auto& tmp:ast.child())
|
||||
{
|
||||
if(tmp.type()==ast_if || tmp.type()==ast_elsif)
|
||||
|
@ -796,8 +795,8 @@ void nasal_codegen::conditional_gen(const nasal_ast& ast)
|
|||
|
||||
void nasal_codegen::loop_gen(const nasal_ast& ast)
|
||||
{
|
||||
continue_ptr.push_front(std::vector<int>());
|
||||
break_ptr.push_front(std::vector<int>());
|
||||
continue_ptr.push_front(std::vector<i32>());
|
||||
break_ptr.push_front(std::vector<i32>());
|
||||
switch(ast.type())
|
||||
{
|
||||
case ast_while: while_gen(ast); break;
|
||||
|
@ -807,7 +806,7 @@ void nasal_codegen::loop_gen(const nasal_ast& ast)
|
|||
}
|
||||
}
|
||||
|
||||
void nasal_codegen::load_continue_break(int continue_place,int break_place)
|
||||
void nasal_codegen::load_continue_break(i32 continue_place,i32 break_place)
|
||||
{
|
||||
for(auto i:continue_ptr.front())
|
||||
code[i].num=continue_place;
|
||||
|
|
|
@ -192,7 +192,7 @@ void nasal_dbg::interact()
|
|||
std::cout<<"cannot find file named `"<<res[1]<<"`\n";
|
||||
bk_fidx=0;
|
||||
}
|
||||
int tmp=atoi(res[2].c_str());
|
||||
i32 tmp=atoi(res[2].c_str());
|
||||
if(tmp<=0)
|
||||
std::cout<<"incorrect line number `"<<res[2]<<"`\n";
|
||||
else
|
||||
|
|
|
@ -56,7 +56,7 @@ public:
|
|||
++error;
|
||||
const string& code=res[line-1];
|
||||
std::cerr<<"["<<stage<<"] "<<file<<":"<<line<<":"<<column<<" "<<info<<"\n"<<code<<"\n";
|
||||
for(int i=0;i<(int)column-1;++i)
|
||||
for(i32 i=0;i<(i32)column-1;++i)
|
||||
std::cerr<<char(" \t"[code[i]=='\t']);
|
||||
std::cerr<<"^\n";
|
||||
}
|
||||
|
|
20
nasal_gc.h
20
nasal_gc.h
|
@ -104,8 +104,8 @@ struct nas_vec
|
|||
nas_vec():printed(false){}
|
||||
void print();
|
||||
usize size(){return elems.size();}
|
||||
nas_ref get_val(const int);
|
||||
nas_ref* get_mem(const int);
|
||||
nas_ref get_val(const i32);
|
||||
nas_ref* get_mem(const i32);
|
||||
};
|
||||
|
||||
struct nas_hash
|
||||
|
@ -249,19 +249,19 @@ struct nas_val
|
|||
~nas_val();
|
||||
};
|
||||
|
||||
nas_ref nas_vec::get_val(const int index)
|
||||
nas_ref nas_vec::get_val(const i32 n)
|
||||
{
|
||||
int size=elems.size();
|
||||
if(index<-size || index>=size)
|
||||
i32 size=elems.size();
|
||||
if(n<-size || n>=size)
|
||||
return {vm_none};
|
||||
return elems[index>=0?index:index+size];
|
||||
return elems[n>=0?n:n+size];
|
||||
}
|
||||
nas_ref* nas_vec::get_mem(const int index)
|
||||
nas_ref* nas_vec::get_mem(const i32 n)
|
||||
{
|
||||
int size=elems.size();
|
||||
if(index<-size || index>=size)
|
||||
i32 size=elems.size();
|
||||
if(n<-size || n>=size)
|
||||
return nullptr;
|
||||
return &elems[index>=0?index:index+size];
|
||||
return &elems[n>=0?n:n+size];
|
||||
}
|
||||
void nas_vec::print()
|
||||
{
|
||||
|
|
|
@ -123,7 +123,7 @@ void nasal_parse::compile(const nasal_lexer& lexer)
|
|||
}
|
||||
void nasal_parse::die(u32 line,string info,bool report_prev=false)
|
||||
{
|
||||
int col=(int)tokens[ptr].col-(tokens[ptr].type==tok_eof?0:(int)tokens[ptr].str.length());
|
||||
i32 col=(i32)tokens[ptr].col-(tokens[ptr].type==tok_eof?0:(i32)tokens[ptr].str.length());
|
||||
if(tokens[ptr].type==tok_str)
|
||||
col-=2; // tok_str's str has no \"
|
||||
if(report_prev && ptr) // used to report lack of ',' ';'
|
||||
|
|
12
nasal_vm.h
12
nasal_vm.h
|
@ -640,8 +640,8 @@ inline void nasal_vm::o_callv()
|
|||
else if(vec.type==vm_str)
|
||||
{
|
||||
string& str=vec.str();
|
||||
int num=val.tonum();
|
||||
int len=str.length();
|
||||
i32 num=val.tonum();
|
||||
i32 len=str.length();
|
||||
if(num<-len || num>=len)
|
||||
die("callv: index out of range:"+std::to_string(val.tonum()));
|
||||
top[0]={vm_num,f64((u8)str[num>=0? num:num+len])};
|
||||
|
@ -802,9 +802,9 @@ inline void nasal_vm::o_slc2()
|
|||
std::vector<nas_ref>& aim=top[0].vec().elems;
|
||||
|
||||
u8 type1=val1.type,type2=val2.type;
|
||||
int num1=val1.tonum();
|
||||
int num2=val2.tonum();
|
||||
int size=ref.size();
|
||||
i32 num1=val1.tonum();
|
||||
i32 num2=val2.tonum();
|
||||
i32 size=ref.size();
|
||||
if(type1==vm_nil && type2==vm_nil)
|
||||
{
|
||||
num1=0;
|
||||
|
@ -822,7 +822,7 @@ inline void nasal_vm::o_slc2()
|
|||
else if(num2<-size || num2>=size)
|
||||
die("slc2: end index out of range: "+std::to_string(num2));
|
||||
else
|
||||
for(int i=num1;i<=num2;++i)
|
||||
for(i32 i=num1;i<=num2;++i)
|
||||
aim.push_back(i>=0?ref[i]:ref[i+size]);
|
||||
}
|
||||
inline void nasal_vm::o_mcallg()
|
||||
|
|
Loading…
Reference in New Issue