Added more tests
This commit is contained in:
parent
ef66ec9ebd
commit
1a80064f9b
@ -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);
|
||||
|
||||
|
||||
@ -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<llvm::ConstantInt>(decimalConstant)->getValue().getSExtValue();
|
||||
assert(constantValue == decimalValue);
|
||||
#endif
|
||||
return std::any{Node(NODE_LITERAL, DATATYPE_INTEGER, decimalConstant)};
|
||||
}
|
||||
|
||||
|
||||
@ -1,15 +1,63 @@
|
||||
#include "Compiler.hpp"
|
||||
#include "Node.hpp"
|
||||
#include "llvm/IR/Constants.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(IntegerTest, Literal)
|
||||
TEST(IntegerTest, LiteralOne)
|
||||
{
|
||||
auto compiler = new Compiler("1;", "main");
|
||||
auto compiler = std::make_unique<Compiler>("1;", "main");
|
||||
auto result = compiler->compile();
|
||||
auto integer = std::any_cast<Node>(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<Compiler>("67890;", "main");
|
||||
auto result = compiler->compile();
|
||||
auto integer = std::any_cast<Node>(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<Compiler>("+67890;", "main");
|
||||
auto result = compiler->compile();
|
||||
auto integer = std::any_cast<Node>(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<Compiler>("-67890;", "main");
|
||||
auto result = compiler->compile();
|
||||
auto integer = std::any_cast<Node>(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<llvm::ConstantInt>(value);
|
||||
auto expected_value_constant = llvm::dyn_cast<llvm::ConstantInt>(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);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user