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

View File

@ -1,12 +1,17 @@
# 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))
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);},
@ -23,7 +28,6 @@ var file={
eof: func() {return io.eof(fd);}
};
}
};
var find_all_files_with_extension = func(path, extensions...){
var in_vec = func(ext) {
@ -45,20 +49,23 @@ var find_all_files_with_extension=func(path,extensions...){
}
var find_all_files = func(path){
if(!io.exists(path))
if (!io.exists(path)) {
return [];
}
var dd = unix.opendir(path);
var res = [];
while(var n = unix.readdir(dd))
if(unix.isfile(path~"/"~n))
if(unix.isfile(path~"/"~n)) {
append(res, n);
}
unix.closedir(dd);
return res;
}
var recursive_find_files = func(path) {
if(!io.exists(path))
if (!io.exists(path)) {
return nil;
}
var dd = unix.opendir(path);
var res = {
dir: path,
@ -69,10 +76,10 @@ var recursive_find_files=func(path){
append(res.files,n);
} elsif (unix.isdir(path~"/"~n) and n!="." and n!="..") {
var tmp = recursive_find_files(path~"/"~n);
if(tmp!=nil)
if (tmp!=nil) {
append(res.files,tmp);
}
}
}
unix.closedir(dd);
return res;

View File

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

View File

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