📝 optimize code format

This commit is contained in:
ValKmjolnir 2023-08-13 00:31:11 +08:00
parent e131c8da4b
commit 7d81246695
5 changed files with 248 additions and 236 deletions

View File

@ -1,16 +1,16 @@
# lib csv.nas
# ValKmjolnir 2022/10/15
var read_csv=func(path,delimeter=",",endline="\n"){
var context=io.readfile(path);
context=split(endline,context);
var read = func(path, delimeter=",", endline="\n"){
var context = io.readfile(path);
context = split(endline, context);
forindex(var i;context){
context[i]=split(delimeter,context[i]);
context[i] = split(delimeter,context[i]);
}
if(size(context)<=1){
die("incorrect csv file <"~path~">: "~size(context)~" line(s).");
}
return {
property:context[0],
data:context[1:]
property: context[0],
data: context[1:]
};
}

View File

@ -1,78 +1,85 @@
# lib file.nas
# ValKmjolnir 2022/3/6
var file={
SEEK_SET:io.SEEK_SET,
SEEK_CUR:io.SEEK_CUR,
SEEK_END:io.SEEK_END,
new: func(filename,mode="r"){
if(!io.exists(filename))
return nil;
var fd=io.open(filename,mode);
return {
close: func(){io.close(fd);},
read: func(len){
var buf=mut("");
io.read(fd,buf,len);
return buf;
},
write: func(str){return io.write(fd,str);},
seek: func(pos,whence){return io.seek(fd,pos,whence);},
tell: func(){return io.tell(fd);},
readln: func(){return io.readln(fd);},
stat: func(){return io.stat(filename);},
eof: func(){return io.eof(fd);}
};
}
};
var find_all_files_with_extension=func(path,extensions...){
var in_vec=func(ext){
foreach(var i;extensions){
if(ext==i){
var SEEK_SET = io.SEEK_SET;
var SEEK_CUR = io.SEEK_CUR;
var SEEK_END = io.SEEK_END;
var new = func(filename, mode="r"){
if (!io.exists(filename)) {
return nil;
}
var fd = io.open(filename, mode);
return {
close: func() {io.close(fd);},
read: func(len) {
var buf = mut("");
io.read(fd, buf, len);
return buf;
},
write: func(str) {return io.write(fd, str);},
seek: func(pos, whence) {return io.seek(fd, pos, whence);},
tell: func() {return io.tell(fd);},
readln: func() {return io.readln(fd);},
stat: func() {return io.stat(filename);},
eof: func() {return io.eof(fd);}
};
}
var find_all_files_with_extension = func(path, extensions...){
var in_vec = func(ext) {
foreach(var i;extensions) {
if (ext==i){
return 1;
}
}
return 0;
}
var res=[];
var res = [];
foreach(var f;find_all_files(path)){
var tmp=split('.',f);
if(size(tmp)>1 and in_vec(tmp[-1])){
append(res,f);
var tmp = split('.', f);
if (size(tmp)>1 and in_vec(tmp[-1])) {
append(res, f);
}
}
return res;
}
var find_all_files=func(path){
if(!io.exists(path))
var find_all_files = func(path){
if (!io.exists(path)) {
return [];
var dd=unix.opendir(path);
var res=[];
while(var n=unix.readdir(dd))
if(unix.isfile(path~"/"~n))
append(res,n);
}
var dd = unix.opendir(path);
var res = [];
while(var n = unix.readdir(dd))
if(unix.isfile(path~"/"~n)) {
append(res, n);
}
unix.closedir(dd);
return res;
}
var recursive_find_files=func(path){
if(!io.exists(path))
var recursive_find_files = func(path) {
if (!io.exists(path)) {
return nil;
var dd=unix.opendir(path);
var res={
dir:path,
files:[]
}
var dd = unix.opendir(path);
var res = {
dir: path,
files: []
};
while(var n=unix.readdir(dd)){
if(unix.isfile(path~"/"~n)){
while(var n = unix.readdir(dd)) {
if (unix.isfile(path~"/"~n)) {
append(res.files,n);
}elsif(unix.isdir(path~"/"~n) and n!="." and n!=".."){
var tmp=recursive_find_files(path~"/"~n);
if(tmp!=nil)
} elsif (unix.isdir(path~"/"~n) and n!="." and n!="..") {
var tmp = recursive_find_files(path~"/"~n);
if (tmp!=nil) {
append(res.files,tmp);
}
}
}
unix.closedir(dd);
return res;

View File

@ -1,59 +1,59 @@
# lib json.nas
# 2021 ValKmjolnir
var JSON=func() {
var (
j_eof,
j_lbrace,
j_rbrace,
j_lbrkt,
j_rbrkt,
j_comma,
j_colon,
j_str,
j_num,
j_id
)=(0,1,2,3,4,5,6,7,8,9);
var j_content=[
"eof",
"`{`",
"`}`",
"`[`",
"`]`",
"`,`",
"`:`",
"string",
"number",
"identifier"
];
var (
_j_eof, _j_lbrace, _j_rbrace, _j_lbrkt, _j_rbrkt,
_j_comma, _j_colon, _j_str, _j_num, _j_id
) = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
var text='';
var line=1;
var text_size=0;
var ptr=0;
var token={content:'',type:''};
var _j_content = [
"eof",
"`{`",
"`}`",
"`[`",
"`]`",
"`,`",
"`:`",
"string",
"number",
"identifier"
];
var init=func() {
text='';
line=1;
text_size=0;
ptr=0;
token={content:'',type:''};
var JSON = func() {
var text = "";
var line = 1;
var text_size = 0;
var ptr = 0;
var token = {
content: "",
type: ""
};
var init = func() {
text = "";
line = 1;
text_size = 0;
ptr = 0;
token = {
content: "",
type: ""
};
}
var isnum=func(c) {
var isnum = func(c) {
return '0'<=c and c<='9';
}
var isid=func(c) {
var tmp=c[0];
var isid = func(c) {
var tmp = c[0];
return ('a'[0]<=tmp and tmp<='z'[0]) or
('A'[0]<=tmp and tmp<='Z'[0]) or
c=='_';
('A'[0]<=tmp and tmp<='Z'[0]) or
c=='_';
}
var check=func() {
var c=char(text[ptr]);
var check = func() {
var c = char(text[ptr]);
return (
c=='{' or c=='}' or
c=='[' or c==']' or
@ -63,151 +63,152 @@ var JSON=func() {
);
}
var get=func(str) {
var get = func(str) {
init();
if(!size(str)) {
if (!size(str)) {
println("JSON.parse: empty string");
str="[]";
str = "[]";
}
text=str;
text_size=size(text);
text = str;
text_size = size(text);
return;
}
var next=func() {
var next = func() {
while(ptr<text_size and !check()) {
if(char(text[ptr])=='\n')
line+=1;
ptr+=1;
if (char(text[ptr])=='\n') {
line += 1;
}
ptr += 1;
}
if(ptr>=text_size) {
token.content="eof";
token.type=j_eof;
token.content = "eof";
token.type = _j_eof;
return;
}
var c=char(text[ptr]);
if(c=='{') {
token.content='{';
token.type=j_lbrace;
} elsif(c=='}') {
token.content='}';
token.type=j_rbrace;
} elsif(c=='[') {
token.content='[';
token.type=j_lbrkt;
} elsif(c==']') {
token.content=']';
token.type=j_rbrkt;
} elsif(c==',') {
token.content=',';
token.type=j_comma;
} elsif(c==':') {
token.content=':';
token.type=j_colon;
} elsif(c=='\"' or c=='\'') {
var strbegin=c;
var s="";
ptr+=1;
var c = char(text[ptr]);
if (c=='{') {
token.content = '{';
token.type = _j_lbrace;
} elsif (c=='}') {
token.content = '}';
token.type = _j_rbrace;
} elsif (c=='[') {
token.content = '[';
token.type = _j_lbrkt;
} elsif (c==']') {
token.content = ']';
token.type = _j_rbrkt;
} elsif (c==',') {
token.content = ',';
token.type = _j_comma;
} elsif (c==':') {
token.content = ':';
token.type = _j_colon;
} elsif (c=='\"' or c=='\'') {
var strbegin = c;
var s = "";
ptr += 1;
while(ptr<text_size and char(text[ptr])!=strbegin) {
s~=char(text[ptr]);
ptr+=1;
if(char(text[ptr-1])=="\\" and ptr<text_size) {
s~=char(text[ptr]);
ptr+=1;
s ~= char(text[ptr]);
ptr += 1;
if (char(text[ptr-1])=="\\" and ptr<text_size) {
s ~= char(text[ptr]);
ptr += 1;
}
}
token.content=s;
token.type=j_str;
} elsif(isnum(c)) {
var s=c;
ptr+=1;
token.type=_j_str;
} elsif (isnum(c)) {
var s = c;
ptr += 1;
while(ptr<text_size and ((isnum(char(text[ptr])) or char(text[ptr])=='.'))) {
s~=char(text[ptr]);
ptr+=1;
s ~= char(text[ptr]);
ptr += 1;
}
ptr-=1;
token.content=num(s);
token.type=j_num;
} elsif(isid(c)) {
var s=c;
ptr+=1;
ptr -= 1;
token.content = num(s);
token.type = _j_num;
} elsif (isid(c)) {
var s = c;
ptr += 1;
while(ptr<text_size and (isid(char(text[ptr])) or isnum(char(text[ptr])))) {
s~=char(text[ptr]);
ptr+=1;
s ~= char(text[ptr]);
ptr += 1;
}
ptr-=1;
token.content=s;
token.type=j_id;
ptr -= 1;
token.content = s;
token.type = _j_id;
}
ptr+=1;
ptr += 1;
return;
}
var match=func(type) {
var match = func(type) {
if(token.type!=type)
println("JSON.parse: line ",line,": expect ",j_content[type]," but get `",token.content,"`.");
println("JSON.parse: line ",line,": expect ",_j_content[type]," but get `",token.content,"`.");
next();
return;
}
var member=func(hash) {
var name=token.content;
if(token.type==j_rbrace) {
var member = func(hash) {
var name = token.content;
if(token.type==_j_rbrace) {
return;
}
if(token.type==j_str) {
match(j_str);
if(token.type==_j_str) {
match(_j_str);
} else {
match(j_id);
match(_j_id);
}
match(j_colon);
if(token.type==j_lbrace) {
hash[name]=hash_gen();
} elsif(token.type==j_lbrkt) {
hash[name]=vec_gen();
} elsif(token.type==j_str or token.type==j_num) {
hash[name]=token.content;
match(_j_colon);
if(token.type==_j_lbrace) {
hash[name] = hash_gen();
} elsif(token.type==_j_lbrkt) {
hash[name] = vec_gen();
} elsif(token.type==_j_str or token.type==_j_num) {
hash[name] = token.content;
next();
}
return;
}
var hash_gen=func() {
var hash={};
match(j_lbrace);
var hash_gen = func() {
var hash = {};
match(_j_lbrace);
member(hash);
while(token.type==j_comma) {
match(j_comma);
while(token.type==_j_comma) {
match(_j_comma);
member(hash);
}
match(j_rbrace);
match(_j_rbrace);
return hash;
}
var vec_gen=func() {
var vec=[];
match(j_lbrkt);
if(token.type==j_lbrace) {
append(vec,hash_gen());
} elsif(token.type==j_lbrkt) {
append(vec,vec_gen());
} elsif(token.type==j_str or token.type==j_num) {
append(vec,token.content);
var vec_gen = func() {
var vec = [];
match(_j_lbrkt);
if(token.type==_j_lbrace) {
append(vec, hash_gen());
} elsif(token.type==_j_lbrkt) {
append(vec, vec_gen());
} elsif(token.type==_j_str or token.type==_j_num) {
append(vec, token.content);
next();
}
while(token.type==j_comma) {
match(j_comma);
if(token.type==j_lbrace) {
append(vec,hash_gen());
} elsif(token.type==j_lbrkt) {
append(vec,vec_gen());
} elsif(token.type==j_str or token.type==j_num) {
append(vec,token.content);
while(token.type==_j_comma) {
match(_j_comma);
if(token.type==_j_lbrace) {
append(vec, hash_gen());
} elsif(token.type==_j_lbrkt) {
append(vec, vec_gen());
} elsif(token.type==_j_str or token.type==_j_num) {
append(vec, token.content);
next();
}
}
match(j_rbrkt);
match(_j_rbrkt);
return vec;
}
@ -220,10 +221,10 @@ var JSON=func() {
get(str);
next();
if(token.type==j_lbrkt) {
var res=vec_gen();
if (token.type==_j_lbrkt) {
var res = vec_gen();
} else {
var res=hash_gen();
var res = hash_gen();
}
init();
@ -232,55 +233,55 @@ var JSON=func() {
};
}();
JSON.stringify=func(object) {
JSON.stringify = func(object) {
if(typeof(object)!="hash" and typeof(object)!="vec") {
println("JSON.stringify: must use hashmap or vector");
return "[]";
}
var s="";
var gen=func(elem) {
var t=typeof(elem);
var s = "";
var gen = func(elem) {
var t = typeof(elem);
if(t=="num") {
s~=str(elem);
s ~= str(elem);
} elsif(t=="str") {
s~='"'~elem~'"';
s ~= '"'~elem~'"';
} elsif(t=="vec") {
vgen(elem);
} elsif(t=="hash") {
hgen(elem);
} else {
s~='"undefined"';
s ~= '"undefined"';
}
}
var vgen=func(v) {
s~="[";
var vsize=size(v);
for(var i=0;i<vsize;i+=1) {
var vgen = func(v) {
s ~= "[";
var vsize = size(v);
for(var i = 0; i<vsize; i += 1) {
gen(v[i]);
if(i!=vsize-1) {
if (i!=vsize-1) {
s~=",";
}
}
s~="]";
s ~= "]";
}
var hgen=func(h) {
s~="{";
var k=keys(h);
var vsize=size(k);
for(var i=0;i<vsize;i+=1) {
s~=k[i]~":";
var hgen = func(h) {
s ~= "{";
var k = keys(h);
var vsize = size(k);
for(var i = 0; i<vsize; i += 1) {
s ~= k[i]~":";
gen(h[k[i]]);
if(i!=vsize-1) {
s~=",";
if (i!=vsize-1) {
s ~= ",";
}
}
s~="}";
s ~= "}";
}
if(typeof(object)=="vec") {
if (typeof(object)=="vec") {
vgen(object);
} else {
hgen(object);

View File

@ -1,20 +1,24 @@
# padding.nas
# ValKmjolnir 2022/9/4
var leftpad=func(s,len,char=" "){
if(typeof(s)=="num")
s=str(s);
var strlen=size(s);
for(var i=strlen;i<len;i+=1)
s=char~s;
var leftpad = func(s, len, char=" ") {
if (typeof(s)=="num") {
s = str(s);
}
var strlen = size(s);
for(var i = strlen; i<len; i += 1) {
s = char~s;
}
return s;
}
var rightpad=func(s,len,char=" "){
if(typeof(s)=="num")
s=str(s);
var strlen=size(s);
for(var i=strlen;i<len;i+=1)
s~=char;
var rightpad = func(s, len, char=" ") {
if (typeof(s)=="num") {
s = str(s);
}
var strlen = size(s);
for(var i = strlen; i<len; i += 1) {
s ~= char;
}
return s;
}

View File

@ -1,10 +1,10 @@
# string.nas
# ValKmjolnir 2022/10/5
var join=func(sep, vec){
var join = func(sep, vec){
var len = size(vec);
var res = "";
for(var i = 0; i<len; i+=1) {
for(var i = 0; i<len; i += 1) {
res ~= vec[i];
res ~= (i==len-1? "":sep);
}