This commit is contained in:
Li Haokun 2021-08-09 19:13:39 +08:00 committed by GitHub
parent 65dfef0a33
commit 90ac468aa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 27 deletions

View File

@ -45,7 +45,7 @@ void logo()
void die(const char* stage,std::string& filename)
{
std::cout<<">> ["<<stage<<"] in <"<<filename<<">: error(s) occurred,stop.\n";
std::cout<<"["<<stage<<"] in <"<<filename<<">: error(s) occurred,stop.\n";
return;
}
@ -86,7 +86,7 @@ void execute(std::string& file,std::string& command)
die("import",file);
return;
}
codegen.main_progress(import.get_root());
codegen.main_progress(import.get_root(),import.get_file());
if(codegen.get_error())
{
die("codegen",file);

13
nasal.h
View File

@ -49,7 +49,7 @@ inline double oct_to_double(const char* str)
for(;*str;++str)
{
ret*=8;
if('0'<=*str && *str<='8')
if('0'<=*str && *str<'8')
ret+=(*str-'0');
else
return nan("");
@ -106,17 +106,6 @@ double str2num(const char* str)
return is_negative?-ret_num:ret_num;
}
/*
trans_number_to_string:
convert number to string
*/
std::string num2str(double number)
{
std::ostringstream ss;
ss<<number;
return ss.str();
}
/*
show raw string
*/

View File

@ -395,7 +395,7 @@ nasal_val* builtin_str(std::vector<nasal_val*>& local_scope,nasal_gc& gc)
return nullptr;
}
nasal_val* ret_addr=gc.gc_alloc(vm_str);
*ret_addr->ptr.str=val_addr->to_string();
*ret_addr->ptr.str=std::to_string(val_addr->ptr.num);
return ret_addr;
}
nasal_val* builtin_size(std::vector<nasal_val*>& local_scope,nasal_gc& gc)

View File

@ -201,6 +201,7 @@ private:
std::unordered_map<std::string,int> string_table;
std::vector<double> num_res_table;
std::vector<std::string> str_res_table;
std::vector<std::string> file_name;
std::vector<opcode> exec_code;
std::list<std::vector<int>> continue_ptr;
std::list<std::vector<int>> break_ptr;
@ -248,7 +249,7 @@ private:
void ret_gen(nasal_ast&);
public:
int get_error(){return error;}
void main_progress(nasal_ast&);
void main_progress(nasal_ast&,std::vector<std::string>&);
void print_op(int);
void print_byte_code();
std::vector<std::string>& get_str_table(){return str_res_table;}
@ -259,7 +260,7 @@ public:
void nasal_codegen::die(std::string info,int line)
{
++error;
std::cout<<"[code] line "<<line<<": "<<info<<"\n";
std::cout<<"[code] <"<<file_name[fileindex]<<"> line "<<line<<": "<<info<<"\n";
return;
}
@ -1160,7 +1161,7 @@ void nasal_codegen::ret_gen(nasal_ast& ast)
return;
}
void nasal_codegen::main_progress(nasal_ast& ast)
void nasal_codegen::main_progress(nasal_ast& ast,std::vector<std::string>& files)
{
error=0;
in_foreach=0;
@ -1169,6 +1170,7 @@ void nasal_codegen::main_progress(nasal_ast& ast)
number_table.clear();
string_table.clear();
file_name=files;
exec_code.clear();
global.clear();

View File

@ -96,6 +96,14 @@ nasal_val** nasal_vec::get_mem(int index)
}
void nasal_vec::print()
{
static int depth=0;
++depth;
if(depth>1024)
{
std::cout<<"[..]";
--depth;
return;
}
if(!elems.size())
{
std::cout<<"[]";
@ -116,6 +124,7 @@ void nasal_vec::print()
}
std::cout<<",]"[(++iter)==elems.size()];
}
--depth;
return;
}
@ -160,6 +169,14 @@ nasal_val** nasal_hash::get_mem(std::string& key)
}
void nasal_hash::print()
{
static int depth=0;
++depth;
if(depth>1024)
{
std::cout<<"{..}";
--depth;
return;
}
if(!elems.size())
{
std::cout<<"{}";
@ -182,6 +199,7 @@ void nasal_hash::print()
}
std::cout<<",}"[(++iter)==elems.size()];
}
--depth;
return;
}
@ -238,7 +256,7 @@ std::string nasal_val::to_string()
if(type==vm_str)
return *ptr.str;
else if(type==vm_num)
return num2str(ptr.num);
return std::to_string(ptr.num);
return "";
}

View File

@ -132,7 +132,7 @@ void nasal_lexer::openfile(std::string& filename)
{
char c=fin.get();
if(fin.eof())
break;
break;
res.push_back(c);
}
fin.close();

View File

@ -569,7 +569,7 @@ inline void nasal_vm::opr_callv()
{
stack_top[0]=vec_addr->ptr.vec->get_val(val->to_number());
if(!stack_top[0])
die("callv: index out of range:"+num2str(val->to_number()));
die("callv: index out of range:"+std::to_string(val->to_number()));
}
else if(vec_addr->type==vm_hash)
{
@ -594,7 +594,7 @@ inline void nasal_vm::opr_callv()
int str_size=str.length();
if(num<-str_size || num>=str_size)
{
die("callv: index out of range:"+num2str(val->to_number()));
die("callv: index out of range:"+std::to_string(val->to_number()));
return;
}
stack_top[0]=gc.gc_alloc(vm_num);
@ -615,7 +615,7 @@ inline void nasal_vm::opr_callvi()
// cannot use operator[],because this may cause overflow
(++stack_top)[0]=val->ptr.vec->get_val(imm[pc]);
if(!stack_top[0])
die("callvi: index out of range:"+num2str(imm[pc]));
die("callvi: index out of range:"+std::to_string(imm[pc]));
return;
}
inline void nasal_vm::opr_callh()
@ -753,7 +753,7 @@ inline void nasal_vm::opr_slc()
nasal_val* val=(stack_top--)[0];
nasal_val* res=stack_top[-1]->ptr.vec->get_val(val->to_number());
if(!res)
die("slc: index out of range:"+num2str(val->to_number()));
die("slc: index out of range:"+std::to_string(val->to_number()));
stack_top[0]->ptr.vec->elems.push_back(res);
return;
}
@ -781,9 +781,9 @@ inline void nasal_vm::opr_slc2()
if(num1>=num2)
die("slc2: begin index must be less than end index");
else if(num1<-ref_size || num1>=ref_size)
die("slc2: begin index out of range: "+num2str(num1));
die("slc2: begin index out of range: "+std::to_string(num1));
else if(num2<-ref_size || num2>=ref_size)
die("slc2: end index out of range: "+num2str(num2));
die("slc2: end index out of range: "+std::to_string(num2));
else
for(int i=num1;i<=num2;++i)
aim.push_back(i>=0?ref[i]:ref[i+ref_size]);
@ -810,7 +810,7 @@ inline void nasal_vm::opr_mcallv()
{
mem_addr=vec_addr->ptr.vec->get_mem(val->to_number());
if(!mem_addr)
die("mcallv: index out of range:"+num2str(val->to_number()));
die("mcallv: index out of range:"+std::to_string(val->to_number()));
}
else if(type==vm_hash)
{