From 90ac468aa9b16b8a94f2a48f50a85c99d5de084b Mon Sep 17 00:00:00 2001 From: Li Haokun Date: Mon, 9 Aug 2021 19:13:39 +0800 Subject: [PATCH] update --- main.cpp | 4 ++-- nasal.h | 13 +------------ nasal_builtin.h | 2 +- nasal_codegen.h | 8 +++++--- nasal_gc.h | 20 +++++++++++++++++++- nasal_lexer.h | 2 +- nasal_vm.h | 14 +++++++------- 7 files changed, 36 insertions(+), 27 deletions(-) diff --git a/main.cpp b/main.cpp index 2587d00..b0fbb47 100644 --- a/main.cpp +++ b/main.cpp @@ -45,7 +45,7 @@ void logo() void die(const char* stage,std::string& filename) { - std::cout<<">> ["<: error(s) occurred,stop.\n"; + std::cout<<"["<: 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); diff --git a/nasal.h b/nasal.h index f089d18..89b7f8d 100644 --- a/nasal.h +++ b/nasal.h @@ -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<& 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& local_scope,nasal_gc& gc) diff --git a/nasal_codegen.h b/nasal_codegen.h index c70d488..0911725 100644 --- a/nasal_codegen.h +++ b/nasal_codegen.h @@ -201,6 +201,7 @@ private: std::unordered_map string_table; std::vector num_res_table; std::vector str_res_table; + std::vector file_name; std::vector exec_code; std::list> continue_ptr; std::list> 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&); void print_op(int); void print_byte_code(); std::vector& 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 "<& 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(); diff --git a/nasal_gc.h b/nasal_gc.h index 983d5e8..be7e115 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -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 ""; } diff --git a/nasal_lexer.h b/nasal_lexer.h index facd681..4ae5734 100644 --- a/nasal_lexer.h +++ b/nasal_lexer.h @@ -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(); diff --git a/nasal_vm.h b/nasal_vm.h index 9229ca1..92c4e98 100644 --- a/nasal_vm.h +++ b/nasal_vm.h @@ -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) {