Repository navigation
Expand file tree
/
Copy pathparser.cpp
More file actions
405 lines (361 loc) · 11.5 KB
/
Copy pathparser.cpp
File metadata and controls
405 lines (361 loc) · 11.5 KB
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
//
// Created by Oleksander Krainiak on 12.09.2026.
//
#include "parser.h"
namespace Parser {
static Log::Logger logger{"Parser"};
Lexer::Token Parser::Current() const {
// if (!m_Current) logger.Log<Log::Level::Error>("Empty Source");
return m_Current;
}
Lexer::Token Parser::Peek() const {
// if (!m_Peek) logger.Log<Log::Level::Error>("Empty Source");
return m_Peek;
}
Lexer::Token Parser::Advance() {
m_Current = m_Peek;
m_Peek = m_Lexer.Next();
return Current();
}
bool Parser::MatchCurrentToken(Lexer::TokenType t) {
if (auto cur = Current(); cur.Type == t) {
Advance();
return true;
}
return false;
}
void Parser::AssertCurrentToken(Lexer::TokenType t) const {
if (auto cur = Current(); cur.Type == t) {
return;
}
logger.Log<Log::Level::Error>("Expected a token of {} type, a {} found (on line {})",
static_cast<int>(t),
static_cast<int>(Current().Type),
Current().Line
);
}
void Parser::AssertPeekToken(Lexer::TokenType t) const {
if (auto cur = Peek(); cur.Type == t) {
return;
}
logger.Log<Log::Level::Error>("Expected a token of {} type, a {} found",
static_cast<int>(t),
static_cast<int>(Peek().Type)
);
}
bool Parser::MatchPeekToken(Lexer::TokenType t) {
if (auto peek = Peek(); peek.Type == t) {
Advance();
return true;
}
return false;
}
}
ast::Precedence precedenceOf(Lexer::Token t) {
switch (t.Type) {
case Lexer::TokenType::plus:
case Lexer::TokenType::minus:
return ast::Precedence::add;
case Lexer::TokenType::asterisk:
case Lexer::TokenType::percent:
case Lexer::TokenType::slash:
case Lexer::TokenType::lShift:
case Lexer::TokenType::rShift:
case Lexer::TokenType::caret:
case Lexer::TokenType::pipe:
case Lexer::TokenType::amp:
return ast::Precedence::mul;
case Lexer::TokenType::cmp:
return ast::Precedence::cmp;
default: return ast::Precedence::lowest;
}
}
ast::Precedence Parser::Parser::peekPrecedence() const {
return precedenceOf(Peek());
}
void Parser::Parser::runInstructionFromTokenType(Lexer::TokenType t) {
switch (t) {
case Lexer::TokenType::plus:
vm.AddInstruction(vm::ByteCodeType::Add);
break;
case Lexer::TokenType::minus:
vm.AddInstruction(vm::ByteCodeType::Minus);
break;
case Lexer::TokenType::asterisk:
vm.AddInstruction(vm::ByteCodeType::Multiply);
break;
case Lexer::TokenType::slash:
vm.AddInstruction(vm::ByteCodeType::Div);
break;
case Lexer::TokenType::percent:
vm.AddInstruction(vm::ByteCodeType::Modulo);
break;
case Lexer::TokenType::caret:
vm.AddInstruction(vm::ByteCodeType::Bitxor);
break;
case Lexer::TokenType::pipe:
vm.AddInstruction(vm::ByteCodeType::Bitor);
break;
case Lexer::TokenType::amp:
vm.AddInstruction(vm::ByteCodeType::Bitand);
break;
case Lexer::TokenType::lShift:
vm.AddInstruction(vm::ByteCodeType::Lshift);
break;
case Lexer::TokenType::rShift:
vm.AddInstruction(vm::ByteCodeType::Rshift);
break;
case Lexer::TokenType::cmp:
vm.AddInstruction(vm::ByteCodeType::Cmp);
break;
default:
}
}
void Parser::Parser::parseExpression(ast::Precedence precedence) {
prefixParse();
while (precedence < peekPrecedence()) {
Advance();
auto op = Current();
auto prec = precedenceOf(op);
Advance();
parseExpression(prec);
runInstructionFromTokenType(op.Type);
}
}
void Parser::Parser::prefixParse() {
switch (Current().Type) {
case Lexer::TokenType::id:
vm.AddInstruction({
.Type = vm::ByteCodeType::LoadMem,
.LoadedData = std::make_unique<vm::UintData>(AddVar(Current().Literal)),
});
break;
case Lexer::TokenType::number: {
vm::BigInt num(Current().Literal);
vm.AddInstruction({
.Type = vm::ByteCodeType::LoadConst,
.LoadedData = std::make_unique<vm::BigIntData>(num),
});
}
break;
case Lexer::TokenType::bitNot: {
Advance();
parseExpression(ast::Precedence::prefix);
vm.AddInstruction(vm::ByteCodeType::Bitnot);
}
break;
case Lexer::TokenType::minus: {
Advance();
parseExpression(ast::Precedence::prefix);
vm.AddInstruction({
.Type = vm::ByteCodeType::LoadConst,
.LoadedData = std::make_unique<vm::BigIntData>(-1),
});
vm.AddInstruction(vm::ByteCodeType::Multiply);
}
break;
case Lexer::TokenType::lPar: {
Advance();
parseExpression();
AssertPeekToken(Lexer::TokenType::rPar);
Advance();
}
break;
case Lexer::TokenType::newline:
Advance();
prefixParse();
break;
default:
logger.Log<Log::Level::Error>("Unknown Expression");
}
}
uint64_t Parser::Parser::AddLabel(std::string_view name) {
if (labels.contains(name)) return labels.at(name);
return labels[name] = labels.size();
}
uint64_t Parser::Parser::AddVar(std::string_view name) {
if (vars.contains(name)) return vars.at(name);
return vars[name] = vars.size();
}
void Parser::Parser::parseGoto() {
AssertCurrentToken(Lexer::TokenType::Goto);
Advance();
auto tok = Current();
if (tok.Type != Lexer::TokenType::id) {
logger.Log<Log::Level::Error>(
"Goto to a weird label: `{}`. must be either an id or a number", tok.Literal);
}
auto labelID = AddLabel(tok.Literal);
vm.AddInstruction({
.Type = vm::ByteCodeType::Goto,
.LoadedData = std::make_unique<vm::UintData>(labelID),
});
}
void Parser::Parser::parseAssign() {
AssertCurrentToken(Lexer::TokenType::id);
auto name = Current();
auto nameID = AddVar(name.Literal);
Advance();
AssertCurrentToken(Lexer::TokenType::equals);
Advance();
parseExpression();
vm.AddInstruction({
.Type = vm::ByteCodeType::SaveToMem,
.LoadedData = std::make_unique<vm::UintData>(nameID),
});
}
void Parser::Parser::parseLabel() {
AssertCurrentToken(Lexer::TokenType::label);
auto name = Current().Literal;
Advance();
name.remove_suffix(1);
auto id = AddLabel(name);
vm.AddLabel(id);
parseStatement();
}
void Parser::Parser::parseIf() {
static int counter = 0;
// these characters specifically because they can't be used in a label, so there are no overlaps
constexpr char def_name[] = "(^-^)";
std::string name = def_name;
name.append(std::to_string(counter++));
auto label = AddLabel(name);
AssertCurrentToken(Lexer::TokenType::If);
Advance();
AssertCurrentToken(Lexer::TokenType::lPar);
Advance();
parseExpression();
AssertPeekToken(Lexer::TokenType::rPar);
Advance();
Advance();
/*
* constructing this thingger:
* if(!<cond>) goto <1>
* <expr>
* <1>:
*
*/
vm.AddInstruction({
.Type = vm::ByteCodeType::IfNotSoGoTo,
.LoadedData = std::make_unique<vm::UintData>(label),
});
parseStatement();
vm.AddLabel(label);
}
void Parser::Parser::parseOut() {
AssertCurrentToken(Lexer::TokenType::Out);
Advance();
std::string format_string{};
std::vector code = std::move(vm.m_Code);
std::deque<std::vector<vm::ByteCodeInstruction>> outStuff{};
while (Current().Type != Lexer::TokenType::str) {
std::string lit{Current().Literal};
lit = std::regex_replace(lit, std::regex("%"), "%%");
format_string += lit;
Advance();
parseExpression();
std::vector res = std::move(vm.m_Code);
outStuff.emplace_back(std::move(res));
Advance();
if (MatchCurrentToken(Lexer::TokenType::format)) {
format_string += '%';
AssertCurrentToken(Lexer::TokenType::id);
format_string += Current().Literal;
} else {
format_string += "%d";
}
}
format_string += Current().Literal;
vm.m_Code = std::move(code);
for (auto it = outStuff.rbegin(); it != outStuff.rend(); ++it) {
vm.m_Code.insert(
vm.m_Code.end(),
std::make_move_iterator(it->begin()),
std::make_move_iterator(it->end())
);
}
vm.AddInstruction({
.Type = vm::ByteCodeType::Out,
.LoadedData = std::make_unique<vm::StringData>(format_string)
});
}
void Parser::Parser::parseIn() {
AssertCurrentToken(Lexer::TokenType::In);
Advance();
std::string format_string{};
std::vector<std::string_view> names{};
while (Current().Type != Lexer::TokenType::str) {
std::string lit{Current().Literal};
lit = std::regex_replace(lit, std::regex("%"), "%%");
format_string += lit;
Advance();
AssertCurrentToken(Lexer::TokenType::id);
auto id = Current();
Advance();
names.emplace_back(id.Literal);
if (MatchCurrentToken(Lexer::TokenType::format)) {
format_string += "%";
AssertCurrentToken(Lexer::TokenType::id);
format_string += Current().Literal;
} else {
format_string += "%d";
}
}
vm.AddInstruction({
.Type = vm::ByteCodeType::In,
.LoadedData = std::make_unique<vm::StringData>(format_string)
});
for (auto name : names | std::ranges::views::reverse) {
auto nameID = AddVar(name);
vm.AddInstruction({
.Type = vm::ByteCodeType::SaveToMem,
.LoadedData = std::make_unique<vm::UintData>(nameID),
});
}
}
void Parser::Parser::parseStatement() {
using enum Lexer::TokenType;
switch (Current().Type) {
case Goto:
parseGoto();
break;
case id:
if (Peek().Type == equals) {
parseAssign();
} else {
logger.Log<Log::Level::Warning>("parsing something as an expression statement.\n"
"this shouldn't happen.");
parseExpression();
}
break;
case label:
parseLabel();
break;
case If:
parseIf();
break;
case Out:
parseOut();
break;
case In:
parseIn();
break;
case newline:
Advance();
parseStatement();
break;
default:
logger.Log<Log::Level::Warning>("parsing something as an expression statement.\n"
"this shouldn't happen.");
parseExpression();
}
}
void Parser::Parser::parseProgram() {
while (Current().Type != Lexer::TokenType::eof) {
parseStatement();
if (Peek().Type == Lexer::TokenType::eof) break;
Advance();
AssertCurrentToken(Lexer::TokenType::newline);
Advance();
}
}