📝 unify code style of ./stl ./test

This commit is contained in:
ValKmjolnir
2022-07-02 13:53:50 +08:00
parent c993c77b78
commit 8b8fb79013
20 changed files with 200 additions and 375 deletions

1
.gitattributes vendored Normal file
View File

@@ -0,0 +1 @@
*.nas linguist-language=nasal

View File

@@ -3,8 +3,7 @@ var libfib=func(){
var fib=dylib.dlsym(dl,"fib");
var qfib=dylib.dlsym(dl,"quick_fib");
var call=dylib.dlcall;
return
{
return {
fib: func(x){return call(fib,x)},
qfib:func(x){return call(qfib,x)}
};

View File

@@ -1,9 +1,7 @@
#ifndef __NASAL_H__
#define __NASAL_H__
#define __nasver "10.0"
#include <unistd.h>
#include <cstdint>
@@ -150,10 +148,7 @@ int utf8_hdchk(char head)
std::string chrhex(const char c)
{
std::string res="";
res+="0123456789abcdef"[(c&0xf0)>>4];
res+="0123456789abcdef"[c&0x0f];
return res;
return {"0123456789abcdef"[(c&0xf0)>>4],"0123456789abcdef"[c&0x0f]};
}
std::string rawstr(const std::string& str)

View File

@@ -215,19 +215,16 @@ var geodinfo=func(lat,lon){
}
println("[\e[32m props \e[0m] [",os.time(),"] init props");
var props=
{
var props={
globals:nil,
Node:nil,
getNode:func(path,index)
{
getNode:func(path,index){
path=split('/',path);
var tmp=me.globals;
var path_size=size(path);
for(var i=0;i<path_size-1;i+=1)
tmp=tmp.val[path[i]];
if(path_size>0)
{
if(path_size>0){
if(contains(tmp.val,path[i]~'['~index~']'))
return tmp.val[path[i]~'['~index~']'];
else
@@ -239,10 +236,8 @@ var props=
println("[\e[32m props \e[0m] [",os.time(),"] init props.Node");
props.Node=
{
new:func(values=nil)
{
props.Node={
new:func(values=nil){
var res={
parents:fg_env_props_node_traits,
val:{},
@@ -253,35 +248,29 @@ props.Node=
res.val=values;
return res;
},
addChild:func(name)
{
if(!contains(me.val,name))
{
addChild:func(name){
if(!contains(me.val,name)){
me.val[name]=props.Node.new();
me.val[name].parent=me;
return 1;
}
return 0;
},
addChildren:func(name,cnt=0)
{
for(var i=0;i<cnt;i+=1)
{
addChildren:func(name,cnt=0){
for(var i=0;i<cnt;i+=1){
var label=name~'['~i~']';
me.val[label]=props.Node.new();
me.val[label].parent=me;
}
return;
},
setValue:func(path,val)
{
setValue:func(path,val){
path=split('/',path);
var tmp=me;
foreach(var label;path)
tmp=tmp.val[label];
tmp.val=val;
if(typeof(val)=='str')
{
if(typeof(val)=='str'){
if(val=='true' or val=='false')
tmp.type='BOOL';
else
@@ -291,56 +280,46 @@ props.Node=
tmp.type='DOUBLE';
return;
},
setIntValue:func(num)
{
setIntValue:func(num){
me.val=num;
me.type='INT';
return;
},
setBoolValue:func(state)
{
setBoolValue:func(state){
me.val=state;
me.type='BOOL';
return;
},
setDoubleValue:func(num)
{
setDoubleValue:func(num){
me.val=num;
me.type='DOUBLE';
return;
},
getValue:func(){return me.val;},
getName:func()
{
getName:func(){
var val=me.parent.val;
foreach(var k;keys(val))
if(val[k]==me)
return k;
return '';
},
getParent:func()
{
getParent:func(){
return me.parent;
},
getPath:func()
{
getPath:func(){
if(me.parent==nil) return '';
return me.parent.getPath()~'/'~me.getName();
},
equals:func(node){return me==node;},
debug:func(s='')
{
if(typeof(me.val)=='hash')
{
debug:func(s=''){
if(typeof(me.val)=='hash'){
var key=keys(me.val);
if(!size(key))
{
if(!size(key)){
println("\e[91m{}\e[0m");
return;
}
println('\e[91m{\e[0m');
foreach(var k;key)
{
foreach(var k;key){
print(s~" ","\e[34m",k,"\e[0m\e[95m:\e[0m");
me.val[k].debug(s~" ");
}

View File

@@ -1,7 +1,6 @@
# list.nas
# valkmjolnir 2021/3/31
var list=func()
{
var list=func(){
var (begin,end)=(nil,nil);
return{
push_back:func(elem){

View File

@@ -1,7 +1,6 @@
# queue.nas
# valkmjolnir 2021/3/31
var queue=func()
{
var queue=func(){
var (begin,end)=(nil,nil);
return{
push:func(elem){

View File

@@ -1,11 +1,9 @@
# sort.nas
# valkmjolnir 2021/4/2
var sort=func(vec,left,right,cmp=func(a,b){return a<=b;})
{
var sort=func(vec,left,right,cmp=func(a,b){return a<=b;}){
if(left>=right) return nil;
var (L,R,tmp)=(left,right,vec[left]);
while(left<right)
{
while(left<right){
while(left<right and cmp(tmp,vec[right]))
right-=1;
while(left<right and cmp(vec[left],tmp))

View File

@@ -1,7 +1,6 @@
# stack.nas
# valkmjolnir 2021/3/31
var stack=func()
{
var stack=func(){
var next=nil;
return{
push:func(elem){

View File

@@ -27,11 +27,9 @@ var char_ttf=[
["██╗ ██╗","╚██╗ ██╔╝"," ╚████╔╝ "," ╚██╔╝ "," ██║ "," ╚═╝ "],
["████████╗","╚════██╔╝"," ██╔═╝ "," ██╔═╝ ","████████╗","╚═══════╝"],
];
var trans_ttf=func(string)
{
var trans_ttf=func(string){
var str=["","","","","",""];
for(var i=0;i<size(string);i+=1)
{
for(var i=0;i<size(string);i+=1){
var number=string[i];
if(97<=number and number<=122)
for(var j=0;j<6;j+=1)
@@ -47,45 +45,37 @@ var trans_ttf=func(string)
println(i);
return;
}
var curve1=func()
{
var curve1=func(){
var shadow=["░","▒","▓","█","▀","▄","▐","▌"];
rand(100);
var s="";
for(var i=0;i<10;i+=1)
{
for(var i=0;i<10;i+=1){
for(var j=0;j<40;j+=1)
s~=shadow[int(8*rand())];
s~='\n';
}
print(s);
}
var curve2=func()
{
var curve2=func(){
var table=["╚","═","╝","╔","║","╗"];
rand(100);
var s="";
for(var i=0;i<10;i+=1)
{
for(var i=0;i<10;i+=1){
for(var j=0;j<40;j+=1)
s~=table[int(6*rand())];
s~='\n';
}
print(s);
}
var curve3=func()
{
var curve3=func(){
var s=["","","","","",""];
var cnt=0;
foreach(var char;char_ttf)
{
foreach(var char;char_ttf){
cnt+=1;
forindex(var i;char)
s[i]~=char[i];
if(cnt==9)
{
forindex(var i;s)
{
if(cnt==9){
forindex(var i;s){
println(s[i]);
s[i]='';
}
@@ -94,13 +84,10 @@ var curve3=func()
}
return;
}
var curve4=func()
{
var curve4=func(){
var arr=[0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6,7,8];
for(var loop=0;loop<10;loop+=1)
{
for(var i=26;i>=0;i-=1)
{
for(var loop=0;loop<10;loop+=1){
for(var i=26;i>=0;i-=1){
var rand_index=int(i*rand());
(arr[i],arr[rand_index])=(arr[rand_index],arr[i]);
}

View File

@@ -21,16 +21,14 @@ var road_check_func = func(){
var position_info = geodinfo(lat,lon);
var position_names = position_info[1].names;
if((position_names[0]=="Freeway") or (position_names[0]=="Road"))
{
if((position_names[0]=="Freeway") or (position_names[0]=="Road")){
var car_heading = 0;
var lat_change = 0;
var lon_change = 0;
var left_range = 0;
var right_range = 0;
for(var i=0;i>-0.00005;i-=0.000001)
{
for(var i=0;i>-0.00005;i-=0.000001){
car_heading = props.getNode("/orientation/heading-deg",1).getValue();
lat_change = math.sin(D2R*car_heading);
lon_change = -math.cos(D2R*car_heading);
@@ -43,8 +41,7 @@ var road_check_func = func(){
else
break;
}
for(var i=0;i<0.00005;i+=0.000001)
{
for(var i=0;i<0.00005;i+=0.000001){
car_heading = props.getNode("/orientation/heading-deg",1).getValue();
lat_change = math.sin(D2R*car_heading);
lon_change = -math.cos(D2R*car_heading);
@@ -80,14 +77,11 @@ var road_check_func = func(){
var road_check_timer = maketimer(0.01,road_check_func);
var toggle_auto_pilot = func(){
if(!road_check_timer.isRunning)
{
if(!road_check_timer.isRunning){
intergral=0;
road_check_timer.start();
props.getNode("/sim/messages/copilot",1).setValue('/',"ze dong sheng teaan see tong yee tse yung. Auto Sheng Teaan System Activated!");
}
else
{
}else{
road_check_timer.stop();
props.getNode("/sim/messages/copilot",1).setValue('/',"ze dong sheng teaan see tong yee guan bee. Auto Sheng Teaan System is off.");
}

View File

@@ -152,20 +152,16 @@ var bf=func(program)
var stack=[];
var len=size(program);
var f="#automatically generated by bfconvertor.nas\nimport('lib.nas');\nvar ptr=0;\nvar paper=[];\nsetsize(paper,131072);\n";
for(var i=0;i<len;i+=1)
{
for(var i=0;i<len;i+=1){
var c=chr(program[i]);
if(c=='+' or c=='-')
{
if(c=='+' or c=='-'){
var cnt=0;
for(;i<len;i+=1)
{
for(;i<len;i+=1){
if(chr(program[i])=='+')
cnt+=1;
elsif(chr(program[i])=='-')
cnt-=1;
elsif(chr(program[i])!='\n')
{
elsif(chr(program[i])!='\n'){
i-=1;
break;
}
@@ -177,18 +173,14 @@ var bf=func(program)
f~="paper[ptr]+="~cnt~";\n";
elsif(cnt<0)
f~="paper[ptr]-="~(-cnt)~";\n";
}
elsif(c=='<' or c=='>')
{
}elsif(c=='<' or c=='>'){
var cnt=0;
for(;i<len;i+=1)
{
for(;i<len;i+=1){
if(chr(program[i])=='>')
cnt+=1;
elsif(chr(program[i])=='<')
cnt-=1;
elsif(chr(program[i])!='\n')
{
elsif(chr(program[i])!='\n'){
i-=1;
break;
}
@@ -200,21 +192,15 @@ var bf=func(program)
f~="ptr+="~cnt~";\n";
elsif(cnt<0)
f~="ptr-="~(-cnt)~";\n";
}
elsif(c==',')
{
}elsif(c==','){
for(var j=0;j<size(stack);j+=1)
f~='\t';
f~="paper[ptr]=input();\n";
}
elsif(c=='.')
{
}elsif(c=='.'){
for(var j=0;j<size(stack);j+=1)
f~='\t';
f~="print(chr(paper[ptr]));\n";
}
elsif(c=='[')
{
}elsif(c=='['){
for(var j=0;j<size(stack);j+=1)
f~='\t';
f~="while(paper[ptr])\n";
@@ -222,11 +208,8 @@ var bf=func(program)
f~='\t';
f~="{\n";
append(stack,0);
}
elsif(c==']')
{
if(!size(stack))
{
}elsif(c==']'){
if(!size(stack)){
println("lack [");
return;
}
@@ -236,8 +219,7 @@ var bf=func(program)
f~="}\n";
}
}
if(size(stack))
{
if(size(stack)){
println("lack ]");
return;
}

View File

@@ -3,18 +3,15 @@ import("stl/queue.nas");
rand(time(0));
var pixel=[' ','#','.','*'];
var map=[];
for(var i=0;i<10;i+=1)
{
for(var i=0;i<10;i+=1){
append(map,[]);
for(var j=0;j<20;j+=1)
append(map[i],(rand()>0.7));
}
var prt=func()
{
var prt=func(){
var s="\e[0;0H+--------------------+\n";
for(var i=0;i<10;i+=1)
{
for(var i=0;i<10;i+=1){
s~="|";
for(var j=0;j<20;j+=1)
s~=pixel[map[i][j]];
@@ -25,30 +22,25 @@ var prt=func()
unix.sleep(1/144);
}
var bfs=func(begin,end)
{
var bfs=func(begin,end){
var move=[[1,0],[0,1],[-1,0],[0,-1]];
var que=queue();
que.push(begin);
map[begin[0]][begin[1]]=2;
map[end[0]][end[1]]=0;
while(!que.empty())
{
while(!que.empty()){
var vertex=que.front();
que.pop();
foreach(var i;move)
{
foreach(var i;move){
var x=vertex[0]+i[0];
var y=vertex[1]+i[1];
if(x==end[0] and y==end[1])
{
if(x==end[0] and y==end[1]){
map[x][y]=3;
prt();
print("reached.\n");
return;
}
if(0<=x and x<10 and 0<=y and y<20 and map[x][y]==0)
{
if(0<=x and x<10 and 0<=y and y<20 and map[x][y]==0){
que.push([x,y]);
map[x][y]=2;
}

View File

@@ -1,7 +1,6 @@
rand(time(0));
var new_neuron=func()
{
var new_neuron=func(){
return {
in:0,
out:0,
@@ -11,22 +10,18 @@ var new_neuron=func()
};
}
var tanh=func(x)
{
var tanh=func(x){
var (a,b)=(math.exp(x),math.exp(-x));
return (a-b)/(a+b);
}
var difftanh=func(x)
{
var difftanh=func(x){
x=tanh(x);
return 1-x*x;
}
var sigmoid=func(x)
{
var sigmoid=func(x){
return 1/(1+math.exp(-x));
}
var diffsigmoid=func(x)
{
var diffsigmoid=func(x){
x=sigmoid(x);
return x*(1-x);
}
@@ -36,8 +31,7 @@ var training_set=[[0,0],[0,1],[1,0],[1,1]];
var expect=[0,1,1,0];
var hidden=[];
for(var i=0;i<hnum;i+=1)
{
for(var i=0;i<hnum;i+=1){
append(hidden,new_neuron());
for(var j=0;j<inum;j+=1)
append(hidden[i].w,rand()>0.5?-2*rand():2*rand());
@@ -45,26 +39,22 @@ for(var i=0;i<hnum;i+=1)
}
var output=[];
for(var i=0;i<onum;i+=1)
{
for(var i=0;i<onum;i+=1){
append(output,new_neuron());
for(var j=0;j<hnum;j+=1)
append(output[i].w,rand()>0.5?-2*rand():2*rand());
output[i].bia=rand()>0.5?-5*rand():5*rand();
}
var forward=func(x)
{
var forward=func(x){
var input=training_set[x];
for(var i=0;i<hnum;i+=1)
{
for(var i=0;i<hnum;i+=1){
hidden[i].in=hidden[i].bia;
for(var j=0;j<inum;j+=1)
hidden[i].in+=hidden[i].w[j]*input[j];
hidden[i].out=tanh(hidden[i].in);
}
for(var i=0;i<onum;i+=1)
{
for(var i=0;i<onum;i+=1){
output[i].in=output[i].bia;
for(var j=0;j<hnum;j+=1)
output[i].in+=output[i].w[j]*hidden[j].out;
@@ -72,18 +62,15 @@ var forward=func(x)
}
return;
}
var run=func(vec)
{
var run=func(vec){
var input=vec;
for(var i=0;i<hnum;i+=1)
{
for(var i=0;i<hnum;i+=1){
hidden[i].in=hidden[i].bia;
for(var j=0;j<inum;j+=1)
hidden[i].in+=hidden[i].w[j]*input[j];
hidden[i].out=tanh(hidden[i].in);
}
for(var i=0;i<onum;i+=1)
{
for(var i=0;i<onum;i+=1){
output[i].in=output[i].bia;
for(var j=0;j<hnum;j+=1)
output[i].in+=output[i].w[j]*hidden[j].out;
@@ -91,29 +78,24 @@ var run=func(vec)
}
return;
}
var get_error=func(x)
{
var get_error=func(x){
return 0.5*(expect[x]-output[0].out)*(expect[x]-output[0].out);
}
var backward=func(x)
{
var backward=func(x){
var input=training_set[x];
output[0].diff=(expect[x]-output[0].out)*diffsigmoid(output[0].in);
for(var i=0;i<hnum;i+=1)
{
for(var i=0;i<hnum;i+=1){
hidden[i].diff=0;
for(var j=0;j<onum;j+=1)
hidden[i].diff+=output[j].w[i]*output[j].diff;
hidden[i].diff*=difftanh(hidden[i].in);
}
for(var i=0;i<hnum;i+=1)
{
for(var i=0;i<hnum;i+=1){
hidden[i].bia+=hidden[i].diff;
for(var j=0;j<inum;j+=1)
hidden[i].w[j]+=hidden[i].diff*input[j];
}
for(var i=0;i<onum;i+=1)
{
for(var i=0;i<onum;i+=1){
output[i].bia+=output[i].diff;
for(var j=0;j<hnum;j+=1)
output[i].w[j]+=output[i].diff*hidden[j].out;
@@ -122,11 +104,9 @@ var backward=func(x)
}
var (cnt,error)=(0,100);
while(error>0.0005)
{
while(error>0.0005){
error=0;
for(var i=0;i<4;i+=1)
{
for(var i=0;i<4;i+=1){
forward(i);
error+=get_error(i);
backward(i);
@@ -134,8 +114,7 @@ while(error>0.0005)
cnt+=1;
}
print('finished after ',cnt,' epoch.\n');
foreach(var v;training_set)
{
foreach(var v;training_set){
run(v);
print(v,': ',output[0].out,'\n');
}

View File

@@ -235,30 +235,37 @@ var html_read_file=func(filename){
var respond={
ok:func(html){
println("[",os.time(),"] 200 OK");
return "Http/1.1 200 OK\n\n"~html~"\n";
},
not_found:"Http/1.1 404 NOT FOUND\n\n<!DOCTYPE html>
<head>
<title> 404 not found </title>
<meta charset=\"utf-8\">
</head>
<body>
<text>
404 NOT FOUND!
</text>
</body>
</html>\n",
teapot:"Http/1.1 418 I'm a teapot\n\n<!DOCTYPE html>
<head>
<title> I'm a teapot </title>
<meta charset=\"utf-8\">
</head>
<body>
<text>
This server cannot brew coffee because it is a teapot.
</text>
</body>
</html>\n"
not_found:func(){
println("[",os.time(),"] 404 NOT FOUND");
return "Http/1.1 404 NOT FOUND\n\n<!DOCTYPE html>
<head>
<title> 404 not found </title>
<meta charset=\"utf-8\">
</head>
<body>
<text>
404 NOT FOUND!
</text>
</body>
</html>\n";
},
teapot:func(){
println("[",os.time(),"] 418 I'm a teapot");
return "Http/1.1 418 I'm a teapot\n\n<!DOCTYPE html>
<head>
<title> I'm a teapot </title>
<meta charset=\"utf-8\">
</head>
<body>
<text>
This server cannot brew coffee because it is a teapot.
</text>
</body>
</html>\n";
}
};
var files=func(){
@@ -314,9 +321,9 @@ while(1){
http.send(client,respond.ok(page~html_read_file("./test/"~filename)~page_back));
}
elsif(filename=="teapot")
http.send(client,respond.teapot);
http.send(client,respond.teapot());
else
http.send(client,respond.not_found);
http.send(client,respond.not_found());
}
}elsif(data.type=="POST"){
http.send(client,respond.not_found);

View File

@@ -61,8 +61,7 @@ var JSON=func(){
('A'[0]<=tmp and tmp<='Z'[0]) or
c=='_';
}
var check=func()
{
var check=func(){
var c=text[ptr];
return (
c=='{' or c=='}' or
@@ -73,8 +72,7 @@ var JSON=func(){
);
}
var get=func(str)
{
var get=func(str){
init();
if(!size(str))
die("empty string");
@@ -82,84 +80,61 @@ var JSON=func(){
text_size=size(text);
return;
}
var next=func()
{
while(ptr<text_size and !check())
{
var next=func(){
while(ptr<text_size and !check()){
if(text[ptr]=='\n')
line+=1;
ptr+=1;
}
if(ptr>=text_size)
{
if(ptr>=text_size){
token.content="eof";
token.type=j_eof;
return;
}
var c=text[ptr];
if(c=='{')
{
if(c=='{'){
token.content='{';
token.type=j_lbrace;
}
elsif(c=='}')
{
}elsif(c=='}'){
token.content='}';
token.type=j_rbrace;
}
elsif(c=='[')
{
}elsif(c=='['){
token.content='[';
token.type=j_lbracket;
}
elsif(c==']')
{
}elsif(c==']'){
token.content=']';
token.type=j_rbracket;
}
elsif(c==',')
{
}elsif(c==','){
token.content=',';
token.type=j_comma;
}
elsif(c==':')
{
}elsif(c==':'){
token.content=':';
token.type=j_colon;
}
elsif(c=='\"' or c=='\'')
{
}elsif(c=='\"' or c=='\''){
var strbegin=c;
var s="";
ptr+=1;
while(ptr<text_size and text[ptr]!=strbegin)
{
while(ptr<text_size and text[ptr]!=strbegin){
s~=text[ptr];
ptr+=1;
}
token.content=s;
token.type=j_str;
}
elsif(isnum(c))
{
}elsif(isnum(c)){
var s=c;
ptr+=1;
while(ptr<text_size and ((isnum(text[ptr]) or text[ptr]=='.')))
{
while(ptr<text_size and ((isnum(text[ptr]) or text[ptr]=='.'))){
s~=text[ptr];
ptr+=1;
}
ptr-=1;
token.content=num(s);
token.type=j_num;
}
elsif(isid(c))
{
}elsif(isid(c)){
var s=c;
ptr+=1;
while(ptr<text_size and (isid(text[ptr]) or isnum(text[ptr])))
{
while(ptr<text_size and (isid(text[ptr]) or isnum(text[ptr]))){
s~=text[ptr];
ptr+=1;
}
@@ -171,21 +146,18 @@ var JSON=func(){
return;
}
var match=func(type)
{
var match=func(type){
if(token.type!=type)
print("line ",line,": expect ",j_content[type]," but get `",token.content,"`.\n");
next();
return;
}
var hash_gen=func()
{
var hash_gen=func(){
var hash={};
match(j_lbrace);
member(hash);
while(token.type==j_comma)
{
while(token.type==j_comma){
match(j_comma);
member(hash);
}
@@ -193,28 +165,24 @@ var JSON=func(){
return hash;
}
var vec_gen=func()
{
var vec_gen=func(){
var vec=[];
match(j_lbracket);
if(token.type==j_lbrace)
append(vec,hash_gen());
elsif(token.type==j_lbracket)
append(vec,vec_gen());
elsif(token.type==j_str or token.type==j_num)
{
elsif(token.type==j_str or token.type==j_num){
append(vec,token.content);
next();
}
while(token.type==j_comma)
{
while(token.type==j_comma){
match(j_comma);
if(token.type==j_lbrace)
append(vec,me.hash_gen());
elsif(token.type==j_lbracket)
append(vec,vec_gen());
elsif(token.type==j_str or token.type==j_num)
{
elsif(token.type==j_str or token.type==j_num){
append(vec,token.content);
next();
}
@@ -223,8 +191,7 @@ var JSON=func(){
return vec;
}
var member=func(hash)
{
var member=func(hash){
var name=token.content;
if(token.type==j_rbrace)
return;
@@ -237,16 +204,14 @@ var JSON=func(){
hash[name]=hash_gen();
elsif(token.type==j_lbracket)
hash[name]=vec_gen();
elsif(token.type==j_str or token.type==j_num)
{
elsif(token.type==j_str or token.type==j_num){
hash[name]=token.content;
next();
}
return;
}
return {
parse:func(str)
{
parse:func(str){
if(typeof(str)!="str")
die("JSON.parse: must use string");
get(str);
@@ -254,8 +219,7 @@ var JSON=func(){
match(j_lbrace);
member(content);
while(token.type==j_comma)
{
while(token.type==j_comma){
match(j_comma);
member(content);
}

View File

@@ -4,28 +4,23 @@ var lexer=func(file)
var s=io.fin(file);
var len=size(s);
var line=0;
var gen=func(tok)
{
var gen=func(tok){
append(token,{
line:line,
token:tok
});
}
return
{
jmp_note:func()
{
return {
jmp_note:func(){
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()
{
id_gen:func(){
var tmp="";
while(ptr<len)
{
while(ptr<len){
var c=s[ptr];
if(('a'[0]<=c and c<='z'[0])
or ('A'[0]<=c and c<='Z'[0])
@@ -38,15 +33,12 @@ var lexer=func(file)
}
gen(tmp);
},
str_gen:func()
{
str_gen:func(){
var str="";
var mark=chr(s[ptr]);
ptr+=1;
while(ptr<len and chr(s[ptr])!=mark)
{
if(chr(s[ptr])=='\\')
{
while(ptr<len and chr(s[ptr])!=mark){
if(chr(s[ptr])=='\\'){
ptr+=1;
var c=chr(s[ptr]);
if (c=='a' ) str~='\a';
@@ -63,9 +55,7 @@ var lexer=func(file)
elsif(c=='\'') str~='\'';
elsif(c=='\"') str~='\"';
else str~=c;
}
else
{
}else{
if(s[ptr]=='\n'[0])
line+=1;
str~=chr(s[ptr]);
@@ -77,60 +67,48 @@ var lexer=func(file)
ptr+=1;
gen(str);
},
num_gen:func()
{
num_gen:func(){
var number=chr(s[ptr]);
ptr+=1;
if(ptr<len and chr(s[ptr])=='x')
{
if(ptr<len and chr(s[ptr])=='x'){
ptr+=1;
while(ptr<len and
('a'[0]<=s[ptr] and s[ptr]<='f'[0]
or '0'[0]<=s[ptr] and s[ptr]<='9'[0]))
{
or '0'[0]<=s[ptr] and s[ptr]<='9'[0])){
number~=chr(s[ptr]);
ptr+=1;
}
gen(num(number));
return;
}elsif(ptr<len and chr(s[ptr])=='o'){
ptr+=1;
while(ptr<len and ('0'[0]<=s[ptr] and s[ptr]<='7'[0])){
number~=chr(s[ptr]);
ptr+=1;
}
gen(num(number));
return;
}
elsif(ptr<len and chr(s[ptr])=='o')
{
ptr+=1;
while(ptr<len and ('0'[0]<=s[ptr] and s[ptr]<='7'[0]))
{
number~=chr(s[ptr]);
ptr+=1;
}
gen(num(number));
return;
}
while(ptr<len and ('0'[0]<=s[ptr] and s[ptr]<='9'[0]))
{
while(ptr<len and ('0'[0]<=s[ptr] and s[ptr]<='9'[0])){
number~=chr(s[ptr]);
ptr+=1;
}
if(ptr<len and chr(s[ptr])=='.')
{
if(ptr<len and chr(s[ptr])=='.'){
number~=chr(s[ptr]);
ptr+=1;
while(ptr<len and ('0'[0]<=s[ptr] and s[ptr]<='9'[0]))
{
while(ptr<len and ('0'[0]<=s[ptr] and s[ptr]<='9'[0])){
number~=chr(s[ptr]);
ptr+=1;
}
}
if(chr(s[ptr])=='e' or chr(s[ptr])=='E')
{
if(chr(s[ptr])=='e' or chr(s[ptr])=='E'){
number~=chr(s[ptr]);
ptr+=1;
if(chr(s[ptr])=='-' or chr(s[ptr])=='+')
{
if(chr(s[ptr])=='-' or chr(s[ptr])=='+'){
number~=chr(s[ptr]);
ptr+=1;
}
while(ptr<len and ('0'[0]<=s[ptr] and s[ptr]<='9'[0]))
{
while(ptr<len and ('0'[0]<=s[ptr] and s[ptr]<='9'[0])){
number~=chr(s[ptr]);
ptr+=1;
}
@@ -140,30 +118,23 @@ var lexer=func(file)
println("error number: ",number);
gen(num(number));
},
opr_gen:func()
{
opr_gen:func(){
var c=chr(s[ptr]);
if(c=='+' or c=='-' or c=='~' or c=='/' or c=='*' or c=='>' or c=='<' or c=='!' or c=='=')
{
if(c=='+' or c=='-' or c=='~' or c=='/' or c=='*' or c=='>' or c=='<' or c=='!' or c=='='){
var tmp=c;
ptr+=1;
if(ptr<len and chr(s[ptr])=='=')
{
if(ptr<len and chr(s[ptr])=='='){
tmp~=chr(s[ptr]);
ptr+=1;
}
gen(tmp);
return;
}
elsif(c=='.')
{
if(ptr+2<len and chr(s[ptr+1])=='.' and chr(s[ptr+2])=='.')
{
}elsif(c=='.'){
if(ptr+2<len and chr(s[ptr+1])=='.' and chr(s[ptr+2])=='.'){
gen("...");
ptr+=3;
}
else
{
else{
gen(".");
ptr+=1;
}
@@ -174,16 +145,13 @@ var lexer=func(file)
ptr+=1;
return;
},
compile:func()
{
compile:func(){
line=1;
while(ptr<len)
{
while(ptr<len){
var c=s[ptr];
if(c=='#'[0])
me.jmp_note();
elsif(c=='\n'[0])
{
elsif(c=='\n'[0]){
line+=1;
ptr+=1;
}

View File

@@ -1,18 +1,15 @@
var map=nil;
var check=func(x,y)
{
var check=func(x,y){
if(x>14) x=0;
if(y>19) y=0;
return map[x][y];
}
var new_map=func()
{
var new_map=func(){
var tmp=[];
setsize(tmp,15);
forindex(var i;tmp)
{
forindex(var i;tmp){
tmp[i]=[];
setsize(tmp[i],20);
}
@@ -22,8 +19,7 @@ var new_map=func()
var prt=func()
{
var s='\e[0;0H';
foreach(var line;map)
{
foreach(var line;map){
foreach(var elem;line)
s~=elem~' ';
s~='\n';
@@ -32,8 +28,7 @@ var prt=func()
unix.sleep(1/144);
}
func()
{
func(){
if(os.platform()=="windows")
system("chcp 65001");
print("\ec");
@@ -43,13 +38,11 @@ func()
forindex(var j;map[i])
map[i][j]=rand()>0.7?'O':' ';
var calc=[[0,1],[1,0],[0,-1],[-1,0],[1,1],[1,-1],[-1,-1],[-1,1]];
for(var r=0;r<100;r+=1)
{
for(var r=0;r<100;r+=1){
prt(map);
var tmp=new_map();
for(var i=0;i<15;i+=1)
for(var j=0;j<20;j+=1)
{
for(var j=0;j<20;j+=1){
var cnt=0;
foreach(var k;calc)
cnt+=(check(i+k[0],j+k[1])=='O');

View File

@@ -1,6 +1,5 @@
for(;;)break;
for(;;)
{
for(;;){
var a=1;
break;
}
@@ -10,21 +9,18 @@ for(var i=1;i<10;i+=1)print(i,'\n');
while(1)break;
var j=0;
while(j<10)
{
while(j<10){
print(j,'\n');
j+=1;
}
forindex(var j;[0,1,2,3])print(j,'\n');
forindex(var j;[0,1,2,3])
{
forindex(var j;[0,1,2,3]){
var a=j;
print(a*a,'\n');
}
foreach(var j;[0,1,2,3])print([0,1,2,3][j],'\n');
foreach(var j;[0,1,2,3])
{
foreach(var j;[0,1,2,3]){
var a=[0,1,2,3][j];
print(a*a-1,'\n');
}

View File

@@ -1,20 +1,16 @@
var (yMin,yMax,xMin,xMax,line)=(-0.2,0.2,-1.5,-1.0,"");
var (yDel,xDel)=(yMax-yMin,xMax-xMin);
for(var yPixel=0;yPixel<24;yPixel+=1)
{
for(var yPixel=0;yPixel<24;yPixel+=1){
var y=(yPixel/24)*yDel+yMin;
for(var xPixel=0;xPixel<80;xPixel+=1)
{
for(var xPixel=0;xPixel<80;xPixel+=1){
var x=(xPixel/80)*xDel+xMin;
var pixel=" ";
var (x0,y0)=(x,y);
for(var iter=0;iter<80;iter+=1)
{
for(var iter=0;iter<80;iter+=1){
var x1=(x0*x0)-(y0*y0)+x;
var y1=2*x0*y0+y;
(x0,y0)=(x1,y1);
if((x0*x0)+(y0*y0)>4)
{
if((x0*x0)+(y0*y0)>4){
pixel=chr(" .:;+=xX$&"[iter/8]);
break;
}

View File

@@ -1,9 +1,7 @@
var sort=func(vec,left,right)
{
var sort=func(vec,left,right){
if(left>=right) return;
var (L,R,tmp)=(left,right,vec[left]);
while(left<right)
{
while(left<right){
while(left<right and tmp<=vec[right])
right-=1;
while(left<right and tmp>=vec[left])