forked from FlatAssembler/AECforWebAssembly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeNode.cpp
397 lines (390 loc) · 19.7 KB
/
TreeNode.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
/*
* "TreeNode" is supposed to basically be a composite, representing the
* tree structure (abstract syntax tree, AST) in such a way that other
* code doesn't have to deal with whether it's interacting with a leaf
* or some other node in a tree. It isn't a true composite because almost
* none of the operations that make sense for other nodes make sense for
* the root node.
*/
#include "AssemblyCode.cpp"
#include "bitManipulations.cpp"
#include "compilingContext.cpp"
#include "stringManipulationHelperFunctions.cpp"
#include <ciso646> // Necessary for Microsoft C++ Compiler.
#include <cmath>
#include <iostream>
#include <map>
#include <regex>
#include <set>
#include <string>
#include <vector>
#pragma once
class TreeNode {
enum class Associativity { left, right };
static std::vector<TreeNode>
applyBinaryOperators(std::vector<TreeNode> input,
std::vector<std::string> operators,
Associativity associativity);
protected:
std::set<std::string> getStringsInSubnodes() const {
auto setToBeReturned = std::set<std::string>();
if (text == "asm(" or text == "asm_i32(" or text == "asm_i64(" or
text == "asm_f32(" or
text == "asm_f64(") // Inline assembly isn't a string that can be
// manipulated, and storing it in memory wastes
// memory (potentially a lot of it).
return std::set<std::string>(); // That's why we will return an empty set,
// as if we had no strings in our subnodes
// (even though we have at least one).
if (text.size() and text[0] == '"') {
setToBeReturned.insert(text);
return setToBeReturned;
}
for (auto child : children) {
auto stringsInChild = child.getStringsInSubnodes();
setToBeReturned.insert(stringsInChild.begin(), stringsInChild.end());
}
return setToBeReturned;
}
public:
// HappySkeptic suggested me to try to make shared parts of TreeNode static to
// save on stack memory:
// https://atheistforums.org/thread-63150-post-2053689.html#pid2053689
static std::map<std::string, int> basicDataTypeSizes;
static std::map<std::string, AssemblyCode::AssemblyType>
mappingOfAECTypesToWebAssemblyTypes;
static std::map<AssemblyCode::AssemblyType, std::string>
stringRepresentationOfWebAssemblyType;
static std::set<std::string> AECkeywords;
static bool staticPropertiesInitialized;
std::vector<TreeNode> children;
std::string text;
int lineNumber, columnNumber;
TreeNode() {
if (!staticPropertiesInitialized) { // https://atheistforums.org/thread-63150-post-2055509.html#pid2055509
basicDataTypeSizes["Integer32"] = 4;
basicDataTypeSizes["Character"] = 1;
basicDataTypeSizes["Decimal32"] = 4;
basicDataTypeSizes["Integer64"] = 8;
basicDataTypeSizes["Decimal64"] = 8;
basicDataTypeSizes["Integer16"] = 2;
for (auto iterator = basicDataTypeSizes.begin();
iterator != basicDataTypeSizes.end(); iterator++)
if (iterator->first.find("Pointer") == std::string::npos)
basicDataTypeSizes[iterator->first + "Pointer"] =
4; // JavaScript (WebAssembly) virtual machine is 32-bit (pointers
// being 32 bits or 4 bytes long), unless somebody switches to
// the 64-bit mode (which is almost never done).
basicDataTypeSizes["Nothing"] = 0;
mappingOfAECTypesToWebAssemblyTypes =
std::map<std::string, AssemblyCode::AssemblyType>(
{{"Integer32", AssemblyCode::AssemblyType::i32},
{"Integer64", AssemblyCode::AssemblyType::i64},
{"Decimal32", AssemblyCode::AssemblyType::f32},
{"Decimal64", AssemblyCode::AssemblyType::f64},
{"Nothing", AssemblyCode::AssemblyType::null}});
for (auto pair : basicDataTypeSizes)
if (!mappingOfAECTypesToWebAssemblyTypes.count(pair.first))
mappingOfAECTypesToWebAssemblyTypes[pair.first] =
AssemblyCode::AssemblyType::i32;
AECkeywords = std::set<std::string>({"Function", "Which",
"Returns", "Nothing",
"Is", "External",
"Does", "EndFunction",
"If", "Then",
"ElseIf", "Else",
"EndIf", "While",
"Loop", "EndWhile",
"Structure", "Consists",
"Of", "EndStructure",
"Character", "CharacterPointer",
"Integer16", "Integer16Pointer",
"Integer32", "Integer32Pointer",
"Integer64", "Integer64Pointer",
"Decimal32", "Decimal32Pointer",
"Decimal64", "Decimal64Pointer"});
stringRepresentationOfWebAssemblyType =
std::map<AssemblyCode::AssemblyType, std::string>(
{{AssemblyCode::AssemblyType::i32, "i32"},
{AssemblyCode::AssemblyType::i64, "i64"},
{AssemblyCode::AssemblyType::f32, "f32"},
{AssemblyCode::AssemblyType::f64, "f64"},
{AssemblyCode::AssemblyType::null, "null"}});
staticPropertiesInitialized = true;
}
lineNumber = columnNumber = 0;
}
TreeNode(std::string newText, int newLine, int newColumn) {
*this = TreeNode(); // For some weird reason, just "TreeNode()" won't do the
// trick.
text = newText;
lineNumber = newLine;
columnNumber = newColumn;
}
static std::vector<TreeNode>
tokenize(std::string input); // See "tokenizer.cpp" for the implementation.
static std::string JSONifyArrayOfTokens(
std::vector<TreeNode> tokenizedString) { // For debugging the tokenizer.
std::string ret = "[";
if (!tokenizedString.size())
return "[]";
for (unsigned int i = 0; i < tokenizedString.size(); i++)
if (i != tokenizedString.size() - 1)
ret += "'" + tokenizedString[i].text + "',";
else
ret += "'" + tokenizedString[i].text + "']";
return ret;
}
static std::vector<TreeNode> parse(
std::vector<TreeNode> input); // See "parser.cpp" for the implementation.
static std::vector<TreeNode> parseExpression(
std::vector<TreeNode> input); // Made public for debugging purposes.
std::string getLispExpression()
const { // Again, for debugging purposes (and maybe, some day, I will want
// to compile my language to Lisp).
if (children.size() == 0)
return text;
std::string LispExpression = "(" +
((text.back() == '(' or text.back() == '[')
? (text.substr(0, text.size() - 1))
: (text)) +
" ";
for (unsigned int i = 0; i < children.size(); i++)
if (i == children.size() - 1)
LispExpression += children[i].getLispExpression() + ")";
else
LispExpression += children[i].getLispExpression() + " ";
return LispExpression;
}
virtual int interpretAsACompileTimeIntegerConstant() const {
if ((text == "<" or text == ">" or text == "<=" or text == ">=") and
children.at(0).text == text) { // Chained comparisons (`a < b < c`).
TreeNode andNode("and", lineNumber, columnNumber),
secondChild(text, lineNumber, columnNumber);
secondChild.children = {children.at(0).children.at(1), children.at(1)};
andNode.children = {children.at(0), secondChild};
return andNode.interpretAsACompileTimeIntegerConstant();
}
if (isInteger(text))
return std::stoi(text, 0, 0);
if (text == "+" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() +
children[1].interpretAsACompileTimeIntegerConstant();
if (text == "-" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() -
children[1].interpretAsACompileTimeIntegerConstant();
if (text == "*" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() *
children[1].interpretAsACompileTimeIntegerConstant();
if (text == "/" and children.size() == 2) {
if (children[1].interpretAsACompileTimeIntegerConstant() == 0) {
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Interpreter error: Division by zero in a compile-time "
"integer constant!"
<< std::endl;
std::exit(1);
}
return children[0].interpretAsACompileTimeIntegerConstant() /
children[1].interpretAsACompileTimeIntegerConstant();
}
if (text == "and" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() bitand
children[1].interpretAsACompileTimeIntegerConstant();
if (text == "or" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() bitor
children[1].interpretAsACompileTimeIntegerConstant();
if (text == "?:" and children.size() == 3)
return children[0].interpretAsACompileTimeIntegerConstant()
? children[1].interpretAsACompileTimeIntegerConstant()
: children[2].interpretAsACompileTimeIntegerConstant();
if (text == "mod(" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() %
children[1].interpretAsACompileTimeIntegerConstant();
if (text == "<" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() <
children[1].interpretAsACompileTimeIntegerConstant();
if (text == ">" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() >
children[1].interpretAsACompileTimeIntegerConstant();
if (text == "=" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() ==
children[1].interpretAsACompileTimeIntegerConstant();
if (text == "<=" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() <=
children[1].interpretAsACompileTimeIntegerConstant();
if (text == ">=" and children.size() == 2)
return children[0].interpretAsACompileTimeIntegerConstant() >=
children[1].interpretAsACompileTimeIntegerConstant();
if (text == "not(" and children.size() == 1)
return not(children[0].interpretAsACompileTimeIntegerConstant());
std::cerr << "Line " << lineNumber << ", Column " << columnNumber
<< ", Interpreter error: \"" << text
<< "\" isn't a valid token in a compile-time integer constant."
<< std::endl;
return 0;
}
virtual double interpretAsACompileTimeDecimalConstant() const {
if ((text == "<" or text == ">" or text == "<=" or text == ">=") and
children.at(0).text == text) { // Chained comparisons (`a < b < c`).
TreeNode andNode("and", lineNumber, columnNumber),
secondChild(text, lineNumber, columnNumber);
secondChild.children = {children.at(0).children.at(1), children.at(1)};
andNode.children = {children.at(0), secondChild};
return andNode.interpretAsACompileTimeDecimalConstant();
}
if (isInteger(text))
return std::stoi(text, 0, 0);
if (isDecimalNumber(text))
return std::stod(text);
if (text == "+" and children.size() == 2)
return children[0].interpretAsACompileTimeDecimalConstant() +
children[1].interpretAsACompileTimeDecimalConstant();
if (text == "-" and children.size() == 2)
return children[0].interpretAsACompileTimeDecimalConstant() -
children[1].interpretAsACompileTimeDecimalConstant();
if (text == "*" and children.size() == 2)
return children[0].interpretAsACompileTimeDecimalConstant() *
children[1].interpretAsACompileTimeDecimalConstant();
if (text == "/" and children.size() == 2)
return children[0]
.interpretAsACompileTimeDecimalConstant() / // Dividing a real
// number by zero
// in C++ won't
// crash a program.
// I don't know
// what the
// standard says,
// but, in my
// experience, it
// results in
// putting some
// special value
// (NaN or inf)
// into the result.
children[1].interpretAsACompileTimeDecimalConstant();
if (text == "and" and children.size() == 2)
return children[0].interpretAsACompileTimeDecimalConstant() and
children[1].interpretAsACompileTimeDecimalConstant();
if (text == "or" and children.size() == 2)
return children[0].interpretAsACompileTimeDecimalConstant() or
children[1].interpretAsACompileTimeDecimalConstant();
if (text == "?:" and children.size() == 3)
return children[0].interpretAsACompileTimeDecimalConstant()
? children[1].interpretAsACompileTimeDecimalConstant()
: children[2].interpretAsACompileTimeDecimalConstant();
if (text == "mod(" and
children.size() ==
2) // Let's give the users of the language the access to standard
// library functions when writing compile-time decimal constants.
return fmod(children[0].interpretAsACompileTimeDecimalConstant(),
children[1].interpretAsACompileTimeDecimalConstant());
if (text == "sin(" and children.size() == 1)
return sin(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "cos(" and children.size() == 1)
return cos(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "tan(" and children.size() == 1)
return tan(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "sqrt(" and children.size() == 1)
return sqrt(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "atan(" and children.size() == 1)
return atan(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "atan2(" and children.size() == 2)
return atan2(children[0].interpretAsACompileTimeDecimalConstant(),
children[1].interpretAsACompileTimeDecimalConstant());
if (text == "asin(" and children.size() == 1)
return asin(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "acos(" and children.size() == 1)
return acos(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "log(" and children.size() == 1)
return log(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "log2(" and children.size() == 1)
return log2(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "log10(" and children.size() == 1)
return log10(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "exp(" and children.size() == 1)
return exp(children[0].interpretAsACompileTimeDecimalConstant());
if (text == "pow(" and children.size() == 2)
return pow(children[0].interpretAsACompileTimeDecimalConstant(),
children[1].interpretAsACompileTimeDecimalConstant());
if (text == "pi")
#ifndef M_PI // Microsoft C++ Compiler (I haven't tested this code in it,
// but I suppose it will reject my code without this).
#define M_PI (atan(1) * 4)
#endif
return M_PI;
if (text == "e")
return exp(1.); // That seems to be the only way to get 'e' on
// GCC 7 on Solaris 11.
if (text == "<" and children.size() == 2)
return children[0].interpretAsACompileTimeDecimalConstant() <
children[1].interpretAsACompileTimeDecimalConstant();
if (text == ">" and children.size() == 2)
return children[0].interpretAsACompileTimeDecimalConstant() >
children[1].interpretAsACompileTimeDecimalConstant();
if (text == "=" and children.size() == 2)
return children[0].interpretAsACompileTimeDecimalConstant() ==
children[1].interpretAsACompileTimeDecimalConstant();
if (text == "<=" and children.size() == 2)
return children[0].interpretAsACompileTimeDecimalConstant() <=
children[1].interpretAsACompileTimeDecimalConstant();
if (text == ">=" and children.size() == 2)
return children[0].interpretAsACompileTimeDecimalConstant() >=
children[1].interpretAsACompileTimeDecimalConstant();
std::cerr
<< "Line " << lineNumber << ", Column " << columnNumber
<< ", Interpreter error: \"" << text
<< "\" isn't a valid token in a compile-time decimal-number constant."
<< std::endl;
return 0;
}
static std::vector<TreeNode>
parseVariableDeclaration(std::vector<TreeNode> input);
virtual AssemblyCode compile(CompilationContext context) const;
virtual AssemblyCode compileAPointer(const CompilationContext &context) const;
virtual std::string
getType(const CompilationContext &context) const; // Integer32...
virtual ~TreeNode() =
default; // https://discord.com/channels/172018499005317120/172018499005317120/809830734256406569
};
std::string convertInlineAssemblyToAssembly(TreeNode inlineAssemblyNode) {
std::string inlineAssembly = inlineAssemblyNode.text;
if (inlineAssembly.front() != '"' or inlineAssembly.back() != '"') {
std::cerr << "Line " << inlineAssemblyNode.lineNumber << ", Column "
<< inlineAssemblyNode.columnNumber
<< ", Compiler error: Inline assembly doesn't appear to be a "
"string. Aborting the compilation (or we will produce "
"syntactically incorrect assembly)."
<< std::endl;
std::exit(1);
}
inlineAssembly = ";;Inline assembly begins.\n" +
inlineAssembly.substr(1, inlineAssembly.size() - 2) +
"\n;;Inline assembly ends.";
// CLANG 10 (but not GCC 9.3.0) appears to miscompile "regex_replace" on
// Oracle Linux 7.
std::string temporaryString;
for (unsigned int i = 0; i < inlineAssembly.size(); i++)
if (inlineAssembly.substr(i, 2) == R"(\\)") {
temporaryString += '\\';
i++;
} else if (inlineAssembly.substr(i, 2) == R"(\n)") {
temporaryString += '\n';
i++;
} else if (inlineAssembly.substr(i, 2) == R"(\t)") {
temporaryString += '\t';
i++;
} else if (inlineAssembly.substr(i, 2) == R"(\")") {
temporaryString += '"';
i++;
} else
temporaryString += inlineAssembly[i];
inlineAssembly = temporaryString;
return inlineAssembly;
}
std::map<std::string, int> TreeNode::basicDataTypeSizes;
std::map<std::string, AssemblyCode::AssemblyType>
TreeNode::mappingOfAECTypesToWebAssemblyTypes;
std::map<AssemblyCode::AssemblyType, std::string>
TreeNode::stringRepresentationOfWebAssemblyType;
std::set<std::string> TreeNode::AECkeywords;
bool TreeNode::staticPropertiesInitialized = false;