Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
Replace some throw by MessageCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
Gashmob committed Aug 26, 2023
1 parent 56cc263 commit 8d7218a
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 58 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ DEV WARNING <code> <message>
| ^
```

| Dev code | Meaning | File |
|:------------:|:--------------------------------------------|:-----------------------------------|
| <kbd>2</kbd> | Lexer found a character that is not regular | `src/lib/ast/CharacterLiteral.cpp` |
| Dev code | Meaning | File |
|:------------:|:-----------------------------------------------------|:---------------------------------------|
| <kbd>2</kbd> | Lexer found a character that is not regular | `src/lib/ast/CharacterLiteral.cpp` |
| <kbd>3</kdb> | A function that should not be called has been called | Several files (refer to error message) |

These codes are for events that should not happen, but if they do, it's better to fix them.
54 changes: 28 additions & 26 deletions src/lib/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,14 @@ namespace filc::ast {
[[nodiscard]] virtual auto dumpPreLambdaType(AbstractType *return_type,
AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * = 0;
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * = 0;

[[nodiscard]] virtual auto dumpPostLambdaType(AbstractType *return_type,
AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * = 0;
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * = 0;

protected:
Operator() = default;
Expand Down Expand Up @@ -404,15 +406,15 @@ namespace filc::ast {

[[nodiscard]] auto dump() const -> std::string override;

[[nodiscard]] auto dumpPreLambdaType(AbstractType *return_type,
AbstractType *called_on,
[[nodiscard]] auto dumpPreLambdaType(AbstractType *return_type, AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * override;
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * override;

[[nodiscard]] auto dumpPostLambdaType(AbstractType *return_type,
AbstractType *called_on,
[[nodiscard]] auto dumpPostLambdaType(AbstractType *return_type, AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * override;
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * override;

private:
OPERATOR _operator;
Expand All @@ -428,15 +430,15 @@ namespace filc::ast {

[[nodiscard]] auto dump() const -> std::string override;

[[nodiscard]] auto dumpPreLambdaType(AbstractType *return_type,
AbstractType *called_on,
[[nodiscard]] auto dumpPreLambdaType(AbstractType *return_type, AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * override;
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * override;

[[nodiscard]] auto dumpPostLambdaType(AbstractType *return_type,
AbstractType *called_on,
[[nodiscard]] auto dumpPostLambdaType(AbstractType *return_type, AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * override;
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * override;

private:
AbstractExpression *_expression;
Expand All @@ -452,15 +454,15 @@ namespace filc::ast {

[[nodiscard]] auto dump() const -> std::string override;

[[nodiscard]] auto dumpPreLambdaType(AbstractType *return_type,
AbstractType *called_on,
[[nodiscard]] auto dumpPreLambdaType(AbstractType *return_type, AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * override;
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * override;

[[nodiscard]] auto dumpPostLambdaType(AbstractType *return_type,
AbstractType *called_on,
[[nodiscard]] auto dumpPostLambdaType(AbstractType *return_type, AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * override;
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * override;

private:
std::vector<AbstractExpression *> _expressions;
Expand All @@ -476,15 +478,15 @@ namespace filc::ast {

[[nodiscard]] auto dump() const -> std::string override;

[[nodiscard]] auto dumpPreLambdaType(AbstractType *return_type,
AbstractType *called_on,
[[nodiscard]] auto dumpPreLambdaType(AbstractType *return_type, AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * override;
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * override;

[[nodiscard]] auto dumpPostLambdaType(AbstractType *return_type,
AbstractType *called_on,
[[nodiscard]] auto dumpPostLambdaType(AbstractType *return_type, AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * override;
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * override;

private:
Operator *_inner_operator;
Expand Down
8 changes: 6 additions & 2 deletions src/lib/ast/AbstractExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/
#include "AST.h"
#include <stdexcept>
#include "Error.h"

namespace filc::ast {
AbstractExpression::~AbstractExpression() {
Expand Down Expand Up @@ -56,6 +56,10 @@ namespace filc::ast {
auto AbstractExpression::resolveType(filc::environment::Environment *environment,
filc::message::MessageCollector *collector,
AbstractType *preferred_type) -> void {
throw std::logic_error("Not implemented");
collector->addError(
new filc::message::Error(filc::message::FATAL_ERROR,
"resolveType not implemented",
getPosition())
);
}
}
15 changes: 12 additions & 3 deletions src/lib/ast/ArrayOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* SOFTWARE.
*/
#include "AST.h"
#include "DevWarning.h"

namespace filc::ast {
ArrayOperator::ArrayOperator(filc::ast::AbstractExpression *expression)
Expand All @@ -42,14 +43,22 @@ namespace filc::ast {
auto ArrayOperator::dumpPreLambdaType(AbstractType *return_type,
AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * {
throw std::logic_error("Should not be called");
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * {
collector->addError(new filc::message::DevWarning(
3,
position,
"ArrayOperator::dumpPreLambdaType should not be called but has been called"
));

return nullptr;
}

auto ArrayOperator::dumpPostLambdaType(AbstractType *return_type,
AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * {
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * {
_expression->resolveType(environment, collector);
auto *expression_type = _expression->getExpressionType();
if (expression_type == nullptr) {
Expand Down
23 changes: 19 additions & 4 deletions src/lib/ast/AssignationOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* SOFTWARE.
*/
#include "AST.h"
#include "DevWarning.h"

namespace filc::ast {
AssignationOperator::AssignationOperator(filc::ast::Operator *inner_operator)
Expand All @@ -42,14 +43,28 @@ namespace filc::ast {
auto AssignationOperator::dumpPreLambdaType(AbstractType *return_type,
AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * {
throw std::logic_error("Should not be called");
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * {
collector->addError(new filc::message::DevWarning(
3,
position,
"AssignationOperator::dumpPreLambdaType should not be called but has been called"
));

return nullptr;
}

auto AssignationOperator::dumpPostLambdaType(AbstractType *return_type,
AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * {
throw std::logic_error("Should not be called");
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * {
collector->addError(new filc::message::DevWarning(
3,
position,
"AssignationOperator::dumpPostLambdaType should not be called but has been called"
));

return nullptr;
}
}
6 changes: 4 additions & 2 deletions src/lib/ast/ClassicOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ namespace filc::ast {
auto ClassicOperator::dumpPreLambdaType(AbstractType *return_type,
AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * {
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * {
return new LambdaType({}, return_type, called_on);
}

auto ClassicOperator::dumpPostLambdaType(AbstractType *return_type,
AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * {
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * {
if (!environment->hasType("void")) {
environment->addType(new filc::ast::Type(new filc::ast::Identifier("void")));
}
Expand Down
15 changes: 12 additions & 3 deletions src/lib/ast/FunctionOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* SOFTWARE.
*/
#include "AST.h"
#include "DevWarning.h"

namespace filc::ast {
FunctionOperator::FunctionOperator(const std::vector<AbstractExpression *> &expressions)
Expand All @@ -44,14 +45,22 @@ namespace filc::ast {
auto FunctionOperator::dumpPreLambdaType(AbstractType *return_type,
AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * {
throw std::logic_error("Should not be called");
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * {
collector->addError(new filc::message::DevWarning(
3,
position,
"FunctionOperator::dumpPreLambdaType should not be called but has been called"
));

return nullptr;
}

auto FunctionOperator::dumpPostLambdaType(AbstractType *return_type,
AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * {
filc::message::MessageCollector *collector,
filc::utils::Position *position) const -> LambdaType * {
std::vector<AbstractType *> args_types;
for (const auto &expression: _expressions) {
expression->resolveType(environment, collector, nullptr);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ast/PostUnaryCalcul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ namespace filc::ast {
}

auto operator_name = "operator" + getOperator()->dump();
auto *operator_type = getOperator()->dumpPostLambdaType(variable_type, variable_type, environment, collector);
auto *operator_type = getOperator()->dumpPostLambdaType(variable_type, variable_type, environment, collector,
getPosition());
if (environment->getName(operator_name, operator_type) == nullptr) {
collector->addError(
new filc::message::Error(filc::message::ERROR,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ast/PreUnaryCalcul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ namespace filc::ast {
}

auto operator_name = "operator" + getOperator()->dump();
auto *operator_type = getOperator()->dumpPreLambdaType(variable_type, variable_type, environment, collector);
auto *operator_type = getOperator()->dumpPreLambdaType(variable_type, variable_type, environment, collector,
getPosition());
if (environment->getName(operator_name, operator_type) == nullptr) {
collector->addError(
new filc::message::Error(filc::message::ERROR,
Expand Down
5 changes: 5 additions & 0 deletions src/lib/message/MessageCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ namespace filc::message {

return collector;
}

auto MessageCollector::flush() -> void {
_errors.clear();
_messages.clear();
}
}
2 changes: 2 additions & 0 deletions src/lib/message/MessageCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ namespace filc::message {

static auto getCollector(LEVEL level = FATAL_ERROR) -> MessageCollector *;

auto flush() -> void;

private:
LEVEL _level;
std::vector<Message *> _messages;
Expand Down
16 changes: 12 additions & 4 deletions tests/unit/ast/AbstractExpressionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,19 @@ TEST(AbstractExpression, expression_type) {
ASSERT_TYPE("int", obj1.getExpressionType());
}

#define COLLECTOR filc::message::MessageCollector::getCollector()

// NOLINTBEGIN(readability-function-cognitive-complexity)
TEST(AbstractExpression, resolveType) {
class : public filc::ast::AbstractExpression {
} obj1;

ASSERT_THROW(obj1.resolveType(nullptr, nullptr, nullptr), std::logic_error);
ASSERT_THROW(obj1.resolveType(new filc::environment::Environment, nullptr, nullptr), std::logic_error);
obj1.resolveType(nullptr, COLLECTOR, nullptr);
ASSERT_TRUE(COLLECTOR->hasErrors());
COLLECTOR->flush();
obj1.resolveType(new filc::environment::Environment, COLLECTOR, nullptr);
ASSERT_TRUE(COLLECTOR->hasErrors());
COLLECTOR->flush();

class : public filc::ast::AbstractExpression {
public:
Expand All @@ -78,7 +84,9 @@ TEST(AbstractExpression, resolveType) {
}
} obj2;

ASSERT_NO_THROW(obj2.resolveType(nullptr, nullptr, nullptr));
ASSERT_NO_THROW(obj2.resolveType(new filc::environment::Environment, nullptr, nullptr));
obj2.resolveType(nullptr, COLLECTOR, nullptr);
ASSERT_FALSE(COLLECTOR->hasErrors());
obj2.resolveType(new filc::environment::Environment, COLLECTOR, nullptr);
ASSERT_FALSE(COLLECTOR->hasErrors());
}
// NOLINTEND(readability-function-cognitive-complexity)
2 changes: 1 addition & 1 deletion tests/unit/ast/IdentifierTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TEST(Identifier, resolveType) {
filc::ast::Identifier id1("hello");
id1.resolveType(environment, collector, nullptr);
ASSERT_TRUE(collector->hasErrors());
collector->printAll();
collector->flush();

environment->addName("hello", new filc::ast::Type(new filc::ast::Identifier("int")));
id1.resolveType(environment, collector, nullptr);
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/ast/ProgramTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ TEST(Program, filename) {
TEST(Program, resolveEnvironment) {
filc::grammar::Parser parser1(FIXTURES_PATH "/grammar/module1.fil", COLLECTOR);
auto *program1 = parser1.getProgram();
ASSERT_NO_THROW(program1->resolveEnvironment(COLLECTOR));
program1->resolveEnvironment(COLLECTOR);
ASSERT_FALSE(COLLECTOR->hasErrors());

COLLECTOR->flush();
filc::grammar::Parser parser2(FIXTURES_PATH "/grammar/while1.fil", COLLECTOR);
auto *program2 = parser2.getProgram();
ASSERT_THROW(program2->resolveEnvironment(COLLECTOR), std::logic_error);
program2->resolveEnvironment(COLLECTOR);
ASSERT_TRUE(COLLECTOR->hasErrors());
}
Loading

0 comments on commit 8d7218a

Please sign in to comment.