change test files

This commit is contained in:
Valk Richard Li 2021-03-03 09:20:42 +08:00
parent 19b590f3bb
commit c21d40c466
21 changed files with 189 additions and 929 deletions

View File

@ -1,96 +0,0 @@
# NasNeuron lib written by ValKmjolnir
# Basic Class NasMatrix
# NasMatGen : generate a new matrix
# NasMatAdd : add two matrixes
# NasMatSub : sub two matrixes
# NasMatMul : multiply two matrix
# NasMatTrans: transpose a matrix
# NasMatPrt : print a matrix
var NasMatrix=
{
NasMatGen:func(row,col)
{
var GenMat={Row:row,Col:col,Elem:[]};
for(var i=0;i<row;i+=1)
{
var TmpVec=[];
for(var j=0;j<col;j+=1)
append(TmpVec,0);
append(GenMat.Elem,TmpVec);
}
return GenMat;
},
NasMatAdd:func(mat1,mat2)
{
var ResultMat=nil;
if(mat1.Row==mat2.Row and mat1.Col==mat2.Col)
{
ResultMat=me.NasMatGen(mat1.Row,mat1.Col);
for(var i=0;i<ResultMat.Row;i+=1)
for(var j=0;j<ResultMat.Col;j+=1)
ResultMat.Elem[i][j]=mat1.Elem[i][j]+mat2.Elem[i][j];
}
else
print("NasNeuron: Mat1 and Mat2 have different rows and cols.");
return ResultMat;
},
NasMatSub:func(mat1,mat2)
{
var ResultMat=nil;
if(mat1.Row==mat2.Row and mat1.Col==mat2.Col)
{
ResultMat=me.NasMatGen(mat1.Row,mat1.Col);
for(var i=0;i<ResultMat.Row;i+=1)
for(var j=0;j<ResultMat.Col;j+=1)
ResultMat.Elem[i][j]=mat1.Elem[i][j]-mat2.Elem[i][j];
}
else
print("NasNeuron: Mat1 and Mat2 have different rows and cols.");
return ResultMat;
},
NasMatMul:func(mat1,mat2)
{
var ResultMat=nil;
if(mat1.Col==mat2.Row)
{
ResultMat=me.NasMatGen(mat1.Row,mat2.Col);
for(var i=0;i<ResultMat.Row;i+=1)
for(var j=0;j<ResultMat.Col;j+=1)
{
var sum=0;
for(var k=0;k<mat1.Col;k+=1)
sum+=mat1.Elem[i][k]*mat2.Elem[k][j];
ResultMat.Elem[i][j]=sum;
}
}
else
print("NasNeuron: Mat1's Col is different from Mat2's Row.");
return ResultMat;
},
NasMatTrans:func(mat)
{
var ResultMat=nil;
ResultMat=me.NasMatGen(mat.Col,mat.Row);
for(var i=0;i<ResultMat.Row;i+=1)
for(var j=0;j<ResultMat.Col;j+=1)
ResultMat.Elem[i][j]=mat.Elem[j][i];
return ResultMat;
},
NasMatPrt:func(mat)
{
for(var i=0;i<mat.Row;i+=1)
{
for(var j=0;j<mat.Col;j+=1)
print(mat.Elem[i][j],' ');
print('\n');
}
return nil;
}
};
NasMatrix.NasMatPrt(
NasMatrix.NasMatGen(
10,100
)
);

View File

@ -329,5 +329,4 @@ var main=func()
return nil;
}
main();

View File

@ -45,7 +45,7 @@ var trans_ttf=func(string)
str[j]~=char_ttf[0][j];
}
foreach(var i;str)
print(i);
println(i);
return;
}
var curve1=func()
@ -100,7 +100,7 @@ var curve4=func()
{
forindex(var i;s)
{
print(s[i]);
println(s[i]);
s[i]='';
}
cnt=0;
@ -123,7 +123,7 @@ var curve5=func()
var s="";
for(var i=0;i<size(arr);i+=1)
s~=shadow[arr[i]];
print(s);
println(s);
}
return;
}

View File

@ -55,11 +55,7 @@ var bfs=func(begin,end)
}
prt();
var x=num(input());
var y=num(input());
var begin=[x,y];
x=num(input());
y=num(input());
var end=[x,y];
var begin=[input(),input()];
var end=[input(),input()];
bfs(begin,end);
prt();

View File

