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

Commit

Permalink
Add FloatLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
Gashmob committed Jul 25, 2023
1 parent dbf07ac commit 51c0028
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/lib/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ namespace filc::ast {
public:
explicit IntegerLiteral(int value);
};

class FloatLiteral : public AbstractLiteral<double> {
public:
explicit FloatLiteral(double value);
};
}

#endif //FILC_AST_H
2 changes: 2 additions & 0 deletions src/lib/ast/AST_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace filc::ast {
class BooleanLiteral;

class IntegerLiteral;

class FloatLiteral;
}

#endif //FILC_AST_DECL_H
29 changes: 29 additions & 0 deletions src/lib/ast/FloatLiteral.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 {
FloatLiteral::FloatLiteral(double value)
: AbstractLiteral<double>(value) {}
}
5 changes: 4 additions & 1 deletion src/lib/grammar/FilParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
4 changes: 3 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/Fixtures/grammar/float1.fil
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module test

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

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

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

3.14159265359
36 changes: 36 additions & 0 deletions tests/unit/ast/FloatLiteralTest.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

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());
}
30 changes: 30 additions & 0 deletions tests/unit/grammar/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,34 @@ TEST(Parser, IntegerLiteral) {
auto *expression3 = static_cast<filc::ast::IntegerLiteral *>(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<filc::ast::FloatLiteral *>(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<filc::ast::FloatLiteral *>(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<filc::ast::FloatLiteral *>(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<filc::ast::FloatLiteral *>(program4->getExpressions()[0]);
ASSERT_NE(nullptr, expression4);
ASSERT_EQ(3.14159265359, expression4->getValue());
}

0 comments on commit 51c0028

Please sign in to comment.