Skip to content

Commit c45fabf

Browse files
committed
Access global objects
1 parent 9428ac2 commit c45fabf

File tree

11 files changed

+57
-38
lines changed

11 files changed

+57
-38
lines changed

src/nodes/functions/function.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ FunctionNode::FunctionNode(
2626

2727
std::unique_ptr<NodeResult> FunctionNode::evaluate(PSC::Context &ctx) {
2828
if (ctx.getFunction(functionName) != nullptr)
29-
throw PSC::RedeclarationError(token, ctx, functionName);
29+
throw PSC::RedefinitionError(token, ctx, functionName);
3030

3131
PSC::DataType returnDataType = ctx.getType(returnType);
3232
if (returnDataType == PSC::DataType::NONE)

src/nodes/functions/procedure.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ProcedureNode::ProcedureNode(
2424

2525
std::unique_ptr<NodeResult> ProcedureNode::evaluate(PSC::Context &ctx) {
2626
if (ctx.getProcedure(procedureName) != nullptr)
27-
throw PSC::RedeclarationError(token, ctx, procedureName);
27+
throw PSC::RedefinitionError(token, ctx, procedureName);
2828

2929
size_t parametersSize = parameterNames.size();
3030
std::vector<PSC::Parameter> parameters;

src/nodes/variable/array.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ std::unique_ptr<NodeResult> ArrayDeclareNode::evaluate(PSC::Context &ctx) {
1515
if (bounds.size() % 2 != 0 || bounds.size() == 0) std::abort();
1616

1717
for (auto identifier : identifiers) {
18-
if (ctx.getArray(identifier->value) != nullptr)
18+
if (ctx.getArray(identifier->value, false) != nullptr)
1919
throw PSC::RedeclarationError(token, ctx, identifier->value);
2020
}
2121

src/nodes/variable/composite.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ CompositeDefineNode::CompositeDefineNode(const Token &token, const Token &name,
88
: Node(token), name(name), initBlock(initBlock) {}
99

1010
std::unique_ptr<NodeResult> CompositeDefineNode::evaluate(PSC::Context &ctx) {
11-
if (ctx.isIdentifierType(name))
12-
throw PSC::RuntimeError(token, ctx, "Redefinition of type '" + name.value + "'");
11+
if (ctx.isIdentifierType(name, false))
12+
throw PSC::RedefinitionError(token, ctx, name.value);
1313

1414
PSC::CompositeTypeDefinition definition(name.value, initBlock);
1515
ctx.createCompositeDefinition(std::move(definition));

src/nodes/variable/enum.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ EnumDefineNode::EnumDefineNode(const Token &token, const Token &name, std::vecto
88
: Node(token), name(name), values(std::move(values)) {}
99

1010
std::unique_ptr<NodeResult> EnumDefineNode::evaluate(PSC::Context &ctx) {
11-
if (ctx.isIdentifierType(name))
12-
throw PSC::RuntimeError(token, ctx, "Redefinition of type '" + name.value + "'");
11+
if (ctx.isIdentifierType(name, false))
12+
throw PSC::RedefinitionError(token, ctx, name.value);
1313

1414
PSC::EnumTypeDefinition definition(name.value, std::move(values));
1515
ctx.createEnumDefinition(std::move(definition));

src/nodes/variable/pointer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ std::unique_ptr<NodeResult> PointerDefineNode::evaluate(PSC::Context &ctx) {
1212
if (pointerType == PSC::DataType::NONE)
1313
throw PSC::NotDefinedError(token, ctx, "Type '" + type.value + "'");
1414

15-
if (ctx.isIdentifierType(name))
16-
throw PSC::RuntimeError(token, ctx, "Redefinition of type '" + name.value + "'");
15+
if (ctx.isIdentifierType(name, false))
16+
throw PSC::RedefinitionError(token, ctx, name.value);
1717

1818
PSC::PointerTypeDefinition definition(name.value, pointerType);
1919
ctx.createPointerDefinition(std::move(definition));

src/nodes/variable/variable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ DeclareNode::DeclareNode(const Token &token, std::vector<const Token*> &&identif
99

1010
std::unique_ptr<NodeResult> DeclareNode::evaluate(PSC::Context &ctx) {
1111
for (auto identifier : identifiers) {
12-
if (ctx.getVariable(identifier->value) != nullptr)
12+
if (ctx.getVariable(identifier->value, false) != nullptr)
1313
throw PSC::RedeclarationError(token, ctx, identifier->value);
1414

1515
if (ctx.isIdentifierType(*identifier))
@@ -33,7 +33,7 @@ ConstDeclareNode::ConstDeclareNode(const Token &token, Node &node, const Token &
3333
std::unique_ptr<NodeResult> ConstDeclareNode::evaluate(PSC::Context &ctx) {
3434
auto value = node.evaluate(ctx);
3535

36-
if (ctx.getVariable(identifier.value) != nullptr)
36+
if (ctx.getVariable(identifier.value, false) != nullptr)
3737
throw PSC::RedeclarationError(token, ctx, identifier.value);
3838

3939
ctx.addVariable(new PSC::Variable(identifier.value, value->type, true, &ctx, value->data.get()));

src/psc/error.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ RedeclarationError::RedeclarationError(const Token &token, const Context &contex
9595
: RuntimeError(token, context, "Redeclaration of '" + identifier + "'")
9696
{}
9797

98+
RedefinitionError::RedefinitionError(const Token &token, const Context &context, const std::string &identifier)
99+
: RuntimeError(token, context, "Redefinition of '" + identifier + "'")
100+
{}
101+
98102
ConstAssignError::ConstAssignError(const Token &token, const Context &context, const std::string &constant)
99103
: RuntimeError(token, context, "Assignment to constant '" + constant + "'")
100104
{}

src/psc/error.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ namespace PSC {
7979
RedeclarationError(const Token &token, const Context &context, const std::string &identifier);
8080
};
8181

82+
class RedefinitionError : public RuntimeError {
83+
public:
84+
RedefinitionError(const Token &token, const Context &context, const std::string &identifier);
85+
};
86+
8287
class ConstAssignError : public RuntimeError {
8388
public:
8489
ConstAssignError(const Token &token, const Context &context, const std::string &constant);

src/psc/scope/context.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ Context *Context::getParent() const {
9090
return parent;
9191
}
9292

93+
Context *Context::getGlobalContext() {
94+
Context *ctx = this;
95+
while (ctx->parent != nullptr) {ctx = ctx->parent;}
96+
return ctx;
97+
}
98+
9399
const std::string &Context::getName() const {
94100
return name;
95101
}
@@ -98,12 +104,13 @@ void Context::addVariable(Variable *variable) {
98104
variables.emplace_back(variable);
99105
}
100106

101-
Variable *Context::getVariable(const std::string &varName) {
107+
Variable *Context::getVariable(const std::string &varName, bool global) {
102108
for (auto &var : variables) {
103109
if (var->name == varName) return var.get();
104110
}
105111

106-
return nullptr;
112+
if (!global || parent == nullptr) return nullptr;
113+
return getGlobalContext()->getVariable(varName);
107114
}
108115

109116
void Context::addProcedure(std::unique_ptr<Procedure> &&procedure) {
@@ -142,17 +149,18 @@ void Context::addArray(std::unique_ptr<Array> &&array) {
142149
arrays.emplace_back(std::move(array));
143150
}
144151

145-
Array *Context::getArray(const std::string &arrayName) {
152+
Array *Context::getArray(const std::string &arrayName, bool global) {
146153
for (auto &array : arrays) {
147154
if (arrayName == array->name) {
148155
return array.get();
149156
}
150157
}
151158

152-
return nullptr;
159+
if (!global || parent == nullptr) return nullptr;
160+
return getGlobalContext()->getArray(arrayName);
153161
}
154162

155-
PSC::DataType Context::getType(const Token &token) {
163+
PSC::DataType Context::getType(const Token &token, bool global) {
156164
if (token.type == TokenType::DATA_TYPE) {
157165
if (token.value == "INTEGER") return PSC::DataType(PSC::DataType::INTEGER);
158166
else if (token.value == "REAL") return PSC::DataType(PSC::DataType::REAL);
@@ -161,15 +169,15 @@ PSC::DataType Context::getType(const Token &token) {
161169
else if (token.value == "STRING") return PSC::DataType(PSC::DataType::STRING);
162170
else std::abort();
163171
} else if (token.type == TokenType::IDENTIFIER) {
164-
auto *enumDefinition = getEnumDefinition(token.value);
172+
auto *enumDefinition = getEnumDefinition(token.value, global);
165173
if (enumDefinition != nullptr)
166174
return PSC::DataType(PSC::DataType::ENUM, &enumDefinition->name);
167175

168-
auto *pointerDefinition = getPointerDefinition(token.value);
176+
auto *pointerDefinition = getPointerDefinition(token.value, global);
169177
if (pointerDefinition != nullptr)
170178
return PSC::DataType(PSC::DataType::POINTER, &pointerDefinition->name);
171179

172-
auto *compositeDefinition = getCompositeDefinition(token.value);
180+
auto *compositeDefinition = getCompositeDefinition(token.value, global);
173181
if (compositeDefinition != nullptr)
174182
return PSC::DataType(PSC::DataType::COMPOSITE, &compositeDefinition->name);
175183

@@ -178,19 +186,19 @@ PSC::DataType Context::getType(const Token &token) {
178186
std::abort();
179187
}
180188

181-
bool Context::isIdentifierType(const Token &identifier) {
182-
PSC::DataType dataType = getType(identifier);
189+
bool Context::isIdentifierType(const Token &identifier, bool global) {
190+
PSC::DataType dataType = getType(identifier, global);
183191
if (dataType != PSC::DataType::NONE)
184192
return true;
185193

186-
std::unique_ptr<PSC::Enum> enumEl(getEnumElement(identifier.value));
194+
std::unique_ptr<PSC::Enum> enumEl(getEnumElement(identifier.value, global));
187195
if (enumEl.get() != nullptr)
188196
return true;
189197

190198
return false;
191199
}
192200

193-
Enum *Context::getEnumElement(const std::string &value) {
201+
Enum *Context::getEnumElement(const std::string &value, bool global) {
194202
for (auto &definition : enums) {
195203
for (size_t i = 0; i < definition->values.size(); i++) {
196204
if (definition->values[i] == value) {
@@ -200,7 +208,7 @@ Enum *Context::getEnumElement(const std::string &value) {
200208
}
201209
}
202210
}
203-
if (parent != nullptr) return parent->getEnumElement(value);
211+
if (global && parent != nullptr) return getGlobalContext()->getEnumElement(value);
204212
return nullptr;
205213
}
206214

@@ -216,26 +224,26 @@ void Context::createCompositeDefinition(CompositeTypeDefinition &&definition) {
216224
composites.emplace_back(std::make_unique<CompositeTypeDefinition>(std::move(definition)));
217225
}
218226

219-
const EnumTypeDefinition *Context::getEnumDefinition(const std::string &name) {
227+
const EnumTypeDefinition *Context::getEnumDefinition(const std::string &name, bool global) {
220228
for (const auto &e : enums) {
221229
if (e->name == name) return e.get();
222230
}
223-
if (parent != nullptr) return parent->getEnumDefinition(name);
231+
if (global && parent != nullptr) return getGlobalContext()->getEnumDefinition(name);
224232
return nullptr;
225233
}
226234

227-
const PointerTypeDefinition *Context::getPointerDefinition(const std::string &name) {
235+
const PointerTypeDefinition *Context::getPointerDefinition(const std::string &name, bool global) {
228236
for (const auto &p : pointers) {
229237
if (p->name == name) return p.get();
230238
}
231-
if (parent != nullptr) return parent->getPointerDefinition(name);
239+
if (global && parent != nullptr) return getGlobalContext()->getPointerDefinition(name);
232240
return nullptr;
233241
}
234242

235-
const CompositeTypeDefinition *Context::getCompositeDefinition(const std::string &name) {
243+
const CompositeTypeDefinition *Context::getCompositeDefinition(const std::string &name, bool global) {
236244
for (const auto &c : composites) {
237245
if (c->name == name) return c.get();
238246
}
239-
if (parent != nullptr) return parent->getCompositeDefinition(name);
247+
if (global && parent != nullptr) return getGlobalContext()->getCompositeDefinition(name);
240248
return nullptr;
241249
}

0 commit comments

Comments
 (0)