Move the Error expression to it's own namespace
This commit is contained in:
parent
a486b03d10
commit
17b778bc49
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/* -*- C++ -*-
|
||||||
* Serene programming language.
|
* 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
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -22,25 +22,23 @@
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "serene/error.hpp"
|
#ifndef SERENE_ERRORS_CONSTANTS_H
|
||||||
#include "serene/expr.hpp"
|
#define SERENE_ERRORS_CONSTANTS_H
|
||||||
#include "serene/llvm/IR/Value.h"
|
|
||||||
#include "serene/namespace.h"
|
#include <map>
|
||||||
#include "serene/state.hpp"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <fmt/core.h>
|
|
||||||
#include <llvm/IR/Constants.h>
|
|
||||||
#include <llvm/IR/Type.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
namespace serene {
|
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
|
} // namespace serene
|
||||||
|
#endif
|
|
@ -25,6 +25,48 @@
|
||||||
#ifndef SERENE_ERRORS_ERROR_H
|
#ifndef SERENE_ERRORS_ERROR_H
|
||||||
#define SERENE_ERRORS_ERROR_H
|
#define SERENE_ERRORS_ERROR_H
|
||||||
|
|
||||||
|
#include "serene/errors/constants.h"
|
||||||
|
#include "serene/exprs/expression.h"
|
||||||
#include "llvm/Support/Error.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
|
#endif
|
||||||
|
|
|
@ -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
|
|
|
@ -7,7 +7,6 @@ set(HEADER_LIST
|
||||||
"${INCLUDE_DIR}/serene/exprs/list.h"
|
"${INCLUDE_DIR}/serene/exprs/list.h"
|
||||||
"${INCLUDE_DIR}/serene/exprs/number.h"
|
"${INCLUDE_DIR}/serene/exprs/number.h"
|
||||||
"${INCLUDE_DIR}/serene/exprs/def.h"
|
"${INCLUDE_DIR}/serene/exprs/def.h"
|
||||||
"${INCLUDE_DIR}/serene/exprs/error.h"
|
|
||||||
|
|
||||||
# Reader
|
# Reader
|
||||||
"${INCLUDE_DIR}/serene/reader/reader.h"
|
"${INCLUDE_DIR}/serene/reader/reader.h"
|
||||||
|
@ -18,6 +17,7 @@ set(HEADER_LIST
|
||||||
"${INCLUDE_DIR}/serene/errors.h"
|
"${INCLUDE_DIR}/serene/errors.h"
|
||||||
"${INCLUDE_DIR}/serene/errors/error.h"
|
"${INCLUDE_DIR}/serene/errors/error.h"
|
||||||
"${INCLUDE_DIR}/serene/errors/errc.h"
|
"${INCLUDE_DIR}/serene/errors/errc.h"
|
||||||
|
"${INCLUDE_DIR}/serene/errors/constants.h"
|
||||||
|
|
||||||
|
|
||||||
"${INCLUDE_DIR}/serene/expr.hpp"
|
"${INCLUDE_DIR}/serene/expr.hpp"
|
||||||
|
@ -42,14 +42,13 @@ add_library(serene
|
||||||
exprs/number.cpp
|
exprs/number.cpp
|
||||||
exprs/expression.cpp
|
exprs/expression.cpp
|
||||||
exprs/def.cpp
|
exprs/def.cpp
|
||||||
exprs/error.cpp
|
|
||||||
|
|
||||||
serene.cpp
|
serene.cpp
|
||||||
symbol.cpp
|
symbol.cpp
|
||||||
list.cpp
|
list.cpp
|
||||||
namespace.cpp
|
namespace.cpp
|
||||||
state.cpp
|
state.cpp
|
||||||
error.cpp
|
|
||||||
number.cpp
|
number.cpp
|
||||||
|
|
||||||
# Reader
|
# Reader
|
||||||
|
@ -58,6 +57,9 @@ add_library(serene
|
||||||
reader/errors.cpp
|
reader/errors.cpp
|
||||||
reader/semantics.cpp
|
reader/semantics.cpp
|
||||||
|
|
||||||
|
# Errors
|
||||||
|
errors/error.cpp
|
||||||
|
|
||||||
# IR
|
# IR
|
||||||
sir/sir.cpp
|
sir/sir.cpp
|
||||||
sir/dialect.cpp
|
sir/dialect.cpp
|
||||||
|
|
|
@ -22,13 +22,15 @@
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "serene/exprs/error.h"
|
#include "serene/errors/error.h"
|
||||||
#include "llvm/Support/FormatVariadic.h"
|
#include "llvm/Support/FormatVariadic.h"
|
||||||
|
|
||||||
namespace serene {
|
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 {
|
std::string Error::toString() const {
|
||||||
return llvm::formatv("<Error [loc: {0} | {1}]: {2}>",
|
return llvm::formatv("<Error [loc: {0} | {1}]: {2}>",
|
||||||
|
@ -36,13 +38,13 @@ std::string Error::toString() const {
|
||||||
this->location.end.toString(), this->message);
|
this->location.end.toString(), this->message);
|
||||||
}
|
}
|
||||||
|
|
||||||
maybe_node Error::analyze(reader::SemanticContext &ctx) {
|
serene::exprs::maybe_node Error::analyze(reader::SemanticContext &ctx) {
|
||||||
return Result<node>::Success(nullptr);
|
return Result<serene::exprs::node>::Success(nullptr);
|
||||||
};
|
};
|
||||||
|
|
||||||
bool Error::classof(const Expression *e) {
|
bool Error::classof(const serene::exprs::Expression *e) {
|
||||||
return e->getType() == ExprType::Error;
|
return e->getType() == serene::exprs::ExprType::Error;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace exprs
|
} // namespace errors
|
||||||
} // namespace serene
|
} // namespace serene
|
|
@ -24,20 +24,23 @@
|
||||||
|
|
||||||
#include "../test_helpers.cpp.inc"
|
#include "../test_helpers.cpp.inc"
|
||||||
#include "serene/exprs/symbol.h"
|
#include "serene/exprs/symbol.h"
|
||||||
#include "serene/exprs/error.h"
|
#include "serene/errors.h"
|
||||||
#include "llvm/Support/Casting.h"
|
#include "llvm/Support/Casting.h"
|
||||||
#include <catch2/catch.hpp>
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace serene {
|
namespace serene {
|
||||||
namespace exprs {
|
namespace errors {
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("Error Expression", "[expression]") {
|
TEST_CASE("Error Expression", "[expression]") {
|
||||||
std::unique_ptr<reader::LocationRange> range(dummyLocation());
|
std::unique_ptr<reader::LocationRange> range(dummyLocation());
|
||||||
|
|
||||||
node sym = make<Symbol>(*range.get(), llvm::StringRef("example"));
|
exprs::node sym = exprs::make<exprs::Symbol>(*range.get(), llvm::StringRef("example"));
|
||||||
node err = make<Error>(ErrType::Semantic, sym, "Something Failed");
|
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>");
|
CHECK(err->toString() == "<Error [loc: 2:20:40 | 3:30:80]: Something Failed>");
|
||||||
|
|
||||||
auto error = llvm::dyn_cast<Error>(err.get());
|
auto error = llvm::dyn_cast<Error>(err.get());
|
|
@ -23,7 +23,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define CATCH_CONFIG_MAIN
|
#define CATCH_CONFIG_MAIN
|
||||||
#include "./exprs/error_tests.cpp.inc"
|
#include "./errors/error_tests.cpp.inc"
|
||||||
#include "./exprs/expression_tests.cpp.inc"
|
#include "./exprs/expression_tests.cpp.inc"
|
||||||
#include "./exprs/list_tests.cpp.inc"
|
#include "./exprs/list_tests.cpp.inc"
|
||||||
#include "./exprs/number_tests.cpp.inc"
|
#include "./exprs/number_tests.cpp.inc"
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
#include "serene/reader/location.h"
|
#include "serene/reader/location.h"
|
||||||
|
|
||||||
namespace serene {
|
namespace serene {
|
||||||
namespace exprs {
|
|
||||||
|
|
||||||
reader::LocationRange *dummyLocation() {
|
reader::LocationRange *dummyLocation() {
|
||||||
reader::Location start;
|
reader::Location start;
|
||||||
|
@ -46,7 +46,6 @@ reader::LocationRange *dummyLocation() {
|
||||||
return new reader::LocationRange(start, end);
|
return new reader::LocationRange(start, end);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace exprs
|
|
||||||
} // namespace serene
|
} // namespace serene
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue