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

Commit

Permalink
Add resolution of UnaryCalcul
Browse files Browse the repository at this point in the history
It does for Pre and Post
  • Loading branch information
Gashmob committed Aug 25, 2023
1 parent 5414ba3 commit 698193c
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 32 deletions.
21 changes: 18 additions & 3 deletions src/lib/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ namespace filc::ast {

[[nodiscard]] auto getOperator() const -> Operator *;

auto resolveType(filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> void override;

protected:
Identifier *_variable;
Operator *_operator;
Expand All @@ -290,9 +293,6 @@ namespace filc::ast {
class PreUnaryCalcul : public UnaryCalcul {
public:
PreUnaryCalcul(Identifier *variable, Operator *p_operator);

auto resolveType(filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> void override;
};

class PostUnaryCalcul : public UnaryCalcul {
Expand Down Expand Up @@ -332,6 +332,9 @@ namespace filc::ast {

virtual auto dump() -> std::string = 0;

virtual auto dumpLambdaType(filc::ast::AbstractType *return_type, filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> LambdaType * = 0;

protected:
Operator() = default;
};
Expand Down Expand Up @@ -366,6 +369,9 @@ namespace filc::ast {

auto dump() -> std::string override;

auto dumpLambdaType(filc::ast::AbstractType *return_type, filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> LambdaType * override;

private:
OPERATOR _operator;
};
Expand All @@ -380,6 +386,9 @@ namespace filc::ast {

auto dump() -> std::string override;

auto dumpLambdaType(filc::ast::AbstractType *return_type, filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> LambdaType * override;

private:
AbstractExpression *_expression;
};
Expand All @@ -394,6 +403,9 @@ namespace filc::ast {

auto dump() -> std::string override;

auto dumpLambdaType(filc::ast::AbstractType *return_type, filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> LambdaType * override;

private:
std::vector<AbstractExpression *> _expressions;
};
Expand All @@ -408,6 +420,9 @@ namespace filc::ast {

auto dump() -> std::string override;

auto dumpLambdaType(filc::ast::AbstractType *return_type, filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> LambdaType * override;

private:
Operator *_inner_operator;
};
Expand Down
14 changes: 13 additions & 1 deletion src/lib/ast/ArrayOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ namespace filc::ast {
delete _expression;
}

std::string ArrayOperator::dump() {
auto ArrayOperator::dump() -> std::string {
return "[]";
}

auto ArrayOperator::dumpLambdaType(filc::ast::AbstractType *return_type,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> LambdaType * {
_expression->resolveType(environment, collector);
auto *expression_type = _expression->getExpressionType();
if (expression_type == nullptr) {
return nullptr;
}

return new LambdaType({expression_type}, return_type);
}
}
8 changes: 7 additions & 1 deletion src/lib/ast/AssignationOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ namespace filc::ast {
return _inner_operator;
}

std::string AssignationOperator::dump() {
auto AssignationOperator::dump() -> std::string {
return _inner_operator->dump() + "=";
}

auto AssignationOperator::dumpLambdaType(filc::ast::AbstractType *return_type,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> LambdaType * {
throw std::logic_error("Should not be called");
}
}
10 changes: 9 additions & 1 deletion src/lib/ast/ClassicOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace filc::ast {
return _operator;
}

std::string ClassicOperator::dump() {
auto ClassicOperator::dump() -> std::string {
switch (_operator) {
case PLUSPLUS:
return "++";
Expand Down Expand Up @@ -72,5 +72,13 @@ namespace filc::ast {
case OR:
return "||";
}

throw std::logic_error("Should not come here");
}

auto ClassicOperator::dumpLambdaType(filc::ast::AbstractType *return_type,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> LambdaType * {
return new LambdaType({}, return_type);
}
}
20 changes: 19 additions & 1 deletion src/lib/ast/FunctionOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,25 @@ namespace filc::ast {
}
}

std::string FunctionOperator::dump() {
auto FunctionOperator::dump() -> std::string {
return "()";
}

auto FunctionOperator::dumpLambdaType(filc::ast::AbstractType *return_type,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> LambdaType * {
std::vector<AbstractType *> args_types;
for (const auto &expression: _expressions) {
expression->resolveType(environment, collector);
auto *expression_type = expression->getExpressionType();
if (expression_type != nullptr) {
args_types.push_back(expression_type);
}
}
if (args_types.size() != _expressions.size()) {
return nullptr;
}

return new LambdaType(args_types, return_type);
}
}
23 changes: 0 additions & 23 deletions src/lib/ast/PreUnaryCalcul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,8 @@
* SOFTWARE.
*/
#include "AST.h"
#include "Error.h"

namespace filc::ast {
PreUnaryCalcul::PreUnaryCalcul(filc::ast::Identifier *variable, filc::ast::Operator *p_operator)
: UnaryCalcul(variable, p_operator) {}

auto PreUnaryCalcul::resolveType(filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> void {
_variable->resolveType(environment, collector);
auto *variable_type = _variable->getExpressionType();
if (variable_type == nullptr) {
return;
}

auto operator_name = "operator" + _operator->dump();
if (environment->getName(operator_name, new LambdaType({}, variable_type)) == nullptr) {
collector->addError(
new filc::message::Error(filc::message::ERROR,
"There is no operator " + _operator->dump() +
" for type " + variable_type->dump(),
getPosition())
);
return;
}

setExpressionType(variable_type);
}
}
24 changes: 24 additions & 0 deletions src/lib/ast/UnaryCalcul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* SOFTWARE.
*/
#include "AST.h"
#include "Error.h"

namespace filc::ast {
UnaryCalcul::UnaryCalcul(filc::ast::Identifier *variable, Operator *p_operator)
Expand All @@ -39,4 +40,27 @@ namespace filc::ast {
delete _variable;
delete _operator;
}

auto UnaryCalcul::resolveType(filc::environment::Environment *environment,
filc::message::MessageCollector *collector) -> void {
_variable->resolveType(environment, collector);
auto *variable_type = _variable->getExpressionType();
if (variable_type == nullptr) {
return;
}

auto operator_name = "operator" + _operator->dump();
if (environment->getName(operator_name,
_operator->dumpLambdaType(variable_type, environment, collector)) == nullptr) {
collector->addError(
new filc::message::Error(filc::message::ERROR,
"There is no operator " + _operator->dump() +
" for type " + variable_type->dump(),
getPosition())
);
return;
}

setExpressionType(variable_type);
}
}
4 changes: 4 additions & 0 deletions tests/unit/Fixtures/ast/post_unary_calcul1.fil
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module test

var a: int = 3
a++
15 changes: 14 additions & 1 deletion tests/unit/ast/PostUnaryCalculTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/
#include "AST.h"
#include <gtest/gtest.h>
#include "Parser.h"
#include "test_tools.h"

TEST(PostUnaryCalcul, constructor) {
Expand All @@ -32,4 +32,17 @@ TEST(PostUnaryCalcul, constructor) {
);
ASSERT_IDENTIFIER("abcd", puc1.getVariable());
ASSERT_CLASSIC_OPERATOR(MOD, puc1.getOperator());
}

#define FIXTURES_PATH "../../tests/unit/Fixtures"

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

TEST(PostUnaryCalcul, resolveType) {
filc::grammar::Parser parser1(FIXTURES_PATH "/ast/post_unary_calcul1.fil", COLLECTOR);
auto *program1 = parser1.getProgram();
ASSERT_NO_THROW(program1->resolveEnvironment(COLLECTOR));
ASSERT_THAT(program1->getExpressions(), SizeIs(2));
ASSERT_TYPE("int", program1->getExpressions()[0]->getExpressionType());
ASSERT_TYPE("int", program1->getExpressions()[1]->getExpressionType());
}
2 changes: 1 addition & 1 deletion tests/unit/ast/PreUnaryCalculTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TEST(PreUnaryCalcul, constructor) {
#define COLLECTOR filc::message::MessageCollector::getCollector()

TEST(PreUnaryCalcul, resolveType) {
filc::grammar::Parser parser1(FIXTURES_PATH "/ast/unary_calcul1.fil", COLLECTOR);
filc::grammar::Parser parser1(FIXTURES_PATH "/ast/pre_unary_calcul1.fil", COLLECTOR);
auto *program1 = parser1.getProgram();
ASSERT_NO_THROW(program1->resolveEnvironment(COLLECTOR));
ASSERT_THAT(program1->getExpressions(), SizeIs(2));
Expand Down

0 comments on commit 698193c

Please sign in to comment.