-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTreeNode.js
340 lines (333 loc) · 13.8 KB
/
TreeNode.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"use strict";
function LevenshtainDistance(A, B) {
// Will be used to find the closest label to the one that the user has.
// Adapted from:
// https://github.com/royalpranjal/Interview-Bit/blob/master/DynamicProgramming/EditDistance.cpp
const row = A.length;
const col = B.length;
const temp = [];
for (let i = 0; i < row + 1; i++) {
let tmp = [];
for (let j = 0; j < col + 1; j++) {
tmp.push(0);
}
temp.push(tmp);
}
const min =
(a, b) => {
if (a < b)
return a;
else
return b;
}
for (let i = 0; i < temp.length; i++) {
for (let j = 0; j < temp[0].length; j++) {
if (j == 0) {
temp[i][j] = i;
} else if (i == 0) {
temp[i][j] = j;
} else if (A[i - 1] == B[j - 1]) {
temp[i][j] = temp[i - 1][j - 1];
} else {
temp[i][j] = min(temp[i - 1][j - 1], temp[i - 1][j]);
temp[i][j] = min(temp[i][j - 1], temp[i][j]);
temp[i][j] = temp[i][j] + 1;
}
}
}
return temp[row][col];
}
class TreeNode {
constructor(text, lineNumber) {
this.text = text;
this.lineNumber = lineNumber;
this.children = [];
}
getLispExpression() {
if (!this.children.length)
return '"' + (this.text == "\n" ? "\\n" : this.text) + '"';
let ret = '("' + this.text + '" ';
for (let i = 0; i < this.children.length; i++)
if (i < this.children.length - 1)
ret += this.children[i].getLispExpression() + " ";
else
ret += this.children[i].getLispExpression() + ")";
return ret;
}
interpretAsArithmeticExpression(constants, labels) {
if (!(constants instanceof Map)) {
alert(
'Internal compiler error: The "constants" argument is not of the type "Map"!');
}
if (labels && !(labels instanceof Map)) {
alert(
'Internal compiler error: The "labels" argument is not of the type "Map"!');
}
if (labels && labels.has(this.text)) {
return labels.get(this.text);
}
if (constants.has(this.text))
return constants.get(this.text);
if (this.children.length != 2 &&
(this.text == "*" || this.text == "/" || this.text == "+" ||
this.text == "-" || this.text == "<" || this.text == "=" ||
this.text == ">" || this.text == "&" || this.text == "|")) {
alert("Line #" + this.lineNumber + ": The binary operator " + this.text +
" has less than two operands.");
return NaN;
}
if (this.text == "^")
return (this.children[0].interpretAsArithmeticExpression(constants) **
this.children[1].interpretAsArithmeticExpression(constants));
if (this.text == "*")
return (this.children[0].interpretAsArithmeticExpression(constants) *
this.children[1].interpretAsArithmeticExpression(constants));
if (this.text == "/")
return (this.children[0].interpretAsArithmeticExpression(constants) /
this.children[1].interpretAsArithmeticExpression(constants));
if (this.text == "+")
return (this.children[0].interpretAsArithmeticExpression(constants) +
this.children[1].interpretAsArithmeticExpression(constants));
if (this.text == "-")
return (this.children[0].interpretAsArithmeticExpression(constants) -
this.children[1].interpretAsArithmeticExpression(constants));
if (this.text == "<")
return (this.children[0].interpretAsArithmeticExpression(constants) <
this.children[1].interpretAsArithmeticExpression(constants));
if (this.text == ">")
return (this.children[0].interpretAsArithmeticExpression(constants) >
this.children[1].interpretAsArithmeticExpression(constants));
if (this.text == "=")
return (this.children[0].interpretAsArithmeticExpression(constants) ==
this.children[1].interpretAsArithmeticExpression(constants));
if (this.text == "&")
return (this.children[0].interpretAsArithmeticExpression(constants) &&
this.children[1].interpretAsArithmeticExpression(constants));
if (this.text == "|")
return (this.children[0].interpretAsArithmeticExpression(constants) ||
this.children[1].interpretAsArithmeticExpression(constants));
if (this.text == "?:")
return (
this.children[0].interpretAsArithmeticExpression(constants)
? this.children[1].interpretAsArithmeticExpression(constants,
labels)
: this.children[2].interpretAsArithmeticExpression(
constants,
labels)); // We are passing "labels" in an attempt to mend
// this bug:
// https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/38
if (this.text == "()") {
if (this.children.length != 1) {
alert("Line #" + this.lineNumber +
": The node '()' doesn't have exactly 1 child.");
return NaN;
}
return this.children[0].interpretAsArithmeticExpression(constants);
}
if (this.text == "invertBits()") {
if (this.children.length != 1) {
alert("Line #" + this.lineNumber +
": The node 'invertBits()' doesn't have exactly 1 child.");
return NaN;
}
return ~this.children[0].interpretAsArithmeticExpression(constants);
}
if (this.text == "bitand()") {
if (this.children.length != 3 || this.children[1].text != ',') {
alert(
"Line #" + this.lineNumber +
": The node 'bitand()' doesn't have exactly 3 children (a comma is also a child node).");
return NaN;
}
return this.children[0].interpretAsArithmeticExpression(constants) &
this.children[2].interpretAsArithmeticExpression(constants);
}
if (this.text == "bitor()") {
if (this.children.length != 3 || this.children[1].text != ',') {
alert(
"Line #" + this.lineNumber +
": The node 'bitor()' doesn't have exactly 3 children (a comma is also a child node).");
return NaN;
}
return this.children[0].interpretAsArithmeticExpression(constants) |
this.children[2].interpretAsArithmeticExpression(constants);
}
if (this.text == "mod()") {
if (this.children.length != 3 || this.children[1].text != ',') {
alert(
"Line #" + this.lineNumber +
": The node 'mod()' doesn't have exactly 3 children (a comma is also a child node).");
return NaN;
}
return this.children[0].interpretAsArithmeticExpression(constants) %
this.children[2].interpretAsArithmeticExpression(constants);
}
if (/\'d$/.test(this.text)) {
for (let i = 0; i < this.text.length - 2; i++)
if (this.text.charCodeAt(i) < '0'.charCodeAt(0) ||
this.text.charCodeAt(i) > '9'.charCodeAt(0)) {
alert(
"Line #" + this.lineNumber + ": `" + this.text +
"` is not a valid decimal constant!"); // https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/27
return NaN;
}
return parseInt(this.text.substring(
0,
this.text.length -
2)); // https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/30
}
if (/\'o$/.test(this.text)) {
for (let i = 0; i < this.text.length - 2; i++)
if (this.text.charCodeAt(i) < '0'.charCodeAt(0) ||
this.text.charCodeAt(i) > '7'.charCodeAt(0)) {
alert(
"Line #" + this.lineNumber + ": `" + this.text +
"` is not a valid octal constant!"); // https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/27
return NaN;
}
return parseInt(
this.text.substring(0, this.text.length - 2),
8); // https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/30
}
if (/\'b$/.test(this.text)) {
for (let i = 0; i < this.text.length - 2; i++)
if (!(this.text[i] == '0' || this.text[i] == '1')) {
alert("Line #" + this.lineNumber + ": `" + this.text +
"` is not a valid binary constant!");
return NaN;
}
return parseInt(
this.text.substring(0, this.text.length - 2),
2); // https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/30
}
if (/\'x$/.test(this.text)) {
if (!(/^([a-f]|[0-9])*\'x$/i.test(this.text))) {
alert("Line #" + this.lineNumber + ": `" + this.text +
"` is not a valid hexadecimal constant!");
return NaN;
}
return parseInt(
this.text.substring(0, this.text.length - 2),
16); // https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/30
}
if (/^([a-f]|[0-9])*$/i.test(this.text))
return parseInt(
this.text,
((typeof default_base_of_literals_in_assembly) == "undefined")
? 16
: default_base_of_literals_in_assembly);
if (this.text[0] === '"' && this.text.length === 3)
return this.text.charCodeAt(1);
let keys = [];
constants.forEach((value, key) => { keys.push(key); });
let smallest_Levenshtain_distance = keys[0];
for (const key of keys) {
if (LevenshtainDistance(key, this.text) <
LevenshtainDistance(smallest_Levenshtain_distance, this.text)) {
smallest_Levenshtain_distance = key;
}
}
if (confirm("Instead of \"" + this.text + "\", in the line #" +
this.lineNumber + ", did you perhaps mean \"" +
smallest_Levenshtain_distance + "\"?")) {
if (constants.has(smallest_Levenshtain_distance)) {
constants.set(this.text, constants.get(smallest_Levenshtain_distance));
return constants.get(this.text);
}
}
alert('Some part of the assembler tried to interpret the token "' +
this.text + '" in the line #' + this.lineNumber +
" as a part of an arithmetic expression, which makes no sense.");
return NaN;
}
checkTypes() {
// Let's check for inconsistencies that would be impossible in C++, but are
// possible in JavaScript.
if (typeof this.text !== "string") {
alert("Internal compiler error: For some token in the line #" +
this.lineNumber + ', the "text" property is not of type "string"');
return false;
}
if (!(this.children instanceof Array)) {
alert('Internal compiler error: The "children" property of the "' +
this.text + '" token in the line #' + this.lineNumber +
" is not an array!");
return false;
}
for (const child of this.children)
if (!(child instanceof TreeNode)) {
alert('Internal compiler error: The node "this.text" in the line #' +
this.lineNumber +
" has a child that is not an instance of TreeNode!");
return false;
}
return true;
}
getRegisterNumber(registers) {
if (registers.has(this.text))
return registers.get(this.text)
.substring(1)
.toLowerCase(); // https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/30
if (/^s(\d|[a-f])$/i.test(this.text))
return this.text.substring(1)
.toLowerCase(); // https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/30
// https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/37#issuecomment-2778246629
let register_name_with_Levenshtain_distance_of_one;
registers.forEach((value, key) => {
if (LevenshtainDistance(key, this.text) == 1)
register_name_with_Levenshtain_distance_of_one = key;
});
if (register_name_with_Levenshtain_distance_of_one) {
if (confirm(
"Instead of \"" + this.text + "\", in the line #" +
this.lineNumber + ", did you perhaps mean \"" +
register_name_with_Levenshtain_distance_of_one +
"\", an alternate name for the register \"" +
registers.get(register_name_with_Levenshtain_distance_of_one) +
"\"?")) {
registers.set(
this.text,
registers.get(register_name_with_Levenshtain_distance_of_one));
return registers.get(this.text).substring(1).toLowerCase();
}
}
return "none";
}
getLabelAddress(labels, constants) {
if (labels.has(this.text))
return formatAsAddress(labels.get(this.text));
if (constants.has(this.text))
return formatAsAddress(constants.get(this.text));
if (/^(\d|[a-f])*$/i.test(this.text) ||
[ "+", "-", "*", "/", "^", "?:" ].includes(this.text))
return formatAsAddress(this.interpretAsArithmeticExpression(
constants,
labels)); // We are passing the `labels` argument to resolve this bug:
// https://github.com/FlatAssembler/PicoBlaze_Simulator_in_JS/issues/38
if (this.text == "()")
return "none"; // Must not detect "()" as a label.
let keys = [];
labels.forEach((value, key) => { keys.push(key); });
constants.forEach((value, key) => { keys.push(key); });
let smallest_Levenshtain_distance = keys[0];
for (const key of keys) {
if (LevenshtainDistance(key, this.text) <
LevenshtainDistance(smallest_Levenshtain_distance, this.text)) {
smallest_Levenshtain_distance = key;
}
}
if (confirm("Instead of \"" + this.text + "\", in the line #" +
this.lineNumber + ", did you perhaps mean \"" +
smallest_Levenshtain_distance + "\"?")) {
if (labels.has(smallest_Levenshtain_distance)) {
labels.set(this.text, labels.get(smallest_Levenshtain_distance));
return formatAsAddress(labels.get(smallest_Levenshtain_distance));
}
if (constants.has(smallest_Levenshtain_distance)) {
constants.set(this.text, constants.get(smallest_Levenshtain_distance));
return formatAsAddress(constants.get(smallest_Levenshtain_distance));
}
}
return "none";
}
}