-
Notifications
You must be signed in to change notification settings - Fork 60
/
bqn.js
executable file
·210 lines (199 loc) · 7.87 KB
/
bqn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#! /usr/bin/env node
"use strict";
let path = require('path');
let fs = require('fs');
let bqn = require("./docs/bqn.js");
module.exports = bqn;
let {fmt,fmtErr,sysvals,sysargs,makebqn,makerepl}=bqn;
let {has,list,str,unstr,dynsys,req1str,makens}=bqn.util;
let strlist = l=>list(l.map(str),str(""));
let bqn_state=makebqn((x,w,u,s)=>(u(s,w),x));
let bqn_nostate=makebqn(x=>x);
let show = x => console.log(fmt(x));
sysvals.show = (x,w) => { show(x); return x; };
sysvals.out = (x,w) => { console.log(req1str("•Out",x,w)); return x; };
sysvals.exit = (x,w) => process.exit(Number.isInteger(x)?x:0);
let dir = f=>f===path.sep?f:f+path.sep; // BQN uses trailing slash
let pathjoin = (base, rel) => {
if (path.isAbsolute(rel)) return rel;
if (!has(base)) throw Error("Using relative path with no absolute base path known");
if (rel.length==0) return base;
if (base.length==0) return rel;
let b = path.sep === base[base.length-1] ? base : base + path.sep;
return b + rel;
}
let getres = p => {
let res;
if (p) { p=unstr(p); res = (e,f)=>pathjoin(p,f); }
else { res = (e,f) => { if (!path.isAbsolute(f)) throw Error(e+": Paths must be absolute when not running from a file"); return f; }; }
return e => (x,w) => res(e,req1str(e,x,w));
}
let withres = (e,fn) => dynsys(state => fn(state.resolve(e), state));
let ff = (fr,fw,o) => resolve => (x,w) => {
let f = resolve(has(w)?w:x);
if (has(w)) { fs.writeFileSync(f,fw(x),o); return str(f); }
else { return fr(fs.readFileSync(f,o)); }
};
let fchars = ff(str,x=>req1str("•FChars",x),"utf-8");
let flines = ff(s=>strlist(s.replace(/\n$/,'').split('\n')),s=>s.map(x=>req1str("•FLines",x)).join('\n')+'\n',"utf-8");
let fbytes = ff(s=>list(Array.from(s).map(c=>String.fromCodePoint(c))),s=>Buffer.from(s.map(c=>c.codePointAt(0))));
sysvals.fchars = withres("•FChars",fchars);
sysvals.flines = withres("•FLines",flines);
sysvals.fbytes = withres("•FBytes",fbytes);
sysvals.file = dynsys(state => {
let p = state.path;
let res = state.resolve;
let files = {
// Paths and parsing
path: p,
at: (x,w) => {
let r = v => req1str("•file.At",v);
return str(pathjoin(has(w)?r(w):unstr(p), r(x)));
},
name: (x,w) => str(path.basename(req1str("•file.Name",x,w))),
extension: (x,w) => str(path.extname (req1str("•file.Extension",x,w))),
basename: (x,w) => str(path.parse(req1str("•file.BaseName",x,w)).name),
parent: (x,w) => str(dir(path.dirname(res("•file.Parent")(x,w)))),
parts: (x,w) => { let p=path.parse(res("•file.Parts")(x,w));
return strlist([dir(p.dir),p.name,p.ext]); },
// Metadata
exists: (x,w) => fs.existsSync(res("•file.Exists")(x,w))?1:0,
type: (x,w) => {
let c = fs.constants;
switch (c.S_IFMT & fs.lstatSync(res("•file.Type")(x,w)).mode) {
case c.S_IFREG: return 'f';
case c.S_IFDIR: return 'd';
case c.S_IFLNK: return 'l';
case c.S_IFIFO: return 'p';
case c.S_IFSOCK: return 's';
case c.S_IFBLK: return 'b';
case c.S_IFCHR: return 'c';
}
},
created: (x,w) => fs.statSync(res("•file.Created" )(x,w)).birthtimeMs/1000,
accessed: (x,w) => fs.statSync(res("•file.Accessed")(x,w)).atimeMs/1000,
modified: (x,w) => fs.statSync(res("•file.Modified")(x,w)).mtimeMs/1000,
size: (x,w) => fs.statSync(res("•file.Size" )(x,w)).size,
permissions: (x,w) => {
let f=res("•file.Permissions")(x);
let mode=fs.statSync(f).mode;
if (has(w)) {
if (!w.sh||w.sh.length!==1||w.sh[0]!==3) throw Error("•file.Permissions: 𝕨 must be a list of 3 numbers");
if (!w.every(n=>Number.isInteger(n)&&0<=n&&n<8)) throw Error("•file.Permissions: each permission must belong to ↕8");
let p=0; w.map(n=>p=8*p+n);
fs.chmodSync(f,(mode&fs.constants.S_IFMT)|p); return w;
} else {
let p=[]; for (let i=3;i--;) { p[i]=mode&7; mode=Math.floor(mode/8); }
return list(p,0);
}
},
owner: (x,w) => {
let f=res("•file.Owner")(x);
if (has(w)) {
if (!w.sh||w.sh.length!==1||w.sh[0]!==2) throw Error("•file.Owner: 𝕨 must be a uid‿gid pair");
fs.chownSync(f,w[0],w[1]); return w;
} else {
let s=fs.statSync(f); return list([s.uid,s.gid],0);
}
},
realpath: (x,w) => fs.realpathSync(res("•file.RealPath")(x)),
// Access
rename: (x,w) => {let r=res("•file.Rename"),f=r(w); fs.renameSync (r(x),f); return str(f);},
copy: (x,w) => {let r=res("•file.Copy" ),f=r(w); fs.copyFileSync(r(x),f); return str(f);},
createdir: (x,w) => {let f=res("•file.CreateDir")(x,w); fs.mkdirSync(f); return str(f);},
remove: (x,w) => {fs.rmSync(res("•file.Remove")(x,w)); return 1;},
removedir: (x,w) => {fs.rmSync(res("•file.RemoveDir")(x,w),{recursive:true,force:true}); return 1;},
list: (x,w) => strlist(fs.readdirSync(res("•file.List")(x,w))),
chars: fchars(res("•file.Chars")),
lines: flines(res("•file.Lines")),
bytes: fbytes(res("•file.Bytes")),
// TODO Open Return an open file object based on 𝕩
}
return makens(Object.keys(files), Object.values(files));
});
sysvals.getline = () => {
let l = 1024, b = Buffer.alloc(l);
let fd = fs.openSync("/dev/stdin", "rs");
let r = '';
do {
let n = fs.readSync(fd, b, 0, b.length);
if (!n) return '\0';
r += b.toString('utf-8', 0, n);
} while (r[r.length-1]!=='\n');
fs.closeSync(fd);
return str(r.slice(0,-1));
}
sysvals.platform = dynsys(_ => {
let ja = ["ia32", "x64", "arm", "arm64" ];
let ba = ["x86", "x86-64", "aarch32", "aarch64"];
let a = process.arch; let ai = ja.indexOf(a);
let cpu = makens(["arch"], [str(ai>=0 ? ba[ai] : a)]);
let bqn = makens(["impl"], [str("JS BQN")]);
let os = process.platform; if (os==="win32") os="windows";
return makens(
["os", "environment","cpu","bqn"],
[str(os), str("nodejs"), cpu, bqn ]
);
});
sysargs.resolve = sysargs.parres = getres();
let push_state = st => { st.parres = st.resolve; }
let update_state = (st,w) => {
w=w||[];
st.path=w[0]&&str(st.parres("Setting •path")(w[0]));
st.resolve = getres(st.path);
st.state=list(w); st.name=w[1]; st.args=w[2];
}
sysvals.path=dynsys(s=>s.path);
sysvals.name=dynsys(s=>s.name);
sysvals.args=dynsys(s=>s.args);
sysvals.state=dynsys(s=>s.state);
sysvals.wdpath=dynsys(_=>str(dir(path.resolve('.'))));
bqn.setexec(update_state, push_state);
let bqn_file = (st,f,t,w) => bqn_state(st)(
t, [ str(dir(path.dirname(f))), str(path.basename(f)), w ]
);
let imports = {};
sysvals.import = withres("•Import", (resolve,state) => (x,w) => {
let f = resolve(x);
let save = r=>r;
if (!has(w)) {
let c=imports[f]; if (has(c)) return c;
save = r => (imports[f]=r);
w=list([]);
}
return save(bqn_file(state, f, fs.readFileSync(f,'utf-8'), w));
});
if (!module.parent) {
let args = process.argv.slice(2);
let arg0 = args[0];
let cl_state = () => {
let s = str("");
let w = [str(dir(path.resolve('.'))), s, list([],s)];
update_state(sysargs, w); return sysargs;
}
let exec = fn => src => {
try {
fn(src);
} catch(e) {
console.error('[31m'+fmtErr(e)+'[39m');
}
}
if (!has(arg0) || arg0==='-r') {
let stdin = process.stdin;
let repl = makerepl(cl_state(), 1);
let e = exec(s=>show(repl(s)));
stdin.on('end', () => { process.exit(); });
stdin.on('readable', () => {
let inp; while ((inp=stdin.read())!==null) {
if (!/^[ \t]*[#\n]/.test(inp)) e(inp.toString());
}
});
} else if (arg0[0] !== '-' || (arg0==='-f'&&(arg0=(args=args.slice(1))[0],1))) {
let f=arg0, a=strlist(args.slice(1));
exec(s=>bqn_file(sysargs, path.resolve(f),s,a))(fs.readFileSync(f,'utf-8'));
} else if (arg0 === '-e' || arg0 === '-p') {
let ev=bqn_nostate(cl_state());
let evs = arg0!=='-p' ? ev : (s=>show(ev(s)));
args.slice(1).map(exec(evs));
}
}