From 6ce565163cf811381af60029ae3e5d44f3787ae6 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Thu, 29 Apr 2021 23:39:58 +0100 Subject: [PATCH] Add support for current namespace in the context --- include/serene/context.h | 11 +++++++++++ src/serene/context.cpp | 19 +++++++++++++++++++ src/serene/exprs/def.cpp | 7 +++++++ src/tests/context_tests.cpp.inc | 12 ++++++++++++ 4 files changed, 49 insertions(+) diff --git a/include/serene/context.h b/include/serene/context.h index 27ae48f..18ec914 100644 --- a/include/serene/context.h +++ b/include/serene/context.h @@ -39,12 +39,23 @@ using Node = std::shared_ptr; class SereneContext { std::map> 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 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 getCurrentNS(); + std::shared_ptr getNS(llvm::StringRef ns_name); SereneContext(){}; }; diff --git a/src/serene/context.cpp b/src/serene/context.cpp index c1a1c2f..8fd6c78 100644 --- a/src/serene/context.cpp +++ b/src/serene/context.cpp @@ -39,5 +39,24 @@ std::shared_ptr 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 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 diff --git a/src/serene/exprs/def.cpp b/src/serene/exprs/def.cpp index ecfba68..3b6d0df 100644 --- a/src/serene/exprs/def.cpp +++ b/src/serene/exprs/def.cpp @@ -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(list->location, binding->name, analyzedValue); return Result::success(def); + // } else { + // return Result::error() + // } }; } // namespace exprs } // namespace serene diff --git a/src/tests/context_tests.cpp.inc b/src/tests/context_tests.cpp.inc index 5b004f6..1dd1b50 100644 --- a/src/tests/context_tests.cpp.inc +++ b/src/tests/context_tests.cpp.inc @@ -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("/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