-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSymbolTable.cpp
More file actions
100 lines (86 loc) · 2.9 KB
/
SymbolTable.cpp
File metadata and controls
100 lines (86 loc) · 2.9 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
#include "SymbolTable.h"
#include "Symbol.h"
#include "Type.h"
#include <vector>
#include <iostream>
/*
Define methods for symbol table
*/
using namespace std;
//Enter a context
void SymbolTable::EnterScope() {
currFrame = new SymbolTableFrame(currFrame);
}
//Exit a context
void SymbolTable::ExitScope() {
currFrame = currFrame->innerFrame;
}
SymbolTable::SymbolTable() {
SymbolTableFrame* baseFrame = new SymbolTableFrame(NULL);
Structure* ioParas = new Structure("");
ioParas->Collect("number", new Int());
FuncType* ioFunctionType = new FuncType(ioParas, new EmptyType());
Symbol* readFunctionSymbol = new Symbol("read", true, false, 0);
readFunctionSymbol->type = ioFunctionType;
baseFrame->Add(readFunctionSymbol);
Symbol* writeFunctionSymbol = new Symbol("write", true, false, 0);
writeFunctionSymbol->type = ioFunctionType;
baseFrame->Add(writeFunctionSymbol);
currFrame = new SymbolTableFrame(baseFrame);
}
//Add symbol to symbol table
void SymbolTable::Add(Symbol *symbol) {
currFrame->Add(symbol);
}
Symbol* SymbolTable::Get(std::string name) {
return currFrame->Get(name);
}
//A newly defined symbol founded
Symbol* SymbolTable::RegisterSymbol(std::string name, bool isFunction, bool isType, int dimension) {
return currFrame->RegisterSymbol(name, isFunction, isType, dimension);
}
SymbolTableFrame* SymbolTable::GetCurrFrame() {
return currFrame;
}
void SymbolTableFrame::Add(Symbol* symbol) {
symbols.push_back(symbol);
}
//Get symbol from a symbol table frame
Symbol* SymbolTableFrame::Get(std::string name) {
for(std::vector<Symbol*>::iterator itor = symbols.begin(); itor != symbols.end(); ++itor) {
if((*itor)->name == name) {
if(innerFrame != NULL && innerFrame->innerFrame == NULL) {
(*itor)->isGlobal = true;
}
return *itor;
}
}
if(innerFrame == NULL) {
cout << "Use of undefined symbol " << name << " ";
return NULL;
}
return innerFrame->Get(name);
}
Symbol* SymbolTableFrame::GetInCurrFrame(std::string name) {
for(std::vector<Symbol*>::iterator itor = symbols.begin(); itor != symbols.end(); ++itor) {
if((*itor)->name == name) {
return *itor;
}
}
return NULL;
}
Symbol* SymbolTableFrame::RegisterSymbol(std::string name, bool isFunction, bool isType, int dimension) {
for(std::vector<Symbol*>::iterator itor = symbols.begin(); itor != symbols.end(); ++itor) {
if((*itor)->name == name) {
cout << "Redefinition of symbol " << name << " ";
return NULL;
}
}
Symbol* newSymbol = new Symbol(name, isFunction, isType, dimension);
Add(newSymbol);
if(innerFrame != NULL && innerFrame->innerFrame == NULL) {
newSymbol->isGlobal = true;
}
return newSymbol;
}
SymbolTableFrame::SymbolTableFrame(SymbolTableFrame* innerFrame) : innerFrame(innerFrame) {}