diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index d9c8580b..d1488e30 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -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} diff --git a/src/lib/ast/AST.h b/src/lib/ast/AST.h index 5ab5fcbe..f21e57fe 100644 --- a/src/lib/ast/AST.h +++ b/src/lib/ast/AST.h @@ -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 { diff --git a/src/lib/ast/AST_decl.h b/src/lib/ast/AST_decl.h index 5fefe217..0af7ed84 100644 --- a/src/lib/ast/AST_decl.h +++ b/src/lib/ast/AST_decl.h @@ -58,6 +58,10 @@ namespace filc::ast { class UnaryCalcul; + class PreUnaryCalcul; + + class PostUnaryCalcul; + class Operator; class ClassicOperator; diff --git a/src/lib/ast/PostUnaryCalcul.cpp b/src/lib/ast/PostUnaryCalcul.cpp new file mode 100644 index 00000000..350c7660 --- /dev/null +++ b/src/lib/ast/PostUnaryCalcul.cpp @@ -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) {} +} diff --git a/src/lib/ast/PreUnaryCalcul.cpp b/src/lib/ast/PreUnaryCalcul.cpp new file mode 100644 index 00000000..17d49a8e --- /dev/null +++ b/src/lib/ast/PreUnaryCalcul.cpp @@ -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) {} +} diff --git a/src/lib/ast/UnaryCalcul.cpp b/src/lib/ast/UnaryCalcul.cpp index fd0ac0fa..aa9a21fe 100644 --- a/src/lib/ast/UnaryCalcul.cpp +++ b/src/lib/ast/UnaryCalcul.cpp @@ -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; + } } diff --git a/src/lib/grammar/FilParser.g4 b/src/lib/grammar/FilParser.g4 index 6276f397..c66145c5 100644 --- a/src/lib/grammar/FilParser.g4 +++ b/src/lib/grammar/FilParser.g4 @@ -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] diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7f8067f5..729861d0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/unit/Fixtures/grammar/unary_calcul2.fil b/tests/unit/Fixtures/grammar/unary_calcul2.fil new file mode 100644 index 00000000..949980ed --- /dev/null +++ b/tests/unit/Fixtures/grammar/unary_calcul2.fil @@ -0,0 +1,3 @@ +module test + +--b \ No newline at end of file diff --git a/tests/unit/Fixtures/grammar/unary_calcul3.fil b/tests/unit/Fixtures/grammar/unary_calcul3.fil new file mode 100644 index 00000000..07d5810e --- /dev/null +++ b/tests/unit/Fixtures/grammar/unary_calcul3.fil @@ -0,0 +1,3 @@ +module test + +multiply(2, 3) \ No newline at end of file diff --git a/tests/unit/Fixtures/grammar/unary_calcul4.fil b/tests/unit/Fixtures/grammar/unary_calcul4.fil new file mode 100644 index 00000000..aab0e4c7 --- /dev/null +++ b/tests/unit/Fixtures/grammar/unary_calcul4.fil @@ -0,0 +1,3 @@ +module test + +array[4] \ No newline at end of file diff --git a/tests/unit/ast/PostUnaryCalculTest.cpp b/tests/unit/ast/PostUnaryCalculTest.cpp new file mode 100644 index 00000000..c2e3510b --- /dev/null +++ b/tests/unit/ast/PostUnaryCalculTest.cpp @@ -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 + +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(puc1.getOperator())->getOperator() + ); +} \ No newline at end of file diff --git a/tests/unit/ast/PreUnaryCalculTest.cpp b/tests/unit/ast/PreUnaryCalculTest.cpp new file mode 100644 index 00000000..e3230d47 --- /dev/null +++ b/tests/unit/ast/PreUnaryCalculTest.cpp @@ -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 + +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(puc1.getOperator())->getOperator() + ); +} \ No newline at end of file diff --git a/tests/unit/ast/UnaryCalculTest.cpp b/tests/unit/ast/UnaryCalculTest.cpp index 530d1329..a42f0258 100644 --- a/tests/unit/ast/UnaryCalculTest.cpp +++ b/tests/unit/ast/UnaryCalculTest.cpp @@ -25,6 +25,11 @@ #include 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(uc1.getOperator())->getOperator() + ); } \ No newline at end of file diff --git a/tests/unit/grammar/ParserTest.cpp b/tests/unit/grammar/ParserTest.cpp index 1785097a..b6ee55aa 100644 --- a/tests/unit/grammar/ParserTest.cpp +++ b/tests/unit/grammar/ParserTest.cpp @@ -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(program1->getExpressions()[0]); + auto *expression1 = static_cast(program1->getExpressions()[0]); ASSERT_NE(nullptr, expression1); ASSERT_STREQ("a", expression1->getVariable()->getName().c_str()); + ASSERT_EQ( + filc::ast::ClassicOperator::PLUSPLUS, + static_cast(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(program2->getExpressions()[0]); + ASSERT_NE(nullptr, expression2); + ASSERT_STREQ("b", expression2->getVariable()->getName().c_str()); + ASSERT_EQ( + filc::ast::ClassicOperator::MINUSMINUS, + static_cast(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(program3->getExpressions()[0]); + ASSERT_NE(nullptr, expression3); + ASSERT_STREQ("multiply", expression3->getVariable()->getName().c_str()); + auto *operator3 = static_cast(expression3->getOperator()); + ASSERT_THAT(operator3->getExpressions(), SizeIs(2)); + ASSERT_EQ(2, static_cast(operator3->getExpressions()[0])->getValue()); + ASSERT_EQ(3, static_cast(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(program4->getExpressions()[0]); + ASSERT_NE(nullptr, expression4); + ASSERT_STREQ("array", expression4->getVariable()->getName().c_str()); + auto *operator4 = static_cast(expression4->getOperator()); + ASSERT_EQ(4, static_cast(operator4->getExpression())->getValue()); } \ No newline at end of file