-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemanticAnalyzer.cpp
More file actions
85 lines (78 loc) · 3.16 KB
/
semanticAnalyzer.cpp
File metadata and controls
85 lines (78 loc) · 3.16 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
#include <iostream>
#include "semanticAnalyzer.h"
#include "varType.h"
#include "util/symbolTableUtil.h"
SemanticAnalyzer::SemanticAnalyzer(ASTNode * root)
: root (root) {
symbolTable = new SymbolTable();
}
SymbolTable * SemanticAnalyzer::analyze() {
parseNode(root);
printSymbolTable(symbolTable);
return symbolTable;
}
void SemanticAnalyzer::parseNode(ASTNode * node) {
switch (node->nodeType) {
case AST_DECLARATION:
case AST_FUNCTION:
{
VarType varType;
std::string functionName = "";
for (auto i = node->children.begin(); i != node->children.end(); i++) {
ASTNode * childNode = *i;
// Is TypeNode
if (i == node->children.begin()) {
varType = ((TypeNode *)*i)->varType;
} else if(node->nodeType == AST_DECLARATION) {
// Is AST_VARDECLARATION
IdentifierNode * idNode = (IdentifierNode *)(*i)->children[0];
std::string varName = idNode->name;
symbolTable->addVariable(varType, varName);
} else if (node->nodeType == AST_FUNCTION && i == node->children.begin() + 1) {
// Second child of AST_FUNCTION is an Identifier
IdentifierNode * idNode = (IdentifierNode *)(*i);
functionName = idNode->name;
symbolTable->addFunction(varType, functionName);
} else if (node->nodeType == AST_FUNCTION && childNode->nodeType == AST_PARAMETERLIST) {
// Open the scope for the body of the function
symbolTable->openScope(functionName);
parseNode(childNode);
} else {
parseNode(childNode);
}
}
break;
}
case AST_PARAMETERLIST:
{
for (auto i = node->children.begin(); i != node->children.end(); i++) {
ASTNode * childNode = *i; // Parameter
TypeNode * paramType = (TypeNode*) childNode->children[0];
IdentifierNode * paramIdentifier = (IdentifierNode*) childNode->children[1];
symbolTable->addParam(paramType->varType, paramIdentifier->name);
}
break;
}
case AST_FUNCTIONBODY:
{
for (auto & n : node->children) {
parseNode(n);
}
symbolTable->closeScope();
break;
}
case AST_IDENTIFIER:
{
IdentifierNode * idNode= (IdentifierNode*) node;
STEntry * entry = symbolTable->lookup(idNode->name);
if(entry == nullptr) {
std::cout << "Identifier \"" << idNode->name << "\" is undefined." << std::endl;
}
break;
}
default:
for (auto & n : node->children) {
parseNode(n);
}
}
}