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" <<"nasal <file>\n"
<<"file:\n" <<"file:\n"
<<" input file name to execute script file.\n\n" <<" input file name to execute script file.\n\n"
<<"nasal [options] <file>\n" <<"nasal [options...] <file>\n"
<<"option:\n" <<"option:\n"
<<" -l, --lex | view token info.\n" <<" -l, --lex | view token info.\n"
<<" -a, --ast | view abstract syntax tree.\n" <<" -a, --ast | view abstract syntax tree.\n"
@ -107,14 +107,19 @@ void execute(const std::string& file,const uint32_t cmd)
int main(int argc,const char* argv[]) int main(int argc,const char* argv[])
{ {
if(argc==2 && (!strcmp(argv[1],"-v") || !strcmp(argv[1],"--version"))) if(argc==2)
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)
{ {
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;
}
uint32_t cmd=0; uint32_t cmd=0;
for(int i=1;i<argc-1;++i) for(int i=1;i<argc-1;++i)
{ {
@ -135,8 +140,5 @@ int main(int argc,const char* argv[])
err(); err();
} }
execute(argv[argc-1],cmd); execute(argv[argc-1],cmd);
}
else
err();
return 0; 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 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 clang++ -std=c++11 -O3 main.cpp -o nasal -fno-exceptions
clean: test:nasal
-@ rm 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

@ -343,7 +343,8 @@ 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 auto& vec=newv.vec()->elems;// top-imm[pc] stores the vector
vec.resize(imm[pc]); 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) for(uint32_t i=0;i<imm[pc];++i)
vec[i]=top[i]; vec[i]=top[i];
top[0]=newv; top[0]=newv;