From 30650bb64f95b78554cd1d187a90d507c518eba2 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Thu, 23 Dec 2021 14:09:54 +0800 Subject: [PATCH] update error info (except parser) prepare for nasal_err module --- main.cpp | 11 ++--- nasal_codegen.h | 2 +- nasal_dbg.h | 104 ++++++++++++++++++++++++------------------------ nasal_lexer.h | 14 ++++--- nasal_vm.h | 10 ++--- 5 files changed, 72 insertions(+), 69 deletions(-) diff --git a/main.cpp b/main.cpp index f71acac..911aad0 100644 --- a/main.cpp +++ b/main.cpp @@ -77,6 +77,8 @@ void execute(const std::string& file,const uint32_t cmd) lexer.scan(file); if(lexer.err()) die("lexer",file); + if(cmd&VM_LEXINFO) + lexer.print(); // parser gets lexer's token list to compile parse.compile(lexer); @@ -87,18 +89,17 @@ void execute(const std::string& file,const uint32_t cmd) linker.link(parse,file); if(linker.err()) die("import",file); + if(cmd&VM_ASTINFO) + parse.print(); // code generator gets parser's ast and linker's import file list to generate code gen.compile(parse,linker); if(gen.err()) die("code",file); - - if(cmd&VM_LEXINFO) - lexer.print(); - if(cmd&VM_ASTINFO) - parse.print(); if(cmd&VM_CODEINFO) gen.print(); + + // run bytecode if(cmd&VM_DEBUG) { nasal_dbg dbg; diff --git a/nasal_codegen.h b/nasal_codegen.h index 8c531e5..8a1032b 100644 --- a/nasal_codegen.h +++ b/nasal_codegen.h @@ -258,7 +258,7 @@ public: void nasal_codegen::die(const std::string info,const int line) { ++error; - std::cout<<"[code] <"< line "< "<\n"; std::exit(-1); } + std::string tmp; while(!fin.eof()) { - std::string tmp; std::getline(fin,tmp); src.push_back(tmp); } @@ -124,7 +124,7 @@ void nasal_dbg::interact() if(bytecode[pc].op==op_intg) { std::cout - <<"nasal debug mode\n" + <<"[debug] nasal debug mode\n" <<"input \'h\' to get help\n"; } else if(bytecode[pc].op==op_nop || bytecode[pc].op==op_exit) @@ -143,57 +143,57 @@ void nasal_dbg::interact() printf(">> "); std::getline(std::cin,cmd); auto res=parse(cmd); - switch(res.size()) + if(res.size()==1) { - case 1: - if(res[0]=="h" || res[0]=="help") - help(); - else if(res[0]=="bt" || res[0]=="backtrace") - traceback(); - else if(res[0]=="c" || res[0]=="continue") - return; - else if(res[0]=="g" || res[0]=="global") - global_state(); - else if(res[0]=="l" || res[0]=="local") - local_state(); - else if(res[0]=="u" || res[0]=="upval") - upval_state(); - else if(res[0]=="a" || res[0]=="all") - { - global_state(); - local_state(); - upval_state(); - } - else if(res[0]=="n" || res[0]=="next") - { - next_step=true; - return; - } - else if(res[0]=="q" || res[0]=="exit") - std::exit(0); - else - err(); - break; - case 3: - if(res[0]=="bk" || res[0]=="break") - { - 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; - } - else - err(); - break; - default:err();break; + if(res[0]=="h" || res[0]=="help") + help(); + else if(res[0]=="bt" || res[0]=="backtrace") + traceback(); + else if(res[0]=="c" || res[0]=="continue") + return; + else if(res[0]=="g" || res[0]=="global") + global_state(); + else if(res[0]=="l" || res[0]=="local") + local_state(); + else if(res[0]=="u" || res[0]=="upval") + upval_state(); + else if(res[0]=="a" || res[0]=="all") + { + global_state(); + local_state(); + upval_state(); + } + else if(res[0]=="n" || res[0]=="next") + { + next_step=true; + return; + } + else if(res[0]=="q" || res[0]=="exit") + std::exit(0); + else + err(); } + else if(res.size()==3) + { + if(res[0]=="bk" || res[0]=="break") + { + 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; + } + else + err(); + } + else + err(); } } @@ -246,7 +246,7 @@ vmexit: die("stack overflow"); gc.clear(); imm.clear(); - printf("debugger exited\n"); + printf("[debug] debugger exited\n"); return; #define dbg(op) {interact();op();if(gc.top tokens; @@ -120,6 +121,7 @@ public: void nasal_lexer::open(const std::string& file) { + filename=file; std::ifstream fin(file,std::ios::binary); if(fin.fail()) { @@ -142,7 +144,7 @@ uint32_t nasal_lexer::get_type(const std::string& tk_str) void nasal_lexer::die(const char* info) { ++error; - std::cout<<"[lexer] line "< %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_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 member}\n",(uint64_t)p,val.hash()->elems.size());break; - case vm_obj: printf("| obj | <0x%lx> object=0x%lx\n",(uint64_t)p,(uint64_t)val.obj()->ptr);break; + case vm_hash: printf("| hash | <0x%lx> {%lu val}\n",(uint64_t)p,val.hash()->elems.size());break; + case vm_obj: printf("| obj | <0x%lx> obj:0x%lx\n",(uint64_t)p,(uint64_t)val.obj()->ptr);break; default: printf("| ??? | <0x%lx>\n",(uint64_t)p);break; } } @@ -175,7 +175,7 @@ void nasal_vm::bytecodeinfo(const char* header,const uint32_t p) printf(" (0x%x[0x%x])",(c.num>>16)&0xffff,c.num&0xffff);break; default:break; } - printf(" (<%s> line %d)\n",files[c.fidx].c_str(),c.line); + printf(" (%s:%d)\n",files[c.fidx].c_str(),c.line); } void nasal_vm::traceback() {