Change toString format and finalize Def::isValid signature

This commit is contained in:
Sameer Rahmani 2021-04-22 00:49:11 +01:00
parent 751eb49538
commit f614d35b7f
16 changed files with 66 additions and 69 deletions

View File

@ -1,4 +1,5 @@
add_executable(serenec serene.cpp)
# Make sure to generate files related to the dialects first
add_dependencies(serenec SereneDialectGen)
@ -14,7 +15,6 @@ endif()
target_link_libraries(serenec PRIVATE
serene
${llvm_libs}
fmt::fmt
MLIRAnalysis
MLIRIR
MLIRParser

View File

@ -30,14 +30,30 @@
namespace serene {
namespace errors {
enum ErrID {
E0000,
E0001,
};
static std::map<ErrID, std::string> ErrDesc = {
{E0000, "Can't find any description for this error.\n"}, {E0001, ""}};
struct ErrorVariant {
ErrID id;
std::string description;
std::string longDescription;
ErrorVariant(ErrID id, std::string desc, std::string longDesc)
: id(id), description(desc), longDescription(longDesc){};
};
static ErrorVariant
UnknownError(E0000, "Can't find any description for this error.", "");
static ErrorVariant
DefExpectSymbol(E0001, "The first argument to 'def' has to be a Symbol.",
"");
static std::map<ErrID, ErrorVariant *> ErrDesc = {{E0000, &UnknownError},
{E0001, &DefExpectSymbol}};
} // namespace errors
} // namespace serene

View File

@ -44,19 +44,14 @@ enum class ErrType {
class Error : public ::serene::exprs::Expression {
public:
ErrID id;
ErrType errorType;
ErrorVariant *variant;
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),
Error(ErrorVariant *err, serene::exprs::node &t, llvm::StringRef msg)
: serene::exprs::Expression(t->location), variant(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;

View File

@ -25,9 +25,11 @@
#ifndef EXPRS_DEF_H
#define EXPRS_DEF_H
#include "serene/errors/error.h"
#include "serene/exprs/expression.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include <memory>
#include <string>
namespace serene {
@ -51,7 +53,7 @@ public:
maybe_node analyze(reader::SemanticContext &);
static bool classof(const Expression *e);
static llvm::Error isValid(const List *);
static std::shared_ptr<errors::Error> isValid(const List *);
~Def() = default;
};

View File

@ -59,6 +59,7 @@ public:
return make_error_code(errc::no_such_file_or_directory);
}
};
} // namespace reader
} // namespace serene
#endif

View File

