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 PreUnaryCalcul
Browse files Browse the repository at this point in the history
  • Loading branch information
Gashmob committed Aug 25, 2023
1 parent b0f9f88 commit 5414ba3
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 6 deletions.
21 changes: 18 additions & 3 deletions src/lib/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ namespace filc::ast {

[[nodiscard]] auto getExpressionType() const -> AbstractType *;

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

private:
bool _exported{false};
Expand All @@ -102,7 +103,8 @@ namespace filc::ast {

[[nodiscard]] auto getName() const -> const std::string &;

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

private:
std::string _name;
Expand Down Expand Up @@ -288,6 +290,9 @@ 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 @@ -325,6 +330,8 @@ namespace filc::ast {

auto operator=(Operator &&other) -> Operator & = default;

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

protected:
Operator() = default;
};
Expand Down Expand Up @@ -357,6 +364,8 @@ namespace filc::ast {

[[nodiscard]] auto getOperator() const -> OPERATOR;

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

private:
OPERATOR _operator;
};
Expand All @@ -369,6 +378,8 @@ namespace filc::ast {

[[nodiscard]] auto getExpression() const -> AbstractExpression *;

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

private:
AbstractExpression *_expression;
};
Expand All @@ -381,6 +392,8 @@ namespace filc::ast {

[[nodiscard]] auto getExpressions() const -> const std::vector<AbstractExpression *> &;

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

private:
std::vector<AbstractExpression *> _expressions;
};
Expand All @@ -393,6 +406,8 @@ namespace filc::ast {

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

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

private:
Operator *_inner_operator;
};
Expand Down Expand Up @@ -461,7 +476,7 @@ namespace filc::ast {
private:
AbstractExpression *_condition;
std::vector<AbstractExpression *> _body;
If* _else;
If *_else;
};

class Switch : public AbstractExpression {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/ast/ArrayOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ namespace filc::ast {
ArrayOperator::~ArrayOperator() {
delete _expression;
}

std::string ArrayOperator::dump() {
return "[]";
}
}
4 changes: 4 additions & 0 deletions src/lib/ast/AssignationOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ namespace filc::ast {
auto AssignationOperator::getInnerOperator() const -> Operator * {
return _inner_operator;
}

std::string AssignationOperator::dump() {
throw std::logic_error("Should not be called");
}
}
43 changes: 43 additions & 0 deletions src/lib/ast/ClassicOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,47 @@ namespace filc::ast {
auto ClassicOperator::getOperator() const -> OPERATOR {
return _operator;
}

std::string ClassicOperator::dump() {
switch (_operator) {
case PLUSPLUS:
return "++";
case MINUSMINUS:
return "--";
case PLUS:
return "+";
case MINUS:
return "-";
case REF:
return "&";
case STAR:
return "*";
case NOT:
return "!";
case DIV:
return "/";
case MOD:
return "%";
case FLEFT:
return "<<";
case FRIGHT:
return ">>";
case LESS:
return "<";
case GREATER:
return ">";
case EQEQ:
return "==";
case LEQ:
return "<=";
case GEQ:
return ">=";
case NEQ:
return "!=";
case AND:
return "&&";
case OR:
return "||";
}
}
}
4 changes: 4 additions & 0 deletions src/lib/ast/FunctionOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ namespace filc::ast {
delete expression;
}
}

std::string FunctionOperator::dump() {
return "()";
}
}
23 changes: 23 additions & 0 deletions src/lib/ast/PreUnaryCalcul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,31 @@
* 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);
}
}
3 changes: 2 additions & 1 deletion src/lib/ast/VariableDeclaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace filc::ast {
return _constant;
}

auto VariableDeclaration::getIdentifier() const -> Identifier* {
auto VariableDeclaration::getIdentifier() const -> Identifier * {
return _identifier;
}

Expand Down Expand Up @@ -80,6 +80,7 @@ namespace filc::ast {
return;
}

environment->addName(_identifier->getName(), _type);
setExpressionType(_type);
}
}
21 changes: 20 additions & 1 deletion src/lib/environment/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ namespace filc::environment {
auto *float_type = new filc::ast::Type(new filc::ast::Identifier("float"));
auto *char_type = new filc::ast::Type(new filc::ast::Identifier("char"));
auto *bool_type = new filc::ast::Type(new filc::ast::Identifier("bool"));
auto *void_type = new filc::ast::Type(new filc::ast::Identifier("void"));
auto is_ok = global->addType(int_type)
&& global->addType(double_type)
&& global->addType(float_type)
&& global->addType(char_type)
&& global->addType(bool_type);
&& global->addType(bool_type)
&& global->addType(void_type);
if (!is_ok) {
throw std::logic_error("Fail to add base types to global environment");
}
Expand Down Expand Up @@ -157,6 +159,23 @@ namespace filc::environment {
if (!is_ok) {
throw std::logic_error("Fail to add base assignations to global environment");
}
// - Prefix unary
is_ok = global->addName("operator++", new filc::ast::LambdaType({}, int_type))
&& global->addName("operator--", new filc::ast::LambdaType({}, int_type))
&& global->addName("operator+", new filc::ast::LambdaType({}, int_type))
&& global->addName("operator-", new filc::ast::LambdaType({}, int_type))
&& global->addName("operator++", new filc::ast::LambdaType({}, double_type))
&& global->addName("operator--", new filc::ast::LambdaType({}, double_type))
&& global->addName("operator+", new filc::ast::LambdaType({}, double_type))
&& global->addName("operator-", new filc::ast::LambdaType({}, double_type))
&& global->addName("operator++", new filc::ast::LambdaType({}, float_type))
&& global->addName("operator--", new filc::ast::LambdaType({}, float_type))
&& global->addName("operator+", new filc::ast::LambdaType({}, float_type))
&& global->addName("operator-", new filc::ast::LambdaType({}, float_type))
&& global->addName("operator!", new filc::ast::LambdaType({}, bool_type));
if (!is_ok) {
throw std::logic_error("Fail to add base prefix unary calcul to global environment");
}

return global;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Fixtures/ast/unary_calcul1.fil
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module test

var a: int = 12
--a
15 changes: 14 additions & 1 deletion tests/unit/ast/PreUnaryCalculTest.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(PreUnaryCalcul, constructor) {
Expand All @@ -32,4 +32,17 @@ TEST(PreUnaryCalcul, constructor) {
);
ASSERT_IDENTIFIER("val1", puc1.getVariable());
ASSERT_CLASSIC_OPERATOR(DIV, puc1.getOperator());
}

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

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

TEST(PreUnaryCalcul, resolveType) {
filc::grammar::Parser parser1(FIXTURES_PATH "/ast/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());
}

0 comments on commit 5414ba3

Please sign in to comment.