Move the Error expression to it's own namespace

This commit is contained in:
Sameer Rahmani 2021-04-19 23:34:39 +01:00
parent a486b03d10
commit 17b778bc49
8 changed files with 82 additions and 109 deletions

View File

@ -1,7 +1,7 @@
/**
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -22,25 +22,23 @@
* SOFTWARE.
*/
#include "serene/error.hpp"
#include "serene/expr.hpp"
#include "serene/llvm/IR/Value.h"
#include "serene/namespace.h"
#include "serene/state.hpp"
#include <assert.h>
#include <fmt/core.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Type.h>
#ifndef SERENE_ERRORS_CONSTANTS_H
#define SERENE_ERRORS_CONSTANTS_H
#include <map>
#include <string>
using namespace std;
using namespace llvm;
namespace serene {
namespace errors {
enum ErrID {
E0000,
E0001,
string Error::string_repr() const { return fmt::format("Error: {}", msg); }
};
string Error::dumpAST() const { return fmt::format("<Error: {}>", this->msg); }
static std::map<ErrID, std::string> ErrDesc = {
{E0000, "Can't find any description for this error.\n"}, {E0001, ""}};
Error::~Error() { EXPR_LOG("Destroying Error"); }
} // namespace errors
} // namespace serene
#endif

View File

@ -25,6 +25,48 @@
#ifndef SERENE_ERRORS_ERROR_H
#define SERENE_ERRORS_ERROR_H
#include "serene/errors/constants.h"
#include "serene/exprs/expression.h"
#include "llvm/Support/Error.h"
namespace serene::errors {
/// This enum represent the expression type and **not** the value type.
enum class ErrType {
Syntax,
Semantic,
Compile,
};
/// This data structure represent the Lisp error. This type of expression
/// doesn't show up in the AST but the compiler might rewrite the AST
/// to contains error expressions
class Error : public ::serene::exprs::Expression {
public:
ErrID id;
ErrType errorType;
serene::exprs::node target;
std::string message;
Error(ErrType err, serene::exprs::node &t, llvm::StringRef msg)
: serene::exprs::Expression(t->location), errorType(err), target(t),
message(msg){};
// Error(reader::LocationRange &loc, ErrType err, node &t, llvm::StringRef
// msg)
// : Expression(loc), errorType(err), target(t), message(msg) {};
serene::exprs::ExprType getType() const;
std::string toString() const;
static bool classof(const serene::exprs::Expression *e);
serene::exprs::maybe_node analyze(reader::SemanticContext &);
~Error() = default;
};
}; // namespace serene::errors
#endif

View File

