From add5e0c2cdffb64879e3a24b224dab4c7d563b19 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Fri, 9 Sep 2022 01:00:09 +0800 Subject: [PATCH] :memo: update test file & add new test file `word_collector.nas` --- test/calc.nas | 22 +++++++++---- test/md5compare.nas | 73 +++++++++++++++++++++-------------------- test/watchdog.nas | 25 ++++++++++---- test/word_collector.nas | 47 ++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 49 deletions(-) create mode 100644 test/word_collector.nas diff --git a/test/calc.nas b/test/calc.nas index 55a7df4..cd6863e 100644 --- a/test/calc.nas +++ b/test/calc.nas @@ -75,6 +75,7 @@ var testfile=[ "utf8chk.nas", "watchdog.nas", "wavecollapse.nas", + "word_collector.nas", "ycombinator.nas" ]; @@ -94,6 +95,7 @@ var longest=func(vec...){ len=size(f)>len?size(f):len; return len; } +var padding_length=longest(source,lib,testfile,module); var getname=func(s){ var (len,ch)=(size(s),' '[0]); @@ -108,21 +110,29 @@ var count=func(s,c){ return cnt; } +var column=func(number){ + number=number>=1000?int(number/1000)~'k':str(number); + return rightpad(number,6); +} + var calc=func(codetype,files,path=""){ println(codetype); var (bytes,line,semi,line_cnt,semi_cnt)=(0,0,0,0,0); - var padding_length=longest(source,lib,testfile,module); forindex(var i;files){ var s=io.fin(getname(path~files[i])); (line_cnt,semi_cnt)=(count(s,'\n'),count(s,';')); - println(rightpad(files[i],padding_length), - '| ',line_cnt,'\tline | ',semi_cnt,' \tsemi'); + println(rightpad(files[i],padding_length),'| ', + column(line_cnt),'line | ', + column(semi_cnt),'semi | ', + rightpad(str(int(size(s)/1024)),6),'kb'); bytes+=size(s); line+=line_cnt; semi+=semi_cnt; } - println('total: | ',line,'\tline | ',semi,' \tsemi'); - println(' | ',int(bytes/1024),'\tkb'); + println(rightpad("total:",padding_length),'| ', + column(line),'line | ', + column(semi),'semi | ', + rightpad(str(int(bytes/1024)),6),'kb'); return int(bytes/1024); } @@ -130,4 +140,4 @@ var all=calc("source code:",source) +calc("lib:",lib,"stl/") +calc("test file:",testfile,"test/") +calc("module:",module,"module/"); -println('\ntotal: | ',all,'\tkb'); \ No newline at end of file +println('\n',rightpad("total:",padding_length),'| ',rightpad(str(all),6),'kb'); \ No newline at end of file diff --git a/test/md5compare.nas b/test/md5compare.nas index b339a31..06d8fb8 100644 --- a/test/md5compare.nas +++ b/test/md5compare.nas @@ -32,43 +32,44 @@ var compare=func(){ var filechecksum=func(){ var files=[ - "./stl/fg_env.nas", "./stl/file.nas", - "./stl/lib.nas", "./stl/list.nas", - "./stl/log.nas", "./stl/module.nas", + "./stl/fg_env.nas", "./stl/file.nas", + "./stl/lib.nas", "./stl/list.nas", + "./stl/log.nas", "./stl/module.nas", "./stl/padding.nas", - "./stl/process_bar.nas", "./stl/queue.nas", - "./stl/result.nas", "./stl/sort.nas", - "./stl/stack.nas", "./test/ascii-art.nas", - "./test/auto_crash.nas", "./test/bf.nas", - "./test/bfcolored.nas", "./test/bfconvertor.nas", - "./test/bfs.nas", "./test/bigloop.nas", - "./test/bp.nas", "./test/calc.nas", - "./test/choice.nas", "./test/class.nas", - "./test/coroutine.nas", "./test/diff.nas", - "./test/exception.nas", "./test/fib.nas", - "./test/filesystem.nas", "./test/hexdump.nas", - "./test/httptest.nas", "./test/json.nas", - "./test/leetcode1319.nas", "./test/lexer.nas", - "./test/life.nas", "./test/loop.nas", - "./test/mandel.nas", "./test/mandelbrot.nas", - "./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/watchdog.nas", "./test/wavecollapse.nas", - "./test/ycombinator.nas", "LICENSE", - "main.cpp", "makefile", - "nasal_ast.h", "nasal_builtin.h", - "nasal_codegen.h", "nasal_dbg.h", - "nasal_err.h", "nasal_gc.h", - "nasal_import.h", "nasal_lexer.h", - "nasal_opt.h", "nasal_parse.h", - "nasal_vm.h", "nasal.ebnf", - "nasal.h", "README.md" + "./stl/process_bar.nas", "./stl/queue.nas", + "./stl/result.nas", "./stl/sort.nas", + "./stl/stack.nas", "./test/ascii-art.nas", + "./test/auto_crash.nas", "./test/bf.nas", + "./test/bfcolored.nas", "./test/bfconvertor.nas", + "./test/bfs.nas", "./test/bigloop.nas", + "./test/bp.nas", "./test/calc.nas", + "./test/choice.nas", "./test/class.nas", + "./test/coroutine.nas", "./test/diff.nas", + "./test/exception.nas", "./test/fib.nas", + "./test/filesystem.nas", "./test/hexdump.nas", + "./test/httptest.nas", "./test/json.nas", + "./test/leetcode1319.nas", "./test/lexer.nas", + "./test/life.nas", "./test/loop.nas", + "./test/mandel.nas", "./test/mandelbrot.nas", + "./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/watchdog.nas", "./test/wavecollapse.nas", + "./test/word_collector.nas", + "./test/ycombinator.nas", "LICENSE", + "main.cpp", "makefile", + "nasal_ast.h", "nasal_builtin.h", + "nasal_codegen.h", "nasal_dbg.h", + "nasal_err.h", "nasal_gc.h", + "nasal_import.h", "nasal_lexer.h", + "nasal_opt.h", "nasal_parse.h", + "nasal_vm.h", "nasal.ebnf", + "nasal.h", "README.md" ]; var byte=0; var total=size(files); diff --git a/test/watchdog.nas b/test/watchdog.nas index 1da5b41..bfc6cea 100644 --- a/test/watchdog.nas +++ b/test/watchdog.nas @@ -4,18 +4,18 @@ var os_time=func(){ var err_hd=func(){ return "[\e[91;1merror\e[0m] "; } -var watching_hd=func(){ - return "[\e[96;1mwatching\e[0m] "; +var info_hd=func(){ + return "[\e[96;1minfo\e[0m] "; } var modified_hd=func(){ return "[\e[92;1mmodified\e[0m] "; } var usage=func(){ - println(os_time(),"[\e[92;1musage\e[0m] \e[1mnasal watchdog.nas \e[0m"); + println(os_time(),info_hd(),"\e[1musage: nasal watchdog.nas [\"argv\"]\e[0m"); } var argv=runtime.argv(); -if(size(argv)!=1){ +if(size(argv)<1){ println(os_time(),err_hd(),"\e[1mneed correct file path to watch\e[0m"); usage(); exit(-1); @@ -27,8 +27,14 @@ if(!io.exists(filename)){ exit(-1); } +var args=[]; +if(size(argv)==2){ + println(os_time(),info_hd(),"\e[1mwith argument(s) ",argv[1],"\e[0m"); + args=split(" ",argv[1]); +} + var modified_time=fstat(filename).st_mtime; -println(os_time(),watching_hd(),"\e[1m",filename," ..\e[0m"); +println(os_time(),info_hd(),"\e[1mwatching ",filename," ..\e[0m"); while(1){ unix.sleep(1); if(!io.exists(filename)){ @@ -39,9 +45,14 @@ while(1){ if(latest_modified_time!=modified_time){ modified_time=latest_modified_time; println(os_time(),modified_hd(),"\e[1m",filename,"\e[0m"); - var ret=system((os.platform()=="windows"?"":"./")~"nasal "~filename); + var cmd=(os.platform()=="windows"?"":"./")~"nasal "~filename; + foreach(var i;args) + cmd~=" "~i; + var ret=system(cmd); if(ret!=0){ - println(os_time(),err_hd(),"\e[1mprocess returned value ",ret,"\e[0m"); + println(os_time(),err_hd(),"\e[1mprocess returned ",ret,"\e[0m"); + }else{ + println(os_time(),info_hd(),"\e[1mprocess returned ",ret,"\e[0m"); } } } \ No newline at end of file diff --git a/test/word_collector.nas b/test/word_collector.nas new file mode 100644 index 0000000..8c86d77 --- /dev/null +++ b/test/word_collector.nas @@ -0,0 +1,47 @@ +import.stl.sort; +var argv=runtime.argv(); + +if(size(argv)<1){ + println("no input files."); + exit(-1); +} + +var all_exists=1; +foreach(var f;argv){ + if(!io.exists(f)){ + println("cannot open file <",f,">"); + all_exists=0; + } +} + +if(!all_exists){ + exit(-1);; +} + +var to_lower=func(c){ + if('a'[0]<=c[0] and c[0]<='z'[0]) + return c; + elsif('A'[0]<=c[0] and c[0]<='Z'[0]) + return chr(c[0]-'A'[0]+'a'[0]); +} + +var file_content=""; +foreach(var f;argv) + file_content~=io.fin(f)~" "; +var token={}; +var len=size(file_content); +var s=""; +for(var i=0;i