update error info (except parser)
prepare for nasal_err module
This commit is contained in:
parent
d87aef82b7
commit
30650bb64f
11
main.cpp
11
main.cpp
|
@ -77,6 +77,8 @@ void execute(const std::string& file,const uint32_t cmd)
|
||||||
lexer.scan(file);
|
lexer.scan(file);
|
||||||
if(lexer.err())
|
if(lexer.err())
|
||||||
die("lexer",file);
|
die("lexer",file);
|
||||||
|
if(cmd&VM_LEXINFO)
|
||||||
|
lexer.print();
|
||||||
|
|
||||||
// parser gets lexer's token list to compile
|
// parser gets lexer's token list to compile
|
||||||
parse.compile(lexer);
|
parse.compile(lexer);
|
||||||
|
@ -87,18 +89,17 @@ void execute(const std::string& file,const uint32_t cmd)
|
||||||
linker.link(parse,file);
|
linker.link(parse,file);
|
||||||
if(linker.err())
|
if(linker.err())
|
||||||
die("import",file);
|
die("import",file);
|
||||||
|
if(cmd&VM_ASTINFO)
|
||||||
|
parse.print();
|
||||||
|
|
||||||
// code generator gets parser's ast and linker's import file list to generate code
|
// code generator gets parser's ast and linker's import file list to generate code
|
||||||
gen.compile(parse,linker);
|
gen.compile(parse,linker);
|
||||||
if(gen.err())
|
if(gen.err())
|
||||||
die("code",file);
|
die("code",file);
|
||||||
|
|
||||||
if(cmd&VM_LEXINFO)
|
|
||||||
lexer.print();
|
|
||||||
if(cmd&VM_ASTINFO)
|
|
||||||
parse.print();
|
|
||||||
if(cmd&VM_CODEINFO)
|
if(cmd&VM_CODEINFO)
|
||||||
gen.print();
|
gen.print();
|
||||||
|
|
||||||
|
// run bytecode
|
||||||
if(cmd&VM_DEBUG)
|
if(cmd&VM_DEBUG)
|
||||||
{
|
{
|
||||||
nasal_dbg dbg;
|
nasal_dbg dbg;
|
||||||
|
|
|
@ -258,7 +258,7 @@ public:
|
||||||
void nasal_codegen::die(const std::string info,const int line)
|
void nasal_codegen::die(const std::string info,const int line)
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
std::cout<<"[code] <"<<file[fileindex]<<"> line "<<line<<": "<<info<<"\n";
|
std::cout<<"[code] <"<<file[fileindex]<<":"<<line<<"> "<<info<<"\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void nasal_codegen::regist_number(const double num)
|
void nasal_codegen::regist_number(const double num)
|
||||||
|
|
104
nasal_dbg.h
104
nasal_dbg.h
|
@ -90,9 +90,9 @@ void nasal_dbg::load_src(const std::string& filename)
|
||||||
std::cout<<"[debug] cannot open source code file <"<<filename<<">\n";
|
std::cout<<"[debug] cannot open source code file <"<<filename<<">\n";
|
||||||
std::exit(-1);
|
std::exit(-1);
|
||||||
}
|
}
|
||||||
|
std::string tmp;
|
||||||
while(!fin.eof())
|
while(!fin.eof())
|
||||||
{
|
{
|
||||||
std::string tmp;
|
|
||||||
std::getline(fin,tmp);
|
std::getline(fin,tmp);
|
||||||
src.push_back(tmp);
|
src.push_back(tmp);
|
||||||
}
|
}
|
||||||
|
@ -124,7 +124,7 @@ void nasal_dbg::interact()
|
||||||
if(bytecode[pc].op==op_intg)
|
if(bytecode[pc].op==op_intg)
|
||||||
{
|
{
|
||||||
std::cout
|
std::cout
|
||||||
<<"nasal debug mode\n"
|
<<"[debug] nasal debug mode\n"
|
||||||
<<"input \'h\' to get help\n";
|
<<"input \'h\' to get help\n";
|
||||||
}
|
}
|
||||||
else if(bytecode[pc].op==op_nop || bytecode[pc].op==op_exit)
|
else if(bytecode[pc].op==op_nop || bytecode[pc].op==op_exit)
|
||||||
|
@ -143,57 +143,57 @@ void nasal_dbg::interact()
|
||||||
printf(">> ");
|
printf(">> ");
|
||||||
std::getline(std::cin,cmd);
|
std::getline(std::cin,cmd);
|
||||||
auto res=parse(cmd);
|
auto res=parse(cmd);
|
||||||
switch(res.size())
|
if(res.size()==1)
|
||||||
{
|
{
|
||||||
case 1:
|
if(res[0]=="h" || res[0]=="help")
|
||||||
if(res[0]=="h" || res[0]=="help")
|
help();
|
||||||
help();
|
else if(res[0]=="bt" || res[0]=="backtrace")
|
||||||
else if(res[0]=="bt" || res[0]=="backtrace")
|
traceback();
|
||||||
traceback();
|
else if(res[0]=="c" || res[0]=="continue")
|
||||||
else if(res[0]=="c" || res[0]=="continue")
|
return;
|
||||||
return;
|
else if(res[0]=="g" || res[0]=="global")
|
||||||
else if(res[0]=="g" || res[0]=="global")
|
global_state();
|
||||||
global_state();
|
else if(res[0]=="l" || res[0]=="local")
|
||||||
else if(res[0]=="l" || res[0]=="local")
|
local_state();
|
||||||
local_state();
|
else if(res[0]=="u" || res[0]=="upval")
|
||||||
else if(res[0]=="u" || res[0]=="upval")
|
upval_state();
|
||||||
upval_state();
|
else if(res[0]=="a" || res[0]=="all")
|
||||||
else if(res[0]=="a" || res[0]=="all")
|
{
|
||||||
{
|
global_state();
|
||||||
global_state();
|
local_state();
|
||||||
local_state();
|
upval_state();
|
||||||
upval_state();
|
}
|
||||||
}
|
else if(res[0]=="n" || res[0]=="next")
|
||||||
else if(res[0]=="n" || res[0]=="next")
|
{
|
||||||
{
|
next_step=true;
|
||||||
next_step=true;
|
return;
|
||||||
return;
|
}
|
||||||
}
|
else if(res[0]=="q" || res[0]=="exit")
|
||||||
else if(res[0]=="q" || res[0]=="exit")
|
std::exit(0);
|
||||||
std::exit(0);
|
else
|
||||||
else
|
err();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
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");
|
die("stack overflow");
|
||||||
gc.clear();
|
gc.clear();
|
||||||
imm.clear();
|
imm.clear();
|
||||||
printf("debugger exited\n");
|
printf("[debug] debugger exited\n");
|
||||||
return;
|
return;
|
||||||
#define dbg(op) {interact();op();if(gc.top<canary)goto *code[++pc];goto vmexit;}
|
#define dbg(op) {interact();op();if(gc.top<canary)goto *code[++pc];goto vmexit;}
|
||||||
|
|
||||||
|
|
|
@ -101,6 +101,7 @@ private:
|
||||||
uint32_t error;
|
uint32_t error;
|
||||||
uint32_t line;
|
uint32_t line;
|
||||||
uint32_t ptr;
|
uint32_t ptr;
|
||||||
|
std::string filename;
|
||||||
std::string code;
|
std::string code;
|
||||||
std::string res;
|
std::string res;
|
||||||
std::vector<token> tokens;
|
std::vector<token> tokens;
|
||||||
|
@ -120,6 +121,7 @@ public:
|
||||||
|
|
||||||
void nasal_lexer::open(const std::string& file)
|
void nasal_lexer::open(const std::string& file)
|
||||||
{
|
{
|
||||||
|
filename=file;
|
||||||
std::ifstream fin(file,std::ios::binary);
|
std::ifstream fin(file,std::ios::binary);
|
||||||
if(fin.fail())
|
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)
|
void nasal_lexer::die(const char* info)
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
std::cout<<"[lexer] line "<<line<<" column "<<code.length()<<": \n"<<code<<'\n';
|
std::cout<<"[lexer] "<<filename<<":"<<line<<":"<<code.length()<<"\n"<<code<<'\n';
|
||||||
for(int i=0;i<(int)code.size()-1;++i)
|
for(int i=0;i<(int)code.size()-1;++i)
|
||||||
std::cout<<char(" \t"[code[i]=='\t']);
|
std::cout<<char(" \t"[code[i]=='\t']);
|
||||||
std::cout<<'^'<<info<<'\n';
|
std::cout<<'^'<<info<<'\n';
|
||||||
|
@ -168,7 +170,7 @@ std::string nasal_lexer::num_gen()
|
||||||
str+=res[ptr++];
|
str+=res[ptr++];
|
||||||
code+=str;
|
code+=str;
|
||||||
if(str.length()<3)// "0x"
|
if(str.length()<3)// "0x"
|
||||||
die("incorrect number.");
|
die("invalid number.");
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
// generate oct number
|
// generate oct number
|
||||||
|
@ -180,7 +182,7 @@ std::string nasal_lexer::num_gen()
|
||||||
str+=res[ptr++];
|
str+=res[ptr++];
|
||||||
code+=str;
|
code+=str;
|
||||||
if(str.length()<3)// "0o"
|
if(str.length()<3)// "0o"
|
||||||
die("incorrect number.");
|
die("invalid number.");
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
// generate dec number
|
// generate dec number
|
||||||
|
@ -197,7 +199,7 @@ std::string nasal_lexer::num_gen()
|
||||||
if(str.back()=='.')
|
if(str.back()=='.')
|
||||||
{
|
{
|
||||||
code+=str;
|
code+=str;
|
||||||
die("incorrect number.");
|
die("invalid number.");
|
||||||
return "0";
|
return "0";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,7 +214,7 @@ std::string nasal_lexer::num_gen()
|
||||||
if(str.back()=='e' || str.back()=='E' || str.back()=='-' || str.back()=='+')
|
if(str.back()=='e' || str.back()=='E' || str.back()=='-' || str.back()=='+')
|
||||||
{
|
{
|
||||||
code+=str;
|
code+=str;
|
||||||
die("incorrect number.");
|
die("invalid number.");
|
||||||
return "0";
|
return "0";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -304,7 +306,7 @@ void nasal_lexer::scan(const std::string& file)
|
||||||
code+=res[ptr];
|
code+=res[ptr];
|
||||||
uint32_t type=get_type(str);
|
uint32_t type=get_type(str);
|
||||||
if(!type)
|
if(!type)
|
||||||
die("incorrect operator.");
|
die("invalid operator.");
|
||||||
tokens.push_back({line,type,str});
|
tokens.push_back({line,type,str});
|
||||||
++ptr;
|
++ptr;
|
||||||
}
|
}
|
||||||
|
|
10
nasal_vm.h
10
nasal_vm.h
|
@ -141,15 +141,15 @@ void nasal_vm::valinfo(nasal_ref& val)
|
||||||
switch(val.type)
|
switch(val.type)
|
||||||
{
|
{
|
||||||
case vm_none: printf("| null |\n");break;
|
case vm_none: printf("| null |\n");break;
|
||||||
case vm_ret: printf("| addr | pc=0x%x\n",val.ret());break;
|
case vm_ret: printf("| addr | pc:0x%x\n",val.ret());break;
|
||||||
case vm_cnt: printf("| cnt | %ld\n",val.cnt());break;
|
case vm_cnt: printf("| cnt | %ld\n",val.cnt());break;
|
||||||
case vm_nil: printf("| nil |\n");break;
|
case vm_nil: printf("| nil |\n");break;
|
||||||
case vm_num: printf("| num | ");std::cout<<val.num()<<'\n';break;
|
case vm_num: printf("| num | ");std::cout<<val.num()<<'\n';break;
|
||||||
case vm_str: printf("| str | <0x%lx> %s\n",(uint64_t)p,rawstr(*val.str()).c_str());break;
|
case vm_str: printf("| str | <0x%lx> %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_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_hash: printf("| hash | <0x%lx> {%lu val}\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_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;
|
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;
|
printf(" (0x%x[0x%x])",(c.num>>16)&0xffff,c.num&0xffff);break;
|
||||||
default: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()
|
void nasal_vm::traceback()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue