several updates.

1. merge `--chkpath` `-cp` with `-d`

2. complete test/ppmgen.nas

3. update comments about why we don't use `atof` to convert string to
number

4. update .gitignore
This commit is contained in:
ValKmjolnir 2022-08-19 23:25:46 +08:00
parent 692f8ccefe
commit caf048aae4
4 changed files with 34 additions and 24 deletions

7
.gitignore vendored
View File

@ -46,4 +46,9 @@ nasal.exe
# misc
.vscode
dump
a.ppm
# macOS special cache directory
.DS_Store
# ppm picture generated by ppmgen.nas
*.ppm

View File

@ -6,10 +6,9 @@ const u32 VM_CODEINFO =0x04;
const u32 VM_EXECTIME =0x08;
const u32 VM_OPCALLNUM=0x10;
const u32 VM_EXEC =0x20;
const u32 VM_DBGINFO =0x40;
const u32 VM_DETAIL =0x40;
const u32 VM_DEBUG =0x80;
const u32 VM_OPTIMIZE =0x100;
const u32 VM_SHOWPATH =0x200;
void help()
{
@ -33,12 +32,12 @@ void help()
<<" -t, --time | get the running time.\n"
<<" -o, --opcnt | count used operands.\n"
<<" | available in debug mode.\n"
<<" -d, --detail | get detail crash info.\n"
<<" -d, --detail | get detail runtime crash info.\n"
<<" | get detail linker path-not-found info.\n"
<<" | get garbage collector info if didn't crash.\n"
<<" -op, --optimize| use optimizer(beta).\n"
<<" | if want to use -op and run, please use -op -e/-t/-d.\n"
<<" -dbg, --debug | debug mode (this will ignore -t -d).\n"
<<" -cp, --chkpath | show path if linker cannot find files.\n"
<<"file:\n"
<<" input file name to execute.\n"
<<"argv:\n"
@ -90,7 +89,7 @@ void execute(const string& file,const std::vector<string>& argv,const u32 cmd)
// parser gets lexer's token list to compile
parse.compile(lexer);
// linker gets parser's ast and load import files to this ast
linker.link(parse,file,cmd&VM_SHOWPATH);
linker.link(parse,file,cmd&VM_DETAIL);
// optimizer does simple optimization on ast
if(cmd&VM_OPTIMIZE)
optimize(parse.ast());
@ -108,12 +107,12 @@ void execute(const string& file,const std::vector<string>& argv,const u32 cmd)
else if(cmd&VM_EXECTIME)
{
auto start=std::chrono::high_resolution_clock::now();
vm.run(gen,linker,argv,cmd&VM_DBGINFO);
vm.run(gen,linker,argv,cmd&VM_DETAIL);
auto end=std::chrono::high_resolution_clock::now();
std::clog<<"process exited after "<<(end-start).count()*1.0/std::chrono::high_resolution_clock::duration::period::den<<"s.\n";
}
else if(cmd&VM_EXEC)
vm.run(gen,linker,argv,cmd&VM_DBGINFO);
vm.run(gen,linker,argv,cmd&VM_DETAIL);
}
i32 main(i32 argc,const char* argv[])
@ -143,10 +142,9 @@ i32 main(i32 argc,const char* argv[])
{"--exec",VM_EXEC},{"-e",VM_EXEC},
{"--opcnt",VM_OPCALLNUM},{"-o",VM_OPCALLNUM},
{"--time",VM_EXECTIME|VM_EXEC},{"-t",VM_EXECTIME|VM_EXEC},
{"--detail",VM_DBGINFO|VM_EXEC},{"-d",VM_DBGINFO|VM_EXEC},
{"--detail",VM_DETAIL|VM_EXEC},{"-d",VM_DETAIL|VM_EXEC},
{"--optimize",VM_OPTIMIZE},{"-op",VM_OPTIMIZE},
{"--debug",VM_DEBUG},{"-dbg",VM_DEBUG},
{"--chkpath",VM_SHOWPATH},{"-cp",VM_SHOWPATH} // this could be merged to -d
{"--debug",VM_DEBUG},{"-dbg",VM_DEBUG}
};
u32 cmd=0;
string filename;

15
nasal.h
View File

@ -52,7 +52,7 @@ using std::string;
const u32 STACK_DEPTH=1024;
inline f64 hex_to_double(const char* str)
inline f64 hex2f(const char* str)
{
f64 ret=0;
for(;*str;++str)
@ -69,7 +69,7 @@ inline f64 hex_to_double(const char* str)
}
return ret;
}
inline f64 oct_to_double(const char* str)
inline f64 oct2f(const char* str)
{
f64 ret=0;
for(;*str;++str)
@ -82,7 +82,10 @@ inline f64 oct_to_double(const char* str)
}
return ret;
}
inline f64 dec_to_double(const char* str)
// we have the same reason not using atof here just as andy's interpreter does.
// it is not platform independent, and may have strange output.
// so we write a new function here to convert str to number manually.
inline f64 dec2f(const char* str)
{
f64 ret=0,negative=1,num_pow=0;
while('0'<=*str && *str<='9')
@ -124,11 +127,11 @@ f64 str2num(const char* str)
if(!*str)
return nan("");
if(str[0]=='0' && str[1]=='x')
res=hex_to_double(str+2);
res=hex2f(str+2);
else if(str[0]=='0' && str[1]=='o')
res=oct_to_double(str+2);
res=oct2f(str+2);
else
res=dec_to_double(str);
res=dec2f(str);
return negative?-res:res;
}

View File

@ -1,20 +1,24 @@
var RD=func(i,j){
return chr(bits.u32_and(255,j*0.25+i*0.001));
return bits.u32_and(255,j/255*255.999);
}
var GR=func(i,j){
return chr(bits.u32_and(255,j*0.15));
return bits.u32_and(255,i/255*255.999);
}
var BL=func(i,j){
return chr(bits.u32_and(255,j*0.05));
return bits.u32_and(255,0.25*255.999);
}
var pixel_write=func(fd,i,j){
var color=RD(i,j)~GR(i,j)~BL(i,j);
var color=RD(i,j)~" "~GR(i,j)~" "~BL(i,j)~" ";
io.write(fd,color);
}
var fd=io.open("a.ppm","wb");
io.write(fd,"P6\n512 512\n255\n");
for(var i=0;i<512;i+=1)
for(var j=0;j<512;j+=1)
# P3 use ASCII number
# P6 use binary character
io.write(fd,"P3\n256 256\n255\n");
for(var i=255;i>=0;i-=1){
for(var j=0;j<256;j+=1)
pixel_write(fd,i,j);
io.write(fd,"\n");
}
io.close(fd);