Add a very basic Error Expression to be used in semantic analysis

This commit is contained in:
Sameer Rahmani 2021-04-19 19:49:05 +01:00
parent 9f90e42bbb
commit a486b03d10
7 changed files with 174 additions and 3 deletions

View File

@ -0,0 +1,73 @@
/* -*- 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

@ -45,6 +45,7 @@ enum class ExprType {
List,
Number,
Def,
Error,
};
class Expression;

View File

@ -37,9 +37,6 @@ namespace exprs {
/// in the context of the AST and nothing else.
class Symbol : public Expression {
// private:
// using Expression::analyze;
public:
std::string name;

View File

@ -7,6 +7,7 @@ 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"
@ -41,6 +42,7 @@ add_library(serene
exprs/number.cpp
exprs/expression.cpp
exprs/def.cpp
exprs/error.cpp
serene.cpp
symbol.cpp

View File

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

View File

@ -0,0 +1,49 @@
/* -*- 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.
*/
#include "../test_helpers.cpp.inc"
#include "serene/exprs/symbol.h"
#include "serene/exprs/error.h"
#include "llvm/Support/Casting.h"
#include <catch2/catch.hpp>
namespace serene {
namespace exprs {
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");
REQUIRE(err->getType() == ExprType::Error);
CHECK(err->toString() == "<Error [loc: 2:20:40 | 3:30:80]: Something Failed>");
auto error = llvm::dyn_cast<Error>(err.get());
CHECK(error->errorType == ErrType::Semantic);
CHECK(error->target == sym);
};
} // namespace exprs
} // namespace serene

View File

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