@ -1,38 +0,0 @@
if(this_token.type==__if)
{
parse.push(this_token);
return;
}
elsif(this_token.type==__elsif)
{
parse.push(this_token);
return;
}
else if(this_token.type!=__else)
{
exit(0);
}
elsif(this_token.type==__elsif)
{
parse.push(this_token);
return;
}
elsif(this_token.type==__elsif)
{
parse.push(this_token);
return;
}
else if(this==(1+2+3*1))
{
return nil;
}
else
{
parse.push(this_token);
return;
}
if(!id)
{
exit(0);
}

19
test/class.nas Normal file
View File

@ -0,0 +1,19 @@
import("lib.nas");
var student=
{
new:func(name,age)
{
return {
parents:[student],
name:name,
age:age
};
},
print_info:func(){println(me.name,' ',me.age);},
get_age:func(){return me.age;},
get_name:func(){return me.name;}
};
var s=student.new('tiansuohaoer',24);
s.print_info();
println(s.get_age(),' ',s.get_name());

View File

@ -1,6 +0,0 @@
print("hello world!\n");
print("This is the first program for nasal--\n");
var cnt=0;
for(var i=1;i<101;i+=1)
cnt+=i;
print(cnt);

View File

@ -1,10 +0,0 @@
var f=func(n,m,dynamic...)
{
print(n+m," ",dynamic);
n=dynamic;
m=dynamic;
n+=m;
return dynamic;
};
print(f(1,1,0,0,0,0,0,(1+2+3+(1+2+3+4)))[3]);
function([0,1,2,3],{str:"str"});

View File

@ -1,6 +1,6 @@
import("lib.nas");
var s=io.fin("a.nas");
var s=io.fin(input());
s=split('',s);
var len=size(s);
var ptr=0;
@ -59,7 +59,7 @@ var generate_str=func()
ptr+=1;
}
if(ptr>=len)
print("read eof when generating string.");
print("read eof when generating string.\n");
ptr+=1;
return tok_str;
}
@ -178,6 +178,6 @@ while(ptr<len)
}
foreach(var i;token)
{
print("(",cnt," | ",i,")");
print("(",cnt," | ",i,")\n");
cnt+=1;
}

View File

