-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTreeNode.js
220 lines (220 loc) · 9.41 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
"use strict";
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) {
if (!(constants instanceof Map)) {
alert(
'Internal compiler error: The "constants" argument is not of the type "Map"!');
}
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)
: this.children[2].interpretAsArithmeticExpression(constants));
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);
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
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))
// Must not detect "()" as a label.
return formatAsAddress(this.interpretAsArithmeticExpression(constants));
return "none";
}
}