diff --git a/nasal_builtin.h b/nasal_builtin.h index af93dc7..28c2fa8 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -417,10 +417,10 @@ nasal_ref builtin_size(nasal_ref* local,nasal_gc& gc) double num; switch(val.type) { - case vm_num: num=val.num(); break; - case vm_str: num=val.str().length(); break; - case vm_vec: num=val.vec().elems.size(); break; - case vm_hash: num=val.hash().elems.size();break; + case vm_num: num=val.num(); break; + case vm_str: num=val.str().length();break; + case vm_vec: num=val.vec().size(); break; + case vm_hash: num=val.hash().size(); break; } return {vm_num,num}; } diff --git a/nasal_dbg.h b/nasal_dbg.h index ace5ec9..c74e58e 100644 --- a/nasal_dbg.h +++ b/nasal_dbg.h @@ -151,24 +151,19 @@ void nasal_dbg::interact() else err(); } - else if(res.size()==3) + else if(res.size()==3 && (res[0]=="bk" || res[0]=="break")) { - if(res[0]=="bk" || res[0]=="break") + bk_fidx=get_fileindex(res[1]); + if(bk_fidx==65535) { - bk_fidx=get_fileindex(res[1]); - if(bk_fidx==65535) - { - printf("cannot find file named \"%s\"\n",res[1].c_str()); - bk_fidx=0; - } - int tmp=atoi(res[2].c_str()); - if(tmp<=0) - printf("incorrect line number \"%s\"\n",res[2].c_str()); - else - bk_line=tmp; + printf("cannot find file named \"%s\"\n",res[1].c_str()); + bk_fidx=0; } + int tmp=atoi(res[2].c_str()); + if(tmp<=0) + printf("incorrect line number \"%s\"\n",res[2].c_str()); else - err(); + bk_line=tmp; } else err(); diff --git a/nasal_gc.h b/nasal_gc.h index dda1859..2709468 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -98,22 +98,24 @@ struct nasal_ref struct nasal_vec { - uint32_t depth; + bool printed; std::vector elems; - nasal_vec():depth(0){} + nasal_vec():printed(false){} void print(); + size_t size(){return elems.size();} nasal_ref get_val(const int); nasal_ref* get_mem(const int); }; struct nasal_hash { - uint32_t depth; + bool printed; std::unordered_map elems; - nasal_hash():depth(0){} + nasal_hash():printed(false){} void print(); + size_t size(){return elems.size();} nasal_ref get_val(const std::string&); nasal_ref* get_mem(const std::string&); }; @@ -140,7 +142,7 @@ struct nasal_upval std::vector elems; nasal_upval(){onstk=true;stk=nullptr;size=0;} - nasal_ref& operator[](const int); + nasal_ref& operator[](const int i){return onstk?stk[i]:elems[i];} void clear(){onstk=true;elems.clear();size=0;} }; @@ -198,12 +200,12 @@ nasal_ref* nasal_vec::get_mem(const int index) } void nasal_vec::print() { - if(!elems.size() || depth>3) + if(!elems.size() || printed) { std::cout<<(elems.size()?"[..]":"[]"); return; } - ++depth; + printed=true; size_t iter=0; std::cout<<'['; for(auto& i:elems) @@ -211,7 +213,7 @@ void nasal_vec::print() i.print(); std::cout<<",]"[(++iter)==elems.size()]; } - --depth; + printed=false; } nasal_ref nasal_hash::get_val(const std::string& key) @@ -254,12 +256,12 @@ nasal_ref* nasal_hash::get_mem(const std::string& key) } void nasal_hash::print() { - if(!elems.size() || depth>3) + if(!elems.size() || printed) { std::cout<<(elems.size()?"{..}":"{}"); return; } - ++depth; + printed=true; size_t iter=0; std::cout<<'{'; for(auto& i:elems) @@ -268,7 +270,7 @@ void nasal_hash::print() i.second.print(); std::cout<<",}"[(++iter)==elems.size()]; } - --depth; + printed=false; } void nasal_func::clear() @@ -279,13 +281,6 @@ void nasal_func::clear() keys.clear(); } -nasal_ref& nasal_upval::operator[](const int index) -{ - if(onstk) - return stk[index]; - return elems[index]; -} - nasal_val::nasal_val(uint8_t val_type) { mark=GC_COLLECTED; diff --git a/nasal_opt.h b/nasal_opt.h index 1afa078..cd27acc 100644 --- a/nasal_opt.h +++ b/nasal_opt.h @@ -37,6 +37,14 @@ void calc_const(nasal_ast& root) auto& vec=root.child(); for(auto& i:vec) calc_const(i); + if(vec.size()==1 && root.type()==ast_neg && vec[0].type()==ast_num) + { + double res=-vec[0].num(); + root.set_num(res); + root.child().clear(); + root.set_type(ast_num); + return; + } if(vec.size()!=2) return; if(root.type()!=ast_add && diff --git a/nasal_vm.h b/nasal_vm.h index ede4f91..bcf082e 100644 --- a/nasal_vm.h +++ b/nasal_vm.h @@ -154,8 +154,8 @@ void nasal_vm::valinfo(nasal_ref& val) case vm_num: printf("| num | ");std::cout< %s\n",(uint64_t)p,rawstr(val.str()).c_str());break; case vm_func: printf("| func | <0x%lx> entry:0x%x\n",(uint64_t)p,val.func().entry);break; - case vm_vec: printf("| vec | <0x%lx> [%lu val]\n",(uint64_t)p,val.vec().elems.size());break; - case vm_hash: printf("| hash | <0x%lx> {%lu val}\n",(uint64_t)p,val.hash().elems.size());break; + case vm_vec: printf("| vec | <0x%lx> [%lu val]\n",(uint64_t)p,val.vec().size());break; + case vm_hash: printf("| hash | <0x%lx> {%lu val}\n",(uint64_t)p,val.hash().size());break; case vm_obj: printf("| obj | <0x%lx> obj:0x%lx\n",(uint64_t)p,(uint64_t)val.obj().ptr);break; default: printf("| err | <0x%lx> unknown object\n",(uint64_t)p);break; } @@ -582,7 +582,7 @@ inline void nasal_vm::opr_counter() } inline void nasal_vm::opr_findex() { - if(++gc.top[0].cnt()>=gc.top[-1].vec().elems.size()) + if(++gc.top[0].cnt()>=gc.top[-1].vec().size()) { pc=imm[pc]-1; return; @@ -676,7 +676,7 @@ inline void nasal_vm::opr_callfv() if(local[-1].type!=vm_func) die("callfv: must call a function"); - nasal_func& func=local[-1].func(); + auto& func=local[-1].func(); nasal_ref tmp=local[-1]; local[-1]=gc.funcr; gc.funcr=tmp; @@ -716,7 +716,7 @@ inline void nasal_vm::opr_callfh() if(gc.top[-1].type!=vm_func) die("callfh: must call a function"); - nasal_func& func=gc.top[-1].func(); + auto& func=gc.top[-1].func(); nasal_ref tmp=gc.top[-1]; gc.top[-1]=gc.funcr; gc.funcr=tmp; diff --git a/test/ascii-art.nas b/test/ascii-art.nas index d44c8da..3ff7bf3 100644 --- a/test/ascii-art.nas +++ b/test/ascii-art.nas @@ -151,4 +151,5 @@ curve2(); curve3(); curve4(); curve5(); -curve6(); \ No newline at end of file +curve6(); +println("🟩🟥"); \ No newline at end of file