-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcvt.js
More file actions
executable file
·274 lines (252 loc) · 7.98 KB
/
Copy pathmcvt.js
File metadata and controls
executable file
·274 lines (252 loc) · 7.98 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env node
const readline = require('node:readline');
const da = require('./lib/dimension.js');
const grapher = require('./lib/grapher.js');
const index = require('./lib/index.js');
const ls = require('./lib/languagestring.js');
const loader = require('./lib/loader.js');
const interp = require('./lib/mcvt-interp.js');
const ops = require('./lib/mcvt-ops.js');
const unit = require('./lib/unitparser.js');
// UTILITIES FOR PRINTING ITEMS FROM SEARCH INDEX
function fmtsym(s) {
return s ? (s.join ? s.join(',') : s) : '';
}
function fmtname(n) {
return n ? ls.get(n, 'en', '*') : '';
}
function fmtdim(d, t) {
if (da.empty(d)) return '';
const ds = da.str(d);
if (t === 'unit-type') return ds;
const tn = unit.type({'dimension': d})['name'];
const ts = tn ? ls.get(tn, 'en', '*') : '';
if (!ts || ts === ds) return ds;
if (ds.replaceAll('-',' ') === ts) return ts;
return ts + ' (' + ds + ')';
}
function rept(s, w) {
let r = '';
while (w > 0) { r += s; w--; }
return r;
}
function width(s, w) {
if (w <= 0) return '';
if (s === undefined || s === null) return rept(' ', w);
if (s.length < w) return s + rept(' ', w - s.length);
if (w <= 3) return '...'.substring(0, w);
return s.substring(0, w - 3) + '...';
}
function printItems(items) {
console.log('');
const cols = [
['d', ...items.map(item => item['disambiguated'] ? '*' : ' ')],
['id', ...items.map(item => item['key'])],
['type', ...items.map(item => item['type'])],
['sym', ...items.map(item => fmtsym(item['value']['symbol']))],
['name', ...items.map(item => fmtname(item['value']['name']))],
['dimension', ...items.map(item => fmtdim(item['value']['dimension'], item['type']))]
];
const colWidths = cols.map(col => Math.max(...col.map(s => s.length)));
for (let r = 0; r <= items.length; r++) {
const row = [];
for (let c = 0; c < cols.length; c++) {
row.push(width(cols[c][r], colWidths[c] + 4));
}
console.log(row.join(''));
if (r === 0) {
const hr = [];
for (let c = 0; c < cols.length; c++) {
hr.push(width(rept('-', colWidths[c]), colWidths[c] + 4));
}
console.log(hr.join(''));
}
}
console.log('');
}
// COMMANDS AND THEIR UTILITIES
function glob(s) {
let r = '^';
for (let i = 0; i < s.length; i++) {
switch (s[i]) {
case '?': r += '.'; break;
case '*': r += '.*'; break;
case '.': case '+': case '^': case '$': r += '\\' + s[i]; break;
case '(': case ')': case '[': case ']': r += '\\' + s[i]; break;
case '{': case '}': case '|': case '\\': r += '\\' + s[i]; break;
default: r += s[i]; break;
}
}
return new RegExp(r + '$');
}
function listItems(regexp, logerr) {
const collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
const compare = (a,b) => collator.compare(a['type'], b['type']) || collator.compare(a['key'], b['key']);
const keys = Object.keys(index.index).filter(k => k.match(regexp));
const arrays = keys.map(k => index.index[k].filter(v => v['key'] === k));
const items = arrays.reduce((a,b) => a.concat(b), []).sort(compare);
if (items.length) {
printItems(items);
return true;
} else {
if (logerr) console.log('Found no objects with matching ids.');
return false;
}
}
function lookupItems(q, logerr, trace) {
const items = index.lookup(q);
if (items) {
printItems(items);
return true;
} else try {
const u = unit.parse(q);
index.build({'type': 'unit'}, 'en', {[q]: u});
printItems(index.lookup(q));
return true;
} catch (e) {
if (logerr) console.log(trace ? e : e.message);
return false;
}
}
function tokenToId(token) {
if ('id' in token) return token['id'];
if ('value' in token) return token['value'];
return token['image'];
}
const idPattern = /^([\p{L}\p{M}][\p{L}\p{M}\p{N}]*|[\p{Sc}][\p{L}\p{M}\p{N}]+|[\p{Sc}][^\p{Cc}\p{Cf}\p{Z}]?)$/u;
function printContext(context) {
const collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
const keys = Object.keys(context).sort(collator.compare);
for (const k of keys) {
const ks = k.match(idPattern) ? k : ('`' + k + '`');
console.log(ks + ' := ' + ops.str(context[k]));
}
}
function evaluateArray(tokens, context, trace) {
try {
const expr = interp.parse(tokens);
const result = interp.evaluate(expr, context);
const values = (ops.type(result) === 'array') ? result : [result];
let i = 0; for (const v of values) context['$' + (++i)] = v;
context['$0'] = result; context['$*'] = ops.str(result);
context['$@'] = values; context['$#'] = i;
while (('$' + (++i)) in context) delete context['$' + i];
} catch (e) {
console.log(trace ? e : e.message);
}
}
function evaluate(tokens, context, trace) {
try {
const expr = interp.parse(tokens);
const result = interp.evaluate(expr, context);
console.log(ops.str(result));
} catch (e) {
console.log(trace ? e : e.message);
}
}
function graph(tokens, context, trace) {
try {
const expr = interp.parse(tokens);
console.log(grapher.graph(
x => {
context['x'] = x;
return interp.evaluate(expr, context);
},
(context['cols'] > 0) ? Math.ceil(context['cols']) : 80,
(context['rows'] > 0) ? Math.ceil(context['rows']) : 20,
isFinite(context['l']) ? context['l'] : -10,
isFinite(context['r']) ? context['r'] : +10,
isFinite(context['t']) ? context['t'] : +5,
isFinite(context['b']) ? context['b'] : -5,
(context['res'] > 1) ? Math.ceil(context['res']) : 8
));
} catch (e) {
console.log(trace ? e : e.message);
}
}
// COMMAND LINE INTERPRETER
function execute(command, context) {
if (command === 'q' || command === 'quit' || command === 'exit') {
process.exit(0);
return;
}
if (command === 'list' || command.startsWith('list ')) {
listItems(glob(command.substring(5).trim() || '*'), true);
return;
}
if (command === 'lookup' || command.startsWith('lookup ')) {
const q = command.substring(7).trim();
if (!q) listItems(glob('*'), true);
else lookupItems(q, true, ops.bool(context['trace']));
return;
}
if (command === 'clear' || command.startsWith('clear ')) {
const tokens = interp.lex(command.substring(6).trim());
if (!tokens.length) {
for (const k of Object.keys(context)) delete context[k];
} else {
for (const token of tokens) delete context[tokenToId(token)];
}
return;
}
if (command === 'unset' || command.startsWith('unset ')) {
const tokens = interp.lex(command.substring(6).trim());
for (const token of tokens) delete context[tokenToId(token)];
return;
}
if (command === 'set' || command.startsWith('set ')) {
const tokens = interp.lex(command.substring(4).trim());
if (!tokens.length) printContext(context);
else evaluateArray(tokens, context, ops.bool(context['trace']));
return;
}
if (command === 'graph' || command.startsWith('graph ')) {
const tokens = interp.lex(command.substring(6).trim());
if (!tokens.length) console.log();
else graph(tokens, context, ops.bool(context['trace']));
return;
}
if (command === 'print' || command.startsWith('print ')) {
const tokens = interp.lex(command.substring(6).trim());
if (!tokens.length) console.log();
else evaluate(tokens, context, ops.bool(context['trace']));
return;
}
if (command.startsWith('?') || command.startsWith('=')) {
const tokens = interp.lex(command.substring(1).trim());
if (!tokens.length) console.log();
else evaluate(tokens, context, ops.bool(context['trace']));
return;
}
if (lookupItems(command)) return;
const tokens = interp.lex(command);
if (!tokens.length) return;
if (tokens.length === 1 && tokens[0]['id'] && lookupItems(tokens[0]['id'])) return;
evaluate(tokens, context, ops.bool(context['trace']));
}
function shell(context) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'mcvt> '
});
rl.prompt();
rl.on('line', line => {
if (line = line.trim()) execute(line, context);
rl.prompt();
}).on('close', () => {
console.log('');
process.exit(0);
});
}
function main(argv, context) {
if (argv.length > 2) {
for (let i = 2; i < argv.length; i++) {
execute(argv[i], context);
}
} else {
shell(context);
}
}
loader.load('.', 'en');
main(process.argv, {});