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

Commit

Permalink
Add Operator to UnaryCalcul
Browse files Browse the repository at this point in the history
  • Loading branch information
Gashmob committed Aug 5, 2023
1 parent 9d816ce commit e98a361
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ add_library(compiler_lib
ast/ClassicOperator.cpp
ast/ArrayOperator.cpp
ast/FunctionOperator.cpp
ast/PreUnaryCalcul.cpp
ast/PostUnaryCalcul.cpp
)
target_include_directories(compiler_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
Expand Down
17 changes: 15 additions & 2 deletions src/lib/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,25 @@ namespace filc::ast {

class UnaryCalcul : public AbstractExpression {
public:
explicit UnaryCalcul(Identifier *variable);
explicit UnaryCalcul(Identifier *variable, Operator *p_operator);

[[nodiscard]] auto getVariable() const -> Identifier *;

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

protected:
Identifier *_variable;
Operator *_operator;
};

class PreUnaryCalcul : public UnaryCalcul {
public:
PreUnaryCalcul(Identifier *variable, Operator *p_operator);
};

class PostUnaryCalcul : public UnaryCalcul {
public:
PostUnaryCalcul(Identifier *variable, Operator *p_operator);
};

class Operator {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/ast/AST_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ namespace filc::ast {

class UnaryCalcul;

class PreUnaryCalcul;

class PostUnaryCalcul;

class Operator;

class ClassicOperator;
Expand Down
29 changes: 29 additions & 0 deletions src/lib/ast/PostUnaryCalcul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* 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 "AST.h"

namespace filc::ast {
PostUnaryCalcul::PostUnaryCalcul(filc::ast::Identifier *variable, filc::ast::Operator *p_operator)
: UnaryCalcul(variable, p_operator) {}
}
29 changes: 29 additions & 0 deletions src/lib/ast/PreUnaryCalcul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* 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 "AST.h"

namespace filc::ast {
PreUnaryCalcul::PreUnaryCalcul(filc::ast::Identifier *variable, filc::ast::Operator *p_operator)
: UnaryCalcul(variable, p_operator) {}
}
8 changes: 6 additions & 2 deletions src/lib/ast/UnaryCalcul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
#include "AST.h"

namespace filc::ast {
UnaryCalcul::UnaryCalcul(filc::ast::Identifier *variable)
: AbstractExpression(), _variable(variable) {}
UnaryCalcul::UnaryCalcul(filc::ast::Identifier *variable, Operator *p_operator)
: AbstractExpression(), _variable(variable), _operator(p_operator) {}

auto UnaryCalcul::getVariable() const -> Identifier * {
return _variable;
}

auto UnaryCalcul::getOperator() const -> Operator * {
return _operator;
}
}
8 changes: 4 additions & 4 deletions src/lib/grammar/FilParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ type returns[filc::ast::AbstractType *tree]
};

unary_calcul returns[filc::ast::UnaryCalcul *tree]
: i=IDENTIFIER post_operator {
$tree = new filc::ast::UnaryCalcul(new filc::ast::Identifier($i));
: i=IDENTIFIER po=post_operator {
$tree = new filc::ast::PostUnaryCalcul(new filc::ast::Identifier($i), $po.tree);
}
| pre_operator i=IDENTIFIER {
$tree = new filc::ast::UnaryCalcul(new filc::ast::Identifier($i));
| pr=pre_operator i=IDENTIFIER {
$tree = new filc::ast::PreUnaryCalcul(new filc::ast::Identifier($i), $pr.tree);
};

post_operator returns[filc::ast::Operator *tree]
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ add_executable(tests
unit/ast/ClassicOperatorTest.cpp
unit/ast/ArrayOperatorTest.cpp
unit/ast/FunctionOperatorTest.cpp
unit/ast/PreUnaryCalculTest.cpp
unit/ast/PostUnaryCalculTest.cpp
)
target_include_directories(tests PUBLIC unit)

Expand Down
3 changes: 3 additions & 0 deletions tests/unit/Fixtures/grammar/unary_calcul2.fil
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module test

--b
3 changes: 3 additions & 0 deletions tests/unit/Fixtures/grammar/unary_calcul3.fil
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module test

multiply(2, 3)
3 changes: 3 additions & 0 deletions tests/unit/Fixtures/grammar/unary_calcul4.fil
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module test

array[4]
37 changes: 37 additions & 0 deletions tests/unit/ast/PostUnaryCalculTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* 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 "AST.h"
#include <gtest/gtest.h>

TEST(PostUnaryCalcul, constructor) {
filc::ast::PostUnaryCalcul puc1(
new filc::ast::Identifier("abcd"),
new filc::ast::ClassicOperator(filc::ast::ClassicOperator::MOD)
);
ASSERT_STREQ("abcd", puc1.getVariable()->getName().c_str());
ASSERT_EQ(
filc::ast::ClassicOperator::MOD,
static_cast<filc::ast::ClassicOperator *>(puc1.getOperator())->getOperator()
);
}
37 changes: 37 additions & 0 deletions tests/unit/ast/PreUnaryCalculTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* 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 "AST.h"
#include <gtest/gtest.h>

TEST(PreUnaryCalcul, constructor) {
filc::ast::PreUnaryCalcul puc1(
new filc::ast::Identifier("val1"),
new filc::ast::ClassicOperator(filc::ast::ClassicOperator::DIV)
);
ASSERT_STREQ("val1", puc1.getVariable()->getName().c_str());
ASSERT_EQ(
filc::ast::ClassicOperator::DIV,
static_cast<filc::ast::ClassicOperator *>(puc1.getOperator())->getOperator()
);
}
7 changes: 6 additions & 1 deletion tests/unit/ast/UnaryCalculTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
#include <gtest/gtest.h>

TEST(UnaryCalcul, constructor) {
filc::ast::UnaryCalcul uc1(new filc::ast::Identifier("var1"));
filc::ast::ClassicOperator co1(filc::ast::ClassicOperator::AND);
filc::ast::UnaryCalcul uc1(new filc::ast::Identifier("var1"), &co1);
ASSERT_STREQ("var1", uc1.getVariable()->getName().c_str());
ASSERT_EQ(
filc::ast::ClassicOperator::AND,
static_cast<filc::ast::ClassicOperator *>(uc1.getOperator())->getOperator()
);
}
37 changes: 36 additions & 1 deletion tests/unit/grammar/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,42 @@ TEST(Parser, UnaryCalcul) {
filc::grammar::Parser parser1(FIXTURES_PATH "/unary_calcul1.fil");
auto *program1 = parser1.getProgram();
ASSERT_THAT(program1->getExpressions(), SizeIs(1));
auto *expression1 = static_cast<filc::ast::UnaryCalcul *>(program1->getExpressions()[0]);
auto *expression1 = static_cast<filc::ast::PostUnaryCalcul *>(program1->getExpressions()[0]);
ASSERT_NE(nullptr, expression1);
ASSERT_STREQ("a", expression1->getVariable()->getName().c_str());
ASSERT_EQ(
filc::ast::ClassicOperator::PLUSPLUS,
static_cast<filc::ast::ClassicOperator *>(expression1->getOperator())->getOperator()
);

filc::grammar::Parser parser2(FIXTURES_PATH "/unary_calcul2.fil");
auto *program2 = parser2.getProgram();
ASSERT_THAT(program2->getExpressions(), SizeIs(1));
auto *expression2 = static_cast<filc::ast::PreUnaryCalcul *>(program2->getExpressions()[0]);
ASSERT_NE(nullptr, expression2);
ASSERT_STREQ("b", expression2->getVariable()->getName().c_str());
ASSERT_EQ(
filc::ast::ClassicOperator::MINUSMINUS,
static_cast<filc::ast::ClassicOperator *>(expression2->getOperator())->getOperator()
);

filc::grammar::Parser parser3(FIXTURES_PATH "/unary_calcul3.fil");
auto *program3 = parser3.getProgram();
ASSERT_THAT(program3->getExpressions(), SizeIs(1));
auto *expression3 = static_cast<filc::ast::PostUnaryCalcul *>(program3->getExpressions()[0]);
ASSERT_NE(nullptr, expression3);
ASSERT_STREQ("multiply", expression3->getVariable()->getName().c_str());
auto *operator3 = static_cast<filc::ast::FunctionOperator *>(expression3->getOperator());
ASSERT_THAT(operator3->getExpressions(), SizeIs(2));
ASSERT_EQ(2, static_cast<filc::ast::IntegerLiteral *>(operator3->getExpressions()[0])->getValue());
ASSERT_EQ(3, static_cast<filc::ast::IntegerLiteral *>(operator3->getExpressions()[1])->getValue());

filc::grammar::Parser parser4(FIXTURES_PATH "/unary_calcul4.fil");
auto *program4 = parser4.getProgram();
ASSERT_THAT(program4->getExpressions(), SizeIs(1));
auto *expression4 = static_cast<filc::ast::PostUnaryCalcul *>(program4->getExpressions()[0]);
ASSERT_NE(nullptr, expression4);
ASSERT_STREQ("array", expression4->getVariable()->getName().c_str());
auto *operator4 = static_cast<filc::ast::ArrayOperator *>(expression4->getOperator());
ASSERT_EQ(4, static_cast<filc::ast::IntegerLiteral *>(operator4->getExpression())->getValue());
}

0 comments on commit e98a361

Please sign in to comment.