From 17b778bc49b699fe8e9d49e63304002aa735236c Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Mon, 19 Apr 2021 23:34:39 +0100 Subject: [PATCH] Move the Error expression to it's own namespace --- .../serene/errors/constants.h | 32 ++++---- include/serene/errors/error.h | 42 +++++++++++ include/serene/exprs/error.h | 73 ------------------- src/serene/CMakeLists.txt | 8 +- src/serene/{exprs => errors}/error.cpp | 18 +++-- .../{exprs => errors}/error_tests.cpp.inc | 13 ++-- src/tests/serenetests.cpp | 2 +- src/tests/test_helpers.cpp.inc | 3 +- 8 files changed, 82 insertions(+), 109 deletions(-) rename src/serene/error.cpp => include/serene/errors/constants.h (67%) delete mode 100644 include/serene/exprs/error.h rename src/serene/{exprs => errors}/error.cpp (78%) rename src/tests/{exprs => errors}/error_tests.cpp.inc (86%) diff --git a/src/serene/error.cpp b/include/serene/errors/constants.h similarity index 67% rename from src/serene/error.cpp rename to include/serene/errors/constants.h index 708ac9b..90503f0 100644 --- a/src/serene/error.cpp +++ b/include/serene/errors/constants.h @@ -1,7 +1,7 @@ -/** +/* -*- C++ -*- * Serene programming language. * - * Copyright (c) 2020 Sameer Rahmani + * Copyright (c) 2019-2021 Sameer Rahmani * * 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 -#include -#include -#include +#ifndef SERENE_ERRORS_CONSTANTS_H +#define SERENE_ERRORS_CONSTANTS_H + +#include #include -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("", this->msg); } +static std::map ErrDesc = { + {E0000, "Can't find any description for this error.\n"}, {E0001, ""}}; -Error::~Error() { EXPR_LOG("Destroying Error"); } +} // namespace errors } // namespace serene +#endif diff --git a/include/serene/errors/error.h b/include/serene/errors/error.h index 26c6d9f..f9b9aa1 100644 --- a/include/serene/errors/error.h +++ b/include/serene/errors/error.h @@ -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 diff --git a/include/serene/exprs/error.h b/include/serene/exprs/error.h deleted file mode 100644 index d6623b9..0000000 --- a/include/serene/exprs/error.h +++ /dev/null @@ -1,73 +0,0 @@ -/* -*- C++ -*- - * Serene programming language. - * - * Copyright (c) 2019-2021 Sameer Rahmani - * - * 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 - -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 diff --git a/src/serene/CMakeLists.txt b/src/serene/CMakeLists.txt index 1168f35..253653b 100644 --- a/src/serene/CMakeLists.txt +++ b/src/serene/CMakeLists.txt @@ -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 diff --git a/src/serene/exprs/error.cpp b/src/serene/errors/error.cpp similarity index 78% rename from src/serene/exprs/error.cpp rename to src/serene/errors/error.cpp index a29f93a..2e669cf 100644 --- a/src/serene/exprs/error.cpp +++ b/src/serene/errors/error.cpp @@ -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("", @@ -36,13 +38,13 @@ std::string Error::toString() const { this->location.end.toString(), this->message); } -maybe_node Error::analyze(reader::SemanticContext &ctx) { - return Result::Success(nullptr); +serene::exprs::maybe_node Error::analyze(reader::SemanticContext &ctx) { + return Result::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 diff --git a/src/tests/exprs/error_tests.cpp.inc b/src/tests/errors/error_tests.cpp.inc similarity index 86% rename from src/tests/exprs/error_tests.cpp.inc rename to src/tests/errors/error_tests.cpp.inc index 122f7dd..32b7772 100644 --- a/src/tests/exprs/error_tests.cpp.inc +++ b/src/tests/errors/error_tests.cpp.inc @@ -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 + + namespace serene { -namespace exprs { +namespace errors { + TEST_CASE("Error Expression", "[expression]") { std::unique_ptr range(dummyLocation()); - node sym = make(*range.get(), llvm::StringRef("example")); - node err = make(ErrType::Semantic, sym, "Something Failed"); + exprs::node sym = exprs::make(*range.get(), llvm::StringRef("example")); + exprs::node err = exprs::make(ErrType::Semantic, sym, "Something Failed"); - REQUIRE(err->getType() == ExprType::Error); + REQUIRE(err->getType() == exprs::ExprType::Error); CHECK(err->toString() == ""); auto error = llvm::dyn_cast(err.get()); diff --git a/src/tests/serenetests.cpp b/src/tests/serenetests.cpp index 7acedfa..09d0f24 100644 --- a/src/tests/serenetests.cpp +++ b/src/tests/serenetests.cpp @@ -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" diff --git a/src/tests/test_helpers.cpp.inc b/src/tests/test_helpers.cpp.inc index 25ac501..0d66223 100644 --- a/src/tests/test_helpers.cpp.inc +++ b/src/tests/test_helpers.cpp.inc @@ -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