diff --git a/HooLexer.g4 b/HooLexer.g4 index f97ea2d..15f92fa 100644 --- a/HooLexer.g4 +++ b/HooLexer.g4 @@ -2,10 +2,9 @@ lexer grammar HooLexer; IDENTIFIER: Letter LetterOrDigit*; -DECIMAL_LITERAL : '0' | [1-9] [0-9]*; -// HEX_LITERAL : '0' [xX] [0-9a-fA-F] ([0-9a-fA-F_]* [0-9a-fA-F])? [lL]?; -// OCT_LITERAL : '0' '_'* [0-7] ([0-7_]* [0-7])? [lL]?; -// BINARY_LITERAL : '0' [bB] [01] ([01_]* [01])? [lL]?; +fragment DIGIT: [0-9]; + +DECIMAL_LITERAL: '0' | ('+' | '-')? [1-9] [0-9]*; FLOAT_LITERAL: (Digits '.' Digits? | '.' Digits); diff --git a/src/Visitor.cpp b/src/Visitor.cpp index 10c976b..8bbb64d 100644 --- a/src/Visitor.cpp +++ b/src/Visitor.cpp @@ -20,7 +20,11 @@ std::any Visitor::visitLiteral(HooParser::LiteralContext *ctx) auto decimalText = value->getText(); double decimalValue = std::stoll(decimalText); llvm::Type *decimalType = llvm::Type::getInt64Ty(*_context); - llvm::Constant *decimalConstant = llvm::ConstantInt::get(decimalType, decimalValue, true); + llvm::Constant *decimalConstant = llvm::ConstantInt::getSigned(decimalType, decimalValue); +#ifdef DEBUG + auto constantValue = llvm::dyn_cast(decimalConstant)->getValue().getSExtValue(); + assert(constantValue == decimalValue); +#endif return std::any{Node(NODE_LITERAL, DATATYPE_INTEGER, decimalConstant)}; } diff --git a/tests/literal_tests.cpp b/tests/literal_tests.cpp index 3a610d4..8ace715 100644 --- a/tests/literal_tests.cpp +++ b/tests/literal_tests.cpp @@ -1,15 +1,63 @@ #include "Compiler.hpp" #include "Node.hpp" +#include "llvm/IR/Constants.h" #include -TEST(IntegerTest, Literal) +TEST(IntegerTest, LiteralOne) { - auto compiler = new Compiler("1;", "main"); + auto compiler = std::make_unique("1;", "main"); auto result = compiler->compile(); auto integer = std::any_cast(result); ASSERT_EQ(integer.getNodeType(), NODE_LITERAL); ASSERT_EQ(integer.getDataType(), DATATYPE_INTEGER); - ASSERT_EQ(integer.getValue(), llvm::ConstantInt::get(llvm::Type::getInt64Ty(*compiler->getContext()), 1, true)); - delete compiler; + auto value = integer.getValue(); + auto expected_value = llvm::ConstantInt::get(llvm::Type::getInt64Ty(*compiler->getContext()), 1, true); + ASSERT_EQ(value, expected_value); +} + +TEST(IntegerTest, LiteralNumberPositive) +{ + auto compiler = std::make_unique("67890;", "main"); + auto result = compiler->compile(); + auto integer = std::any_cast(result); + ASSERT_EQ(integer.getNodeType(), NODE_LITERAL); + ASSERT_EQ(integer.getDataType(), DATATYPE_INTEGER); + auto value = integer.getValue(); + auto expected_value = llvm::ConstantInt::get(llvm::Type::getInt64Ty(*compiler->getContext()), 67890, true); + ASSERT_EQ(value, expected_value); +} + +TEST(IntegerTest, LiteralNumberPositiveWithPlus) +{ + auto compiler = std::make_unique("+67890;", "main"); + auto result = compiler->compile(); + auto integer = std::any_cast(result); + ASSERT_EQ(integer.getNodeType(), NODE_LITERAL); + ASSERT_EQ(integer.getDataType(), DATATYPE_INTEGER); + auto value = integer.getValue(); + auto expected_value = llvm::ConstantInt::get(llvm::Type::getInt64Ty(*compiler->getContext()), 67890, true); + ASSERT_EQ(value, expected_value); +} + +TEST(IntegerTest, LiteralNumberNegative) +{ + auto compiler = std::make_unique("-67890;", "main"); + auto result = compiler->compile(); + auto integer = std::any_cast(result); + + ASSERT_EQ(integer.getNodeType(), NODE_LITERAL); + ASSERT_EQ(integer.getDataType(), DATATYPE_INTEGER); + + auto value = integer.getValue(); + auto expected_value = llvm::ConstantInt::get( + llvm::Type::getInt64Ty(*compiler->getContext()), -67890, true); + + auto value_constant = llvm::dyn_cast(value); + auto expected_value_constant = llvm::dyn_cast(expected_value); + + ASSERT_NE(value_constant, nullptr); + ASSERT_NE(expected_value_constant, nullptr); + ASSERT_EQ(value_constant->getValue(), expected_value_constant->getValue()); + ASSERT_EQ(value, expected_value); }