@ -8,6 +8,12 @@ var print=func(elements...)
nasal_call_builtin_std_cout(elements);
return nil;
};
var println=func(elements...)
{
nasal_call_builtin_std_cout(elements);
print('\n');
return nil;
}
var append=func(vector,elements...)
{
nasal_call_builtin_push_back(vector,elements);
@ -94,6 +100,26 @@ var substr=func(str,begin,length)
{
return nasal_call_builtin_substr(str,begin,length);
}
var streq=func(a,b)
{
return nasal_call_builtin_streq(a,b);
}
var left=func(string,length)
{
return nasal_call_builtin_left(string,length);
}
var right=func(string,length)
{
return nasal_call_builtin_right(string,length);
}
var cmp=func(a,b)
{
return nasal_call_builtin_cmp(a,b);
}
var chr=func(code) #//Unlike in FG, this chr does not support Extended ASCII
{
return nasal_call_builtin_chr(code);
}
var io=
{
@ -110,58 +136,22 @@ var io=
var bits=
{
bitxor:func(a,b)
{
return nasal_call_builtin_xor(a,b);
},
bitand:func(a,b)
{
return nasal_call_builtin_and(a,b);
},
bitor:func(a,b)
{
return nasal_call_builtin_or(a,b);
},
bitnand:func(a,b)
{
return nasal_call_builtin_nand(a,b);
},
bitnot:func(a)
{
return nasal_call_builtin_not(a);
}
bitxor: func(a,b){return nasal_call_builtin_xor(a,b); },
bitand: func(a,b){return nasal_call_builtin_and(a,b); },
bitor: func(a,b){return nasal_call_builtin_or(a,b); },
bitnand: func(a,b){return nasal_call_builtin_nand(a,b);},
bitnot: func(a) {return nasal_call_builtin_not(a); }
};
var math=
{
e:2.7182818284590452354,
pi:3.14159265358979323846,
sin:func(x)
{
return nasal_call_builtin_sin(x);
},
cos:func(x)
{
return nasal_call_builtin_cos(x);
},
tan:func(x)
{
return nasal_call_builtin_tan(x);
},
exp:func(x)
{
return nasal_call_builtin_exp(x);
},
ln:func(x)
{
return nasal_call_builtin_cpp_math_ln(x);
},
sqrt:func(x)
{
return nasal_call_builtin_cpp_math_sqrt(x);
},
atan2:func(x,y)
{
return nasal_call_builtin_cpp_atan2(x,y);
},
e: 2.7182818284590452354,
pi: 3.14159265358979323846264338327950288,
sin: func(x) {return nasal_call_builtin_sin(x); },
cos: func(x) {return nasal_call_builtin_cos(x); },
tan: func(x) {return nasal_call_builtin_tan(x); },
exp: func(x) {return nasal_call_builtin_exp(x); },
ln: func(x) {return nasal_call_builtin_cpp_math_ln(x); },
sqrt: func(x) {return nasal_call_builtin_cpp_math_sqrt(x);},
atan2: func(x,y){return nasal_call_builtin_cpp_atan2(x,y); }
};

View File

@ -1,3 +1,4 @@
import("lib.nas");
for(;;)break;
for(;;)
{

View File

@ -1,382 +0,0 @@
# game left in corner by ValKmjolnir
# 2020
# lib function defined here
var print=func(elements...)
{
nasal_call_builtin_std_cout(elements);
return nil;
};
var append=func(vector,elements...)
{
nasal_call_builtin_push_back(vector,elements);
return nil;
}
var setsize=func(vector,size)
{
nasal_call_builtin_set_size(vector,size);
return nil;
}
var system=func(str)
{
nasal_call_builtin_system(str);
return;
}
var sleep=func(duration)
{
nasal_call_builtin_sleep(duration);
return;
}
var input=func()
{
return nasal_call_builtin_input();
}
var io=
{
fin:func(filename)
{
return nasal_call_builtin_finput(filename);
},
fout:func(filename,str)
{
nasal_call_builtin_foutput(filename,str);
return;
}
};
var int=func(str)
{
return str+0;
}
var str=func(num)
{
return num~'';
}
# string split
# game elements defined here
var role_property=
{
health:100,
mood:100,
satiety:100,
thirst:100,
health_change:func(x)
{
me.health+=x;
if(me.health<0)
me.health=0;
elsif(me.health>100)
me.health=100;
return nil;
},
mood_change:func(x)
{
me.mood+=x;
if(me.mood<0)
me.mood=0;
elsif(me.mood>100)
me.mood=100;
return nil;
},
satiety_change:func(x)
{
me.satiety+=x;
if(me.satiety<0)
me.satiety=0;
elsif(me.satiety>100)
me.satiety=100;
return nil;
},
thirst_change:func(x)
{
me.thirst+=x;
if(me.thirst<0)
me.thirst=0;
elsif(me.thirst>100)
me.thirst=100;
return nil;
}
};
var screen=
{
picture:[],
info_below_left:[],
info_below_right:[],
clear:func()
{
me.picture=[];
me.info_below_left=[];
me.info_below_right=[];
return;
},
pic_addline:func(_str)
{
append(me.picture,_str);
return;
},
left_add:func(_str)
{
append(me.info_below_left,_str);
return;
},
right_add:func(_str)
{
append(me.info_below_right,_str);
return;
},
prt_screen:func()
{
foreach(var i;me.picture)
print(i);
forindex(var i;me.info_below_left)
print(me.info_below_left[i]~me.info_below_right[i]);
return;
}
};
var first_shown_info=func()
{
system("cls");
var str_list=[
"+-----------------------------------------------+",
"| |",
"| |",
"| Let me tell you a story... |",
"| A story that really happened many years ago...|",
"| Nearly no one knows and cares about it... |",
"| But some children may still suffer from... |",
"| This kind of stories... |",
"| And this kind of stories never stop hurting...|",
"| People that are still alive... |",
"| |",
"| |",
"+-----------------------------------------------+"
];
foreach(var i;str_list)
print(i);
return;
}
var generate_role_property=func()
{
screen.left_add("+-----------------------+");
var str="";
for(var i=10;i<=100;i+=10)
{
if(i<=role_property.health)
str~="=";
else
str~=" ";
}
screen.left_add("|[health ]:"~str~" |");
str="";
for(var i=10;i<=100;i+=10)
{
if(i<=role_property.mood)
str~="=";
else
str~=" ";
}
screen.left_add("|[mood ]:"~str~" |");
str="";
for(var i=10;i<=100;i+=10)
{
if(i<=role_property.satiety)
str~="=";
else
str~=" ";
}
screen.left_add("|[satiety]:"~str~" |");
str="";
for(var i=10;i<=100;i+=10)
{
if(i<=role_property.thirst)
str~="=";
else
str~=" ";
}
screen.left_add("|[thirst ]:"~str~" |");
screen.left_add("+-----------------------+");
return;
}
var generate_choose_list=func()
{
var str_list=[
"-----------------------+",
"[1]| next step |",
"[2]| restart |",
"[3]| store game |",
"[4]| exit |",
"-----------------------+"
];
foreach(var i;str_list)
screen.right_add(i);
return;
}
var next_step=func()
{
role_property.health_change(-1);
role_property.mood_change(-1);
role_property.satiety_change(-1);
role_property.thirst_change(-10);
var str_list=[
"+-----------------------------------------------+",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"+-----------------------------------------------+"
];
foreach(var i;str_list)
screen.pic_addline(i);
return;
}
var restart=func()
{
role_property.health=100;
role_property.mood=100;
role_property.satiety=100;
role_property.thirst=100;
var str_list=[
"+-----------------------------------------------+",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"+-----------------------------------------------+"
];
foreach(var i;str_list)
screen.pic_addline(i);
return;
}
var generate_incorrect_choice_screen=func()
{
var str_list=[
"+-----------------------------------------------+",
"| |",
"| |",
"| |",
"| |",
"| |",
"| make a correct choice. |",
"| |",
"| |",
"| |",
"| |",
"| |",
"+-----------------------------------------------+"
];
foreach(var i;str_list)
screen.pic_addline(i);
}
var generate_goodbye_screen=func()
{
var str_list=[
"+-----------------------------------------------+",
"| |",
"| |",
"| |",
"| |",
"| |",
"| see you next life. |",
"| |",
"| |",
"| |",
"| |",
"| |",
"+-----------------------------------------------+"
];
foreach(var i;str_list)
screen.pic_addline(i);
}
var store_file=func()
{
var str=role_property.health~'\n'~role_property.mood~'\n'~role_property.satiety~'\n'~role_property.thirst~'\n';
io.fout("game-left-in-corner.glic",str);
var str_list=[
"+-----------------------------------------------+",
"| |",
"| |",
"| |",
"| |",
"| |",
"| data stored. |",
"| |",
"| |",
"| |",
"| |",
"| |",
"+-----------------------------------------------+"
];
foreach(var i;str_list)
screen.pic_addline(i);
return;
}
var get_file=func()
{
var str=io.fin("game-left-in-corner.glic");
print(str);
return;
}
var game_main=func()
{
first_shown_info();
screen.clear();
generate_role_property();
generate_choose_list();
screen.prt_screen();
while(1)
{
screen.clear();
print("|your choice[1|2|3|4]: |");
var choice=input();
if((choice!='1') and (choice!='2') and (choice!='3') and (choice!='4'))
generate_incorrect_choice_screen();
elsif(choice=='1')
next_step();
elsif(choice=='2')
restart();
elsif(choice=='3')
store_file();
elsif(choice=='4')
{
system("cls");
screen.clear();
generate_goodbye_screen();
generate_role_property();
generate_choose_list();
screen.prt_screen();
break;
}
system("cls");
generate_role_property();
generate_choose_list();
screen.prt_screen();
if(role_property.health==0 or role_property.mood==0 or role_property.satiety==0 or role_property.thirst==0)
{
print("|you died. |");
print("+-----------------------------------------------+");
system("pause");
screen.clear();
restart();
system("cls");
generate_role_property();
generate_choose_list();
screen.prt_screen();
}
}
return;
}
game_main();

View File

@ -13,16 +13,16 @@ var film_node=[];
for(var i=0;i<1000;i+=1)
append(film_node,node("film"));
var director_node=[];
for(var i=0;i<400;i+=1)
for(var i=0;i<200;i+=1)
append(director_node,node("direct"));
var actor_node=[];
for(var i=0;i<2000;i+=1)
for(var i=0;i<400;i+=1)
append(actor_node,node("actor"));
var writer_node=[];
for(var i=0;i<300;i+=1)
for(var i=0;i<100;i+=1)
append(writer_node,node("writer"));
var type_node=[];
for(var i=0;i<20;i+=1)
for(var i=0;i<40;i+=1)
append(type_node,node("type"));
var lang_node=[];
for(var i=0;i<120;i+=1)
@ -40,14 +40,14 @@ func()
var lang_size=size(lang_node);
var country_size=size(country_node);
var director_link=int(1+rand()*2);
var actor_link=int(1+rand()*10);
var writer_link=int(1+rand());
var type_link=int(1+rand()*3);
var lang_link=int(1+rand()*4);
var country_link=int(1+rand()*2);
foreach(var film;film_node)
{
var director_link=int(1+rand()*2);
var actor_link=int(1+rand()*20);
var writer_link=int(1+rand()*2);
var type_link=int(1+rand()*5);
var lang_link=int(1+rand()*4);
var country_link=int(1+rand()*4);
for(var i=0;i<director_link;i+=1)
{
var director=director_node[rand()*director_size];
@ -118,18 +118,9 @@ var sort_list=func(begin,end)
return;
}
while(1)
var get_next=func(index)
{
var list_size=size(film_list);
list_size=list_size>10?10:list_size;
for(var i=0;i<list_size;i+=1)
print(i,'\t:',film_list[i].name,'\t',count_list[i]);
var choose=input();
if(choose=="exit")
break;
if(num(choose)>=list_size)
die("choose a correct index");
var label_list=film_node[num(choose)].next;
var label_list=film_list[index].next;
film_list=[];
count_list=[];
foreach(var label;label_list)
@ -140,7 +131,7 @@ while(1)
if(film_list[i].name==film.name)
{
has=1;
count_list[i]+=rand();
count_list[i]+=1;
break;
}
if(has==0)
@ -150,6 +141,21 @@ while(1)
}
}
sort_list(0,size(film_list));
return;
}
while(1)
{
var list_size=size(film_list);
list_size=list_size>10?10:list_size;
for(var i=1;i<list_size;i+=1)
println('| ',i,'\t:',film_list[i].name,'\t',count_list[i]);
var choose=input();
if(choose=="exit")
break;
if(num(choose)==0 or num(choose)>=list_size)
die("choose a correct index");
get_next(num(choose));
}
foreach(var film;film_node)

View File

@ -1,246 +0,0 @@
import("lib.nas");
var property_tree=
{
accelerations:
{
'n-z-cg-fps_sec':0,
ned:
{
'down-accel-fps_sec':0,
'east-accel-fps_sec':0,
'north-accel-fps_sec':0,
},
nlf:0,
pilot:
{
'x-accel-fps_sec':0,
'y-accel-fps_sec':0,
'z-accel-fps_sec':0,
},
'pilot-g':1,
'pilot-gdamped':1
},
ai:
{
models:
{
carrier:
{
callsign:'',
controls:{},
environment:{},
id:2,
name:'Nimitz',
navaids:{},
orientation:{},
position:{},
radar:{},
sign:'CVN-68',
sim:{},
subID:0,
submodels:
{
path:'',
serviceable:1
},
'surface-positions':{},
type:'AI',
valid:1,
velocities:{},
waypoint:{}
},
'carrier[1]':
{
callsign:'',
controls:{},
environment:{},
id:3,
name:'Eisenhower',
navaids:{},
orientation:{},
position:{},
radar:{},
sign:'CVN-69',
sim:{},
subID:0,
submodels:
{
path:'',
serviceable:0
},
'surface-positions':{},
type:'AI',
valid:1,
velocities:{},
waypoint:{}
},
count:2,
'model-added':'/ai[0]/models[0]/carrier[1]',
'model-removed':nil,
'num-players':0
},
submodels:
{
contrails:0
},
},
aircraft:
{
icao:
{
equipment:'SDFGY',
surveillance:'S',
type:'ZZZZ',
'wake-turbulence-category':'L'
},
performance:
{
approach:
{
'airspeed-knots':150,
},
climb:'\n\t\t\t\n\t\t\t',
cruise:
{
'airspeed-knots':1000,
'altitude-ft':4500,
},
descent:'\n\t\t\t\n\t\t\t',
maximum:'\n\t\t\t\n\t\t\t',
minimum:'\n\t\t\t\n\t\t\t',
},
settings:
{
fuel_persistent:0,
ground_services_persistent:0,
radio_persistent:0,
tooltips:1,
weight_persistent:0
}
},
autopilot:
{
internal:{},
locks:{},
'route-manager':{},
settings:{},
'target-tracking':{},
},
canvas:
{
'by-index':
{
texture:
{
background:'rgba(0,0,0,0)',
group:{},
name:'Tooltip',
placement:{},
size:600,
'size[1]':200,
status:0,
'status-msg':'OK',
view:300,
'view[1]':100
},
'texture[1]':
{
background:'rgba(0,0,0,0)',
group:{},
mipmapping:1,
name:'SymbolCache1024x1024',
placement:{},
size:1024,
'size[1]':1024,
status:0,
'status-msg':'OK',
view:1024,
'view[1]':1024
},
'texture[2]':
{
background:'rgba(0,0,0,0)',
group:{},
mipmapping:1,
name:'SymbolCache1024x1024',
placement:{},
size:1024,
'size[1]':1024,
status:0,
'status-msg':'OK',
view:1024,
'view[1]':1024
},
}
},
command:{},
consumables:{},
controls:{},
cursor:'Aircraft/ufo/Models/cursor.ac',
devices:{},
earthview:{},
engines:{},
environment:{},
ephemeris:{},
fdm:{},
gear:{},
hazards:{},
input:{},
instrumentation:{},
'local-weather':{},
logging:{},
models:{},
nasal:{},
orientation:{},
position:{},
rendering:{},
scenery:{},
sim:{},
source:'Models',
'surface-positions':{},
systems:{},
velocities:{},
};
var setprop=func(prop,value)
{
if(type(prop)!="string")
die("setprop: prop is not a string");
var path=split('/',prop);
var tmp=property_tree;
var path_size=size(path);
for(var i=0;i<path_size-1;i+=1)
tmp=tmp[path[i]];
tmp[path[path_size-1]]=value;
return;
}
var getprop=func(prop)
{
if(type(prop)!="string")
die("getprop: prop is not a string");
var path=split('/',prop);
var tmp=property_tree;
foreach(var i;path)
tmp=tmp[i];
return tmp;
}
setprop("aircraft/icao/type",'IDG MD-11');
var print_prop=func(depth,prop)
{
var s='';
for(var i=0;i<depth;i+=1)
s~='| ';
if(type(prop)!="hash")
return;
var m=keys(prop);
foreach(var elem;m)
{
print(s,elem,':',prop[elem]);
print_prop(depth+1,prop[elem]);
}
return;
}
print_prop(0,property_tree);

View File

@ -9,6 +9,24 @@
# local node, there is no equivalent of the "relative path" variants
# available in C++; just use node.getNode(path).whatever() instead.
#
##
# Utility. Turns any ghosts it finds (either solo, or in an
# array) into Node objects.
#
var wrap = func(node) {
var argtype = typeof(node);
if(argtype == "ghost") {
return wrapNode(node);
} elsif(argtype == "vector") {
var v = node;
var n = size(v);
for(var i=0; i<n; i+=1) { v[i] = wrapNode(v[i]); }
return v;
}
return node;
}
var Node = {
getNode : func wrap(_getNode(me._g, arg)),
getParent : func wrap(_getParent(me._g, arg)),
@ -166,23 +184,6 @@ var copy = func(src, dest, attr = 0) {
if(attr) dest.setAttribute(src.getAttribute());
}
##
# Utility. Turns any ghosts it finds (either solo, or in an
# array) into Node objects.
#
var wrap = func(node) {
var argtype = typeof(node);
if(argtype == "ghost") {
return wrapNode(node);
} elsif(argtype == "vector") {
var v = node;
var n = size(v);
for(var i=0; i<n; i+=1) { v[i] = wrapNode(v[i]); }
return v;
}
return node;
}
##
# Utility. Returns a new object with its superclass/parent set to the
# Node object and its _g (ghost) field set to the specified object.

View File

@ -1,35 +1,40 @@
# lib queue.nas
var block_alloc=func()
{
return {elem:nil,next:nil};
}
# valkmjolnir 2021/3/3
var new_queue=func()
{
return {next:nil};
return {begin:nil,end:nil};
}
var queue_push=func(queue,elem)
var queue_push=func(queue_head,elem)
{
var tmp=queue;
while(tmp.next!=nil)
tmp=tmp.next;
tmp.next=block_alloc();
tmp.next.elem=elem;
}
var queue_pop=func(queue)
{
var tmp=queue.next;
if(tmp!=nil)
queue.next=tmp.next;
var new_node=
{
elem:elem,
next:nil
};
if(queue_head.begin==nil)
queue_head.begin=queue_head.end=new_node;
else
{
queue_head.end.next=new_node;
queue_head.end=new_node;
}
return;
}
var queue_front=func(queue)
var queue_pop=func(queue_head)
{
var tmp=queue.next;
if(tmp!=nil)
return tmp.elem;
var t=queue_head.begin;
queue_head.begin=queue_head.begin.next;
if(queue_head.begin==nil)
queue_head.end=nil;
return t;
}
var queue_front=func(queue_head)
{
if(queue_head.begin!=nil)
return queue_head.begin.elem;
return nil;
}
var queue_empty=func(queue)
var queue_empty=func(queue_head)
{
return queue.next==nil;
return queue_head.begin==nil;
}

View File

@ -1,4 +1,5 @@
# basic type
import("lib.nas");
nil;
2147483647;
0x7fffffff;
@ -54,7 +55,7 @@ var hash_4={
# function
var func_1=func(){return 1;}
var prt=func(x){print(x);return nil;}
var prt=func(x){println(x);return nil;}
var func_with_dynamic_id=func(a,b,c,d...){return [a,b,c,d];}
var func_with_lack_para=func(a,b,c=1,d=2){return a+b+c+d;}
var func_with_func_para=func(a,f){return f(a);}
@ -72,21 +73,21 @@ var source={
member_1: func func_1(), # this will get a number
member_2: func {return 2.71828;} # this will get a function
};
print(source['member_2']());
print(source.member_2());
println(source['member_2']());
println(source.member_2());
var test_func=func{return 1;}
print(func test_func()); # 1
print(test_func()); # 1
print(func test_func); # nothing
print(test_func); # nothing
print(([0,1,2,3])[1]); # 1
print(({str:"what?"})["str"]); # what?
print(({str:"what?"}).str); # what?
println(func test_func()); # 1
println(test_func()); # 1
println(func test_func); # nothing
println(test_func); # nothing
println(([0,1,2,3])[1]); # 1
println(({str:"what?"})["str"]); # what?
println(({str:"what?"}).str); # what?
# lambda
(func(x){return x>0? x:0;})(12);
(func{print("hello world");})();
(func{println("hello world");})();
(((func(x){return 1.0/math.exp(x);})))(0);
# flexible definition & assignment
@ -123,4 +124,29 @@ nil and 1+7*8;
(number_1 or number_2) and (number_3 or number_4-number_4*1);
[0,1,4,3,2][4]*2-4+1*2*2*2*2*2/8;
{num:0}.num or {what_is_the_secret_of_universe:42}["what_is_the_secret_of_universe"];
"123"~"456"-123456*2/2;
"123"~"456"-123456*2/2;
var hash={str:'hello',f:func{return me.str;}};
var tmp_f=hash.f;
hash=1;
print(tmp_f());
# undefined symbol 'me'
# this means that
# when generating local_scope for function f,
# nasal_gc will not count 'me' as one reference of this hash
var h1={str:'hello',f:func{return me.str;}};
var h2={str:'world',f:func{return nil;}};
h2.f=h1.f;
print(h2.f());
# print 'world'
# this means that 'me' in hash's functions
# only points to the hash this function belongs to
var f1=func(){print(1);return 1;}
var f2=func(){print(2);return 0;}
f1() or f2();
# print '1'
# this means that when using 'or' or 'and',
# if the result is clear when calculating,
# objects behind will not be calculated

View File

@ -1,3 +1,4 @@
import("lib.nas");
var global_value=0;
var global_hash=
{
@ -5,16 +6,16 @@ var global_hash=
var2:2,
var3:func(){return me.var2;}
};
print(global_value);
print(global_hash.var3());
println(global_value);
println(global_hash.var3());
var func1=func()
{
global_value=1;
print(global_value);
println(global_value);
var closure_value=1;
var temp_value=1;
print(temp_value);
println(temp_value);
return func{return closure_value;};
}
@ -23,10 +24,9 @@ var func2=func()
for(var temp_value=0;temp_value<100;temp_value+=1)
{
if(temp_value<10)
print(temp_value,"< 10");
println(temp_value,"< 10");
elsif(10<=temp_value and temp_value<50)
print(temp_value,"< 50");
temp_value=10;
println(temp_value,"< 50");
}
return;
}
@ -41,9 +41,9 @@ var func3=func()
};
}
func1()();
println(func1()());
func2();
func3()();
println(func3()());
if(!global_value)
{

View File

@ -1,5 +0,0 @@
import("lib.nas");
var filename="";
filename=input();
print(filename[0]);