forked from FlatAssembler/AECforWebAssembly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semanticAnalyzer.cpp
414 lines (411 loc) · 18.2 KB
/
semanticAnalyzer.cpp
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Here we put the parts of the compiler that analyze the syntax tree,
* but do no assembly-related things. Stuff related to typing belongs
* here.
*/
#include "CorruptCompilationContextException.cpp"
#include "InvalidTypenameException.cpp"
#include "NotImplementedException.cpp"
#include "TreeNode.cpp"
#include <ciso646> // Necessary for Microsoft C++ Compiler.
std::string getStrongerType(const int lineNumber, const int columnNumber,
const std::string firstType,
const std::string secondType) {
if (firstType == "Nothing" or secondType == "Nothing") {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: Can't add, subtract, multiply or divide "
"with something of the type \"Nothing\"!"
<< std::endl;
exit(1);
}
if (firstType != secondType and
((!TreeNode::basicDataTypeSizes.count(firstType) and
!isPointerType(firstType)) or
(!TreeNode::basicDataTypeSizes.count(secondType) and
!isPointerType(secondType)))) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: Some part of the compiler tried to compare "
"two different structure types, \""
<< firstType << "\" and \"" << secondType
<< "\", for strength. You have presumably put two different "
"structure types as the second and the third operator of the "
"ternary conditional `?:` operator."
<< std::endl;
exit(1);
}
if (isPointerType(firstType) and !isPointerType(secondType))
return firstType;
if (isPointerType(secondType) and !isPointerType(firstType))
return secondType;
if (isPointerType(firstType) and isPointerType(secondType)) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: Can't add, multiply or divide two pointers!"
<< std::endl;
exit(1);
}
if (firstType == "Decimal64" or secondType == "Decimal64")
return "Decimal64";
if (firstType == "Decimal32" or secondType == "Decimal32")
return "Decimal32";
if (firstType == "Integer64" or secondType == "Integer64")
return "Integer64";
if (firstType == "Integer32" or secondType == "Integer32")
return "Integer32";
if (firstType == "Integer16" or secondType == "Integer16")
return "Integer16";
return firstType;
}
std::string TreeNode::getType(const CompilationContext &context) const {
if (text == "nan") // Not-a-number, to signal a numeric error.
return "Decimal32";
if (text == "asm(" and children.size() == 1)
return "Nothing";
if ((text == "asm_i32(" or text == "SizeOf(") and children.size() == 1)
return "Integer32";
if (text == "asm_i64(" and children.size() == 1)
return "Integer64";
if (text == "asm_f32(" and children.size() == 1)
return "Decimal32";
if (text == "asm_f64(" and children.size() == 1)
return "Decimal64";
if (text == "asm(" or text == "asm_i32(" or text == "asm_i64(" or
text == "asm_f32(" or text == "asm_f64(" or text == "SizeOf") {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: The inline assembly operator \"" << text
<< "\" has " << children.size()
<< " operands (it should have 1). Its AST is: "
<< getLispExpression() << std::endl;
std::exit(1);
}
if (isInteger(text))
return "Integer64";
if (isDecimalNumber(text))
return "Decimal64";
if (text == "AddressOf(") {
if (children.empty()) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: \"AddressOf\" is without the argument!"
<< std::endl;
exit(1);
}
if (children.size() > 1) {
std::cerr
<< "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: Can't take the address of multiple variables!"
<< std::endl;
exit(1);
}
if (children[0].getType(context) == "Nothing") {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: \"AddressOf\" has an argument of type "
"\"Nothing\"!"
<< std::endl;
exit(1);
}
return children[0].getType(context) + "Pointer";
}
if (text == "ValueAt(") {
if (children.empty()) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: \"ValueAt\" is without the argument!"
<< std::endl;
exit(1);
}
if (children.size() > 1) {
std::cerr
<< "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: Can't dereference multiple variables at once!"
<< std::endl;
exit(1);
}
if (!isPointerType(children[0].getType(context))) {
std::cerr
<< "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: The argument to \"ValueAt\" is not a pointer!"
<< std::endl;
exit(1);
}
return children[0].getType(context).substr(
0, children[0].getType(context).size() - std::string("Pointer").size());
}
if (context.variableTypes.count(text))
return context.variableTypes.at(text);
if (text[0] == '"') {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Internal compiler error: A pointer to the string " << text
<< " is being attempted to compile before the string itself has "
"been compiled, aborting the compilation!"
<< std::endl;
throw CorruptCompilationContextException(context);
}
if (text == "and" or text == "or" or text == "<" or text == ">" or
text == "=" or text == "<=" or text == ">=" or text == "not(" or
text == "invertBits(") {
if (children.empty()) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: The operator \"" << text
<< "\" has no operands. Aborting the compilation (or else we "
"will segfault)!"
<< std::endl;
exit(1);
}
if (children.size() < 2 and text != "not(" and text != "invertBits(") {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: The binary operator \"" << text
<< "\" has less than two operands. Aborting the compilation "
"(or else we will segfault)!"
<< std::endl;
exit(1);
}
return "Integer32"; // Because "if" and "br_if" in WebAssembly expect a
// "i32", so let's adapt to that.
}
if (text == "mod(") {
if (children.size() != 2) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: \"mod(\" operator requires two integer "
"arguments!"
<< std::endl;
exit(1);
}
if (isDecimalType(children[0].getType(context)) or
isDecimalType(children[1].getType(context))) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: Unfortunately, WebAssembly (unlike x86 "
"assembly) doesn't support computing remaining of division "
"of decimal numbers, so we can't support that either "
"outside of compile-time constants."
<< std::endl;
exit(1);
}
return getStrongerType(lineNumber, columnNumber,
children[0].getType(context),
children[1].getType(context));
}
if (text == "If" or text == "Then" or text == "Else" or text == "While" or
text == "Loop" or text == "Does" or
text == "Return") // Or else the compiler will claim those
// tokens are undeclared variables.
return "Nothing";
if (isValidVariableName(text)) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: The variable name \"" << text
<< "\" is not declared!" << std::endl;
auto most_similar_variable_iterator = std::max_element(
context.variableTypes.begin(), context.variableTypes.end(),
[=](const std::pair<std::string, std::string>
first_potentially_similar_variable,
const std::pair<std::string, std::string>
second_potentially_similar_variable) {
return
#ifndef USING_LEVENSTEIN_DISTANCE
longest_common_subsequence_length(
first_potentially_similar_variable.first, text) <
longest_common_subsequence_length(
second_potentially_similar_variable.first, text);
#else
Levenstein_distance(first_potentially_similar_variable.first,
text) >
Levenstein_distance(second_potentially_similar_variable.first,
text);
#endif
});
if (most_similar_variable_iterator != context.variableTypes.end() &&
longest_common_subsequence_length(most_similar_variable_iterator->first,
text)) {
std::cerr << "By the way, maybe you meant \""
<< most_similar_variable_iterator->first
<< "\", declared at the line "
<< (context.placesOfVariableDeclarations.count(
most_similar_variable_iterator->first)
? context.placesOfVariableDeclarations.at(
most_similar_variable_iterator->first)
: 0)
<< " and of the type \""
<< most_similar_variable_iterator->second << "\""
<< "? " << std::endl;
}
exit(1);
}
if (text == "+" or text == "*" or text == "/") {
if (children.size() != 2) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: The binary operator \"" << text
<< "\" doesn't have exactly two operands. Aborting the "
"compilation (or else we will segfault)!"
<< std::endl;
exit(1);
}
return getStrongerType(lineNumber, columnNumber,
children[0].getType(context),
children[1].getType(context));
}
if (text == "-") {
if (children.size() != 2) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: The binary operator \"" << text
<< "\" doesn't have exactly two operands. Aborting the "
"compilation (or else we will segfault)!"
<< std::endl;
exit(1);
}
if (isPointerType(children[0].getType(context)) and
isPointerType(children[1].getType(context)))
return "Integer32"; // Difference between pointers is an integer of the
// same size as the pointers (32-bit).
return getStrongerType(lineNumber, columnNumber,
children[0].getType(context),
children[1].getType(context));
}
if (text.size() == 2 and text[1] == '=' and text[0] != '<' and
text[0] != '>') // Assignment operators
{
if (children.size() < 2) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: The assignment operator \"" << text
<< "\" has less "
"than two operands. Aborting the compilation, or else the "
"compiler will segfault."
<< std::endl;
exit(1);
}
if (children[1].getType(context) == "Nothing") {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: Attempting to assign something of the "
"type \"Nothing\" to a variable. Aborting the compilation!"
<< std::endl;
}
return children[0].getType(context);
}
auto potentialFunction =
std::find_if(context.functions.begin(), context.functions.end(),
[=](function fn) { return fn.name == text; });
if (potentialFunction != context.functions.end()) {
#define ENABLE_NAMED_FUNCTION_ARGUMENTS
#ifndef ENABLE_NAMED_FUNCTION_ARGUMENTS
for (TreeNode child : children)
if (child.text == ":=")
throw NotImplementedException(
"Line " + std::to_string(child.lineNumber) + ", Column " +
std::to_string(child.columnNumber) +
": Sorry about that, but this compiler does "
"not yet support named function arguments!"); // TODO: Implement
// named function
// arguments.
#endif
return potentialFunction->returnType;
}
if (text.back() == '(' and
(basicDataTypeSizes.count(text.substr(0, text.size() - 1)) or
isPointerType(text.substr(0, text.size() - 1)))) // Casting
return text.substr(0, text.size() - 1);
if (text.back() == '(') {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: Function \"" << text
<< "\" is not declared!" << std::endl;
auto most_similar_function_iterator = std::max_element(
context.functions.begin(), context.functions.end(),
[=](const function first_potentially_similar_function,
const function second_potentially_similar_function) {
return
#ifndef USING_LEVENSTEIN_DISTANCE
longest_common_subsequence_length(
first_potentially_similar_function.name, text) <
longest_common_subsequence_length(
second_potentially_similar_function.name, text);
#else
Levenstein_distance(first_potentially_similar_function.name,
text) >
Levenstein_distance(second_potentially_similar_function.name,
text);
#endif
});
if (most_similar_function_iterator != context.functions.end() &&
longest_common_subsequence_length(most_similar_function_iterator->name,
text)) {
std::cerr << "By the way, maybe you meant \""
<< most_similar_function_iterator->name << "\"?" << std::endl;
}
exit(1);
}
if (text == "?:") {
if (isPointerType(children[1].getType(context)) and
isPointerType(children[2].getType(context)))
return children[1].getType(context);
else
return getStrongerType(lineNumber, columnNumber,
children[1].getType(context),
children[2].getType(context));
}
if (text == ".") { // The dot operator for accessing structure members.
if (children.size() != 2) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Compiler error: Corrupt AST, the \".\" node should have "
"2 children, but it has "
<< children.size() << ". Quitting now!" << std::endl;
exit(1);
}
std::string structureName = children[0].getType(context);
auto iteratorPointingToTheStructure =
std::find_if(context.structures.begin(), context.structures.end(),
[=](structure str) { return str.name == structureName; });
if (iteratorPointingToTheStructure == context.structures.end()) {
std::cerr << "Line " << children[0].lineNumber << ", Column "
<< children[0].columnNumber
<< ", Compiler error: The instance \"" << children[0].text
<< "\" has the type \"" << structureName
<< "\", which doesn't appear to be a structure name. Did you "
"perhaps use `.` instead of `->`? Quitting now!"
<< std::endl;
exit(1);
}
std::string memberName = children[1].text;
if (!iteratorPointingToTheStructure->memberTypes.count(children[1].text)) {
std::cerr << "Line " << children[1].lineNumber << ", Column "
<< children[1].columnNumber
<< ", Compiler error: The instance \"" << children[0].text
<< "\", of the structure named \""
<< iteratorPointingToTheStructure->name
<< "\", doesn't have a member named \"" << memberName
<< "\". Quitting now!" << std::endl;
auto most_similar_variable_iterator = std::max_element(
iteratorPointingToTheStructure->memberTypes.begin(),
iteratorPointingToTheStructure->memberTypes.end(),
[=](const std::pair<std::string, std::string>
first_potentially_similar_variable,
const std::pair<std::string, std::string>
second_potentially_similar_variable) {
return
#ifndef USING_LEVENSTEIN_DISTANCE
longest_common_subsequence_length(
first_potentially_similar_variable.first, memberName) <
longest_common_subsequence_length(
second_potentially_similar_variable.first, memberName);
#else
Levenstein_distance(first_potentially_similar_variable.first,
memberName) >
Levenstein_distance(second_potentially_similar_variable.first,
memberName);
#endif
});
if (most_similar_variable_iterator !=
iteratorPointingToTheStructure->memberTypes.end() &&
longest_common_subsequence_length(
most_similar_variable_iterator->first, memberName)) {
std::cerr << "By the way, maybe you meant \""
<< most_similar_variable_iterator->first << "\"?"
<< std::endl;
}
exit(1);
}
return iteratorPointingToTheStructure->memberTypes.at(memberName);
}
if (text == "->") {
TreeNode dotOperator(".", lineNumber, columnNumber);
TreeNode valueAtOperator("ValueAt(", lineNumber, columnNumber);
valueAtOperator.children.push_back(children[0]);
dotOperator.children = <% valueAtOperator, children[1] %>;
return dotOperator.getType(context);
}
return "Nothing";
}