fix sigsegv error

This commit is contained in:
ValKmjolnir 2021-10-18 21:50:25 +08:00
parent 885b57cd52
commit 56280db2c7
3 changed files with 48 additions and 36 deletions

View File

@ -19,7 +19,7 @@ void help()
<<"nasal <file>\n"
<<"file:\n"
<<" input file name to execute script file.\n\n"
<<"nasal [options] <file>\n"
<<"nasal [options...] <file>\n"
<<"option:\n"
<<" -l, --lex | view token info.\n"
<<" -a, --ast | view abstract syntax tree.\n"
@ -107,36 +107,38 @@ void execute(const std::string& file,const uint32_t cmd)
int main(int argc,const char* argv[])
{
if(argc==2 && (!strcmp(argv[1],"-v") || !strcmp(argv[1],"--version")))
logo();
else if(argc==2 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"--help")))
help();
else if(argc==2 && argv[1][0]!='-')
execute(argv[1],VM_EXEC);
else if(argc>=3)
if(argc==2)
{
uint32_t cmd=0;
for(int i=1;i<argc-1;++i)
{
std::string s(argv[i]);
if(s=="--lex" || s=="-l")
cmd|=VM_LEXINFO;
else if(s=="--ast" || s=="-a")
cmd|=VM_ASTINFO;
else if(s=="--code" || s=="-c")
cmd|=VM_CODEINFO;
else if(s=="--opcnt" || s=="-o")
cmd|=VM_OPCALLNUM|VM_EXEC;
else if(s=="--time" || s=="-t")
cmd|=VM_EXECTIME;
else if(s=="--detail" || s=="-d")
cmd|=VM_DBGINFO|VM_EXEC;
else
err();
}
execute(argv[argc-1],cmd);
std::string s(argv[1]);
if(s=="-v" || s=="--version")
logo();
else if(s=="-h" || s=="--help")
help();
else if(s[0]!='-')
execute(s,VM_EXEC);
else
err();
return 0;
}
else
err();
uint32_t cmd=0;
for(int i=1;i<argc-1;++i)
{
std::string s(argv[i]);
if(s=="--lex" || s=="-l")
cmd|=VM_LEXINFO;
else if(s=="--ast" || s=="-a")
cmd|=VM_ASTINFO;
else if(s=="--code" || s=="-c")
cmd|=VM_CODEINFO;
else if(s=="--opcnt" || s=="-o")
cmd|=VM_OPCALLNUM|VM_EXEC;
else if(s=="--time" || s=="-t")
cmd|=VM_EXECTIME;
else if(s=="--detail" || s=="-d")
cmd|=VM_DBGINFO|VM_EXEC;
else
err();
}
execute(argv[argc-1],cmd);
return 0;
}

View File

@ -1,5 +1,14 @@
.PHONY=clean
.PHONY=test
nasal:main.cpp nasal_ast.h nasal_builtin.h nasal_codegen.h nasal_gc.h nasal_import.h nasal_lexer.h nasal_parse.h nasal_vm.h nasal.h
-@ clang++ -std=c++11 -O3 main.cpp -o nasal -fno-exceptions
clean:
-@ rm nasal
clang++ -std=c++11 -O3 main.cpp -o nasal -fno-exceptions
test:nasal
./nasal test/ascii-art.nas
./nasal test/bp.nas
./nasal -t test/bigloop.nas
./nasal -t test/fib.nas
./nasal test/class.nas
./nasal test/lexer.nas
./nasal -t test/mandelbrot.nas
./nasal -t test/pi.nas
./nasal -t test/ycombinator.nas
./nasal test/exception.nas

View File

@ -340,10 +340,11 @@ inline void nasal_vm::opr_pstr()
}
inline void nasal_vm::opr_newv()
{
nasal_ref newv=gc.alloc(vm_vec);
nasal_ref newv=gc.alloc(vm_vec);
auto& vec=newv.vec()->elems;// top-imm[pc] stores the vector
vec.resize(imm[pc]);
top-=imm[pc]-1;
// use top-=imm[pc]-1 here will cause error if imm[pc] is 0
top=top-imm[pc]+1;
for(uint32_t i=0;i<imm[pc];++i)
vec[i]=top[i];
top[0]=newv;