add math.pow
This commit is contained in:
parent
b66ebbef4b
commit
617ad03d33
1
lib.nas
1
lib.nas
|
@ -260,6 +260,7 @@ var math=
|
|||
pi: 3.14159265358979323846264338327950288,
|
||||
inf: 1/0,
|
||||
nan: 0/0,
|
||||
pow: func(x,y){return __builtin_pow(x,y); },
|
||||
sin: func(x) {return __builtin_sin(x); },
|
||||
cos: func(x) {return __builtin_cos(x); },
|
||||
tan: func(x) {return __builtin_tan(x); },
|
||||
|
|
2
main.cpp
2
main.cpp
|
@ -138,7 +138,7 @@ int main(int argc,const char* argv[])
|
|||
{"--code",VM_CODEINFO},{"-c",VM_CODEINFO},
|
||||
{"--exec",VM_EXEC},{"-e",VM_EXEC},
|
||||
{"--opcnt",VM_OPCALLNUM|VM_EXEC},{"-o",VM_OPCALLNUM|VM_EXEC},
|
||||
{"--time",VM_EXECTIME},{"-t",VM_EXECTIME},
|
||||
{"--time",VM_EXECTIME|VM_EXEC},{"-t",VM_EXECTIME|VM_EXEC},
|
||||
{"--detail",VM_DBGINFO|VM_EXEC},{"-d",VM_DBGINFO|VM_EXEC},
|
||||
{"--optimize",VM_OPTIMIZE},{"-op",VM_OPTIMIZE},
|
||||
{"--debug",VM_DEBUG},{"-dbg",VM_DEBUG}
|
||||
|
|
16
makefile
16
makefile
|
@ -5,9 +5,9 @@ nasal.exe:main.cpp nasal_ast.h nasal_err.h nasal_builtin.h nasal_opt.h nasal_cod
|
|||
g++ -std=c++11 -O3 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static
|
||||
test:nasal
|
||||
@ ./nasal -op -e test/ascii-art.nas
|
||||
@ ./nasal -op -c test/bf.nas
|
||||
@ ./nasal -op -c test/bfcolored.nas
|
||||
@ ./nasal -op -c test/bfconvertor.nas
|
||||
@ ./nasal -op -a -c test/bf.nas
|
||||
@ ./nasal -op -a -c test/bfcolored.nas
|
||||
@ ./nasal -op -a -c test/bfconvertor.nas
|
||||
@ ./nasal -op -e -d test/bfs.nas
|
||||
@ ./nasal -op -t test/bigloop.nas
|
||||
@ ./nasal -op -e test/bp.nas
|
||||
|
@ -33,9 +33,9 @@ test:nasal
|
|||
@ ./nasal -op -e test/qrcode.nas
|
||||
@ ./nasal -op -t -d test/quick_sort.nas
|
||||
@ ./nasal -op -e test/scalar.nas
|
||||
-@ ./nasal -op -t test/snake.nas
|
||||
@ ./nasal -op -e test/trait.nas
|
||||
-@ ./nasal -op -t test/tetris.nas
|
||||
@ ./nasal -op -t -d test/turingmachine.nas
|
||||
@ ./nasal -op -t -d -o test/ycombinator.nas
|
||||
-@ ./nasal -op -c -t test/snake.nas
|
||||
@ ./nasal -op -c -e test/trait.nas
|
||||
-@ ./nasal -op -c -t test/tetris.nas
|
||||
@ ./nasal -op -c -t -d test/turingmachine.nas
|
||||
@ ./nasal -op -c -t -d -o test/ycombinator.nas
|
||||
|
|
@ -29,6 +29,7 @@ nas_native(builtin_and);
|
|||
nas_native(builtin_or);
|
||||
nas_native(builtin_nand);
|
||||
nas_native(builtin_not);
|
||||
nas_native(builtin_pow);
|
||||
nas_native(builtin_sin);
|
||||
nas_native(builtin_cos);
|
||||
nas_native(builtin_tan);
|
||||
|
@ -116,6 +117,7 @@ struct
|
|||
{"__builtin_or", builtin_or },
|
||||
{"__builtin_nand", builtin_nand },
|
||||
{"__builtin_not", builtin_not },
|
||||
{"__builtin_pow", builtin_pow },
|
||||
{"__builtin_sin", builtin_sin },
|
||||
{"__builtin_cos", builtin_cos },
|
||||
{"__builtin_tan", builtin_tan },
|
||||
|
@ -468,6 +470,14 @@ nasal_ref builtin_not(nasal_ref* local,nasal_gc& gc)
|
|||
int number=(int)a.num();
|
||||
return {vm_num,(double)(~number)};
|
||||
}
|
||||
nasal_ref builtin_pow(nasal_ref* local,nasal_gc& gc)
|
||||
{
|
||||
nasal_ref x=local[1];
|
||||
nasal_ref y=local[2];
|
||||
if(x.type!=vm_num || y.type!=vm_num)
|
||||
return builtin_err("pow","\"x\" or \"y\" must be number");
|
||||
return {vm_num,std::pow(x.num(),y.num())};
|
||||
}
|
||||
nasal_ref builtin_sin(nasal_ref* local,nasal_gc& gc)
|
||||
{
|
||||
nasal_ref val=local[1];
|
||||
|
|
|
@ -1165,10 +1165,10 @@ void nasal_codegen::print_op(uint32_t index)
|
|||
printf(" 0x%.8x: %.2x %.2x %.2x %.2x %.2x %s ",
|
||||
index,
|
||||
c.op,
|
||||
uint8_t((c.num&0xff000000)>>24),
|
||||
uint8_t((c.num&0x00ff0000)>>16),
|
||||
uint8_t((c.num&0x0000ff00)>>8),
|
||||
uint8_t(c.num&0x000000ff),
|
||||
uint8_t((c.num>>24)&0xff),
|
||||
uint8_t((c.num>>16)&0xff),
|
||||
uint8_t((c.num>>8)&0xff),
|
||||
uint8_t(c.num&0xff),
|
||||
code_table[c.op].name
|
||||
);
|
||||
// print detail info
|
||||
|
|
|
@ -505,10 +505,10 @@ void nasal_gc::info()
|
|||
};
|
||||
std::cout<<"\ngarbage collector info:\n";
|
||||
for(uint8_t i=vm_str;i<vm_type_size;++i)
|
||||
std::cout<<name[i]<<" | "<<count[i]<<"\n";
|
||||
std::cout<<" "<<name[i]<<" | "<<count[i]<<"\n";
|
||||
std::cout<<"\nmemory allocator info(max size):\n";
|
||||
for(uint8_t i=vm_str;i<vm_type_size;++i)
|
||||
std::cout<<name[i]<<" | "<<(size[i]+1)*increment[i]<<"(+"<<size[i]<<")\n";
|
||||
std::cout<<" "<<name[i]<<" | "<<(size[i]+1)*increment[i]<<"(+"<<size[i]<<")\n";
|
||||
}
|
||||
nasal_ref nasal_gc::alloc(uint8_t type)
|
||||
{
|
||||
|
|
|
@ -296,13 +296,14 @@ void nasal_vm::opcallsort(const uint64_t* arr)
|
|||
opcall.end(),
|
||||
[](op& a,op& b){return a.second>b.second;}
|
||||
);
|
||||
std::cout<<"\noperands call info:";
|
||||
uint64_t total=0;
|
||||
for(auto& i:opcall)
|
||||
{
|
||||
if(!i.second)
|
||||
break;
|
||||
total+=i.second;
|
||||
std::cout<<'\n'<<code_table[i.first].name<<": "<<i.second;
|
||||
std::cout<<"\n "<<code_table[i.first].name<<" : "<<i.second;
|
||||
}
|
||||
std::cout<<"\n total : "<<total<<'\n';
|
||||
}
|
||||
|
|
|
@ -260,6 +260,7 @@ var math=
|
|||
pi: 3.14159265358979323846264338327950288,
|
||||
inf: 1/0,
|
||||
nan: 0/0,
|
||||
pow: func(x,y){return __builtin_pow(x,y); },
|
||||
sin: func(x) {return __builtin_sin(x); },
|
||||
cos: func(x) {return __builtin_cos(x); },
|
||||
tan: func(x) {return __builtin_tan(x); },
|
||||
|
|
|
@ -5,12 +5,22 @@ var lexer=func(file)
|
|||
var (ptr,token)=(0,[]);
|
||||
var s=io.fin(file);
|
||||
var len=size(s);
|
||||
var line=0;
|
||||
var gen=func(tok)
|
||||
{
|
||||
append(token,{
|
||||
line:line,
|
||||
token:tok
|
||||
});
|
||||
}
|
||||
return
|
||||
{
|
||||
jmp_note:func()
|
||||
{
|
||||
while(ptr<len and chr(s[ptr])!='\n')
|
||||
while(ptr<len and s[ptr]!='\n'[0])
|
||||
ptr+=1;
|
||||
if(ptr<len and s[ptr]=='\n'[0])
|
||||
line+=1;
|
||||
ptr+=1;
|
||||
},
|
||||
id_gen:func()
|
||||
|
@ -28,7 +38,7 @@ var lexer=func(file)
|
|||
break;
|
||||
ptr+=1;
|
||||
}
|
||||
append(token,tmp);
|
||||
gen(tmp);
|
||||
},
|
||||
str_gen:func()
|
||||
{
|
||||
|
@ -57,13 +67,17 @@ var lexer=func(file)
|
|||
else str~=c;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(s[ptr]=='\n'[0])
|
||||
line+=1;
|
||||
str~=chr(s[ptr]);
|
||||
}
|
||||
ptr+=1;
|
||||
}
|
||||
if(ptr>=len)
|
||||
print("read eof when generating string.\n");
|
||||
ptr+=1;
|
||||
append(token,str);
|
||||
gen(str);
|
||||
},
|
||||
num_gen:func()
|
||||
{
|
||||
|
@ -79,7 +93,7 @@ var lexer=func(file)
|
|||
number~=chr(s[ptr]);
|
||||
ptr+=1;
|
||||
}
|
||||
append(token,num(number));
|
||||
gen(num(number));
|
||||
return;
|
||||
}
|
||||
elsif(ptr<len and chr(s[ptr])=='o')
|
||||
|
@ -90,7 +104,7 @@ var lexer=func(file)
|
|||
number~=chr(s[ptr]);
|
||||
ptr+=1;
|
||||
}
|
||||
append(token,num(number));
|
||||
gen(num(number));
|
||||
return;
|
||||
}
|
||||
while(ptr<len and ('0'[0]<=s[ptr] and s[ptr]<='9'[0]))
|
||||
|
@ -126,7 +140,7 @@ var lexer=func(file)
|
|||
var last_c=chr(number[-1]);
|
||||
if(last_c=='.' or last_c=='e' or last_c=='E' or last_c=='-' or last_c=='+')
|
||||
println("error number: ",number);
|
||||
append(token,num(number));
|
||||
gen(num(number));
|
||||
},
|
||||
opr_gen:func()
|
||||
{
|
||||
|
@ -140,35 +154,41 @@ var lexer=func(file)
|
|||
tmp~=chr(s[ptr]);
|
||||
ptr+=1;
|
||||
}
|
||||
append(token,tmp);
|
||||
gen(tmp);
|
||||
return;
|
||||
}
|
||||
elsif(c=='.')
|
||||
{
|
||||
if(ptr+2<len and chr(s[ptr+1])=='.' and chr(s[ptr+2])=='.')
|
||||
{
|
||||
append(token,"...");
|
||||
gen("...");
|
||||
ptr+=3;
|
||||
}
|
||||
else
|
||||
{
|
||||
append(token,".");
|
||||
gen(".");
|
||||
ptr+=1;
|
||||
}
|
||||
return;
|
||||
}
|
||||
elsif(c!=' ' and c!='\t' and c!='\n' and c!='\r' and s[ptr]>0)
|
||||
append(token,c);
|
||||
gen(c);
|
||||
ptr+=1;
|
||||
return;
|
||||
},
|
||||
compile:func()
|
||||
{
|
||||
line=1;
|
||||
while(ptr<len)
|
||||
{
|
||||
var c=s[ptr];
|
||||
if(c=='#'[0])
|
||||
me.jmp_note();
|
||||
elsif(c=='\n'[0])
|
||||
{
|
||||
line+=1;
|
||||
ptr+=1;
|
||||
}
|
||||
elsif('a'[0]<=c and c<='z'[0]
|
||||
or 'A'[0]<=c and c<='Z'[0]
|
||||
or c=='_'[0])
|
||||
|
@ -189,5 +209,4 @@ var lexer=func(file)
|
|||
var lex=lexer("test/props.nas");
|
||||
lex.compile();
|
||||
foreach(var tok;lex.get_token())
|
||||
print(tok,' ');
|
||||
print('\n');
|
||||
print('(',tok.line,' | ',tok.token,')\n');
|
Loading…
Reference in New Issue