-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymboltable.cpp
More file actions
54 lines (44 loc) · 1.2 KB
/
symboltable.cpp
File metadata and controls
54 lines (44 loc) · 1.2 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
#include "symboltable.h"
#include <iostream>
// Constructor/deconstructor
SymbolTable::SymbolTable() :
_current_memory_address(5000)
{}
SymbolTable::~SymbolTable()
{}
bool SymbolTable::identifierExists(std::string identifier)
{
if (_symtable.find(identifier) == _symtable.end())
return false;
return true;
}
bool SymbolTable::typeCheck(std::string identifier, std::string type)
{
if (_symtable.find(identifier) == _symtable.end() || _symtable.at(identifier).type != type)
return false;
return true;
}
bool SymbolTable::insert(std::string identifier, std::string type)
{
SymbolTableEntry temp;
temp.memoryAddress = _current_memory_address++;
temp.type = type;
temp.identifier = identifier;
if (!identifierExists(identifier))
{
_symtable[identifier] = temp;
} else {
return false;
}
return true;
}
void SymbolTable::printTable(std::ofstream& file)
{
file << "Identifier \tMemory Location\tType\n";
for (auto x : _symtable)
file << x.first << "\t" << x.second.memoryAddress << "\t" << x.second.type << std::endl;
}
int SymbolTable::getAddress(std::string identifier)
{
return _symtable.at(identifier).memoryAddress;
}