@ -1,73 +0,0 @@
/* -*- C++ -*-
* Serene programming language.
*
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef EXPRS_ERROR_H
#define EXPRS_ERROR_H
#include "serene/exprs/expression.h"
#include "llvm/ADT/StringRef.h"
#include <string>
namespace serene {
namespace exprs {
/// This enum represent the expression type and **not** the value type.
enum class ErrType {
Syntax,
Semantic,
Compile,
};
/// This data structure represent the Lisp error. This type of expression
/// doesn't show up in the AST but the compiler might rewrite the AST
/// to contains error expressions
class Error : public Expression {
public:
ErrType errorType;
node target;
std::string message;
Error(ErrType err, node &t, llvm::StringRef msg)
: Expression(t->location), errorType(err), target(t), message(msg){};
// Error(reader::LocationRange &loc, ErrType err, node &t, llvm::StringRef
// msg)
// : Expression(loc), errorType(err), target(t), message(msg) {};
ExprType getType() const;
std::string toString() const;
static bool classof(const Expression *e);
maybe_node analyze(reader::SemanticContext &);
~Error() = default;
};
} // namespace exprs
} // namespace serene
#endif

View File

@ -7,7 +7,6 @@ set(HEADER_LIST
"${INCLUDE_DIR}/serene/exprs/list.h"
"${INCLUDE_DIR}/serene/exprs/number.h"
"${INCLUDE_DIR}/serene/exprs/def.h"
"${INCLUDE_DIR}/serene/exprs/error.h"
# Reader
"${INCLUDE_DIR}/serene/reader/reader.h"
@ -18,6 +17,7 @@ set(HEADER_LIST
"${INCLUDE_DIR}/serene/errors.h"
"${INCLUDE_DIR}/serene/errors/error.h"
"${INCLUDE_DIR}/serene/errors/errc.h"
"${INCLUDE_DIR}/serene/errors/constants.h"
"${INCLUDE_DIR}/serene/expr.hpp"
@ -42,14 +42,13 @@ add_library(serene
exprs/number.cpp
exprs/expression.cpp
exprs/def.cpp
exprs/error.cpp
serene.cpp
symbol.cpp
list.cpp
namespace.cpp
state.cpp
error.cpp
number.cpp
# Reader
@ -58,6 +57,9 @@ add_library(serene
reader/errors.cpp
reader/semantics.cpp
# Errors
errors/error.cpp
# IR
sir/sir.cpp
sir/dialect.cpp

View File

@ -22,13 +22,15 @@
* SOFTWARE.
*/
#include "serene/exprs/error.h"
#include "serene/errors/error.h"
#include "llvm/Support/FormatVariadic.h"
namespace serene {
namespace exprs {
namespace errors {
ExprType Error::getType() const { return ExprType::Error; };
serene::exprs::ExprType Error::getType() const {
return serene::exprs::ExprType::Error;
};
std::string Error::toString() const {
return llvm::formatv("<Error [loc: {0} | {1}]: {2}>",
@ -36,13 +38,13 @@ std::string Error::toString() const {
this->location.end.toString(), this->message);
}
maybe_node Error::analyze(reader::SemanticContext &ctx) {
return Result<node>::Success(nullptr);
serene::exprs::maybe_node Error::analyze(reader::SemanticContext &ctx) {
return Result<serene::exprs::node>::Success(nullptr);
};
bool Error::classof(const Expression *e) {
return e->getType() == ExprType::Error;
bool Error::classof(const serene::exprs::Expression *e) {
return e->getType() == serene::exprs::ExprType::Error;
};
} // namespace exprs
} // namespace errors
} // namespace serene

View File

@ -24,20 +24,23 @@
#include "../test_helpers.cpp.inc"
#include "serene/exprs/symbol.h"
#include "serene/exprs/error.h"
#include "serene/errors.h"
#include "llvm/Support/Casting.h"
#include <catch2/catch.hpp>
namespace serene {
namespace exprs {
namespace errors {
TEST_CASE("Error Expression", "[expression]") {
std::unique_ptr<reader::LocationRange> range(dummyLocation());
node sym = make<Symbol>(*range.get(), llvm::StringRef("example"));
node err = make<Error>(ErrType::Semantic, sym, "Something Failed");
exprs::node sym = exprs::make<exprs::Symbol>(*range.get(), llvm::StringRef("example"));
exprs::node err = exprs::make<Error>(ErrType::Semantic, sym, "Something Failed");
REQUIRE(err->getType() == ExprType::Error);
REQUIRE(err->getType() == exprs::ExprType::Error);
CHECK(err->toString() == "<Error [loc: 2:20:40 | 3:30:80]: Something Failed>");
auto error = llvm::dyn_cast<Error>(err.get());

View File

@ -23,7 +23,7 @@
*/
#define CATCH_CONFIG_MAIN
#include "./exprs/error_tests.cpp.inc"
#include "./errors/error_tests.cpp.inc"
#include "./exprs/expression_tests.cpp.inc"
#include "./exprs/list_tests.cpp.inc"
#include "./exprs/number_tests.cpp.inc"

View File

@ -29,7 +29,7 @@
#include "serene/reader/location.h"
namespace serene {
namespace exprs {
reader::LocationRange *dummyLocation() {
reader::Location start;
@ -46,7 +46,6 @@ reader::LocationRange *dummyLocation() {
return new reader::LocationRange(start, end);
};
} // namespace exprs
} // namespace serene
#endif