🚀 add lib function `exit()` and add test file `watchdog.nas` to run the nasal file when it is changed.
This commit is contained in:
parent
0e578b3e21
commit
8293f85c5b
5
lib.nas
5
lib.nas
|
@ -61,6 +61,11 @@ var floor=func(val){
|
|||
return __floor(val);
|
||||
}
|
||||
|
||||
# exit using std::exit
|
||||
var exit=func(val=-1){
|
||||
return __exit(val);
|
||||
}
|
||||
|
||||
# abort using std::abort
|
||||
var abort=func(){
|
||||
__abort();
|
||||
|
|
|
@ -56,6 +56,11 @@ nas_ref builtin_println(nas_ref* local,nasal_gc& gc)
|
|||
std::cout<<std::endl;
|
||||
return nil;
|
||||
}
|
||||
nas_ref builtin_exit(nas_ref* local,nasal_gc& gc)
|
||||
{
|
||||
std::exit(local[1].num());
|
||||
return nil;
|
||||
}
|
||||
nas_ref builtin_abort(nas_ref* local,nasal_gc& gc)
|
||||
{
|
||||
std::abort();
|
||||
|
@ -1153,6 +1158,7 @@ struct
|
|||
{
|
||||
{"__print", builtin_print },
|
||||
{"__println", builtin_println },
|
||||
{"__exit", builtin_exit },
|
||||
{"__abort", builtin_abort },
|
||||
{"__append", builtin_append },
|
||||
{"__setsize", builtin_setsize },
|
||||
|
|
|
@ -61,6 +61,11 @@ var floor=func(val){
|
|||
return __floor(val);
|
||||
}
|
||||
|
||||
# exit using std::exit
|
||||
var exit=func(val=-1){
|
||||
return __exit(val);
|
||||
}
|
||||
|
||||
# abort using std::abort
|
||||
var abort=func(){
|
||||
__abort();
|
||||
|
|
|
@ -70,6 +70,7 @@ var testfile=[
|
|||
"trait.nas ",
|
||||
"turingmachine.nas",
|
||||
"utf8chk.nas ",
|
||||
"watchdog.nas ",
|
||||
"wavecollapse.nas ",
|
||||
"ycombinator.nas "
|
||||
];
|
||||
|
|
|
@ -53,12 +53,12 @@ var filechecksum=func(){
|
|||
"./test/md5.nas", "./test/md5compare.nas",
|
||||
"./test/module_test.nas", "./test/nasal_test.nas",
|
||||
"./test/occupation.nas", "./test/pi.nas",
|
||||
"./test/ppmgen.nas",
|
||||
"./test/prime.nas", "./test/qrcode.nas",
|
||||
"./test/quick_sort.nas", "./test/scalar.nas",
|
||||
"./test/snake.nas", "./test/tetris.nas",
|
||||
"./test/trait.nas", "./test/turingmachine.nas",
|
||||
"./test/utf8chk.nas", "./test/wavecollapse.nas",
|
||||
"./test/ppmgen.nas", "./test/prime.nas",
|
||||
"./test/qrcode.nas", "./test/quick_sort.nas",
|
||||
"./test/scalar.nas", "./test/snake.nas",
|
||||
"./test/tetris.nas", "./test/trait.nas",
|
||||
"./test/turingmachine.nas","./test/utf8chk.nas",
|
||||
"./test/watchdog.nas", "./test/wavecollapse.nas",
|
||||
"./test/ycombinator.nas", "LICENSE",
|
||||
"main.cpp", "makefile",
|
||||
"nasal_ast.h", "nasal_builtin.h",
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
var os_time=func(){
|
||||
return "[\e[33m"~os.time()~"\e[0m] ";
|
||||
}
|
||||
var err_hd=func(){
|
||||
return "[\e[91merror\e[0m] ";
|
||||
}
|
||||
var watching_hd=func(){
|
||||
return "[\e[96mwatching\e[0m] ";
|
||||
}
|
||||
var modified_hd=func(){
|
||||
return "[\e[92mmodified\e[0m] ";
|
||||
}
|
||||
var usage=func(){
|
||||
println(os_time(),"[\e[92musage\e[0m] nasal watchdog.nas <filename>");
|
||||
}
|
||||
|
||||
var argv=runtime.argv();
|
||||
if(size(argv)!=1){
|
||||
println(os_time(),err_hd(),"need correct file path to watch");
|
||||
usage();
|
||||
exit(-1);
|
||||
}
|
||||
var filename=argv[0];
|
||||
if(!io.exists(filename)){
|
||||
println(os_time(),err_hd(),"file <",filename,"> does not exist");
|
||||
usage();
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
var modified_time=fstat(filename).st_mtime;
|
||||
println(os_time(),watching_hd(),filename," ..");
|
||||
while(1){
|
||||
unix.sleep(1);
|
||||
if(!io.exists(filename)){
|
||||
println(os_time(),err_hd(),"file <",filename,"> does not exist");
|
||||
break;
|
||||
}
|
||||
var latest_modified_time=fstat(filename).st_mtime;
|
||||
if(latest_modified_time!=modified_time){
|
||||
modified_time=latest_modified_time;
|
||||
println(os_time(),modified_hd(),filename);
|
||||
var ret=system((os.platform()=="windows"?"":"./")~"nasal "~filename);
|
||||
if(ret!=0){
|
||||
println(os_time(),err_hd(),"process returned value ",ret);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue