📝 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,6 +1,6 @@
# lib csv.nas # lib csv.nas
# ValKmjolnir 2022/10/15 # ValKmjolnir 2022/10/15
var read_csv=func(path,delimeter=",",endline="\n"){ var read = func(path, delimeter=",", endline="\n"){
var context = io.readfile(path); var context = io.readfile(path);
context = split(endline, context); context = split(endline, context);
forindex(var i;context){ forindex(var i;context){

View File

@ -1,12 +1,17 @@
# lib file.nas # lib file.nas
# ValKmjolnir 2022/3/6 # ValKmjolnir 2022/3/6
var file={
SEEK_SET:io.SEEK_SET, var SEEK_SET = io.SEEK_SET;
SEEK_CUR:io.SEEK_CUR,
SEEK_END:io.SEEK_END, var SEEK_CUR = io.SEEK_CUR;
new: func(filename,mode="r"){
if(!io.exists(filename)) var SEEK_END = io.SEEK_END;
var new = func(filename, mode="r"){
if (!io.exists(filename)) {
return nil; return nil;
}
var fd = io.open(filename, mode); var fd = io.open(filename, mode);
return { return {
close: func() {io.close(fd);}, close: func() {io.close(fd);},
@ -23,7 +28,6 @@ var file={
eof: func() {return io.eof(fd);} eof: func() {return io.eof(fd);}
}; };
} }
};
var find_all_files_with_extension = func(path, extensions...){ var find_all_files_with_extension = func(path, extensions...){
var in_vec = func(ext) { var in_vec = func(ext) {
@ -45,20 +49,23 @@ var find_all_files_with_extension=func(path,extensions...){
} }
var find_all_files = func(path){ var find_all_files = func(path){
if(!io.exists(path)) if (!io.exists(path)) {
return []; return [];
}
var dd = unix.opendir(path); var dd = unix.opendir(path);
var res = []; var res = [];
while(var n = unix.readdir(dd)) while(var n = unix.readdir(dd))
if(unix.isfile(path~"/"~n)) if(unix.isfile(path~"/"~n)) {
append(res, n); append(res, n);
}
unix.closedir(dd); unix.closedir(dd);
return res; return res;
} }
var recursive_find_files = func(path) { var recursive_find_files = func(path) {
if(!io.exists(path)) if (!io.exists(path)) {
return nil; return nil;
}
var dd = unix.opendir(path); var dd = unix.opendir(path);
var res = { var res = {
dir: path, dir: path,
@ -69,10 +76,10 @@ var recursive_find_files=func(path){
append(res.files,n); append(res.files,n);
} elsif (unix.isdir(path~"/"~n) and n!="." and n!="..") { } elsif (unix.isdir(path~"/"~n) and n!="." and n!="..") {
var tmp = recursive_find_files(path~"/"~n); var tmp = recursive_find_files(path~"/"~n);
if(tmp!=nil) if (tmp!=nil) {
append(res.files,tmp); append(res.files,tmp);
} }
}
} }
unix.closedir(dd); unix.closedir(dd);
return res; return res;

View File

@ -1,20 +1,12 @@
# lib json.nas # lib json.nas
# 2021 ValKmjolnir # 2021 ValKmjolnir
var JSON=func() {
var ( var (
j_eof, _j_eof, _j_lbrace, _j_rbrace, _j_lbrkt, _j_rbrkt,
j_lbrace, _j_comma, _j_colon, _j_str, _j_num, _j_id
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); ) = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
var j_content=[
var _j_content = [
"eof", "eof",
"`{`", "`{`",
"`}`", "`}`",
@ -27,18 +19,26 @@ var JSON=func() {
"identifier" "identifier"
]; ];
var text=''; var JSON = func() {
var text = "";
var line = 1; var line = 1;
var text_size = 0; var text_size = 0;
var ptr = 0; var ptr = 0;
var token={content:'',type:''}; var token = {
content: "",
type: ""
};
var init = func() { var init = func() {
text=''; text = "";
line = 1; line = 1;
text_size = 0; text_size = 0;
ptr = 0; ptr = 0;
token={content:'',type:''}; token = {
content: "",
type: ""
};
} }
var isnum = func(c) { var isnum = func(c) {
@ -76,35 +76,36 @@ var JSON=func() {
var next = func() { var next = func() {
while(ptr<text_size and !check()) { while(ptr<text_size and !check()) {
if(char(text[ptr])=='\n') if (char(text[ptr])=='\n') {
line += 1; line += 1;
}
ptr += 1; ptr += 1;
} }
if(ptr>=text_size) { if(ptr>=text_size) {
token.content = "eof"; token.content = "eof";
token.type=j_eof; token.type = _j_eof;
return; return;
} }
var c = char(text[ptr]); var c = char(text[ptr]);
if (c=='{') { if (c=='{') {
token.content = '{'; token.content = '{';
token.type=j_lbrace; token.type = _j_lbrace;
} elsif (c=='}') { } elsif (c=='}') {
token.content = '}'; token.content = '}';
token.type=j_rbrace; token.type = _j_rbrace;
} elsif (c=='[') { } elsif (c=='[') {
token.content = '['; token.content = '[';
token.type=j_lbrkt; token.type = _j_lbrkt;
} elsif (c==']') { } elsif (c==']') {
token.content = ']'; token.content = ']';
token.type=j_rbrkt; token.type = _j_rbrkt;
} elsif (c==',') { } elsif (c==',') {
token.content = ','; token.content = ',';
token.type=j_comma; token.type = _j_comma;
} elsif (c==':') { } elsif (c==':') {
token.content = ':'; token.content = ':';
token.type=j_colon; token.type = _j_colon;
} elsif (c=='\"' or c=='\'') { } elsif (c=='\"' or c=='\'') {
var strbegin = c; var strbegin = c;
var s = ""; var s = "";
@ -118,7 +119,7 @@ var JSON=func() {
} }
} }
token.content=s; token.content=s;
token.type=j_str; token.type=_j_str;
} elsif (isnum(c)) { } elsif (isnum(c)) {
var s = c; var s = c;
ptr += 1; ptr += 1;
@ -128,7 +129,7 @@ var JSON=func() {
} }
ptr -= 1; ptr -= 1;
token.content = num(s); token.content = num(s);
token.type=j_num; token.type = _j_num;
} elsif (isid(c)) { } elsif (isid(c)) {
var s = c; var s = c;
ptr += 1; ptr += 1;
@ -138,7 +139,7 @@ var JSON=func() {
} }
ptr -= 1; ptr -= 1;
token.content = s; token.content = s;
token.type=j_id; token.type = _j_id;
} }
ptr += 1; ptr += 1;
return; return;
@ -146,27 +147,27 @@ var JSON=func() {
var match = func(type) { var match = func(type) {
if(token.type!=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(); next();
return; return;
} }
var member = func(hash) { var member = func(hash) {
var name = token.content; var name = token.content;
if(token.type==j_rbrace) { if(token.type==_j_rbrace) {
return; return;
} }
if(token.type==j_str) { if(token.type==_j_str) {
match(j_str); match(_j_str);
} else { } else {
match(j_id); match(_j_id);
} }
match(j_colon); match(_j_colon);
if(token.type==j_lbrace) { if(token.type==_j_lbrace) {
hash[name] = hash_gen(); hash[name] = hash_gen();
} elsif(token.type==j_lbrkt) { } elsif(token.type==_j_lbrkt) {
hash[name] = vec_gen(); 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; hash[name] = token.content;
next(); next();
} }
@ -175,39 +176,39 @@ var JSON=func() {
var hash_gen = func() { var hash_gen = func() {
var hash = {}; var hash = {};
match(j_lbrace); match(_j_lbrace);
member(hash); member(hash);
while(token.type==j_comma) { while(token.type==_j_comma) {
match(j_comma); match(_j_comma);
member(hash); member(hash);
} }
match(j_rbrace); match(_j_rbrace);
return hash; return hash;
} }
var vec_gen = func() { var vec_gen = func() {
var vec = []; var vec = [];
match(j_lbrkt); match(_j_lbrkt);
if(token.type==j_lbrace) { if(token.type==_j_lbrace) {
append(vec, hash_gen()); append(vec, hash_gen());
} elsif(token.type==j_lbrkt) { } elsif(token.type==_j_lbrkt) {
append(vec, vec_gen()); 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); append(vec, token.content);
next(); next();
} }
while(token.type==j_comma) { while(token.type==_j_comma) {
match(j_comma); match(_j_comma);
if(token.type==j_lbrace) { if(token.type==_j_lbrace) {
append(vec, hash_gen()); append(vec, hash_gen());
} elsif(token.type==j_lbrkt) { } elsif(token.type==_j_lbrkt) {
append(vec, vec_gen()); 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); append(vec, token.content);
next(); next();
} }
} }
match(j_rbrkt); match(_j_rbrkt);
return vec; return vec;
} }
@ -220,7 +221,7 @@ var JSON=func() {
get(str); get(str);
next(); next();
if(token.type==j_lbrkt) { if (token.type==_j_lbrkt) {
var res = vec_gen(); var res = vec_gen();
} else { } else {
var res = hash_gen(); var res = hash_gen();

View File

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