forked from vii5ard/whitespace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_util.js
70 lines (65 loc) · 1.72 KB
/
ws_util.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
globalThis.ws_util = (function () {
return {
isLocalLabel: function (label) {
return /^\..*$/.test(label);
},
getWsUnsignedNumber: function (num) {
let result = "";
while (num > 0n) {
result = ((num % 2n) ? '\t' : ' ') + result;
num = num / 2n;
}
return result + '\n';
},
getWsSignedNumber: function (num) {
return ((num >= 0n) ? ' ' : '\t') + (num === 0n ? '\n' : this.getWsUnsignedNumber(num < 0n ? -1n * num : num));
},
labelTransformer: function (labelGenerator) {
let length = 0n;
return {
length: length,
labels: {},
getLabel: function (label) {
if (label != null && label in this.labels) {
return this.labels[label];
} else {
const gen = labelGenerator(length++, label);
this.labels[label] = gen;
return gen;
}
}
};
},
getFilename: function (path) {
return path.replace(/^(?:.*[\/\\])?((?:[^\/\\])*)$/, '$1');
},
StrArr: function(str) {
return {
arr: [...str],
offset: 0,
line: 1,
col: 1,
pos: function () {
return { line: this.line, col: this.col };
},
hasNext: function () {
return this.offset < this.arr.length;
},
getNext: function () {
const next = this.arr[this.offset++];
if (next === '\n') {
this.line++;
this.col = 1;
} else {
this.col++;
}
return next;
},
peek: function (seek) {
const offset = this.offset + (seek || 0);
return this.arr[offset];
}
};
}
};
})();