Add the namespace table to the context

This commit is contained in:
Sameer Rahmani 2021-04-26 21:29:44 +01:00
parent c1c1421531
commit 66cecc77f3
3 changed files with 16 additions and 37 deletions

View File

@ -26,19 +26,18 @@
#define SERENE_CONTEXT_H
#include "serene/environment.h"
#include "serene/namespace.h"
#include "llvm/ADT/StringRef.h"
namespace serene {
class Namespace;
namespace exprs {
class Expression;
using Node = std::shared_ptr<Expression>;
} // namespace exprs
struct SereneContext {
// llvm::DenseMap<llvm::StringRef, Namespace> namespaces;
llvm::DenseMap<llvm::StringRef, std::unique_ptr<Namespace>> namespaces;
Environment<llvm::StringRef, exprs::Node> semanticEnv;
SereneContext(){};

View File

@ -25,43 +25,40 @@
#ifndef NAMESPACE_H
#define NAMESPACE_H
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Value.h"
#include "serene/exprs/expression.h"
#include "serene/llvm/IR/Value.h"
#include "llvm/ADT/DenseMap.h"
#include "serene/environment.h"
#include <llvm/ADT/StringRef.h>
#include <llvm/IR/Module.h>
#include <mlir/Support/LogicalResult.h>
#include <string>
#define NAMESPACE_LOG(...) \
DEBUG_WITH_TYPE("NAMESPACE", llvm::dbgs() << __VA_ARGS__ << "\n");
using ScopeMap = llvm::DenseMap<llvm::StringRef, mlir::Value>;
using PairT = std::pair<llvm::StringRef, mlir::Value>;
namespace serene {
class AExpr;
namespace exprs {
class Expression;
using Node = std::shared_ptr<Expression>;
using Ast = std::vector<Node>;
} // namespace exprs
class Namespace {
private:
exprs::Ast tree{};
bool initialized = false;
ScopeMap rootScope;
exprs::Ast tree;
public:
llvm::Optional<llvm::StringRef> filename;
mlir::StringRef name;
llvm::Optional<llvm::StringRef> filename;
/// The root environment of the namespace on the semantic analysis phase.
/// Which is a mapping from names to AST nodes ( no evaluation ).
Environment<llvm::StringRef, exprs::Node> semanticEnv;
Namespace(llvm::StringRef ns_name, llvm::Optional<llvm::StringRef> filename);
exprs::Ast &Tree();
mlir::LogicalResult setTree(exprs::Ast &);
// TODO: Fix it to return llvm::Optional<mlir::Value> instead
llvm::Optional<mlir::Value> lookup(llvm::StringRef name);
mlir::LogicalResult insert_symbol(llvm::StringRef name, mlir::Value v);
void print_scope();
~Namespace();
};

View File

@ -43,14 +43,6 @@ Namespace::Namespace(llvm::StringRef ns_name,
exprs::Ast &Namespace::Tree() { return this->tree; }
llvm::Optional<mlir::Value> Namespace::lookup(llvm::StringRef name) {
if (auto value = rootScope.lookup(name)) {
return value;
}
return llvm::None;
};
mlir::LogicalResult Namespace::setTree(exprs::Ast &t) {
if (initialized) {
return mlir::failure();
@ -60,15 +52,6 @@ mlir::LogicalResult Namespace::setTree(exprs::Ast &t) {
return mlir::success();
}
mlir::LogicalResult Namespace::insert_symbol(mlir::StringRef name,
mlir::Value v) {
rootScope.insert(PairT(name, v));
return mlir::success();
}
void Namespace::print_scope(){};
Namespace::~Namespace() {}
} // namespace serene