Add support for current namespace in the context
This commit is contained in:
parent
f2a76aa007
commit
6ce565163c
|
@ -39,12 +39,23 @@ using Node = std::shared_ptr<Expression>;
|
|||
class SereneContext {
|
||||
std::map<std::string, std::shared_ptr<Namespace>> namespaces;
|
||||
|
||||
// Why string vs pointer? We might rewrite the namespace and
|
||||
// holding a pointer means that it might point to the old version
|
||||
std::string current_ns;
|
||||
|
||||
public:
|
||||
/// Insert the given `ns` into the context. The Context object is
|
||||
/// the owner of all the namespaces. The `ns` will overwrite any
|
||||
/// namespace with the same name.
|
||||
void insertNS(std::shared_ptr<Namespace> ns);
|
||||
|
||||
/// Sets the name of the current namespace in the context and return
|
||||
/// a boolean indicating the status of this operation. The operation
|
||||
/// will fail if the namespace does not exist in the namespace table.
|
||||
bool setCurrentNS(llvm::StringRef ns_name);
|
||||
|
||||
std::shared_ptr<Namespace> getCurrentNS();
|
||||
|
||||
std::shared_ptr<Namespace> getNS(llvm::StringRef ns_name);
|
||||
SereneContext(){};
|
||||
};
|
||||
|
|
|
@ -39,5 +39,24 @@ std::shared_ptr<Namespace> SereneContext::getNS(llvm::StringRef ns_name) {
|
|||
return nullptr;
|
||||
};
|
||||
|
||||
bool SereneContext::setCurrentNS(llvm::StringRef ns_name) {
|
||||
if (namespaces.count(ns_name.str())) {
|
||||
this->current_ns = ns_name;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
std::shared_ptr<Namespace> SereneContext::getCurrentNS() {
|
||||
assert(!this->current_ns.empty() && "Current namespace is not set");
|
||||
|
||||
if (namespaces.count(this->current_ns)) {
|
||||
return namespaces[this->current_ns];
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
};
|
||||
|
||||
SereneContext makeSereneContext() { return SereneContext(); };
|
||||
}; // namespace serene
|
||||
|
|
|
@ -87,8 +87,15 @@ MaybeNode Def::make(SereneContext &ctx, List *list) {
|
|||
return value;
|
||||
}
|
||||
|
||||
// auto result = ctx.getCurrentNS()->semanticEnv.insert_symbol(binding->name,
|
||||
// analyzedValue);
|
||||
|
||||
// if (result.succeeded()) {
|
||||
Node def = exprs::make<Def>(list->location, binding->name, analyzedValue);
|
||||
return Result<Node>::success(def);
|
||||
// } else {
|
||||
// return Result<Node>::error()
|
||||
// }
|
||||
};
|
||||
} // namespace exprs
|
||||
} // namespace serene
|
||||
|
|
|
@ -59,5 +59,17 @@ TEST_CASE("Context tests", "[context]") {
|
|||
|
||||
};
|
||||
|
||||
TEST_CASE("Get and Set current namespace", "[context]") {
|
||||
auto ctx = makeSereneContext();
|
||||
auto userNs = makeNamespace(ctx, "user", llvm::Optional<llvm::StringRef>("/some/file"));
|
||||
|
||||
auto isSet = ctx.setCurrentNS("user");
|
||||
|
||||
REQUIRE(isSet);
|
||||
CHECK(ctx.getCurrentNS() == userNs);
|
||||
|
||||
isSet = ctx.setCurrentNS("user1");
|
||||
REQUIRE_FALSE(isSet);
|
||||
CHECK(ctx.getCurrentNS() == userNs);
|
||||
};
|
||||
} // namespace serene
|
||||
|
|
Loading…
Reference in New Issue