-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast-parser.hpp
388 lines (351 loc) · 12 KB
/
ast-parser.hpp
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
/*
* MIT License
*
* Copyright(c) 2018 Paul Bernitz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files(the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <iostream>
#include <cstdlib>
#include <exception>
#include <array>
#include <numeric>
#include "token-parser.hpp"
namespace c8s
{
// Different types of AST-nodes.
enum class ASTNodeType
{
Program, // Root node of the AST.
EndOfProgram, // Must be the last statement in the program.
Error, // Gets inserted to show where an error happened.
Deletable, // Nodes that are `Deletable` should be removed.
Raw, // raw 6001.
Statement, // A single statement.
Operator, // E.g: ==, +=, + ...
VarDeclaration, // var XYZ
VarExpression, // X += Y
IfStatement, // if A==B:
EndifStatement, // End marker for if-statement bodies.
ForLoop, // for I=0 to 10:
To, // 0 to 10.
Step, // 0 to 10 step 1
EndforLoop, // End marker for for-loop bodies.
Identifier, // Name of a (valid) variable.
NumberLiteral, // Any number (1,2,3 ...)
FunctionCall, // cls()
OpenBrace, // (
ClosingBrace // )
};
// List of all supported operators.
const std::array<std::string, 11> valid_operators = {
":", "=", "==", "!=", "+=", "-=", "<<=", ">>=", "|=", "&=", "^="
};
// A node in the abstract syntax tree.
struct ASTNode
{
ASTNode(ASTNodeType type, std::string value, unsigned line_number, std::vector<ASTNode> params)
: type{ type }, value{ value }, line_number{ line_number }, params{ params } {}
ASTNodeType type;
std::string value;
unsigned line_number;
std::vector<ASTNode> params;
};
// Remove every node from the AST that is marked as `Deletable`.
void remove_deletables(ASTNode& ast)
{
ast.params.erase(std::remove_if(ast.params.begin(), ast.params.end(),
[](const ASTNode& node) { return node.type == ASTNodeType::Deletable; }),
ast.params.end()
);
}
// Create a new node of a given type and continue `walking` the tree.
template<typename T>
auto create_node_and_walk(ASTNodeType node_type, Token tok, std::vector<Token>::iterator &cursor, T walk)
{
ASTNode node = ASTNode{ node_type, tok.value, tok.line_number, {} };
if ((++cursor)->type != TokenType::ClosingStatement)
node.params.push_back(walk(cursor, node));
return node;
}
// Walk the token list.
ASTNode walk(std::vector<Token>::iterator &cursor, const ASTNode& parent)
{
const Token &tok = *cursor;
if (parent.type == ASTNodeType::Statement)
{
if (tok.type == TokenType::Var)
{
ASTNode var_decl = ASTNode{ ASTNodeType::VarDeclaration, (++cursor)->value, tok.line_number, {} };
var_decl.params.push_back(walk(++cursor, var_decl));
return var_decl;
}
if (tok.type == TokenType::Identifier)
{
ASTNode var_expr = ASTNode{ ASTNodeType::VarExpression, tok.value, tok.line_number, {} };
var_expr.params.push_back(walk(++cursor, var_expr));
return var_expr;
}
if (tok.type == TokenType::If)
{
return create_node_and_walk(ASTNodeType::IfStatement, tok, cursor, walk);
}
if (tok.type == TokenType::For)
{
return create_node_and_walk(ASTNodeType::ForLoop, tok, cursor, walk);
}
if (tok.type == TokenType::Raw)
{
ASTNode raw_expr{ ASTNodeType::Raw, tok.value, tok.line_number, {} };
raw_expr.params.push_back(walk(++cursor, raw_expr));
return raw_expr;
}
if (tok.type == TokenType::FunctionCall)
{
return create_node_and_walk(ASTNodeType::FunctionCall, tok, cursor, walk);
}
}
else if (parent.type == ASTNodeType::Identifier)
{
if (tok.type == TokenType::Operator)
{
return create_node_and_walk(ASTNodeType::Operator, tok, cursor, walk);
}
if (tok.type == TokenType::To)
{
return create_node_and_walk(ASTNodeType::To, tok, cursor, walk);
}
if (tok.type == TokenType::Colon)
{
return create_node_and_walk(ASTNodeType::Operator, tok, cursor, walk);
}
}
else if (parent.type == ASTNodeType::FunctionCall)
{
if (tok.type == TokenType::OpenBrace)
{
return create_node_and_walk(ASTNodeType::OpenBrace, tok, cursor, walk);
}
}
else if (parent.type == ASTNodeType::Operator)
{
if (tok.type == TokenType::Identifier)
{
return create_node_and_walk(ASTNodeType::Identifier, tok, cursor, walk);
}
if (tok.type == TokenType::Numerical)
{
return create_node_and_walk(ASTNodeType::NumberLiteral, tok, cursor, walk);
}
}
else if (parent.type == ASTNodeType::VarDeclaration)
{
if (tok.type == TokenType::Identifier)
{
return create_node_and_walk(ASTNodeType::Identifier, tok, cursor, walk);
}
if (tok.type == TokenType::Operator)
{
return create_node_and_walk(ASTNodeType::Operator, tok, cursor, walk);
}
}
else if (parent.type == ASTNodeType::VarExpression)
{
if (tok.type == TokenType::Operator)
{
return create_node_and_walk(ASTNodeType::Operator, tok, cursor, walk);
}
}
else if (parent.type == ASTNodeType::IfStatement)
{
if (tok.type == TokenType::Identifier)
{
return create_node_and_walk(ASTNodeType::Identifier, tok, cursor, walk);
}
// TODO if a==b: does not work => FIXED BY changing faulty opcode 5XY4 to 5XY0.
//return create_node_and_walk(ASTNodeType::IfStatement, tok, cursor, walk);
}
else if (parent.type == ASTNodeType::ForLoop)
{
if (tok.type == TokenType::Identifier)
{
return create_node_and_walk(ASTNodeType::Identifier, tok, cursor, walk);
}
//return create_node_and_walk(ASTNodeType::ForLoop, tok, cursor, walk);
}
else if (parent.type == ASTNodeType::NumberLiteral)
{
if (tok.type == TokenType::To)
{
return create_node_and_walk(ASTNodeType::To, tok, cursor, walk);
}
if (tok.type == TokenType::Step)
{
return create_node_and_walk(ASTNodeType::Step, tok, cursor, walk);
}
if (tok.type == TokenType::Colon)
{
return create_node_and_walk(ASTNodeType::Operator, tok, cursor, walk);
}
}
else if (parent.type == ASTNodeType::To)
{
if (tok.type == TokenType::Numerical)
{
return create_node_and_walk(ASTNodeType::NumberLiteral, tok, cursor, walk);
}
}
else if (parent.type == ASTNodeType::Step)
{
if (tok.type == TokenType::Numerical)
{
return create_node_and_walk(ASTNodeType::NumberLiteral, tok, cursor, walk);
}
}
else if (parent.type == ASTNodeType::Raw)
{
if (tok.type == TokenType::Numerical)
{
++cursor;
return ASTNode{ ASTNodeType::NumberLiteral, tok.value, tok.line_number, {} };
}
}
if (tok.type == TokenType::Endif)
{
++cursor;
return ASTNode{ ASTNodeType::EndifStatement, tok.value, tok.line_number, {} };
}
if (tok.type == TokenType::Endfor)
{
++cursor;
return ASTNode{ ASTNodeType::EndforLoop, tok.value, tok.line_number, {} };
}
if (tok.type == TokenType::ClosingBrace)
{
++cursor;
return ASTNode{ ASTNodeType::ClosingBrace, tok.value, tok.line_number, {} };
}
if (tok.type == TokenType::Numerical)
{
++cursor;
return ASTNode{ ASTNodeType::NumberLiteral, tok.value, tok.line_number, {} };
}
if (tok.type == TokenType::EndOfProgram)
{
++cursor;
return ASTNode{ ASTNodeType::EndOfProgram, "end", tok.line_number, {} };
}
compiler_log::write_error("Syntax error on line " + std::to_string(tok.line_number));
++cursor;
return ASTNode{ ASTNodeType::Error, "error", tok.line_number, {} };
}
// The `bodies` of if-statements and for-loops is moved into the `params` of
// the parent.
bool move_bodies_to_params(ASTNode& ast)
{
for (;;)
{
// Find the last/innermost statement node that does not yet have more than one parent.
unsigned innermost_stmt_index = 0;
ASTNodeType from_type = ASTNodeType::Error;
ASTNodeType to_type = ASTNodeType::Error;
for (unsigned i = 0; i < ast.params.size(); ++i)
{
const auto& params = ast.params[i].params;
const auto type = params.front().type;
if ((type == ASTNodeType::IfStatement || type == ASTNodeType::ForLoop) && params.size() <= 1)
{
innermost_stmt_index = i;
from_type = type;
to_type = (type == ASTNodeType::ForLoop) ? ASTNodeType::EndforLoop : ASTNodeType::EndifStatement;
}
}
// Finish the loop if no nodes are left.
if (from_type == ASTNodeType::Error || to_type == ASTNodeType::Error)
break;
// Log error and return false if the body of the condition is empty.
if (ast.params[innermost_stmt_index + 1].params.front().type == to_type)
{
compiler_log::write_error("Bodies of if-statements can not be empty!");
return false;
}
// Push the nodes that follow onto the statement's `params` until `to_type` is reached.
for (unsigned i = innermost_stmt_index + 1; ast.params[i].params.front().type != to_type; ++i)
{
// Copy the node over to the params of the statement.
ast.params[innermost_stmt_index].params.push_back(ast.params[i]);
// Mark the old node to be deleted.
ast.params[i].type = ASTNodeType::Deletable;
ast.params[i].value = "del";
ast.params[i].params.clear();
// Remove the `to_type`-node.
if (ast.params[i + 1].params.front().type == to_type)
{
ast.params[innermost_stmt_index].params.push_back(ast.params[i + 1]);
ast.params.erase(ast.params.begin() + i + 1);
break;
}
}
// Clean up nodes that were marked as `deletable`.
remove_deletables(ast);
}
return true;
}
// Parse the list of tokens into an AST.
ASTNode parse_tokens_to_ast(std::vector<Token> &token_list)
{
if (token_list.size() == 0 || compiler_log::read_errors().size() > 0)
{
return ASTNode{ ASTNodeType::Error, "error", 0, {} };
}
// Check for missing endif/endfor statements.
auto open_if_for = std::count_if(token_list.begin(), token_list.end(), [](Token t) {
return t.type == TokenType::If || t.type == TokenType::For;
});
auto closed_if_for = std::count_if(token_list.begin(), token_list.end(), [](Token t) {
return t.type == TokenType::Endif || t.type == TokenType::Endfor;
});
if (open_if_for != closed_if_for)
{
compiler_log::write_error("Missing endif/endfor\n");
return ASTNode{ ASTNodeType::Error, "error", 0,{} };
}
auto cursor = token_list.begin();
auto ast = ASTNode{ ASTNodeType::Program, "", 0, {} };
while (cursor != token_list.end())
{
// Skip closing-statements.
if (cursor->type == TokenType::ClosingStatement)
{
++cursor;
continue;
}
// Add statements and recursively call `walk()` on their parameters.
ASTNode stmt = ASTNode{ ASTNodeType::Statement, "stmt", cursor->line_number, {} };
stmt.params.push_back(walk(cursor, stmt));
ast.params.push_back(stmt);
}
move_bodies_to_params(ast);
// Check for empty if-bodies.
if (!move_bodies_to_params(ast) || compiler_log::read_errors().size() != 0)
return ASTNode{ ASTNodeType::Error, "error", 0, {} };
return ast;
}
}