Extract SereneContext to the top level namespace

This commit is contained in:
Sameer Rahmani 2021-04-25 09:56:28 +01:00
parent 499852c48f
commit 4084135851
18 changed files with 36 additions and 33 deletions

View File

@ -27,6 +27,7 @@
#include "serene/reader/reader.h"
#include "serene/reader/semantics.h"
// #include "serene/sir/sir.hpp"
#include "serene/context.h"
#include <iostream>
#include <llvm/Support/CommandLine.h>
@ -73,7 +74,7 @@ int main(int argc, char *argv[]) {
throw std::move(maybeAst.getError());
}
auto &ast = maybeAst.getValue();
auto ctx = reader::makeSereneContext();
auto ctx = makeSereneContext();
auto afterAst = reader::analyze(ctx, ast);
if (afterAst) {

View File

@ -25,6 +25,7 @@
#ifndef SERENE_ERRORS_ERROR_H
#define SERENE_ERRORS_ERROR_H
#include "serene/context.h"
#include "serene/errors/constants.h"
#include "serene/exprs/expression.h"
#include "llvm/Support/Error.h"
@ -57,7 +58,7 @@ public:
static bool classof(const serene::exprs::Expression *e);
serene::exprs::maybe_node analyze(reader::SereneContext &);
serene::exprs::maybe_node analyze(SereneContext &);
~Error() = default;
};

View File

@ -25,6 +25,7 @@
#ifndef EXPRS_DEF_H
#define EXPRS_DEF_H
#include "serene/context.h"
#include "serene/errors/error.h"
#include "serene/exprs/expression.h"
#include "llvm/ADT/StringRef.h"
@ -50,7 +51,7 @@ public:
ExprType getType() const;
std::string toString() const;
maybe_node analyze(reader::SereneContext &);
maybe_node analyze(SereneContext &);
static bool classof(const Expression *e);
@ -60,7 +61,7 @@ public:
///
/// \param ctx The semantic analysis context object.
/// \param list the list containing the `def` form
static maybe_node make(reader::SereneContext &ctx, List *list);
static maybe_node make(SereneContext &ctx, List *list);
~Def() = default;
};

View File

@ -25,6 +25,7 @@
#ifndef EXPRS_EXPRESSION_H
#define EXPRS_EXPRESSION_H
#include "serene/context.h"
#include "serene/reader/location.h"
#include "serene/utils.h"
#include <memory>
@ -88,7 +89,7 @@ public:
/// to a `Def` node that represents defining a new binding.
///
/// \param ctx is the context object of the semantic analyzer.
virtual maybe_node analyze(reader::SereneContext &ctx) = 0;
virtual maybe_node analyze(SereneContext &ctx) = 0;
};
/// Create a new `node` of type `T` and forwards any given parameter

View File

@ -25,6 +25,7 @@
#ifndef EXPRS_FN_H
#define EXPRS_FN_H
#include "serene/context.h"
#include "serene/errors/error.h"
#include "serene/exprs/expression.h"
#include "serene/exprs/list.h"
@ -54,7 +55,7 @@ public:
ExprType getType() const;
std::string toString() const;
maybe_node analyze(reader::SereneContext &);
maybe_node analyze(SereneContext &);
static bool classof(const Expression *e);
@ -65,7 +66,7 @@ public:
///
/// \param ctx The semantic analysis context object.
/// \param list the list containing the `fn` form
static maybe_node make(reader::SereneContext &ctx, List *list);
static maybe_node make(SereneContext &ctx, List *list);
~Fn() = default;
};

View File

@ -25,6 +25,7 @@
#ifndef EXPRS_LIST_H
#define EXPRS_LIST_H
#include "serene/context.h"
#include "serene/exprs/expression.h"
#include "llvm/ADT/Optional.h"
#include <string>
@ -65,7 +66,7 @@ public:
std::vector<node>::iterator begin();
std::vector<node>::iterator end();
maybe_node analyze(reader::SereneContext &);
maybe_node analyze(SereneContext &);
static bool classof(const Expression *e);

View File

@ -25,6 +25,7 @@
#ifndef EXPRS_NUMBER_H
#define EXPRS_NUMBER_H
#include "serene/context.h"
#include "serene/exprs/expression.h"
#include "llvm/Support/FormatVariadic.h"
@ -48,7 +49,7 @@ struct Number : public Expression {
ExprType getType() const;
std::string toString() const;
maybe_node analyze(reader::SereneContext &ctx);
maybe_node analyze(SereneContext &ctx);
static bool classof(const Expression *e);

View File

@ -25,6 +25,7 @@
#ifndef EXPRS_SYMBOL_H
#define EXPRS_SYMBOL_H
#include "serene/context.h"
#include "serene/exprs/expression.h"
#include "llvm/ADT/StringRef.h"
#include <string>
@ -48,7 +49,7 @@ public:
static bool classof(const Expression *e);
maybe_node analyze(reader::SereneContext &);
maybe_node analyze(SereneContext &);
~Symbol() = default;
};

View File

@ -25,24 +25,18 @@
#ifndef READER_SEMANTICS_H
#define READER_SEMANTICS_H
#include "serene/context.h"
#include "serene/exprs/expression.h"
namespace serene::reader {
class SereneContext {
public:
SereneContext(){};
};
/// Creates a new semantic analysis context
SereneContext makeSereneContext();
/// This function is the entrypoint to the Semantic Analysis phase of **Serene**
/// It will call the `analyze` method on every node in the given AST and
/// returns a new AST as the result of the semantic analysis.
///
/// \param ctx The analysis context
/// \param ctx The serene context
/// \prama tree The raw AST to analyze
exprs::maybe_ast analyze(SereneContext &ctx, exprs::ast &tree);
exprs::maybe_ast analyze(serene::SereneContext &ctx, exprs::ast &tree);
}; // namespace serene::reader
#endif

