71 lines
2.5 KiB
C++
71 lines
2.5 KiB
C++
#include "Visitor.hpp"
|
|
#include "Node.hpp"
|
|
|
|
#include <llvm/IR/Constants.h>
|
|
#include <llvm/IR/Type.h>
|
|
#include <llvm/Support/raw_ostream.h>
|
|
|
|
Visitor::Visitor(std::string &moduleName) : _moduleName(moduleName),
|
|
_context(std::make_shared<llvm::LLVMContext>()),
|
|
_module(std::make_shared<llvm::Module>(moduleName, *_context)),
|
|
_builder(std::make_shared<llvm::IRBuilder<>>(*_context))
|
|
{
|
|
}
|
|
|
|
std::any Visitor::visitLiteral(HooParser::LiteralContext *ctx)
|
|
{
|
|
auto value = ctx->DECIMAL_LITERAL();
|
|
if (value)
|
|
{
|
|
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);
|
|
return std::any{Node(NODE_LITERAL, DATATYPE_INTEGER, decimalConstant)};
|
|
}
|
|
|
|
value = ctx->FLOAT_LITERAL();
|
|
if (value)
|
|
{
|
|
auto floatText = value->getText();
|
|
float floatValue = std::stof(floatText);
|
|
llvm::Type *floatType = llvm::Type::getFloatTy(*_context);
|
|
llvm::Constant *floatConstant = llvm::ConstantFP::get(floatType, floatValue);
|
|
return std::any{Node(NODE_LITERAL, DATATYPE_FLOAT, floatConstant)};
|
|
}
|
|
|
|
value = ctx->BOOL_LITERAL();
|
|
if (value)
|
|
{
|
|
auto boolText = value->getText();
|
|
auto boolValue = boolText == "true" ? 1 : 0;
|
|
llvm::Type *boolType = llvm::Type::getInt1Ty(*_context);
|
|
llvm::Constant *boolConstant = llvm::ConstantInt::get(boolType, boolValue, true);
|
|
return std::any{Node(NODE_LITERAL, DATATYPE_BOOL, boolConstant)};
|
|
}
|
|
|
|
value = ctx->CHAR_LITERAL();
|
|
if (value)
|
|
{
|
|
auto charText = value->getText();
|
|
char charValue = charText[1];
|
|
llvm::Type *charType = llvm::Type::getInt8Ty(*_context);
|
|
llvm::Constant *charConstant = llvm::ConstantInt::get(charType, charValue, true);
|
|
return std::any{Node(NODE_LITERAL, DATATYPE_CHAR, charConstant)};
|
|
}
|
|
|
|
value = ctx->STRING_LITERAL();
|
|
if (value)
|
|
{
|
|
auto stringText = value->getText();
|
|
auto stringConstant = llvm::ConstantDataArray::getString(*_context, stringText);
|
|
return std::any{Node(NODE_LITERAL, DATATYPE_STRING, stringConstant)};
|
|
}
|
|
|
|
return std::any{Node(NODE_IGNORE, DATATYPE_VOID, nullptr)};
|
|
}
|
|
|
|
std::any Visitor::visitUnit(HooParser::UnitContext *ctx)
|
|
{
|
|
return visitChildren(ctx);
|
|
} |