-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticAnalyzer.cpp
More file actions
857 lines (734 loc) · 34.1 KB
/
SemanticAnalyzer.cpp
File metadata and controls
857 lines (734 loc) · 34.1 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
#include "SemanticAnalyzer.h"
void SemanticAnalyzer::openScope() {
topScope = new Scope(topScope);
}
void SemanticAnalyzer::closeScope() {
Scope* oldScope = topScope;
topScope = topScope->outer;
delete oldScope;
}
SemanticAnalyzer::Scope* SemanticAnalyzer::lookTopIdScope(const std::string& idName) {
Scope* currentScope = topScope;
while (currentScope != nullptr) {
if (currentScope->symbolTable.isIdExist(idName)) {
return currentScope;
}
currentScope = currentScope->outer;
}
return nullptr;
}
SemanticAnalysisResult SemanticAnalyzer::newError(SemanticAnalysisResult::Error err) {
return SemanticAnalysisResult(err);
}
SemanticAnalysisResult SemanticAnalyzer::newError(SemanticAnalysisResult::Error err, const std::string& message) {
return SemanticAnalysisResult(err, message);
}
SemanticAnalysisResult SemanticAnalyzer::checkVarDecl(DeclVarNode* node) {
std::string idName = node->id->name;
if (topScope->symbolTable.isIdExist(idName)) {
return newError(SemanticAnalysisResult::VAR_REDEFINITION, "Redefinition of variable '" + idName + "'");
}
if (node->expr != nullptr) {
bool oldOperationCheck = operationCheck;
operationCheck = true;
SemanticAnalysisResult checkResult = checkStatement(node->expr);
operationCheck = oldOperationCheck;
if (checkResult.isError()) {
return checkResult;
}
switch (node->expr->type) {
case NodeType::ConstNumber: {
double value = 0;
topScope->symbolTable.addNewIdentifier(idName, value);
break;
}
case NodeType::ConstBool: {
bool value = false;
topScope->symbolTable.addNewIdentifier(idName, value);
break;
}
case NodeType::BinOp: {
BinOpNode* binExpr = static_cast<BinOpNode*>(node->expr);
if (binExpr->binOpType == BinOpType::OperatorPlus ||
binExpr->binOpType == BinOpType::OperatorMinus ||
binExpr->binOpType == BinOpType::OperatorMul ||
binExpr->binOpType == BinOpType::OperatorDiv) {
double value = 0;
topScope->symbolTable.addNewIdentifier(idName, value);
} else if (binExpr->binOpType == BinOpType::OperatorBoolAND ||
binExpr->binOpType == BinOpType::OperatorBoolOR ||
binExpr->binOpType == BinOpType::OperatorEqual ||
binExpr->binOpType == BinOpType::OperatorLess ||
binExpr->binOpType == BinOpType::OperatorGreater) {
bool value = false;
topScope->symbolTable.addNewIdentifier(idName, value);
} else {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE,
"Invalid RHS expression value type");
}
break;
}
case NodeType::Id: {
IdentifierNode* rhsId = static_cast<IdentifierNode*>(node->expr);
Scope* rhsIdScope = lookTopIdScope(rhsId->name);
if (rhsIdScope->symbolTable.getIdValueType(rhsId->name) == ValueType::Number) {
double value = 0;
topScope->symbolTable.addNewIdentifier(idName, value);
} else {
bool value = false;
topScope->symbolTable.addNewIdentifier(idName, value);
}
break;
}
case NodeType::FuncCall: {
FuncCallNode* funcCallNode = static_cast<FuncCallNode*>(node->expr);
const std::string& funcName = funcCallNode->name;
switch (functions->symbolTable.getFuncValueType(funcName)) {
case ValueType::Number: {
double value = 0;
topScope->symbolTable.addNewIdentifier(idName, value);
break;
}
case ValueType::Bool: {
bool value = false;
topScope->symbolTable.addNewIdentifier(idName, value);
break;
}
default: {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE,
"Invalid RHS expression value type");
}
}
break;
}
default: {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE,
"Invalid RHS expression value type");
}
}
} else {
topScope->symbolTable.addNewIdentifier(idName);
}
return SemanticAnalysisResult();
}
SemanticAnalysisResult SemanticAnalyzer::checkFuncDecl(DeclFuncNode* node) {
if (topScope != globalScope) {
return newError(SemanticAnalysisResult::FUNC_DEFINITION_IS_NOT_ALLOWED);
}
if (functions->symbolTable.isFuncExist(node->name)) {
return newError(SemanticAnalysisResult::FUNC_REDEFINITION, "Redefinition of function '" + node->name + "'");
}
SemanticAnalysisResult checkResult;
// since function can see variables in its own scope and also in global scope,
// so make outer scope is global scope,
openScope();
Scope* oldOuterScope = topScope->outer;
topScope->outer = globalScope;
for (const auto& currentId : node->args) {
if (currentId->valueType == ValueType::Number) {
double value = 0;
topScope->symbolTable.addNewIdentifier(currentId->name, value);
} else if (currentId->valueType == ValueType::Bool) {
bool value = 0;
topScope->symbolTable.addNewIdentifier(currentId->name, value);
}
}
bool oldFunctionBodyCheck = functionBodyCheck;
functionBodyCheck = true;
functionReturnType = node->returnType;
for (ASTNode* currentStatement : node->body->stmtList) {
// TODO: сделать проверку, что не void функция всегда возвращает значение
// примерно это можно реализовать так:
// функция всегда возвращает значение, если инструкция вне if ветвления
// иначе надо проверить все ответвления и убедиться что во всех присутствует return инструкция
// также, есть еще for loop который может не выполниться ни разу, не понятно как его проверять, такое должно
// определиться в run-time скорее всего, и пока самое простое решение - потребовать, чтобы всегда возвращалось
// какое-то значение из функций
checkResult = checkStatement(currentStatement);
if (checkResult.isError()) {
break;
}
}
functionBodyCheck = oldFunctionBodyCheck;
topScope->outer = oldOuterScope;
closeScope();
if (!checkResult.isError()) {
functions->symbolTable.addNewFunc(node);
}
return checkResult;
}
SemanticAnalysisResult SemanticAnalyzer::checkReservedFuncCall(FuncCallNode* node) {
if (node->name == "print") {
const std::string funcName = node->name;
DeclFuncNode* func = functions->symbolTable.getFunc(funcName);
if (func->argsSize != node->argsSize) {
return newError(SemanticAnalysisResult::NO_MATCHING_FUNC);
}
for (const auto& callParam : node->args) {
bool oldOperationCheck = operationCheck;
operationCheck = true;
SemanticAnalysisResult checkCallParamResult = checkStatement(callParam);
operationCheck = oldOperationCheck;
if (checkCallParamResult.isError()) {
return checkCallParamResult;
}
ValueType::Type callParamType;
if (callParam->type == NodeType::ConstNumber) {
callParamType = ValueType::Number;
} else if (callParam->type == NodeType::ConstBool) {
callParamType = ValueType::Bool;
} else if (callParam->type == NodeType::Id) {
IdentifierNode* id = static_cast<IdentifierNode*>(callParam);
Scope* idScope = lookTopIdScope(id->name);
callParamType = idScope->symbolTable.getIdValueType(id->name);
} else if (callParam->type == NodeType::FuncCall) {
FuncCallNode* funcCallNode = static_cast<FuncCallNode*>(callParam);
ValueType::Type funcValueType = functions->symbolTable.getFuncValueType(funcCallNode->name);
if (funcValueType == ValueType::Void) {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE,
"Can not use void function call as function parameter");
}
callParamType = funcValueType;
} else if (callParam->type == NodeType::BinOp) {
BinOpNode* binOpNode = static_cast<BinOpNode*>(callParam);
if (binOpNode->binOpType == BinOpType::OperatorPlus ||
binOpNode->binOpType == BinOpType::OperatorMinus ||
binOpNode->binOpType == BinOpType::OperatorMul ||
binOpNode->binOpType == BinOpType::OperatorDiv) {
callParamType = ValueType::Number;
} else {
callParamType = ValueType::Bool;
}
} else {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE, "Invalid function call parameter");
}
if (callParamType != ValueType::Number && callParamType != ValueType::Bool) {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE,
"Print function can take only number or bool argument");
}
}
}
return SemanticAnalysisResult();
}
SemanticAnalysisResult SemanticAnalyzer::checkFuncCall(FuncCallNode* node) {
const std::string& funcName = node->name;
if (!functions->symbolTable.isFuncExist(funcName)) {
return newError(SemanticAnalysisResult::UNDECLARED_FUNC,
"Use of undeclared function '" + funcName + "'");
}
if (isFuncReserved(funcName)) {
return checkReservedFuncCall(node);
}
DeclFuncNode* func = functions->symbolTable.getFunc(funcName);
if (func->argsSize != node->argsSize) {
return newError(SemanticAnalysisResult::NO_MATCHING_FUNC);
}
for (unsigned long currentCallParamNum = 0; currentCallParamNum != node->argsSize; currentCallParamNum++) {
ASTNode* callParam = node->args[currentCallParamNum];
bool oldOperationCheck = operationCheck;
operationCheck = true;
SemanticAnalysisResult checkCallParamResult = checkStatement(callParam);
operationCheck = oldOperationCheck;
if (checkCallParamResult.isError()) {
return checkCallParamResult;
}
ValueType::Type funcParamType = func->args[currentCallParamNum]->valueType;
ValueType::Type callParamType;
if (callParam->type == NodeType::ConstNumber) {
callParamType = ValueType::Number;
} else if (callParam->type == NodeType::ConstBool) {
callParamType = ValueType::Bool;
} else if (callParam->type == NodeType::Id) {
IdentifierNode* id = static_cast<IdentifierNode*>(callParam);
Scope* idScope = lookTopIdScope(id->name);
callParamType = idScope->symbolTable.getIdValueType(id->name);
} else if (callParam->type == NodeType::FuncCall) {
FuncCallNode* funcCallNode = static_cast<FuncCallNode*>(callParam);
ValueType::Type funcValueType = functions->symbolTable.getFuncValueType(funcCallNode->name);
if (funcValueType == ValueType::Void) {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE,
"Can not use void function call as function parameter");
}
callParamType = funcValueType;
} else if (callParam->type == NodeType::BinOp) {
BinOpNode* binOpNode = static_cast<BinOpNode*>(callParam);
if (binOpNode->binOpType == BinOpType::OperatorPlus ||
binOpNode->binOpType == BinOpType::OperatorMinus ||
binOpNode->binOpType == BinOpType::OperatorMul ||
binOpNode->binOpType == BinOpType::OperatorDiv) {
callParamType = ValueType::Number;
} else {
callParamType = ValueType::Bool;
}
} else {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE, "Invalid function call parameter");
}
if (callParamType != funcParamType) {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE,
"Value type of " + std::to_string(currentCallParamNum) +
"-th argument does not match with function parameter type");
}
}
return SemanticAnalysisResult();
}
SemanticAnalysisResult SemanticAnalyzer::checkId(IdentifierNode* node) {
const std::string& idName = node->name;
Scope* idScope = lookTopIdScope(idName);
if (idScope == nullptr) {
return newError(SemanticAnalysisResult::UNDECLARED_VAR, "Use of undeclared variable '" + idName + "'");
}
ValueType::Type idValueType = idScope->symbolTable.getIdValueType(idName);
if (idValueType != ValueType::Number && idValueType != ValueType::Bool) {
if (idValueType == ValueType::Undefined) {
return newError(SemanticAnalysisResult::UNINITIALIZED_VAR,
"Use of uninitialized variable '" + idName + "'");
} else {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE,
"Invalid value type of variable '" + idName + "'");
}
}
return SemanticAnalysisResult();
}
SemanticAnalysisResult SemanticAnalyzer::checkAssignExpr(BinOpNode* node) {
IdentifierNode* id = dynamic_cast<IdentifierNode*>(node->left);
if (id == nullptr) {
return newError(SemanticAnalysisResult::INVALID_LVALUE);
}
const std::string& idName = id->name;
Scope* idScope = lookTopIdScope(idName);
if (idScope == nullptr) {
return newError(SemanticAnalysisResult::UNDECLARED_VAR, "Use of undeclared variable '" + idName + "'");
}
bool oldOperationCheck = operationCheck;
operationCheck = true;
SemanticAnalysisResult exprCheckResult = checkStatement(node->right);
operationCheck = oldOperationCheck;
if (exprCheckResult.isError()) {
return exprCheckResult;
}
BinOpNode* binOpExpr = dynamic_cast<BinOpNode*>(node->right);
IdentifierNode* idExpr = dynamic_cast<IdentifierNode*>(node->right);
ConstNumberNode* numberConst = dynamic_cast<ConstNumberNode*>(node->right);
ConstBoolNode* boolConst = dynamic_cast<ConstBoolNode*>(node->right);
FuncCallNode* funcCallExpr = dynamic_cast<FuncCallNode*>(node->right);
ValueType::Type idValueType = idScope->symbolTable.getIdValueType(idName);
ValueType::Type exprValueType;
if (binOpExpr != nullptr) {
if (binOpExpr->binOpType == BinOpType::OperatorPlus ||
binOpExpr->binOpType == BinOpType::OperatorMinus ||
binOpExpr->binOpType == BinOpType::OperatorMul ||
binOpExpr->binOpType == BinOpType::OperatorDiv) {
exprValueType = ValueType::Number;
} else {
exprValueType = ValueType::Bool;
}
} else if (idExpr != nullptr) {
const std::string& rhsIdName = idExpr->name;
Scope* rhsIdScope = lookTopIdScope(rhsIdName);
exprValueType = rhsIdScope->symbolTable.getIdValueType(rhsIdName);
} else if (numberConst != nullptr) {
exprValueType = ValueType::Number;
} else if (boolConst != nullptr) {
exprValueType = ValueType::Bool;
} else if (funcCallExpr != nullptr) {
exprValueType = functions->symbolTable.getFuncValueType(funcCallExpr->name);
} else {
return newError(SemanticAnalysisResult::INVALID_AST, "Invalid RHS expression");
}
if (idValueType == ValueType::Undefined) {
if (exprValueType == ValueType::Number) {
idScope->symbolTable.setIdValueDouble(idName, 0);
} else {
idScope->symbolTable.setIdValueBool(idName, false);
}
} else {
if (exprValueType != idValueType) {
return newError(SemanticAnalysisResult::INVALID_VALUE_TYPE, "Invalid RHS expression value type");
}
}
return SemanticAnalysisResult();
}
SemanticAnalysisResult SemanticAnalyzer::checkNumberExpr(ASTNode* node) {
bool oldOperationCheck = operationCheck;
operationCheck = true;
SemanticAnalysisResult checkResult;
if (node->type == NodeType::ConstNumber) {
checkResult = checkStatement(node);
} else if (node->type == NodeType::ConstBool) {
checkResult = checkStatement(node);
if (!checkResult.isError()) {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
}
} else if (node->type == NodeType::Id) {
checkResult = checkStatement(node);
if (!checkResult.isError()) {
IdentifierNode* id = static_cast<IdentifierNode*>(node);
Scope* idScope = lookTopIdScope(id->name);
if (idScope->symbolTable.getIdValueType(id->name) != ValueType::Number) {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
}
}
} else if (node->type == NodeType::FuncCall) {
checkResult = checkStatement(node);
if (!checkResult.isError()) {
FuncCallNode* funcCall = static_cast<FuncCallNode*>(node);
if (functions->symbolTable.getFuncValueType(funcCall->name) == ValueType::Void) {
checkResult = newError(SemanticAnalysisResult::INVALID_VALUE_TYPE);
} else if (functions->symbolTable.getFuncValueType(funcCall->name) != ValueType::Number) {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
}
}
} else if (node->type == NodeType::BinOp) {
BinOpNode* binOp = dynamic_cast<BinOpNode*>(node);
if (binOp != nullptr) {
if (binOp->binOpType != BinOpType::OperatorPlus && binOp->binOpType != BinOpType::OperatorMinus &&
binOp->binOpType != BinOpType::OperatorMul && binOp->binOpType != BinOpType::OperatorDiv) {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
} else {
SemanticAnalysisResult leftCheckResult = checkNumberExpr(binOp->left);
if (leftCheckResult.isError()) {
return leftCheckResult;
}
SemanticAnalysisResult rightCheckResult = checkNumberExpr(binOp->right);
if (rightCheckResult.isError()) {
return rightCheckResult;
}
}
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid Binary Operation Node");
}
} else {
return newError(SemanticAnalysisResult::INVALID_AST);
}
operationCheck = oldOperationCheck;
return checkResult;
}
SemanticAnalysisResult SemanticAnalyzer::checkBoolExprComparison(BinOpNode* node) {
SemanticAnalysisResult checkResult;
bool oldOperationCheck = operationCheck;
operationCheck = true;
const SemanticAnalysisResult& leftOperandCheck = checkStatement(node->left);
if (leftOperandCheck.isError()) {
operationCheck = oldOperationCheck;
return leftOperandCheck;
}
const SemanticAnalysisResult& rightOperandCheck = checkStatement(node->right);
if (rightOperandCheck.isError()) {
operationCheck = oldOperationCheck;
return rightOperandCheck;
}
if (node->binOpType == BinOpType::OperatorEqual) {
// operands should be the same types, both int or bool
if (!checkNumberExpr(node->left).isError()) {
if (checkNumberExpr(node->right).isError()) {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
}
} else if (!checkBoolExpr(node->left).isError()) {
if (checkBoolExpr(node->right).isError()) {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
}
}
} else {
// check less than and greater than operators
if (checkNumberExpr(node->left).isError() || checkNumberExpr(node->right).isError()) {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
}
}
operationCheck = oldOperationCheck;
return checkResult;
}
SemanticAnalysisResult SemanticAnalyzer::checkBoolExpr(ASTNode* node) {
bool oldOperationCheck = operationCheck;
operationCheck = true;
SemanticAnalysisResult checkResult;
if (node->type == NodeType::ConstBool) {
checkResult = checkStatement(node);
} else if (node->type == NodeType::ConstNumber) {
checkResult = checkStatement(node);
if (!checkResult.isError()) {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
}
} else if (node->type == NodeType::Id) {
checkResult = checkStatement(node);
if (!checkResult.isError()) {
IdentifierNode* id = static_cast<IdentifierNode*>(node);
Scope* idScope = lookTopIdScope(id->name);
if (idScope->symbolTable.getIdValueType(id->name) != ValueType::Bool) {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
}
}
} else if (node->type == NodeType::FuncCall) {
checkResult = checkStatement(node);
if (!checkResult.isError()) {
FuncCallNode* funcCall = static_cast<FuncCallNode*>(node);
if (functions->symbolTable.getFuncValueType(funcCall->name) == ValueType::Void) {
checkResult = newError(SemanticAnalysisResult::INVALID_VALUE_TYPE);
} else if (functions->symbolTable.getFuncValueType(funcCall->name) != ValueType::Bool) {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
}
}
} else if (node->type == NodeType::BinOp) {
BinOpNode* binOp = dynamic_cast<BinOpNode*>(node);
if (binOp != nullptr) {
if (binOp->binOpType == BinOpType::OperatorEqual || binOp->binOpType == BinOpType::OperatorGreater ||
binOp->binOpType == BinOpType::OperatorLess) {
checkResult = checkBoolExprComparison(binOp);
} else if (binOp->binOpType == BinOpType::OperatorBoolOR ||
binOp->binOpType == BinOpType::OperatorBoolAND) {
SemanticAnalysisResult leftCheckResult = checkBoolExpr(binOp->left);
if (leftCheckResult.isError()) {
return leftCheckResult;
}
SemanticAnalysisResult rightCheckResult = checkBoolExpr(binOp->right);
if (rightCheckResult.isError()) {
return rightCheckResult;
}
} else {
checkResult = newError(SemanticAnalysisResult::INCOMPATIBLE_OPERAND_TYPES);
}
} else {
return newError(SemanticAnalysisResult::INVALID_AST, "Invalid Binary Operation Node");
}
} else {
return newError(SemanticAnalysisResult::INVALID_AST);
}
operationCheck = oldOperationCheck;
return checkResult;
}
SemanticAnalysisResult SemanticAnalyzer::checkBreakStmt() {
if (!forLoopCheck) {
return newError(SemanticAnalysisResult::INVALID_OPERATION, "Break statement is allowed only in for loops");
}
return SemanticAnalysisResult();
}
SemanticAnalysisResult SemanticAnalyzer::checkReturnStmt(ReturnStmtNode* node) {
SemanticAnalysisResult checkResult;
bool oldOperationCheck = operationCheck;
operationCheck = true;
if (functionBodyCheck) {
if (functionReturnType == ValueType::Void) {
if (node->expression != nullptr) {
checkResult = newError(SemanticAnalysisResult::RETURN_TYPE_MISMATCH,
"Void functions can not return any value");
}
} else {
if (functionReturnType == ValueType::Number) {
checkResult = checkNumberExpr(node->expression);
} else if (functionReturnType == ValueType::Bool) {
checkResult = checkBoolExpr(node->expression);
}
}
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_OPERATION,
"Using of Return stmt is allowed only in functions");
}
operationCheck = oldOperationCheck;
return checkResult;
}
SemanticAnalysisResult SemanticAnalyzer::checkBlockStmt(BlockStmtNode* node) {
for (const auto& currentStatement : node->stmtList) {
SemanticAnalysisResult checkResult = checkStatement(currentStatement);
if (checkResult.isError()) {
return checkResult;
}
}
return SemanticAnalysisResult();
}
SemanticAnalysisResult SemanticAnalyzer::checkIfStmt(IfStmtNode* node) {
bool oldOperationCheck = operationCheck;
operationCheck = true;
SemanticAnalysisResult condCheckResult = checkBoolExpr(node->condition);
operationCheck = oldOperationCheck;
if (condCheckResult.isError()) {
return condCheckResult;
}
openScope();
SemanticAnalysisResult bodyCheckResult = checkBlockStmt(node->body);
if (bodyCheckResult.isError()) {
closeScope();
return bodyCheckResult;
}
if (!node->elseIfStmts.empty()) {
for (const auto& currentElseIfStmt : node->elseIfStmts) {
oldOperationCheck = operationCheck;
operationCheck = true;
condCheckResult = checkBoolExpr(currentElseIfStmt->condition);
operationCheck = oldOperationCheck;
if (condCheckResult.isError()) {
closeScope();
return condCheckResult;
}
bodyCheckResult = checkBlockStmt(currentElseIfStmt->body);
if (bodyCheckResult.isError()) {
closeScope();
return bodyCheckResult;
}
}
}
if (node->elseBody != nullptr) {
bodyCheckResult = checkBlockStmt(node->elseBody);
if (bodyCheckResult.isError()) {
closeScope();
return bodyCheckResult;
}
}
closeScope();
return SemanticAnalysisResult();
}
SemanticAnalysisResult SemanticAnalyzer::checkForLoop(ForLoopNode* node) {
openScope();
if (node->init != nullptr) {
SemanticAnalysisResult initCheckResult = checkStatement(node->init);
if (initCheckResult.isError()) {
closeScope();
return initCheckResult;
}
}
if (node->condition != nullptr) {
bool oldOperationCheck = operationCheck;
operationCheck = true;
SemanticAnalysisResult condCheckResult = checkBoolExpr(node->condition);
operationCheck = oldOperationCheck;
if (condCheckResult.isError()) {
closeScope();
return condCheckResult;
}
}
if (node->inc != nullptr) {
SemanticAnalysisResult incStmtCheckResult = checkAssignExpr(node->inc);
if (incStmtCheckResult.isError()) {
closeScope();
return incStmtCheckResult;
}
}
bool oldForLoopCheck = forLoopCheck;
forLoopCheck = true;
SemanticAnalysisResult checkResult = checkBlockStmt(node->body);
closeScope();
forLoopCheck = oldForLoopCheck;
return checkResult;
}
SemanticAnalysisResult SemanticAnalyzer::checkStatement(ASTNode* node) {
SemanticAnalysisResult checkResult;
if (node->type == NodeType::BinOp) {
BinOpNode* binOpNode = dynamic_cast<BinOpNode*>(node);
if (binOpNode != nullptr) {
if (binOpNode->binOpType == BinOpType::OperatorPlus ||
binOpNode->binOpType == BinOpType::OperatorMinus ||
binOpNode->binOpType == BinOpType::OperatorMul ||
binOpNode->binOpType == BinOpType::OperatorDiv) {
checkResult = checkNumberExpr(binOpNode);
if (!operationCheck) {
checkResult = newError(SemanticAnalysisResult::INVALID_OPERATION,
"Number expression evaluated but not used");
}
} else if (binOpNode->binOpType == BinOpType::OperatorBoolAND ||
binOpNode->binOpType == BinOpType::OperatorBoolOR ||
binOpNode->binOpType == BinOpType::OperatorEqual ||
binOpNode->binOpType == BinOpType::OperatorLess ||
binOpNode->binOpType == BinOpType::OperatorGreater) {
checkResult = checkBoolExpr(binOpNode);
if (!operationCheck) {
checkResult = newError(SemanticAnalysisResult::INVALID_OPERATION,
"Bool expression evaluated but not used");
}
} else if (binOpNode->binOpType == BinOpType::OperatorAssign) {
checkResult = checkAssignExpr(binOpNode);
}
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid Binary Operation Node");
}
} else if (node->type == NodeType::DeclVar) {
DeclVarNode* declVar = dynamic_cast<DeclVarNode*>(node);
if (declVar != nullptr) {
checkResult = checkVarDecl(declVar);
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid Variable Declaration Node");
}
} else if (node->type == NodeType::ConstNumber) {
ConstNumberNode* numberValue = dynamic_cast<ConstNumberNode*>(node);
if (numberValue == nullptr) {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid Number Const Node");
} else if (!operationCheck) {
checkResult = newError(SemanticAnalysisResult::INVALID_OPERATION, "Const number evaluated but not used");
}
} else if (node->type == NodeType::ConstBool) {
ConstBoolNode* boolValue = dynamic_cast<ConstBoolNode*>(node);
if (boolValue == nullptr) {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid Bool Const Node");
} else if (!operationCheck) {
checkResult = newError(SemanticAnalysisResult::INVALID_OPERATION, "Const bool evaluated but not used");
}
} else if (node->type == NodeType::Id) {
IdentifierNode* id = dynamic_cast<IdentifierNode*>(node);
if (id != nullptr) {
checkResult = checkId(id);
if (!checkResult.isError() && !operationCheck) {
checkResult = newError(SemanticAnalysisResult::INVALID_OPERATION, "Identifier evaluated but not used");
}
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid Identifier Node");
}
} else if (node->type == NodeType::DeclFunc) {
DeclFuncNode* funcDecl = dynamic_cast<DeclFuncNode*>(node);
if (funcDecl != nullptr) {
checkResult = checkFuncDecl(funcDecl);
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid Function Declaration Node");
}
} else if (node->type == NodeType::FuncCall) {
FuncCallNode* funcCall = dynamic_cast<FuncCallNode*>(node);
if (funcCall != nullptr) {
checkResult = checkFuncCall(funcCall);
if (!checkResult.isError() && !operationCheck && !isFuncReserved(funcCall->name)) {
checkResult = newError(SemanticAnalysisResult::INVALID_OPERATION,
"Function call evaluated but not used");
}
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid Function Call Node");
}
} else if (node->type == NodeType::ReturnStmt) {
ReturnStmtNode* returnStmt = dynamic_cast<ReturnStmtNode*>(node);
if (returnStmt != nullptr) {
checkResult = checkReturnStmt(returnStmt);
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid Return Statement Node");
}
} else if (node->type == NodeType::ForLoop) {
ForLoopNode* forLoop = dynamic_cast<ForLoopNode*>(node);
if (forLoop != nullptr) {
checkResult = checkForLoop(forLoop);
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid For Loop Node");
}
} else if (node->type == NodeType::IfStmt) {
IfStmtNode* ifStmt = dynamic_cast<IfStmtNode*>(node);
if (ifStmt != nullptr) {
checkResult = checkIfStmt(ifStmt);
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid If Statement Node");
}
} else if (node->type == NodeType::BreakStmt) {
BreakStmtNode* breakStmt = dynamic_cast<BreakStmtNode*>(node);
if (breakStmt != nullptr) {
checkResult = checkBreakStmt();
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST, "Invalid Break Statement Node");
}
} else {
checkResult = newError(SemanticAnalysisResult::INVALID_AST);
}
return checkResult;
}
SemanticAnalysisResult SemanticAnalyzer::checkProgram(ProgramTranslationNode* root) {
for (const auto& currentStatement : root->statements) {
const SemanticAnalysisResult& checkResult = checkStatement(currentStatement);
if (checkResult.isError()) {
return checkResult;
}
}
return SemanticAnalysisResult();
}
bool SemanticAnalyzer::isFuncReserved(const std::string& funcName) {
return funcName == "print";
}