@ -33,9 +33,9 @@ serene::exprs::ExprType Error::getType() const {
};
std::string Error::toString() const {
return llvm::formatv("<Error [loc: {0} | {1}]: {2}>",
this->location.start.toString(),
this->location.end.toString(), this->message);
return llvm::formatv(
"<Error [loc: {0} | {1}]: E{2}: {3}>", this->location.start.toString(),
this->location.end.toString(), this->variant->id, this->message);
}
serene::exprs::maybe_node Error::analyze(reader::SemanticContext &ctx) {

View File

@ -32,9 +32,8 @@ namespace exprs {
ExprType Def::getType() const { return ExprType::Def; };
std::string Def::toString() const {
return llvm::formatv(
"<Def [loc: {0} | {1}]: {2} -> {3}>", this->location.start.toString(),
this->location.end.toString(), this->binding, this->value->toString());
return llvm::formatv("<Def {0} -> {1}>", this->binding,
this->value->toString());
}
maybe_node Def::analyze(reader::SemanticContext &ctx) {
@ -45,6 +44,8 @@ bool Def::classof(const Expression *e) {
return e->getType() == ExprType::Def;
};
llvm::Error Def::isValid(const List *list) { return llvm::Error::success(); };
std::shared_ptr<errors::Error> Def::isValid(const List *list) {
return nullptr;
};
} // namespace exprs
} // namespace serene

View File

@ -23,6 +23,7 @@
*/
#include "serene/exprs/list.h"
#include "serene/errors/error.h"
#include "serene/exprs/def.h"
#include "serene/exprs/symbol.h"
#include "llvm/Support/Casting.h"
@ -49,9 +50,7 @@ std::string List::toString() const {
s = llvm::formatv("{0} {1}", s, n->toString());
}
return llvm::formatv("<List [loc: {0} | {1}]: {2}>",
this->location.start.toString(),
this->location.end.toString(), s);
return llvm::formatv("<List {0}>", s);
};
maybe_node List::analyze(reader::SemanticContext &ctx) {
@ -63,9 +62,11 @@ maybe_node List::analyze(reader::SemanticContext &ctx) {
if (sym) {
if (sym->name == "def") {
if (auto err = Def::isValid(this)) {
auto maybeErr = Def::isValid(this);
if (maybeErr) {
// Not a valid `def` form
return Result<node>::error(std::move(err));
return Result<node>::success(maybeErr);
}
Symbol *binding = llvm::dyn_cast<Symbol>(elements[1].get());
@ -79,8 +80,6 @@ maybe_node List::analyze(reader::SemanticContext &ctx) {
}
}
// TODO: Return an error saying the binding has to be
// a symbol
}
}

View File

@ -31,9 +31,7 @@ namespace exprs {
ExprType Number::getType() const { return ExprType::Number; };
std::string Number::toString() const {
return llvm::formatv("<Number [loc: {0} | {1}]: {2}>",
this->location.start.toString(),
this->location.end.toString(), value);
return llvm::formatv("<Number {0}>", value);
}
maybe_node Number::analyze(reader::SemanticContext &ctx) {

View File

@ -31,9 +31,7 @@ namespace exprs {
ExprType Symbol::getType() const { return ExprType::Symbol; };
std::string Symbol::toString() const {
return llvm::formatv("<Symbol [loc: {0} | {1}]: {2}>",
this->location.start.toString(),
this->location.end.toString(), this->name);
return llvm::formatv("<Symbol {0}>", this->name);
}
maybe_node Symbol::analyze(reader::SemanticContext &ctx) {

View File

@ -38,14 +38,14 @@ TEST_CASE("Error Expression", "[expression]") {
std::unique_ptr<reader::LocationRange> range(dummyLocation());
exprs::node sym = exprs::make<exprs::Symbol>(*range.get(), llvm::StringRef("example"));
exprs::node err = exprs::make<Error>(ErrType::Semantic, sym, "Something Failed");
exprs::node err = exprs::make<Error>(&DefExpectSymbol, sym, "Something Failed");
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]: E1: Something Failed>");
auto error = llvm::dyn_cast<Error>(err.get());
CHECK(error->errorType == ErrType::Semantic);
CHECK(error->target == sym);
};
} // namespace exprs

View File

@ -36,12 +36,11 @@ TEST_CASE("Public Expression API", "[expression]") {
auto sym = make<Symbol>(*range.get(), "example");
REQUIRE(sym->getType() == ExprType::Symbol);
CHECK(sym->toString() == "<Symbol [loc: 2:20:40 | 3:30:80]: example>");
CHECK(sym->toString() == "<Symbol example>");
auto list = makeAndCast<List>(*range.get(), sym);
CHECK(list->toString() == "<List [loc: 2:20:40 | 3:30:80]: <Symbol [loc: "
"2:20:40 | 3:30:80]: example>>");
CHECK(list->toString() == "<List <Symbol example>>");
};
} // namespace exprs

View File

@ -39,11 +39,10 @@ TEST_CASE("List Expression", "[expression]") {
node list = make<List>(*range.get());
REQUIRE(list->getType() == ExprType::List);
CHECK(list->toString() == "<List [loc: 2:20:40 | 3:30:80]: ->");
CHECK(list->toString() == "<List ->");
node list2 = make<List>(*range.get(), list);
CHECK(list2->toString() ==
"<List [loc: 2:20:40 | 3:30:80]: <List [loc: 2:20:40 | 3:30:80]: ->>");
CHECK(list2->toString() == "<List <List ->>");
ast elements;
elements.push_back(list);
@ -52,25 +51,21 @@ TEST_CASE("List Expression", "[expression]") {
auto list3 = make<List>(*range.get(), elements);
CHECK(list3->toString() ==
"<List [loc: 2:20:40 | 3:30:80]: <List [loc: 2:20:40 | 3:30:80]: -> "
"<List [loc: 2:20:40 | 3:30:80]: <List [loc: 2:20:40 | 3:30:80]: "
"->> <Symbol [loc: 2:20:40 | 3:30:80]: example>>");
CHECK(list3->toString() == "<List <List -> <List <List ->> <Symbol example>>");
auto l = llvm::dyn_cast<List>(list.get());
l->append(sym1);
REQUIRE(list->getType() == ExprType::List);
CHECK(list->toString() == "<List [loc: 2:20:40 | 3:30:80]: <Symbol [loc: "
"2:20:40 | 3:30:80]: example1>>");
CHECK(list->toString() == "<List <Symbol example1>>");
l->append(sym);
REQUIRE(l->count() == 2);
auto expr = l->at(1);
REQUIRE(expr.hasValue());
CHECK(expr.getValue()->toString() == "<Symbol [loc: 2:20:40 | 3:30:80]: example>");
CHECK(expr.getValue()->toString() == "<Symbol example>");
expr = l->at(2);
REQUIRE_FALSE(expr.hasValue());

View File

@ -38,10 +38,10 @@ TEST_CASE("Number Expression", "[expression]") {
auto num3 = makeAndCast<Number>(*range.get(), "3", true, false);
auto num4 = makeAndCast<Number>(*range.get(), "-3", true, false);
CHECK(num1->toString() == "<Number [loc: 2:20:40 | 3:30:80]: 3>");
CHECK(num2->toString() == "<Number [loc: 2:20:40 | 3:30:80]: 3.4>");
CHECK(num3->toString() == "<Number [loc: 2:20:40 | 3:30:80]: 3>");
CHECK(num4->toString() == "<Number [loc: 2:20:40 | 3:30:80]: -3>");
CHECK(num1->toString() == "<Number 3>");
CHECK(num2->toString() == "<Number 3.4>");
CHECK(num3->toString() == "<Number 3>");
CHECK(num4->toString() == "<Number -3>");
};
} // namespace exprs

View File

@ -34,7 +34,7 @@ TEST_CASE("Public Symbol API", "[expression]") {
auto sym = make<Symbol>(*range.get(), "example");
REQUIRE(sym->getType() == ExprType::Symbol);
CHECK(sym->toString() == "<Symbol [loc: 2:20:40 | 3:30:80]: example>");
CHECK(sym->toString() == "<Symbol example>");
};
} // namespace exprs
} // namespace serene

View File

@ -39,7 +39,7 @@ TEST_CASE("Read numbers", "[reader]") {
auto ast = std::move(maybeAst).getValue();
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() == "<Number [loc: 0:1:1 | 0:1:1]: 3>");
CHECK(ast.front()->toString() == "<Number 3>");
r->setInput("-34");
maybeAst = r->read();
@ -50,7 +50,7 @@ TEST_CASE("Read numbers", "[reader]") {
ast = std::move(maybeAst.getValue());
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() == "<Number [loc: 0:2:2 | 0:3:3]: -34>");
CHECK(ast.front()->toString() == "<Number -34>");
r->setInput("-3.5434");
maybeAst = r->read();
@ -61,7 +61,7 @@ TEST_CASE("Read numbers", "[reader]") {
ast = std::move(maybeAst.getValue());
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() == "<Number [loc: 0:2:2 | 0:7:7]: -3.5434>");
CHECK(ast.front()->toString() == "<Number -3.5434>");
r->setInput("444323 2123 123123");
maybeAst = r->read();
@ -72,9 +72,9 @@ TEST_CASE("Read numbers", "[reader]") {
ast = std::move(maybeAst.getValue());
REQUIRE(ast.size() == 3);
CHECK(ast.front()->toString() == "<Number [loc: 0:1:1 | 0:6:6]: 444323>");
CHECK(ast[1]->toString() == "<Number [loc: 0:8:8 | 0:11:11]: 2123>");
CHECK(ast[2]->toString() == "<Number [loc: 0:13:13 | 0:18:18]: 123123>");
CHECK(ast.front()->toString() == "<Number 444323>");
CHECK(ast[1]->toString() == "<Number 2123>" );
CHECK(ast[2]->toString() == "<Number 123123>");
delete r;
};
@ -89,9 +89,7 @@ TEST_CASE("Read Lists and Symbols", "[reader]") {
auto ast = std::move(maybeAst.getValue());
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() ==
"<List [loc: 0:0:0 | 0:5:5]: <Symbol [loc: 0:2:2 | 0:2:2]: x> <Number "
"[loc: 0:4:4 | 0:4:4]: 1>>");
CHECK(ast.front()->toString() == "<List <Symbol x> <Number 1>>");
r->setInput("(x (y (z)))");
maybeAst = r->read();
@ -102,10 +100,7 @@ TEST_CASE("Read Lists and Symbols", "[reader]") {
ast = std::move(maybeAst.getValue());
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() ==
"<List [loc: 0:0:0 | 0:11:11]: <Symbol [loc: 0:2:2 | 0:2:2]: x> <List "
"[loc: 0:3:3 | 0:10:10]: <Symbol [loc: 0:5:5 | 0:5:5]: y> <List [loc: "
"0:6:6 | 0:9:9]: <Symbol [loc: 0:8:8 | 0:8:8]: z>>>>");
CHECK(ast.front()->toString() == "<List <Symbol x> <List <Symbol y> <List <Symbol z>>>>");
r->setInput("(x \n y)");
maybeAst = r->read();
@ -116,9 +111,7 @@ TEST_CASE("Read Lists and Symbols", "[reader]") {
ast = std::move(maybeAst.getValue());
REQUIRE_FALSE(ast.empty());
CHECK(ast.front()->toString() ==
"<List [loc: 0:0:0 | 1:3:7]: <Symbol [loc: 0:2:2 | 0:2:2]: x> <Symbol "
"[loc: 1:2:6 | 1:2:6]: y>>");
CHECK(ast.front()->toString() == "<List <Symbol x> <Symbol y>>");
delete r;
};