From 51c0028d35fa446ddf30f4bd958bbddf0510d0f6 Mon Sep 17 00:00:00 2001 From: Kevin Traini Date: Tue, 25 Jul 2023 21:40:21 +0200 Subject: [PATCH] Add FloatLiteral --- src/lib/CMakeLists.txt | 2 +- src/lib/ast/AST.h | 5 ++++ src/lib/ast/AST_decl.h | 2 ++ src/lib/ast/FloatLiteral.cpp | 29 +++++++++++++++++++++ src/lib/grammar/FilParser.g4 | 5 +++- tests/CMakeLists.txt | 4 ++- tests/unit/Fixtures/grammar/float1.fil | 3 +++ tests/unit/Fixtures/grammar/float2.fil | 3 +++ tests/unit/Fixtures/grammar/float3.fil | 3 +++ tests/unit/Fixtures/grammar/float4.fil | 3 +++ tests/unit/ast/FloatLiteralTest.cpp | 36 ++++++++++++++++++++++++++ tests/unit/grammar/ParserTest.cpp | 30 +++++++++++++++++++++ 12 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 src/lib/ast/FloatLiteral.cpp create mode 100644 tests/unit/Fixtures/grammar/float1.fil create mode 100644 tests/unit/Fixtures/grammar/float2.fil create mode 100644 tests/unit/Fixtures/grammar/float3.fil create mode 100644 tests/unit/Fixtures/grammar/float4.fil create mode 100644 tests/unit/ast/FloatLiteralTest.cpp diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 6012a2ab..3ebc4d01 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -48,7 +48,7 @@ add_library(compiler_lib # === Message === message/Message.cpp message/MessageCollector.cpp message/Warning.cpp message/Error.cpp # === AST === - ast/Program.cpp ast/AbstractExpression.cpp ast/BooleanLiteral.cpp ast/IntegerLiteral.cpp) + ast/Program.cpp ast/AbstractExpression.cpp ast/BooleanLiteral.cpp ast/IntegerLiteral.cpp ast/FloatLiteral.cpp) target_include_directories(compiler_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} utils message grammar ast diff --git a/src/lib/ast/AST.h b/src/lib/ast/AST.h index 90c7af59..c37ddd5a 100644 --- a/src/lib/ast/AST.h +++ b/src/lib/ast/AST.h @@ -103,6 +103,11 @@ namespace filc::ast { public: explicit IntegerLiteral(int value); }; + + class FloatLiteral : public AbstractLiteral { + public: + explicit FloatLiteral(double value); + }; } #endif //FILC_AST_H diff --git a/src/lib/ast/AST_decl.h b/src/lib/ast/AST_decl.h index 4fad2729..2cbebbf8 100644 --- a/src/lib/ast/AST_decl.h +++ b/src/lib/ast/AST_decl.h @@ -35,6 +35,8 @@ namespace filc::ast { class BooleanLiteral; class IntegerLiteral; + + class FloatLiteral; } #endif //FILC_AST_DECL_H diff --git a/src/lib/ast/FloatLiteral.cpp b/src/lib/ast/FloatLiteral.cpp new file mode 100644 index 00000000..a1df8996 --- /dev/null +++ b/src/lib/ast/FloatLiteral.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 { + FloatLiteral::FloatLiteral(double value) + : AbstractLiteral(value) {} +} \ No newline at end of file diff --git a/src/lib/grammar/FilParser.g4 b/src/lib/grammar/FilParser.g4 index 4d65d8e2..6f708385 100644 --- a/src/lib/grammar/FilParser.g4 +++ b/src/lib/grammar/FilParser.g4 @@ -109,7 +109,10 @@ number returns[filc::ast::AbstractExpression *tree] $tree = new filc::ast::IntegerLiteral(stoi($i.text)); $tree->setPosition(new filc::utils::Position($i)); } - | FLOAT; + | f=FLOAT { + $tree = new filc::ast::FloatLiteral(stod($f.text)); + $tree->setPosition(new filc::utils::Position($f)); + }; variable_declaration : (VAL | VAR) IDENTIFIER COLON type assignation?; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 49f64271..4a203408 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,7 +10,9 @@ FetchContent_MakeAvailable(googletest) add_executable(tests unit/utils/OptionsParserTest.cpp unit/utils/PositionTest.cpp unit/message/MessageTest.cpp unit/message/MessageCollectorTest.cpp - unit/ast/ProgramTest.cpp unit/grammar/ParserTest.cpp unit/ast/BooleanLiteralTest.cpp unit/ast/IntegerLiteralTest.cpp) + unit/grammar/ParserTest.cpp + unit/ast/ProgramTest.cpp unit/ast/BooleanLiteralTest.cpp unit/ast/IntegerLiteralTest.cpp unit/ast/FloatLiteralTest.cpp + ) target_include_directories(tests PUBLIC unit) target_link_libraries(tests PRIVATE GTest::gtest_main compiler_lib) diff --git a/tests/unit/Fixtures/grammar/float1.fil b/tests/unit/Fixtures/grammar/float1.fil new file mode 100644 index 00000000..f1a79a98 --- /dev/null +++ b/tests/unit/Fixtures/grammar/float1.fil @@ -0,0 +1,3 @@ +module test + +42.0 \ No newline at end of file diff --git a/tests/unit/Fixtures/grammar/float2.fil b/tests/unit/Fixtures/grammar/float2.fil new file mode 100644 index 00000000..ccad25ff --- /dev/null +++ b/tests/unit/Fixtures/grammar/float2.fil @@ -0,0 +1,3 @@ +module test + +-2.5 \ No newline at end of file diff --git a/tests/unit/Fixtures/grammar/float3.fil b/tests/unit/Fixtures/grammar/float3.fil new file mode 100644 index 00000000..01fabbcc --- /dev/null +++ b/tests/unit/Fixtures/grammar/float3.fil @@ -0,0 +1,3 @@ +module test + +4.45 \ No newline at end of file diff --git a/tests/unit/Fixtures/grammar/float4.fil b/tests/unit/Fixtures/grammar/float4.fil new file mode 100644 index 00000000..28189a84 --- /dev/null +++ b/tests/unit/Fixtures/grammar/float4.fil @@ -0,0 +1,3 @@ +module test + +3.14159265359 \ No newline at end of file diff --git a/tests/unit/ast/FloatLiteralTest.cpp b/tests/unit/ast/FloatLiteralTest.cpp new file mode 100644 index 00000000..463f0283 --- /dev/null +++ b/tests/unit/ast/FloatLiteralTest.cpp @@ -0,0 +1,36 @@ +/** + * 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(FloatLiteral, constructor) { + filc::ast::FloatLiteral fl1(42); + ASSERT_EQ(42, fl1.getValue()); + + filc::ast::FloatLiteral fl2(2.3); + ASSERT_EQ(2.3, fl2.getValue()); + + filc::ast::FloatLiteral fl3(-4.5); + ASSERT_EQ(-4.5, fl3.getValue()); +} \ No newline at end of file diff --git a/tests/unit/grammar/ParserTest.cpp b/tests/unit/grammar/ParserTest.cpp index f193a722..befe2dce 100644 --- a/tests/unit/grammar/ParserTest.cpp +++ b/tests/unit/grammar/ParserTest.cpp @@ -140,4 +140,34 @@ TEST(Parser, IntegerLiteral) { auto *expression3 = static_cast(program3->getExpressions()[0]); ASSERT_NE(nullptr, expression3); ASSERT_EQ(25, expression3->getValue()); +} + +TEST(Parser, FloatLiteral) { + filc::grammar::Parser parser1(FIXTURES_PATH "/float1.fil"); + auto *program1 = parser1.getProgram(); + ASSERT_THAT(program1->getExpressions(), SizeIs(1)); + auto *expression1 = static_cast(program1->getExpressions()[0]); + ASSERT_NE(nullptr, expression1); + ASSERT_EQ(42.0, expression1->getValue()); + + filc::grammar::Parser parser2(FIXTURES_PATH "/float2.fil"); + auto *program2 = parser2.getProgram(); + ASSERT_THAT(program2->getExpressions(), SizeIs(1)); + auto *expression2 = static_cast(program2->getExpressions()[0]); + ASSERT_NE(nullptr, expression2); + ASSERT_EQ(-2.5, expression2->getValue()); + + filc::grammar::Parser parser3(FIXTURES_PATH "/float3.fil"); + auto *program3 = parser3.getProgram(); + ASSERT_THAT(program3->getExpressions(), SizeIs(1)); + auto *expression3 = static_cast(program3->getExpressions()[0]); + ASSERT_NE(nullptr, expression3); + ASSERT_EQ(4.45, expression3->getValue()); + + filc::grammar::Parser parser4(FIXTURES_PATH "/float4.fil"); + auto *program4 = parser4.getProgram(); + ASSERT_THAT(program4->getExpressions(), SizeIs(1)); + auto *expression4 = static_cast(program4->getExpressions()[0]); + ASSERT_NE(nullptr, expression4); + ASSERT_EQ(3.14159265359, expression4->getValue()); } \ No newline at end of file