-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScope.cpp
151 lines (112 loc) · 3.76 KB
/
Scope.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
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
#include "Scope.h"
const char *Scope::VAR_PREFIX = "dato_";
const char *Scope::FUNC_PREFIX = "func_";
Scope::Scope(Scope *scope, std::string nombre) : parent(scope) {
std::vector<ParameterNode *> *args = ((FunctionNode *) scope->getSymbol(
std::string("func_") + nombre))->getParameters();
if (args)
for (auto parameter : *args)
defineVariable(parameter->getName(), new ParameterNode(parameter->getType(), parameter->getName()));
}
Scope::Scope(Scope *scope) : parent(scope) {
}
Scope *Scope::getParent() {
return this->parent;
}
bool Scope::haveSymbol(std::string symbol) {
return symbolTable.find(symbol) != symbolTable.end();
}
bool Scope::haveVariable(std::string name) {
return haveSymbol(VAR_PREFIX + name);
}
bool Scope::existsVariable(std::string name) {
return existsSymbol(VAR_PREFIX + name);
}
bool Scope::existsFunction(std::string name) {
return existsSymbol(FUNC_PREFIX + name);
}
bool Scope::existsSymbol(std::string symbol) {
return symbolTable.find(symbol) != symbolTable.end() || (this->parent != NULL && parent->existsSymbol(symbol));
}
void Scope::defineSymbol(std::string name, Node *node) {
std::cout << name << " " << node->toString() << "\n";
symbolTable.insert(std::make_pair(name, node));
}
void Scope::defineFunction(std::string name, FunctionNode *node) {
defineSymbol(FUNC_PREFIX + name, node);
}
void Scope::defineVariable(std::string name, VariableNode *node) {
defineSymbol(VAR_PREFIX + name, node);
}
Node *Scope::getSymbol(std::string symbol) {
SymbolTable::iterator it = symbolTable.find(symbol);
if (it != symbolTable.end()) return it->second;
else if (parent) return parent->getSymbol(symbol);
else return NULL;
}
VariableNode *Scope::getVariable(std::string symbol) {
return (VariableNode *) getSymbol(VAR_PREFIX + symbol);
}
FunctionNode *Scope::getFunction(std::string symbol) {
return (FunctionNode *) getSymbol(FUNC_PREFIX + symbol);
}
bool Scope::isEmpty() {
return symbolTable.empty();
}
Scope::~Scope() {
for (auto dato : symbolTable) {
//memManager->libera(dato.second->getType()->getId());
delete dato.second->getType();
delete dato.second;
}
//memManager->saleBloque();
}
std::string ParameterNode::getName() {
return name;
}
string FunctionNode::toString() {
return "funcion";
}
VariableNode::VariableNode(Type *type, int id) : type(type), id(id) {}
Category VariableNode::getCategory() {
return VARIABLE;
}
string VariableNode::toString() {
return type->toString();
}
Type *VariableNode::getType() {
return type;
}
Category ParameterNode::getCategory() {
return PARAMETER;
}
ParameterNode::ParameterNode(Type *t, const std::string &name, int id) : VariableNode(t, id), name(std::move(name)) {}
void ParameterNode::setId(int newId) {
id = newId;
}
FunctionNode::FunctionNode(int label, Type *returned, std::vector<ParameterNode *> *v) : label(label),
returned(returned),
parameters(v) {}
FunctionNode::FunctionNode(Type *returned) : returned(returned), parameters(new std::vector<ParameterNode *>) {}
Category FunctionNode::getCategory() {
return FUNCTION;
}
vector<ParameterNode *> *FunctionNode::getParameters() {
return parameters;
}
Type *FunctionNode::getType() {
return returned;
}
int FunctionNode::getLabel() {
return label;
}
std::size_t FunctionNode::paramterSize() {
if (!parameters) return 0;
std::size_t size = 0;
for (auto ¶meter : *parameters) size += parameter->getType()->size();
return size;
}
int VariableNode::getId() {
gc.flush();
return id;
}