-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolTable.h
72 lines (63 loc) · 1.84 KB
/
symbolTable.h
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
#pragma once
#include <string>
#include <list>
#include "varType.h"
#include "astNode.h"
enum STEntryType {
ST_HEAD,
ST_VAR,
ST_PARAM,
ST_CONST,
ST_TYPE,
ST_FUNCTION,
};
struct STTypeDescriptor {
VarType type;
VarType baseType; //for arrays
int length; //for arrays
STTypeDescriptor(VarType type, VarType baseType = VT_NONE, int length = 0)
: type (type),
baseType (baseType),
length (length) {
}
};
struct STEntry {
//mandatory
STEntryType entryType;
int scopeId;
//optionals
STTypeDescriptor * typeDescriptor; // Set if entryType in (VAR, PARAM, CONST, TYPE, FUNCTION)
STEntry * next = nullptr;
STEntry * parentScope; // scope directly outside this scope, for ST_HEAD only (aka "desc")
std::list<STEntry*> childScopes; // scopes directly within this scope, for ST_HEAD only
std::string name;
// int intVal;
// char charVal;
// bool boolVal;
STEntry() {};
STEntry(STEntryType entryType, int scopeId)
: entryType (entryType),
scopeId (scopeId) {
};
};
class SymbolTable {
private:
void addEntry(STEntryType entryType, VarType varType, std::string varName);
STEntry * universe;
STEntry * currentScope;
int currentScopeId;
STTypeDescriptor * intDescriptor;
STTypeDescriptor * charDescriptor;
STTypeDescriptor * boolDescriptor;
public:
SymbolTable();
void openScope(std::string scopeName);
void closeScope();
void print();
void addVariable(VarType type, std::string varName);
void addFunction(VarType returnType, std::string functionName);
void addParam(VarType paramType, std::string paramName);
STEntry * lookup(std::string name);
STEntry * lookupScope(std::string functionName);
STEntry * getRoot();
};