-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
375 lines (284 loc) · 10.3 KB
/
parser.cpp
File metadata and controls
375 lines (284 loc) · 10.3 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
#include "parser.h"
#include <iostream>
Parser::Parser(const std::vector<Token>& tokens) : tokens(tokens), current(0) {}
Token Parser::peek(int offset) const {
if (current + offset >= tokens.size()) {
return Token(TokenType::SON, "", 0, 0);
}
return tokens[current + offset];
}
Token Parser::previous() const {
return tokens[current - 1];
}
bool Parser::isAtEnd() const {
return peek().type == TokenType::SON;
}
bool Parser::check(TokenType type) const {
if (isAtEnd()) return false;
return peek().type == type;
}
bool Parser::match(TokenType type) {
if (check(type)) {
advance();
return true;
}
return false;
}
bool Parser::match(const std::vector<TokenType>& types) {
for (TokenType type : types) {
if (match(type)) {
return true;
}
}
return false;
}
Token Parser::advance() {
if (!isAtEnd()) {
current++;
}
return previous();
}
Token Parser::consume(TokenType type, const std::string& message) {
if (check(type)) {
return advance();
}
throw std::runtime_error(message + " Satır: " + std::to_string(peek().line) +
", Sütun: " + std::to_string(peek().column));
}
std::unique_ptr<ProgramNode> Parser::parse() {
auto program = std::make_unique<ProgramNode>();
while (!isAtEnd()) {
try {
program->add(statement());
} catch (const std::exception& e) {
// Hata durumunda senkronize et ve devam et
std::cerr << e.what() << std::endl;
// Bir sonraki ifadeye kadar atla
while (!isAtEnd() && previous().type != TokenType::SATIR_SONU) {
advance();
}
}
}
return program;
}
std::unique_ptr<ASTNode> Parser::statement() {
if (match(TokenType::EGER)) {
return ifStatement();
}
if (match(TokenType::DONGU)) {
return loopStatement();
}
if (match(TokenType::FONKSIYON)) {
return functionStatement();
}
if (match(TokenType::DONDUR)) {
return returnStatement();
}
if (match(TokenType::YAZ)) {
return printStatement();
}
if (match(TokenType::SOL_SUSLU)) {
return block();
}
return expressionStatement();
}
std::unique_ptr<BlockNode> Parser::block() {
auto block = std::make_unique<BlockNode>();
while (!check(TokenType::SAG_SUSLU) && !isAtEnd()) {
block->add(statement());
}
consume(TokenType::SAG_SUSLU, "Blok sonunda '}' bekleniyor.");
return block;
}
std::unique_ptr<ASTNode> Parser::expressionStatement() {
auto expr = expression();
// Noktalı virgül opsiyonel, satır sonu veya süslü parantez de ifadeyi sonlandırabilir
if (match(TokenType::SATIR_SONU) || check(TokenType::SAG_SUSLU)) {
// İfade sonlandı
}
return std::make_unique<ExpressionStatementNode>(std::move(expr));
}
std::unique_ptr<ASTNode> Parser::ifStatement() {
consume(TokenType::SOL_PARANTEZ, "'eğer' ifadesinden sonra '(' bekleniyor.");
auto condition = expression();
consume(TokenType::SAG_PARANTEZ, "Koşul ifadesinden sonra ')' bekleniyor.");
consume(TokenType::SOL_SUSLU, "Koşul bloğu için '{' bekleniyor.");
auto thenBranch = block();
std::unique_ptr<BlockNode> elseBranch = nullptr;
if (match(TokenType::DEGILSE)) {
consume(TokenType::SOL_SUSLU, "'değilse' bloğu için '{' bekleniyor.");
elseBranch = block();
}
return std::make_unique<IfNode>(std::move(condition), std::move(thenBranch), std::move(elseBranch));
}
std::unique_ptr<ASTNode> Parser::loopStatement() {
consume(TokenType::SOL_PARANTEZ, "'döngü' ifadesinden sonra '(' bekleniyor.");
auto condition = expression();
consume(TokenType::SAG_PARANTEZ, "Döngü koşulundan sonra ')' bekleniyor.");
consume(TokenType::SOL_SUSLU, "Döngü bloğu için '{' bekleniyor.");
auto body = block();
return std::make_unique<LoopNode>(std::move(condition), std::move(body));
}
std::unique_ptr<ASTNode> Parser::functionStatement() {
Token name = consume(TokenType::TANIMLAYICI, "Fonksiyon adı bekleniyor.");
consume(TokenType::SOL_PARANTEZ, "Fonksiyon adından sonra '(' bekleniyor.");
auto function = std::make_unique<FunctionNode>(name.value, nullptr);
// Parametreler
if (!check(TokenType::SAG_PARANTEZ)) {
do {
Token param = consume(TokenType::TANIMLAYICI, "Parametre adı bekleniyor.");
function->addParameter(param.value);
} while (match(TokenType::VIRGUL));
}
consume(TokenType::SAG_PARANTEZ, "Parametre listesinden sonra ')' bekleniyor.");
consume(TokenType::SOL_SUSLU, "Fonksiyon gövdesi için '{' bekleniyor.");
function->body = block();
return function;
}
std::unique_ptr<ASTNode> Parser::returnStatement() {
if (check(TokenType::SATIR_SONU) || check(TokenType::SAG_SUSLU)) {
return std::make_unique<ReturnNode>();
}
auto value = expression();
return std::make_unique<ReturnNode>(std::move(value));
}
std::unique_ptr<ASTNode> Parser::printStatement() {
auto value = expression();
return std::make_unique<PrintNode>(std::move(value));
}
std::unique_ptr<ExpressionNode> Parser::expression() {
return assignment();
}
std::unique_ptr<ExpressionNode> Parser::assignment() {
auto expr = logicalOr();
if (match(TokenType::ESITTIR)) {
auto value = assignment();
if (auto* identifier = dynamic_cast<IdentifierNode*>(expr.get())) {
return std::make_unique<AssignmentNode>(identifier->name, std::move(value));
}
throw std::runtime_error("Geçersiz atama hedefi.");
}
return expr;
}
std::unique_ptr<ExpressionNode> Parser::logicalOr() {
auto expr = logicalAnd();
while (match(TokenType::VEYA)) {
auto op = previous().type;
auto right = logicalAnd();
expr = std::make_unique<BinaryOpNode>(op, std::move(expr), std::move(right));
}
return expr;
}
std::unique_ptr<ExpressionNode> Parser::logicalAnd() {
auto expr = equality();
while (match(TokenType::VE)) {
auto op = previous().type;
auto right = equality();
expr = std::make_unique<BinaryOpNode>(op, std::move(expr), std::move(right));
}
return expr;
}
std::unique_ptr<ExpressionNode> Parser::equality() {
auto expr = comparison();
while (match({TokenType::ESIT, TokenType::ESIT_DEGIL})) {
auto op = previous().type;
auto right = comparison();
expr = std::make_unique<BinaryOpNode>(op, std::move(expr), std::move(right));
}
return expr;
}
std::unique_ptr<ExpressionNode> Parser::comparison() {
auto expr = term();
while (match({TokenType::KUCUK, TokenType::KUCUK_ESIT, TokenType::BUYUK, TokenType::BUYUK_ESIT})) {
auto op = previous().type;
auto right = term();
expr = std::make_unique<BinaryOpNode>(op, std::move(expr), std::move(right));
}
return expr;
}
std::unique_ptr<ExpressionNode> Parser::term() {
auto expr = factor();
while (match({TokenType::ARTI, TokenType::EKSI})) {
auto op = previous().type;
auto right = factor();
expr = std::make_unique<BinaryOpNode>(op, std::move(expr), std::move(right));
}
return expr;
}
std::unique_ptr<ExpressionNode> Parser::factor() {
auto expr = unary();
while (match({TokenType::CARPI, TokenType::BOLU, TokenType::MOD})) {
auto op = previous().type;
auto right = unary();
expr = std::make_unique<BinaryOpNode>(op, std::move(expr), std::move(right));
}
return expr;
}
std::unique_ptr<ExpressionNode> Parser::unary() {
if (match({TokenType::EKSI, TokenType::DEGIL})) {
auto op = previous().type;
auto right = unary();
return std::make_unique<UnaryOpNode>(op, std::move(right));
}
if (match(TokenType::TIP)) {
consume(TokenType::SOL_PARANTEZ, "'tip' ifadesinden sonra '(' bekleniyor.");
Token typeToken = consume(TokenType::TANIMLAYICI, "Tip adı bekleniyor.");
consume(TokenType::VIRGUL, "Tip adından sonra ',' bekleniyor.");
auto expr = expression();
consume(TokenType::SAG_PARANTEZ, "Tip dönüşümünden sonra ')' bekleniyor.");
return std::make_unique<CastNode>(typeToken.value, std::move(expr));
}
return call();
}
std::unique_ptr<ExpressionNode> Parser::call() {
auto expr = primary();
while (true) {
if (match(TokenType::SOL_PARANTEZ)) {
expr = finishCall(std::move(expr));
} else {
break;
}
}
return expr;
}
std::unique_ptr<ExpressionNode> Parser::finishCall(std::unique_ptr<ExpressionNode> callee) {
std::vector<std::unique_ptr<ExpressionNode>> arguments;
if (!check(TokenType::SAG_PARANTEZ)) {
do {
arguments.push_back(expression());
} while (match(TokenType::VIRGUL));
}
consume(TokenType::SAG_PARANTEZ, "Fonksiyon çağrısından sonra ')' bekleniyor.");
auto* identifier = dynamic_cast<IdentifierNode*>(callee.get());
if (!identifier) {
throw std::runtime_error("Fonksiyon çağrısı için geçersiz ifade.");
}
auto call = std::make_unique<FunctionCallNode>(identifier->name);
for (auto& arg : arguments) {
call->addArgument(std::move(arg));
}
return call;
}
std::unique_ptr<ExpressionNode> Parser::primary() {
if (match(TokenType::DOGRU)) {
return std::make_unique<BooleanNode>(true);
}
if (match(TokenType::YANLIS)) {
return std::make_unique<BooleanNode>(false);
}
if (match(TokenType::SAYI)) {
return std::make_unique<NumberNode>(std::stod(previous().value));
}
if (match(TokenType::METIN)) {
return std::make_unique<StringNode>(previous().value);
}
if (match(TokenType::TANIMLAYICI)) {
return std::make_unique<IdentifierNode>(previous().value);
}
if (match(TokenType::SOL_PARANTEZ)) {
auto expr = expression();
consume(TokenType::SAG_PARANTEZ, "İfadeden sonra ')' bekleniyor.");
return expr;
}
throw std::runtime_error("İfade bekleniyor.");
}