Change the storage type in Environment to StringMap

This commit is contained in:
Sameer Rahmani 2022-03-05 16:03:19 +00:00
parent 500f366bab
commit 7d7293aa32
5 changed files with 22 additions and 20 deletions

View File

@ -20,8 +20,9 @@
#define SERENE_ENVIRONMENT_H
#include "serene/llvm/patches.h"
#include "serene/utils.h"
#include <llvm/ADT/DenseMap.h>
#include <llvm/ADT/StringMap.h>
#include <mlir/Support/LogicalResult.h>
namespace serene {
@ -29,12 +30,12 @@ namespace serene {
/// This class represents a classic lisp environment (or scope) that holds the
/// bindings from type `K` to type `V`. For example an environment of symbols
/// to expressions would be `Environment<Symbol, Node>`
template <typename K, typename V>
template <typename V>
class Environment {
Environment<K, V> *parent;
Environment<V> *parent;
using StorageType = llvm::DenseMap<K, V>;
using StorageType = llvm::StringMap<V>;
// The actual bindings storage
StorageType pairs;
@ -43,7 +44,7 @@ public:
Environment(Environment *parent) : parent(parent){};
/// Look up the given `key` in the environment and return it.
llvm::Optional<V> lookup(K key) {
llvm::Optional<V> lookup(llvm::StringRef key) {
if (auto value = pairs.lookup(key)) {
return value;
}
@ -57,8 +58,9 @@ public:
/// Insert the given `key` with the given `value` into the storage. This
/// operation will shadow an aleady exist `key` in the parent environment
mlir::LogicalResult insert_symbol(K key, V value) {
pairs.insert(std::pair<K, V>(key, value));
mlir::LogicalResult insert_symbol(llvm::StringRef key, V value) {
auto result = pairs.insert_or_assign(key, value);
UNUSED(result);
return mlir::success();
};

View File

@ -80,7 +80,7 @@ using NSPtr = std::shared_ptr<Namespace>;
using MaybeNS = llvm::Expected<NSPtr>;
using MaybeModule = llvm::Optional<llvm::orc::ThreadSafeModule>;
using MaybeModuleOp = llvm::Optional<mlir::OwningOpRef<mlir::ModuleOp>>;
using SemanticEnv = Environment<std::string, exprs::Node>;
using SemanticEnv = Environment<exprs::Node>;
using SemanticEnvPtr = std::unique_ptr<SemanticEnv>;
using SemanticEnvironments = std::vector<SemanticEnvPtr>;
using Form = std::pair<SemanticEnv, exprs::Ast>;

View File

@ -36,7 +36,7 @@ using Ast = std::vector<Node>;
}; // namespace exprs
class Namespace;
using SemanticEnv = Environment<std::string, exprs::Node>;
using SemanticEnv = Environment<exprs::Node>;
namespace semantics {
using AnalyzeResult = llvm::Expected<exprs::Ast>;

View File

@ -46,8 +46,8 @@ add_library(serene
semantics.cpp
# jit.cpp
jit/engine.cpp
jit/layers.cpp
# jit/engine.cpp
# jit/layers.cpp
jit/halley.cpp
errors.cpp

View File

@ -41,7 +41,7 @@ TEST_CASE("Environment tests", "[environment]") {
exprs::Node sym = exprs::make<exprs::Symbol>(*range, "example", "ns");
Environment<llvm::StringRef, exprs::Node> e;
Environment<exprs::Node> e;
llvm::Optional<exprs::Node> result = e.lookup("a");
REQUIRE_FALSE(result.hasValue());
@ -61,7 +61,7 @@ TEST_CASE("Environment tests", "[environment]") {
CHECK(fetchedSym->nsName == "ns");
SECTION("Testing the environment copy") {
Environment<llvm::StringRef, exprs::Node> e1(&e);
Environment<exprs::Node> e1(&e);
result = e1.lookup("b");
REQUIRE_FALSE(result.hasValue());
@ -74,27 +74,27 @@ TEST_CASE("Environment tests", "[environment]") {
};
TEST_CASE("Test generic Environment usage", "[environment]") {
Environment<int, char> env;
Environment<char> env;
auto result = env.lookup(3);
auto result = env.lookup("blah");
REQUIRE_FALSE(result);
auto status = env.insert_symbol(3, 'A');
auto status = env.insert_symbol("blah", 'A');
REQUIRE(status.succeeded());
result = env.lookup(3);
result = env.lookup("blah");
REQUIRE(result);
CHECK(*result == 'A');
result = env.lookup(3);
result = env.lookup("blah");
REQUIRE(result);
CHECK(*result == 'A');
// Test the overwrite functionality
status = env.insert_symbol(3, 'B');
status = env.insert_symbol("blah", 'B');
REQUIRE(status.succeeded());
result = env.lookup(3);
result = env.lookup("blah");
REQUIRE(result);
CHECK(*result == 'B');
}