-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymtab.cpp
More file actions
197 lines (153 loc) · 4.71 KB
/
symtab.cpp
File metadata and controls
197 lines (153 loc) · 4.71 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <cassert>
#include <cstdio>
#include <utility>
#include "symtab.h"
////////////////////////////////////////////////////////////////////////
// Symbol implementation
////////////////////////////////////////////////////////////////////////
Symbol::Symbol(SymbolKind kind, const std::string &name, const std::shared_ptr<Type> &type, SymbolTable *symtab, bool is_defined)
: m_kind(kind)
, m_name(name)
, m_type(type)
, m_symtab(symtab)
, m_is_defined(is_defined),m_vreg(-1), m_offset(-1), m_on_stack(false) {
}
Symbol::~Symbol() {
}
void Symbol::set_is_defined(bool is_defined) {
m_is_defined = is_defined;
}
SymbolKind Symbol::get_kind() const {
return m_kind;
}
const std::string &Symbol::get_name() const {
return m_name;
}
std::shared_ptr<Type> Symbol::get_type() const {
return m_type;
}
SymbolTable *Symbol::get_symtab() const {
return m_symtab;
}
bool Symbol::is_defined() const {
return m_is_defined;
}
void Symbol::set_offset(unsigned int m_next_offset) {
m_on_stack = true;
m_offset = m_next_offset;
}
////////////////////////////////////////////////////////////////////////
// SymbolTable implementation
////////////////////////////////////////////////////////////////////////
SymbolTable::SymbolTable(SymbolTable *parent, std::string name)
: m_parent(parent)
, m_has_params(false) {
m_scope_name = std::move(name);
}
SymbolTable::~SymbolTable() {
}
SymbolTable *SymbolTable::get_parent() const {
return m_parent;
}
std::string SymbolTable::get_name() {
return m_scope_name;
}
bool SymbolTable::has_params() const {
return m_has_params;
}
void SymbolTable::set_has_params(bool has_params) {
m_has_params = has_params;
}
bool SymbolTable::has_symbol_local(const std::string &name) const {
return lookup_local(name) != nullptr;
}
bool SymbolTable::has_symbol_recursive(const std::string &name) const {
return lookup_recursive(name) != nullptr;
}
Symbol *SymbolTable::lookup_local(const std::string &name) const {
auto i = m_lookup.find(name);
return (i != m_lookup.end()) ? m_symbols[i->second] : nullptr;
}
Symbol *SymbolTable::get_symbol(unsigned i) const {
return m_symbols.at(i);
}
Symbol *SymbolTable::declare(SymbolKind sym_kind, const std::string &name, const std::shared_ptr<Type> &type) {
Symbol *sym = new Symbol(sym_kind, name, type, this, true);
add_symbol(sym);
return sym;
}
Symbol *SymbolTable::define(SymbolKind sym_kind, const std::string &name, const std::shared_ptr<Type> &type) {
Symbol *sym = new Symbol(sym_kind, name, type, this, false);
add_symbol(sym);
return sym;
}
Symbol *SymbolTable::define(Symbol * sym) {
add_symbol(sym);
return sym;
}
Symbol *SymbolTable::lookup_recursive(const std::string &name) const {
const SymbolTable *scope = this;
while (scope != nullptr) {
Symbol *sym = scope->lookup_local(name);
if (sym != nullptr)
return sym;
scope = scope->get_parent();
}
return nullptr;
}
Symbol *SymbolTable::lookup_recursive(const std::string &name, SymbolKind type) const {
const SymbolTable *scope = this;
while (scope != nullptr) {
Symbol *sym = scope->lookup_local(name);
if (sym != nullptr && sym->get_kind() == type)
return sym;
scope = scope->get_parent();
}
return nullptr;
}
void SymbolTable::set_fn_type(const std::shared_ptr<Type> &fn_type) {
assert(!m_fn_type);
m_fn_type = fn_type;
}
const Type *SymbolTable::get_fn_type() const {
const SymbolTable *symtab = this;
while (symtab != nullptr) {
if (m_fn_type)
return m_fn_type.get();
symtab = symtab->m_parent;
}
return nullptr;
}
void SymbolTable::add_symbol(Symbol *sym) {
assert(!has_symbol_local(sym->get_name()));
auto pos = unsigned(m_symbols.size());
m_symbols.push_back(sym);
m_lookup[sym->get_name()] = pos;
// Assignment 3 only: print out symbol table entries as they are added
// printf("%d|", get_depth());
// printf("%s|", sym->get_name().c_str());
// switch (sym->get_kind()) {
// case SymbolKind::FUNCTION:
// printf("function|"); break;
// case SymbolKind::VARIABLE:
// printf("variable|"); break;
// case SymbolKind::TYPE:
// printf("type|"); break;
// default:
// assert(false);
// }
//
// printf("%s\n", sym->get_type()->as_str().c_str());
}
int SymbolTable::get_depth() const {
int depth = 0;
const SymbolTable *symtab = m_parent;
while (symtab != nullptr) {
++depth;
symtab = symtab->m_parent;
}
return depth;
}
unsigned SymbolTable::get_num_symbols() const {
return m_symbols.size();
}