Renamed floating point to double

This commit is contained in:
Benoy Bose 2024-11-29 19:30:41 +05:30
parent 35be166f8e
commit 81b677b9fe
9 changed files with 37 additions and 36 deletions

6
.vscode/launch.json vendored
View File

@ -8,7 +8,7 @@
"name": "Integer Test",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/tests/integer_literal_tests",
"program": "${workspaceFolder}/build/tests/integer_tests",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
@ -29,10 +29,10 @@
]
},
{
"name": "Float Test",
"name": "Double Test",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/tests/floatingpoint_literal_tests",
"program": "${workspaceFolder}/build/tests/double_tests",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",

View File

@ -24,5 +24,5 @@ ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(tests)
ENABLE_TESTING()
ADD_TEST(NAME integer_literal_tests COMMAND integer_literal_tests)
ADD_TEST(NAME integer_tests COMMAND integer_tests)
ADD_TEST(NAME double_tests COMMAND double_tests)

4
Hoo.g4
View File

@ -7,8 +7,8 @@ statement: literalStatement;
literalStatement: literal ';';
literal:
DECIMAL_LITERAL
| FLOAT_LITERAL
INTEGER_LITERAL
| DOUBLE_LITERAL
| BOOL_LITERAL
| CHAR_LITERAL
| STRING_LITERAL

View File

@ -4,9 +4,9 @@ IDENTIFIER: Letter LetterOrDigit*;
fragment DIGIT: [0-9];
DECIMAL_LITERAL: '0' | ('+' | '-')? [1-9] [0-9]*;
INTEGER_LITERAL: '0' | ('+' | '-')? [1-9] [0-9]*;
FLOAT_LITERAL: ('+' | '-')? Digits+ '.' Digits+ (
DOUBLE_LITERAL: ('+' | '-')? Digits+ '.' Digits+ (
[eE] [+-]? Digits+
)?;

View File

@ -12,7 +12,7 @@ enum DataType
{
DATATYPE_VOID,
DATATYPE_INTEGER,
DATATYPE_FLOAT,
DATATYPE_DOUBLE,
DATATYPE_BOOL,
DATATYPE_CHAR,
DATATYPE_STRING

View File

@ -14,28 +14,29 @@ Visitor::Visitor(const std::string &moduleName) : _moduleName(moduleName),
std::any Visitor::visitLiteral(HooParser::LiteralContext *ctx)
{
auto value = ctx->DECIMAL_LITERAL();
auto value = ctx->INTEGER_LITERAL();
if (value)
{
auto decimalText = value->getText();
double decimalValue = std::stoll(decimalText);
llvm::Type *decimalType = llvm::Type::getInt64Ty(*_context);
llvm::Constant *decimalConstant = llvm::ConstantInt::getSigned(decimalType, decimalValue);
#ifdef DEBUG
#ifdef NDEBUG
auto constantValue = llvm::dyn_cast<llvm::ConstantInt>(decimalConstant)->getValue().getSExtValue();
assert(constantValue == decimalValue);
#endif
return std::any{Node(NODE_LITERAL, DATATYPE_INTEGER, decimalConstant)};
}
value = ctx->FLOAT_LITERAL();
value = ctx->DOUBLE_LITERAL();
if (value)
{
auto doubleText = value->getText();
auto doubleValue = std::stod(doubleText);
llvm::Constant *doubleConstant = llvm::ConstantFP::get(
llvm::Type::getDoubleTy(*_context), doubleValue);
return std::any{Node(NODE_LITERAL, DATATYPE_FLOAT, doubleConstant)};
return std::any{Node(NODE_LITERAL, DATATYPE_DOUBLE, doubleConstant)};
}
value = ctx->BOOL_LITERAL();

View File

@ -1,9 +1,9 @@
FIND_PACKAGE (GTest REQUIRED)
ADD_EXECUTABLE(integer_literal_tests integer_literal_tests.cpp)
TARGET_LINK_LIBRARIES(integer_literal_tests GTest::GTest GTest::Main hoocore antlr4-runtime LLVMCore LLVMSupport)
TARGET_INCLUDE_DIRECTORIES(integer_literal_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
ADD_EXECUTABLE(integer_tests integer_tests.cpp)
TARGET_LINK_LIBRARIES(integer_tests GTest::GTest GTest::Main hoocore antlr4-runtime LLVMCore LLVMSupport)
TARGET_INCLUDE_DIRECTORIES(integer_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
ADD_EXECUTABLE(floatingpoint_literal_tests floatingpoint_literal_tests.cpp)
TARGET_LINK_LIBRARIES(floatingpoint_literal_tests GTest::GTest GTest::Main hoocore antlr4-runtime LLVMCore LLVMSupport)
TARGET_INCLUDE_DIRECTORIES(floatingpoint_literal_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
ADD_EXECUTABLE(double_tests double_tests.cpp)
TARGET_LINK_LIBRARIES(double_tests GTest::GTest GTest::Main hoocore antlr4-runtime LLVMCore LLVMSupport)
TARGET_INCLUDE_DIRECTORIES(double_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)

View File

@ -3,17 +3,17 @@
#include "llvm/IR/Constants.h"
#include <gtest/gtest.h>
// Utility function to test floating-point literal compilation
void testFloatLiteral(const std::string &input, double expectedValue)
// Utility function to test double literal compilation
void testDoubleLiteral(const std::string &input, double expectedValue)
{
auto compiler = std::make_unique<Compiler>(input, "main");
auto result = compiler->compile();
auto floatNode = std::any_cast<Node>(result);
auto doubleNode = std::any_cast<Node>(result);
ASSERT_EQ(floatNode.getNodeType(), NODE_LITERAL);
ASSERT_EQ(floatNode.getDataType(), DATATYPE_FLOAT);
ASSERT_EQ(doubleNode.getNodeType(), NODE_LITERAL);
ASSERT_EQ(doubleNode.getDataType(), DATATYPE_DOUBLE);
auto value = floatNode.getValue();
auto value = doubleNode.getValue();
auto expected_value = llvm::ConstantFP::get(
llvm::Type::getDoubleTy(*compiler->getContext()), expectedValue);
@ -27,27 +27,27 @@ void testFloatLiteral(const std::string &input, double expectedValue)
}
// Test cases
TEST(FloatTest, LiteralPositive)
TEST(DoubleTest, LiteralPositive)
{
testFloatLiteral("3.14;", 3.14);
testDoubleLiteral("3.14;", 3.14);
}
TEST(FloatTest, LiteralNegative)
TEST(DoubleTest, LiteralNegative)
{
testFloatLiteral("-2.718;", -2.718);
testDoubleLiteral("-2.718;", -2.718);
}
TEST(FloatTest, LiteralZero)
TEST(DoubleTest, LiteralZero)
{
testFloatLiteral("0.0;", 0.0);
testDoubleLiteral("0.0;", 0.0);
}
TEST(FloatTest, LiteralScientificNotationPositive)
TEST(DoubleTest, LiteralScientificNotationPositive)
{
testFloatLiteral("12300.0;", 12300.0);
testDoubleLiteral("12300.0;", 12300.0);
}
TEST(FloatTest, LiteralScientificNotationNegative)
TEST(DoubleTest, LiteralScientificNotationNegative)
{
testFloatLiteral("-0.00567;", -0.00567);
testDoubleLiteral("-0.00567;", -0.00567);
}