⚡ 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:
parent
692f8ccefe
commit
caf048aae4
|
@ -46,4 +46,9 @@ nasal.exe
|
||||||
# misc
|
# misc
|
||||||
.vscode
|
.vscode
|
||||||
dump
|
dump
|
||||||
a.ppm
|
|
||||||
|
# macOS special cache directory
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# ppm picture generated by ppmgen.nas
|
||||||
|
*.ppm
|
18
main.cpp
18
main.cpp
|
@ -6,10 +6,9 @@ const u32 VM_CODEINFO =0x04;
|
||||||
const u32 VM_EXECTIME =0x08;
|
const u32 VM_EXECTIME =0x08;
|
||||||
const u32 VM_OPCALLNUM=0x10;
|
const u32 VM_OPCALLNUM=0x10;
|
||||||
const u32 VM_EXEC =0x20;
|
const u32 VM_EXEC =0x20;
|
||||||
const u32 VM_DBGINFO =0x40;
|
const u32 VM_DETAIL =0x40;
|
||||||
const u32 VM_DEBUG =0x80;
|
const u32 VM_DEBUG =0x80;
|
||||||
const u32 VM_OPTIMIZE =0x100;
|
const u32 VM_OPTIMIZE =0x100;
|
||||||
const u32 VM_SHOWPATH =0x200;
|
|
||||||
|
|
||||||
void help()
|
void help()
|
||||||
{
|
{
|
||||||
|
@ -33,12 +32,12 @@ void help()
|
||||||
<<" -t, --time | get the running time.\n"
|
<<" -t, --time | get the running time.\n"
|
||||||
<<" -o, --opcnt | count used operands.\n"
|
<<" -o, --opcnt | count used operands.\n"
|
||||||
<<" | available in debug mode.\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"
|
<<" | get garbage collector info if didn't crash.\n"
|
||||||
<<" -op, --optimize| use optimizer(beta).\n"
|
<<" -op, --optimize| use optimizer(beta).\n"
|
||||||
<<" | if want to use -op and run, please use -op -e/-t/-d.\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"
|
<<" -dbg, --debug | debug mode (this will ignore -t -d).\n"
|
||||||
<<" -cp, --chkpath | show path if linker cannot find files.\n"
|
|
||||||
<<"file:\n"
|
<<"file:\n"
|
||||||
<<" input file name to execute.\n"
|
<<" input file name to execute.\n"
|
||||||
<<"argv:\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
|
// parser gets lexer's token list to compile
|
||||||
parse.compile(lexer);
|
parse.compile(lexer);
|
||||||
// linker gets parser's ast and load import files to this ast
|
// 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
|
// optimizer does simple optimization on ast
|
||||||
if(cmd&VM_OPTIMIZE)
|
if(cmd&VM_OPTIMIZE)
|
||||||
optimize(parse.ast());
|
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)
|
else if(cmd&VM_EXECTIME)
|
||||||
{
|
{
|
||||||
auto start=std::chrono::high_resolution_clock::now();
|
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();
|
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";
|
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)
|
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[])
|
i32 main(i32 argc,const char* argv[])
|
||||||
|
@ -143,10 +142,9 @@ i32 main(i32 argc,const char* argv[])
|
||||||
{"--exec",VM_EXEC},{"-e",VM_EXEC},
|
{"--exec",VM_EXEC},{"-e",VM_EXEC},
|
||||||
{"--opcnt",VM_OPCALLNUM},{"-o",VM_OPCALLNUM},
|
{"--opcnt",VM_OPCALLNUM},{"-o",VM_OPCALLNUM},
|
||||||
{"--time",VM_EXECTIME|VM_EXEC},{"-t",VM_EXECTIME|VM_EXEC},
|
{"--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},
|
{"--optimize",VM_OPTIMIZE},{"-op",VM_OPTIMIZE},
|
||||||
{"--debug",VM_DEBUG},{"-dbg",VM_DEBUG},
|
{"--debug",VM_DEBUG},{"-dbg",VM_DEBUG}
|
||||||
{"--chkpath",VM_SHOWPATH},{"-cp",VM_SHOWPATH} // this could be merged to -d
|
|
||||||
};
|
};
|
||||||
u32 cmd=0;
|
u32 cmd=0;
|
||||||
string filename;
|
string filename;
|
||||||
|
|
15
nasal.h
15
nasal.h
|
@ -52,7 +52,7 @@ using std::string;
|
||||||
|
|
||||||
const u32 STACK_DEPTH=1024;
|
const u32 STACK_DEPTH=1024;
|
||||||
|
|
||||||
inline f64 hex_to_double(const char* str)
|
inline f64 hex2f(const char* str)
|
||||||
{
|
{
|
||||||
f64 ret=0;
|
f64 ret=0;
|
||||||
for(;*str;++str)
|
for(;*str;++str)
|
||||||
|
@ -69,7 +69,7 @@ inline f64 hex_to_double(const char* str)
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
inline f64 oct_to_double(const char* str)
|
inline f64 oct2f(const char* str)
|
||||||
{
|
{
|
||||||
f64 ret=0;
|
f64 ret=0;
|
||||||
for(;*str;++str)
|
for(;*str;++str)
|
||||||
|
@ -82,7 +82,10 @@ inline f64 oct_to_double(const char* str)
|
||||||
}
|
}
|
||||||
return ret;
|
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;
|
f64 ret=0,negative=1,num_pow=0;
|
||||||
while('0'<=*str && *str<='9')
|
while('0'<=*str && *str<='9')
|
||||||
|
@ -124,11 +127,11 @@ f64 str2num(const char* str)
|
||||||
if(!*str)
|
if(!*str)
|
||||||
return nan("");
|
return nan("");
|
||||||
if(str[0]=='0' && str[1]=='x')
|
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')
|
else if(str[0]=='0' && str[1]=='o')
|
||||||
res=oct_to_double(str+2);
|
res=oct2f(str+2);
|
||||||
else
|
else
|
||||||
res=dec_to_double(str);
|
res=dec2f(str);
|
||||||
return negative?-res:res;
|
return negative?-res:res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,24 @@
|
||||||
var RD=func(i,j){
|
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){
|
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){
|
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 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);
|
io.write(fd,color);
|
||||||
}
|
}
|
||||||
|
|
||||||
var fd=io.open("a.ppm","wb");
|
var fd=io.open("a.ppm","wb");
|
||||||
io.write(fd,"P6\n512 512\n255\n");
|
# P3 use ASCII number
|
||||||
for(var i=0;i<512;i+=1)
|
# P6 use binary character
|
||||||
for(var j=0;j<512;j+=1)
|
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);
|
pixel_write(fd,i,j);
|
||||||
|
io.write(fd,"\n");
|
||||||
|
}
|
||||||
io.close(fd);
|
io.close(fd);
|
Loading…
Reference in New Issue