-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.js
More file actions
206 lines (165 loc) · 5.91 KB
/
interpreter.js
File metadata and controls
206 lines (165 loc) · 5.91 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
// code: A string containing dotcomma code
// input: A string or array of numbers, bigints, and/or strings
// string_out: Whether to output as a string of characters (true) or array of bigints (false), defaults to true if input is a string
function interpret(code, input = [], string_out = undefined) {
// Define useful function(s)
var to_id = function(char) {
return [1, 0, 0, -1]["[.,]".indexOf(char)];
};
// Validate and format input
switch (typeof input) {
case "number":
input = [BigInt(Math.trunc(input || 0))];
break;
case "bigint":
input = [input];
break;
case "string":
string_out = string_out === undefined ? true : string_out;
input = input.split("").map(c => BigInt(c.codePointAt(0))).reverse();
break;
case "boolean":
input = [input ? 1 : 0];
break;
default:
if (!Array.isArray(input))
throw "Invalid input";
input = [].concat.apply([], input.map((i, n) => {
switch (typeof i) {
case "number":
return [BigInt(Math.trunc(i || 0))];
case "bigint":
return [i];
case "string":
return i.split("").map(c => BigInt(c.codePointAt(0))).reverse();
case "boolean":
return [i ? 1 : 0];
default:
throw "Invalid input: input[" + n + "]";
}
})).reverse();
}
// Remove comments and detect unbalanced brackets
code = code.replace(/[^[.,\]]+/g, "");
{
let depth = 0;
for (let i = 0; i < code.length; i++) {
depth += to_id(code[i]);
if (depth < 0)
throw "Unbalanced brackets";
}
if (depth)
throw "Unbalanced brackets";
}
// Ensure code is wrapped in brackets
for (let d = 0, i = 0; i < code.length - 1; i++) {
d += to_id(code[i]);
if (!d) {
code = "[" + code + "]";
break;
}
}
// Interpret
var ptr = 0;
var blocks = [];
var last = 0n;
var loop = [];
var queue = [...input];
var parent = function() {
var result = blocks;
var next = result[result.length - 1];
while (Array.isArray(next[next.length - 1])) {
result = result[result.length - 1];
next = next[next.length - 1];
}
return result;
};
var block = function() {
var result = blocks;
while (Array.isArray(result[result.length - 1]))
result = result[result.length - 1];
return result;
};
var alternative = function(data) {
if (data === undefined || data === null)
return -1n;
return data;
};
while (ptr < code.length) {
switch (code[ptr]) {
case "[":
if (code[ptr - 1] == "," && last < 0) {
for (let d = 0, i = ptr; i < code.length; i++) {
d += to_id(code[i]);
if (!d) {
ptr = i;
break;
}
}
block().push(null);
last = 0n;
break;
}
if (code[ptr - 1] == "." && !last) {
for (let d = 0, i = ptr; i < code.length; i++) {
d += to_id(code[i]);
if (!d) {
ptr = i;
break;
}
}
block().push(null);
last = 0n;
break;
}
block().push([]);
loop.push(code[ptr - 1] == ".");
last = 0n;
break;
case ".":
if (code[ptr - 1] == "[")
last = 1n;
if (code[ptr - 1] == "]")
last = block().reduce((a, r) => a + (r || 0n), 0n);
block().length = 0;
break;
case ",":
if (code[ptr - 1] == "[")
last = alternative(queue.pop());
else if (code[ptr - 1] == "]")
last = alternative(block().pop());
block.length = 0;
if (code[ptr + 1] == "]" && last >= 0)
queue.unshift(last);
break;
case "]":
parent().pop();
block().push(last);
if (loop.pop()) {
if (last) {
for (let d = 0, i = ptr; i >= 0; i--) {
d += to_id(code[i]);
if (!d) {
ptr = i;
break;
}
}
block().push([]);
loop.push(true);
last = 0n;
break;
}
}
last = 0n;
break;
}
ptr += 1;
}
// Return output, by default formats as string if input was string
queue.reverse();
try {
return string_out ? queue.map(c => String.fromCodePoint(Number(BigInt.asIntN(32, c)))).join("") : queue;
} catch (e) {
return queue;
}
}