View File

@ -1,6 +1,7 @@
set(HEADER_LIST
"${INCLUDE_DIR}/serene/serene.h"
"${INCLUDE_DIR}/serene/utils.h"
"${INCLUDE_DIR}/serene/context.h"
"${INCLUDE_DIR}/serene/exprs/expression.h"
"${INCLUDE_DIR}/serene/exprs/symbol.h"
@ -34,7 +35,7 @@ add_library(serene
exprs/expression.cpp
exprs/def.cpp
exprs/fn.cpp
context.cpp
serene.cpp
namespace.cpp

View File

@ -36,7 +36,7 @@ std::string Error::toString() const {
return llvm::formatv("<Error E{0}: {1}>", this->variant->id, this->message);
}
serene::exprs::maybe_node Error::analyze(reader::SereneContext &ctx) {
serene::exprs::maybe_node Error::analyze(SereneContext &ctx) {
return Result<serene::exprs::node>::success(nullptr);
};

View File

@ -38,7 +38,7 @@ std::string Def::toString() const {
this->value->toString());
}
maybe_node Def::analyze(reader::SereneContext &ctx) {
maybe_node Def::analyze(SereneContext &ctx) {
return Result<node>::success(nullptr);
};
@ -46,7 +46,7 @@ bool Def::classof(const Expression *e) {
return e->getType() == ExprType::Def;
};
maybe_node Def::make(reader::SereneContext &ctx, List *list) {
maybe_node Def::make(SereneContext &ctx, List *list) {
// TODO: Add support for docstring as the 3rd argument (4th element)
if (list->count() != 3) {

View File

@ -42,13 +42,13 @@ std::string Fn::toString() const {
this->body.empty() ? "<>" : astToString(&this->body));
}
maybe_node Fn::analyze(reader::SereneContext &ctx) {
maybe_node Fn::analyze(SereneContext &ctx) {
return Result<node>::success(nullptr);
};
bool Fn::classof(const Expression *e) { return e->getType() == ExprType::Fn; };
maybe_node Fn::make(reader::SereneContext &ctx, List *list) {
maybe_node Fn::make(SereneContext &ctx, List *list) {
// TODO: Add support for docstring as the 3rd argument (4th element)
if (list->count() < 2) {
std::string msg =

View File

@ -55,7 +55,7 @@ std::string List::toString() const {
return llvm::formatv("<List {0}>", s);
};
maybe_node List::analyze(reader::SereneContext &ctx) {
maybe_node List::analyze(SereneContext &ctx) {
if (!elements.empty()) {
auto *first = elements[0].get();

View File

@ -34,7 +34,7 @@ std::string Number::toString() const {
return llvm::formatv("<Number {0}>", value);
}
maybe_node Number::analyze(reader::SereneContext &ctx) {
maybe_node Number::analyze(SereneContext &ctx) {
return Result<node>::success(nullptr);
};

View File

@ -34,7 +34,7 @@ std::string Symbol::toString() const {
return llvm::formatv("<Symbol {0}>", this->name);
}
maybe_node Symbol::analyze(reader::SereneContext &ctx) {
maybe_node Symbol::analyze(SereneContext &ctx) {
return Result<node>::success(nullptr);
};

View File

@ -23,11 +23,11 @@
*/
#include "serene/reader/semantics.h"
#include "serene/context.h"
#include "serene/exprs/expression.h"
namespace serene::reader {
SereneContext makeSereneContext() { return SereneContext(); }
/// The entry point to the Semantic analysis phase. It calls the `analyze`
/// method of each node in the given AST and creates a new AST that contains a
/// more comprehensive set of nodes in a semantically correct AST. If the
@ -39,7 +39,7 @@ SereneContext makeSereneContext() { return SereneContext(); }
/// semantic error.
/// \param ctx The semantic analysis context
/// \param inputAst The raw AST to analyze and possibly rewrite.
exprs::maybe_ast analyze(SereneContext &ctx, exprs::ast &inputAst) {
exprs::maybe_ast analyze(serene::SereneContext &ctx, exprs::ast &inputAst) {
// TODO: Fetch the current namespace from the JIT engine later and if it is
// `nil` then the given `ast` has to start with a namespace definition.

View File

@ -80,7 +80,7 @@ TEST_CASE("List Expression", "[expression]") {
};
TEST_CASE("List semantic analysis of 'def'", "[semantic]") {
auto ctx = reader::makeSereneContext();
auto ctx = makeSereneContext();
auto ast = reader::read("(def (a) b)");
auto afterAst = reader::analyze(ctx, ast.getValue());
REQUIRE(afterAst);
@ -109,7 +109,7 @@ TEST_CASE("List semantic analysis of 'def'", "[semantic]") {
}
TEST_CASE("List semantic analysis for 'fn'", "[semantic]") {
auto ctx = reader::makeSereneContext();
auto ctx = makeSereneContext();
auto ast = reader::read("(fn)");
auto afterAst = reader::analyze(ctx, ast.getValue());
@ -150,7 +150,7 @@ TEST_CASE("List semantic analysis for 'fn'", "[semantic]") {
}
TEST_CASE("Complex semantic analysis", "[semantic]") {
auto ctx = reader::makeSereneContext();
auto ctx = makeSereneContext();
auto ast =
reader::read("(def a (fn (x) x))\n((def b (fn (x) (fn (y) y))))\n\n");