📝 delete unnecessary code & change test file.

This commit is contained in:
ValKmjolnir 2022-07-31 19:26:13 +08:00
parent 068743aa4c
commit 04a45064c8
6 changed files with 216 additions and 256 deletions

View File

@ -122,8 +122,7 @@ const char* ast_name[]=
"multi-assign",
"continue",
"break",
"return",
nullptr
"return"
};
class nasal_ast

View File

@ -774,17 +774,15 @@ nas_ref builtin_read(nas_ref* local,nasal_gc& gc)
nas_ref len=local[3];
if(!fd.objchk(nas_obj::file))
return builtin_err("read","not a valid filehandle");
if(buf.type!=vm_str)
return builtin_err("read","\"buf\" must be string");
if(buf.val.gcobj->unmut)
return builtin_err("read","\"buf\" is not a mutable string");
if(buf.type!=vm_str || buf.val.gcobj->unmut)
return builtin_err("read","\"buf\" must be mutable string");
if(len.type!=vm_num)
return builtin_err("read","\"len\" must be number");
if(len.num()<=0 || len.num()>=(1<<30))
return builtin_err("read","\"len\" less than 1 or too large");
char* buff=new char[(usize)len.num()+1];
if(!buff)
return builtin_err("read","memory allocation error");
return builtin_err("read","malloc failed");
f64 res=fread(buff,1,len.num(),(FILE*)fd.obj().ptr);
buf.str()=buff;
delete []buff;
@ -1120,6 +1118,8 @@ nas_ref builtin_dlopen(nas_ref* local,nasal_gc& gc)
return builtin_err("dlopen","\"libname\" must be string");
#ifdef _WIN32
wchar_t* str=new wchar_t[dlname.str().size()+1];
if(!str)
return builtin_err("dlopen","malloc failed");
memset(str,0,sizeof(wchar_t)*dlname.str().size()+1);
mbstowcs(str,dlname.str().c_str(),dlname.str().size()+1);
void* ptr=LoadLibraryW(str);
@ -1201,17 +1201,17 @@ string tohex(u32 num)
}
return str;
}
string md5(const string& source)
string md5(const string& src)
{
std::vector<u32> buff;
u32 num=((source.length()+8)>>6)+1;
u32 buffsize=num<<4;
usize num=((src.length()+8)>>6)+1;
usize buffsize=num<<4;
buff.resize(buffsize,0);
for(u32 i=0;i<source.length();i++)
buff[i>>2]|=((unsigned char)source[i])<<((i&0x3)<<3);
buff[source.length()>>2]|=0x80<<(((source.length()%4))<<3);
buff[buffsize-2]=(source.length()<<3)&0xffffffff;
buff[buffsize-1]=((source.length()<<3)>>32)&0xffffffff;
for(usize i=0;i<src.length();i++)
buff[i>>2]|=((u8)src[i])<<((i&0x3)<<3);
buff[src.length()>>2]|=0x80<<(((src.length()%4))<<3);
buff[buffsize-2]=(src.length()<<3)&0xffffffff;
buff[buffsize-1]=((src.length()<<3)>>32)&0xffffffff;
// u32(abs(sin(i+1))*(2pow32))
const u32 k[]={

View File

@ -80,87 +80,83 @@ enum op_code:u8
op_ret // return
};
struct
const char* code_table[]=
{
u8 type;
const char* name;
}code_table[]=
{
{op_exit, "exit "},
{op_intg, "intg "},
{op_intl, "intl "},
{op_loadg, "loadg "},
{op_loadl, "loadl "},
{op_loadu, "loadu "},
{op_pnum, "pnum "},
{op_pnil, "pnil "},
{op_pstr, "pstr "},
{op_newv, "newv "},
{op_newh, "newh "},
{op_newf, "newf "},
{op_happ, "happ "},
{op_para, "para "},
{op_deft, "def "},
{op_dyn, "dyn "},
{op_unot, "not "},
{op_usub, "usub "},
{op_add, "add "},
{op_sub, "sub "},
{op_mul, "mult "},
{op_div, "div "},
{op_lnk, "lnk "},
{op_addc, "addc "},
{op_subc, "subc "},
{op_mulc, "multc "},
{op_divc, "divc "},
{op_lnkc, "lnkc "},
{op_addeq, "addeq "},
{op_subeq, "subeq "},
{op_muleq, "muleq "},
{op_diveq, "diveq "},
{op_lnkeq, "lnkeq "},
{op_addeqc,"addeqc"},
{op_subeqc,"subeqc"},
{op_muleqc,"muleqc"},
{op_diveqc,"diveqc"},
{op_lnkeqc,"lnkeqc"},
{op_meq, "meq "},
{op_eq, "eq "},
{op_neq, "neq "},
{op_less, "less "},
{op_leq, "leq "},
{op_grt, "grt "},
{op_geq, "geq "},
{op_lessc, "lessc "},
{op_leqc, "leqc "},
{op_grtc, "grtc "},
{op_geqc, "geqc "},
{op_pop, "pop "},
{op_jmp, "jmp "},
{op_jt, "jt "},
{op_jf, "jf "},
{op_cnt, "cnt "},
{op_findex,"findx "},
{op_feach, "feach "},
{op_callg, "callg "},
{op_calll, "calll "},
{op_upval, "upval "},
{op_callv, "callv "},
{op_callvi,"callvi"},
{op_callh, "callh "},
{op_callfv,"callfv"},
{op_callfh,"callfh"},
{op_callb, "callb "},
{op_slcbeg,"slcbeg"},
{op_slcend,"slcend"},
{op_slc, "slc "},
{op_slc2, "slc2 "},
{op_mcallg,"mcallg"},
{op_mcalll,"mcalll"},
{op_mupval,"mupval"},
{op_mcallv,"mcallv"},
{op_mcallh,"mcallh"},
{op_ret, "ret "}
"exit ",
"intg ",
"intl ",
"loadg ",
"loadl ",
"loadu ",
"pnum ",
"pnil ",
"pstr ",
"newv ",
"newh ",
"newf ",
"happ ",
"para ",
"def ",
"dyn ",
"not ",
"usub ",
"add ",
"sub ",
"mult ",
"div ",
"lnk ",
"addc ",
"subc ",
"multc ",
"divc ",
"lnkc ",
"addeq ",
"subeq ",
"muleq ",
"diveq ",
"lnkeq ",
"addeqc",
"subeqc",
"muleqc",
"diveqc",
"lnkeqc",
"meq ",
"eq ",
"neq ",
"less ",
"leq ",
"grt ",
"geq ",
"lessc ",
"leqc ",
"grtc ",
"geqc ",
"pop ",
"jmp ",
"jt ",
"jf ",
"cnt ",
"findx ",
"feach ",
"callg ",
"calll ",
"upval ",
"callv ",
"callvi",
"callh ",
"callfv",
"callfh",
"callb ",
"slcbeg",
"slcend",
"slc ",
"slc2 ",
"mcallg",
"mcalll",
"mupval",
"mcallv",
"mcallh",
"ret "
};
struct opcode
@ -198,7 +194,7 @@ void opcode::print(const char* header,
<<std::setw(2)<<std::setfill('0')<<((num>>16)&0xff)<<" "
<<std::setw(2)<<std::setfill('0')<<((num>>8)&0xff)<<" "
<<std::setw(2)<<std::setfill('0')<<(num&0xff)
<<" "<<code_table[op].name<<" "<<std::dec;
<<" "<<code_table[op]<<" "<<std::dec;
switch(op)
{
case op_addeq: case op_subeq: case op_muleq: case op_diveq:

View File

@ -104,7 +104,7 @@ void nasal_dbg::opcallsort(const u64* arr)
std::clog<<"\n ...";
break;
}
std::clog<<"\n "<<code_table[i.first].name
std::clog<<"\n "<<code_table[i.first]
<<" : "<<i.second<<" ("<<rate<<"%)";
}
std::clog<<"\n total : "<<total<<'\n';

View File

@ -1,85 +1,85 @@
var source=[
"main.cpp ",
"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.h "
"main.cpp ",
"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.h "
];
var lib=[
"stl/fg_env.nas ",
"stl/file.nas ",
"stl/lib.nas ",
"stl/list.nas ",
"stl/log.nas ",
"stl/module.nas ",
"stl/process_bar.nas ",
"stl/queue.nas ",
"stl/result.nas ",
"stl/sort.nas ",
"stl/stack.nas "
"fg_env.nas ",
"file.nas ",
"lib.nas ",
"list.nas ",
"log.nas ",
"module.nas ",
"process_bar.nas ",
"queue.nas ",
"result.nas ",
"sort.nas ",
"stack.nas "
];
var testfile=[
"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/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/ycombinator.nas "
"ascii-art.nas ",
"auto_crash.nas ",
"bf.nas ",
"bfcolored.nas ",
"bfconvertor.nas ",
"bfs.nas ",
"bigloop.nas ",
"bp.nas ",
"calc.nas ",
"choice.nas ",
"class.nas ",
"coroutine.nas ",
"diff.nas ",
"exception.nas ",
"fib.nas ",
"filesystem.nas ",
"hexdump.nas ",
"httptest.nas ",
"json.nas ",
"leetcode1319.nas ",
"lexer.nas ",
"life.nas ",
"loop.nas ",
"mandel.nas ",
"mandelbrot.nas ",
"md5.nas ",
"md5compare.nas ",
"module_test.nas ",
"nasal_test.nas ",
"occupation.nas ",
"pi.nas ",
"prime.nas ",
"qrcode.nas ",
"quick_sort.nas ",
"scalar.nas ",
"snake.nas ",
"tetris.nas ",
"trait.nas ",
"turingmachine.nas",
"utf8chk.nas ",
"wavecollapse.nas ",
"ycombinator.nas "
];
var module=[
"module/fib.cpp ",
"module/keyboard.cpp ",
"module/nasocket.cpp ",
"module/libfib.nas ",
"module/libkey.nas ",
"module/libsock.nas "
"fib.cpp ",
"keyboard.cpp ",
"nasocket.cpp ",
"libfib.nas ",
"libkey.nas ",
"libsock.nas "
];
var getname=func(s){
@ -95,22 +95,21 @@ var count=func(s,c){
return cnt;
}
var calc=func(codetype,files){
var calc=func(codetype,files,path=""){
println(codetype);
var (bytes,line,semi,line_cnt,semi_cnt)=(0,0,0,0,0);
forindex(var i;files){
var s=io.fin(getname(files[i]));
var s=io.fin(getname(path~files[i]));
(line_cnt,semi_cnt)=(count(s,'\n'),count(s,';'));
println(files[i],'| ',line_cnt,' \tline | ',semi_cnt,' \tsemi');
println(files[i],'| ',line_cnt,'\tline | ',semi_cnt,' \tsemi');
bytes+=size(s);
line+=line_cnt;
semi+=semi_cnt;
}
println('total: | ',line,' \tline | ',semi,' \tsemi');
println(' | ',bytes,'\tbytes| ',int(bytes/1024),' \tkb');
println('total: | ',line,'\tline | ',semi,' \tsemi');
println(' | ',int(bytes/1024),'\tkb');
return int(bytes/1024);
}
calc("source code:",source);
calc("lib:",lib);
calc("test file:",testfile);
calc("module:",module);
var all=calc("source code:",source)+calc("lib:",lib,"stl/")+calc("test file:",testfile,"test/")+calc("module:",module,"module/");
println('\ntotal: | ',all,'\tkb');

View File

@ -9,112 +9,78 @@ var compare=func(){
"^","&","*","(",")","-","=","\\","|","[","]","{","}","`"," ","\t","?"
];
return func(begin,end){
var byte=0;
var total=end-begin;
var timestamp=maketimestamp();
timestamp.stamp();
var bar=process_bar.bar(front:os.platform()=="windows"?"sharp":"block",back:"point",sep:"line",length:50);
for(var i=begin;i<end;i+=1){
var s="";
for(var j=0;j<i;j+=1){
s~=ch[rand()*size(ch)];
}
byte+=size(s);
var res=md5(s);
if(cmp(res,_md5(s))){
die("error: "~str(i));
}
print(" ",bar.bar((i-begin+1)/total)," (",i-begin+1,"/",total,")\t",res," max byte: ",end-1," \r");
print(" ",bar.bar((i-begin+1)/total)," (",i-begin+1,"/",total,")\t",res," byte: ",byte," elapsed time: ",timestamp.elapsedMSec()," \r");
}
print("\n");
};
}();
var filechecksum=func(){
var getname=func(s){
var (len,ch)=(size(s),' '[0]);
for(var i=0;i<len and s[i]!=ch;i+=1);
return substr(s,0,i);
}
var files=[
"./stl/fg_env.nas ",
"./stl/file.nas ",
"./stl/lib.nas ",
"./stl/list.nas ",
"./stl/log.nas ",
"./stl/module.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/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/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/fg_env.nas", "./stl/file.nas",
"./stl/lib.nas", "./stl/list.nas",
"./stl/log.nas", "./stl/module.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/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/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);
var timestamp=maketimestamp();
timestamp.stamp();
var bar=process_bar.bar(front:os.platform()=="windows"?"sharp":"block",back:"point",sep:"line",length:50);
forindex(var i;files){
var f=io.fin(getname(files[i]));
var f=io.fin(files[i]);
var res=md5(f);
byte+=size(f);
if(cmp(res,_md5(f))){
die("error: "~files[i]);
}
print(" ",bar.bar((i+1)/total)," (",i+1,"/",total,")\t",res," byte: ",byte," \r");
print(" ",bar.bar((i+1)/total)," (",i+1,"/",total,")\t",res," byte: ",byte," elapsed time: ",timestamp.elapsedMSec()," \r");
}
print("\n");
}