diff --git a/std/csv.nas b/std/csv.nas index bf2e54a..94800a9 100644 --- a/std/csv.nas +++ b/std/csv.nas @@ -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:] }; } \ No newline at end of file diff --git a/std/file.nas b/std/file.nas index 7824c3e..4159561 100644 --- a/std/file.nas +++ b/std/file.nas @@ -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; diff --git a/std/json.nas b/std/json.nas index 145ca01..e2ff68a 100644 --- a/std/json.nas +++ b/std/json.nas @@ -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) { - 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