This repository has been archived by the owner on Jan 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_SymbolManager.cpp
104 lines (94 loc) · 2.5 KB
/
symbol_SymbolManager.cpp
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
//
// Created by JacquesdeH on 2020/10/2.
//
#include <iostream>
#include "symbol_SymbolManager.h"
#include "functional_strext.h"
symbol::SymbolManager::SymbolManager()
{
this->tables.clear();
this->tables.emplace_back();
this->curTable = 0;
}
bool symbol::SymbolManager::hasSymbolInScope(const string &symbol) const
{
return tables[curTable].hasKey(toLower(symbol));
}
bool symbol::SymbolManager::hasSymbolInAll(const string &symbol) const
{
string lowerSymbol = toLower(symbol);
int pTable = curTable;
while (pTable >= 0)
{
if (tables[pTable].hasKey(lowerSymbol))
return true;
pTable--;
}
return false;
}
bool symbol::SymbolManager::declareSymbol(const string& symbol, const symbol::Info &info)
{
string lowerSymbol = toLower(symbol);
if (hasSymbolInScope(lowerSymbol))
{
if (config::USE_STDERR)
std::cerr << "Declaring existing idenfr in scope dup definition" << std::endl;
return false;
}
tables[curTable].insertRecord(lowerSymbol, info);
return true;
}
symbol::Info& symbol::SymbolManager::getInfoInAll(const string &symbol) const
{
string lowerSymbol = toLower(symbol);
static Info ret(config::SymbolType::SYMBOL_DEFAULT, config::DataType::DATA_DEFAULT, 0);
int pTable = curTable;
while (pTable >= 0)
{
if (tables[pTable].hasKey(lowerSymbol))
{
return tables[pTable].getInfo(lowerSymbol);
break;
}
pTable--;
}
if (pTable < 0)
{
// Error!
if (config::USE_STDERR)
std::cerr << "Unable to find " << symbol << " in getInfoInAll() " << std::endl;
}
return ret;
}
void symbol::SymbolManager::pushNewScope()
{
tables.emplace_back();
curTable++;
}
void symbol::SymbolManager::popCurScope()
{
tables.erase(tables.begin() + curTable);
curTable--;
}
symbol::Info &symbol::SymbolManager::getInfoFromLastScope(const string &symbol) const
{
string lowerSymbol = toLower(symbol);
static Info ret(config::SymbolType::SYMBOL_DEFAULT, config::DataType::DATA_DEFAULT, 0);
int pTable = curTable - 1;
while (pTable >= 0)
{
if (tables[pTable].hasKey(lowerSymbol))
{
return tables[pTable].getInfo(lowerSymbol);
break;
}
pTable--;
}
if (pTable < 0)
{
// Error!
if (config::USE_STDERR)
std::cerr << "Unable to find " << symbol << " in getInfoFromLastScope() " << std::endl;
}
return ret;
}