📝 update test file & add new test file `word_collector.nas`

This commit is contained in:
ValKmjolnir 2022-09-09 01:00:09 +08:00
parent 1e0f0f8e7b
commit add5e0c2cd
4 changed files with 118 additions and 49 deletions

View File

@ -75,6 +75,7 @@ var testfile=[
"utf8chk.nas", "utf8chk.nas",
"watchdog.nas", "watchdog.nas",
"wavecollapse.nas", "wavecollapse.nas",
"word_collector.nas",
"ycombinator.nas" "ycombinator.nas"
]; ];
@ -94,6 +95,7 @@ var longest=func(vec...){
len=size(f)>len?size(f):len; len=size(f)>len?size(f):len;
return len; return len;
} }
var padding_length=longest(source,lib,testfile,module);
var getname=func(s){ var getname=func(s){
var (len,ch)=(size(s),' '[0]); var (len,ch)=(size(s),' '[0]);
@ -108,21 +110,29 @@ var count=func(s,c){
return cnt; 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=""){ var calc=func(codetype,files,path=""){
println(codetype); println(codetype);
var (bytes,line,semi,line_cnt,semi_cnt)=(0,0,0,0,0); 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){ forindex(var i;files){
var s=io.fin(getname(path~files[i])); var s=io.fin(getname(path~files[i]));
(line_cnt,semi_cnt)=(count(s,'\n'),count(s,';')); (line_cnt,semi_cnt)=(count(s,'\n'),count(s,';'));
println(rightpad(files[i],padding_length), println(rightpad(files[i],padding_length),'| ',
'| ',line_cnt,'\tline | ',semi_cnt,' \tsemi'); column(line_cnt),'line | ',
column(semi_cnt),'semi | ',
rightpad(str(int(size(s)/1024)),6),'kb');
bytes+=size(s); bytes+=size(s);
line+=line_cnt; line+=line_cnt;
semi+=semi_cnt; semi+=semi_cnt;
} }
println('total: | ',line,'\tline | ',semi,' \tsemi'); println(rightpad("total:",padding_length),'| ',
println(' | ',int(bytes/1024),'\tkb'); column(line),'line | ',
column(semi),'semi | ',
rightpad(str(int(bytes/1024)),6),'kb');
return int(bytes/1024); return int(bytes/1024);
} }
@ -130,4 +140,4 @@ var all=calc("source code:",source)
+calc("lib:",lib,"stl/") +calc("lib:",lib,"stl/")
+calc("test file:",testfile,"test/") +calc("test file:",testfile,"test/")
+calc("module:",module,"module/"); +calc("module:",module,"module/");
println('\ntotal: | ',all,'\tkb'); println('\n',rightpad("total:",padding_length),'| ',rightpad(str(all),6),'kb');

View File

@ -32,43 +32,44 @@ var compare=func(){
var filechecksum=func(){ var filechecksum=func(){
var files=[ var files=[
"./stl/fg_env.nas", "./stl/file.nas", "./stl/fg_env.nas", "./stl/file.nas",
"./stl/lib.nas", "./stl/list.nas", "./stl/lib.nas", "./stl/list.nas",
"./stl/log.nas", "./stl/module.nas", "./stl/log.nas", "./stl/module.nas",
"./stl/padding.nas", "./stl/padding.nas",
"./stl/process_bar.nas", "./stl/queue.nas", "./stl/process_bar.nas", "./stl/queue.nas",
"./stl/result.nas", "./stl/sort.nas", "./stl/result.nas", "./stl/sort.nas",
"./stl/stack.nas", "./test/ascii-art.nas", "./stl/stack.nas", "./test/ascii-art.nas",
"./test/auto_crash.nas", "./test/bf.nas", "./test/auto_crash.nas", "./test/bf.nas",
"./test/bfcolored.nas", "./test/bfconvertor.nas", "./test/bfcolored.nas", "./test/bfconvertor.nas",
"./test/bfs.nas", "./test/bigloop.nas", "./test/bfs.nas", "./test/bigloop.nas",
"./test/bp.nas", "./test/calc.nas", "./test/bp.nas", "./test/calc.nas",
"./test/choice.nas", "./test/class.nas", "./test/choice.nas", "./test/class.nas",
"./test/coroutine.nas", "./test/diff.nas", "./test/coroutine.nas", "./test/diff.nas",
"./test/exception.nas", "./test/fib.nas", "./test/exception.nas", "./test/fib.nas",
"./test/filesystem.nas", "./test/hexdump.nas", "./test/filesystem.nas", "./test/hexdump.nas",
"./test/httptest.nas", "./test/json.nas", "./test/httptest.nas", "./test/json.nas",
"./test/leetcode1319.nas", "./test/lexer.nas", "./test/leetcode1319.nas", "./test/lexer.nas",
"./test/life.nas", "./test/loop.nas", "./test/life.nas", "./test/loop.nas",
"./test/mandel.nas", "./test/mandelbrot.nas", "./test/mandel.nas", "./test/mandelbrot.nas",
"./test/md5.nas", "./test/md5compare.nas", "./test/md5.nas", "./test/md5compare.nas",
"./test/module_test.nas", "./test/nasal_test.nas", "./test/module_test.nas", "./test/nasal_test.nas",
"./test/occupation.nas", "./test/pi.nas", "./test/occupation.nas", "./test/pi.nas",
"./test/ppmgen.nas", "./test/prime.nas", "./test/ppmgen.nas", "./test/prime.nas",
"./test/qrcode.nas", "./test/quick_sort.nas", "./test/qrcode.nas", "./test/quick_sort.nas",
"./test/scalar.nas", "./test/snake.nas", "./test/scalar.nas", "./test/snake.nas",
"./test/tetris.nas", "./test/trait.nas", "./test/tetris.nas", "./test/trait.nas",
"./test/turingmachine.nas","./test/utf8chk.nas", "./test/turingmachine.nas", "./test/utf8chk.nas",
"./test/watchdog.nas", "./test/wavecollapse.nas", "./test/watchdog.nas", "./test/wavecollapse.nas",
"./test/ycombinator.nas", "LICENSE", "./test/word_collector.nas",
"main.cpp", "makefile", "./test/ycombinator.nas", "LICENSE",
"nasal_ast.h", "nasal_builtin.h", "main.cpp", "makefile",
"nasal_codegen.h", "nasal_dbg.h", "nasal_ast.h", "nasal_builtin.h",
"nasal_err.h", "nasal_gc.h", "nasal_codegen.h", "nasal_dbg.h",
"nasal_import.h", "nasal_lexer.h", "nasal_err.h", "nasal_gc.h",
"nasal_opt.h", "nasal_parse.h", "nasal_import.h", "nasal_lexer.h",
"nasal_vm.h", "nasal.ebnf", "nasal_opt.h", "nasal_parse.h",
"nasal.h", "README.md" "nasal_vm.h", "nasal.ebnf",
"nasal.h", "README.md"
]; ];
var byte=0; var byte=0;
var total=size(files); var total=size(files);

View File

@ -4,18 +4,18 @@ var os_time=func(){
var err_hd=func(){ var err_hd=func(){
return "[\e[91;1merror\e[0m] "; return "[\e[91;1merror\e[0m] ";
} }
var watching_hd=func(){ var info_hd=func(){
return "[\e[96;1mwatching\e[0m] "; return "[\e[96;1minfo\e[0m] ";
} }
var modified_hd=func(){ var modified_hd=func(){
return "[\e[92;1mmodified\e[0m] "; return "[\e[92;1mmodified\e[0m] ";
} }
var usage=func(){ var usage=func(){
println(os_time(),"[\e[92;1musage\e[0m] \e[1mnasal watchdog.nas <filename>\e[0m"); println(os_time(),info_hd(),"\e[1musage: nasal watchdog.nas <filename> [\"argv\"]\e[0m");
} }
var argv=runtime.argv(); 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"); println(os_time(),err_hd(),"\e[1mneed correct file path to watch\e[0m");
usage(); usage();
exit(-1); exit(-1);
@ -27,8 +27,14 @@ if(!io.exists(filename)){
exit(-1); 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; 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){ while(1){
unix.sleep(1); unix.sleep(1);
if(!io.exists(filename)){ if(!io.exists(filename)){
@ -39,9 +45,14 @@ while(1){
if(latest_modified_time!=modified_time){ if(latest_modified_time!=modified_time){
modified_time=latest_modified_time; modified_time=latest_modified_time;
println(os_time(),modified_hd(),"\e[1m",filename,"\e[0m"); 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){ 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");
} }
} }
} }

47
test/word_collector.nas Normal file
View File

@ -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<len;i+=1){
var n=file_content[i];
var c=chr(n);
if(('a'[0]<=n and n<='z'[0]) or ('A'[0]<=n and n<='Z'[0]) or c=='\''[0] or c=='-'[0]){
s~=to_lower(c);
}elsif(size(s)){
token[s]+=1;
s="";
}
}
var vec=keys(token);
sort(vec,0,size(vec)-1,func(a,b){return cmp(a,b)<=0;});
println(vec);