Tested literal statement

This commit is contained in:
Benoy Bose 2024-11-28 01:37:29 +05:30
parent 48aecca95d
commit f687b32cb5
10 changed files with 146 additions and 26 deletions

32
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/tests/literal_tests",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

View File

@ -21,3 +21,7 @@ ADD_CUSTOM_TARGET(HooBaseVisitor ALL DEPENDS ${ANTLR_GENERATED_DIR}/HooBaseVisit
INCLUDE_DIRECTORIES(${ANTLR_GENERATED_DIR} ${ANTLR_INCLUDE_DIR})
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(tests)
ENABLE_TESTING()
ADD_TEST(NAME literal_tests COMMAND literal_tests)

View File

@ -36,6 +36,7 @@ RUN apt-get install -y openjdk-21-jdk-headless maven
RUN apt-get install -y llvm llvm-dev
RUN apt-get install -y libedit-dev libzstd-dev libcurlpp-dev
RUN apt-get install -y libgtest-dev
RUN apt-get install -y gdb
COPY --from=build /custom/antlr4-cpp-runtime/usr/ /usr/
COPY --from=build /custom/antlr4/antlr4-4.13.2-complete.jar /usr/local/lib/antlr4-4.13.2-complete.jar
ENV CXX=/usr/bin/clang++-19

View File

@ -1,4 +1,13 @@
ADD_LIBRARY(hoocore STATIC HooCore.cpp HooCore.hpp Node.hpp)
ADD_LIBRARY(hoocore STATIC
${ANTLR_GENERATED_DIR}/HooBaseVisitor.cpp
${ANTLR_GENERATED_DIR}/HooVisitor.cpp
${ANTLR_GENERATED_DIR}/HooLexer.cpp
${ANTLR_GENERATED_DIR}/HooParser.cpp
Visitor.hpp
Visitor.cpp
HooCore.cpp
HooCore.hpp
Node.hpp)
ADD_EXECUTABLE(hoo
Hoo.cpp Visitor.cpp
${ANTLR_GENERATED_DIR}/HooBaseVisitor.cpp

View File

@ -1,19 +1,33 @@
#include "HooCore.hpp"
#include "HooLexer.h"
#include "HooParser.h"
#include "Visitor.hpp"
std::any HooCore::compile(std::string &input, std::string &moduleName)
HooCore::HooCore(const std::string &input, const std::string &moduleName) : _input(input), _moduleName(moduleName)
{
_input_stream = new antlr4::ANTLRInputStream(_input);
_lexer = new HooLexer(_input_stream);
_tokens = new antlr4::CommonTokenStream(_lexer);
_parser = new HooParser(_tokens);
_visitor = new Visitor(_moduleName);
}
HooCore::~HooCore()
{
delete _visitor;
delete _parser;
delete _tokens;
delete _lexer;
delete _input_stream;
}
std::any HooCore::compile()
{
try
{
antlr4::tree::ParseTree *tree = parse(input);
antlr4::tree::ParseTree *tree = parse();
if (tree == nullptr)
{
std::cout << "Parse failed" << std::endl;
}
Visitor *visitor = new Visitor(moduleName);
auto result = visitor->visit(tree);
auto result = _visitor->visit(tree);
return result;
}
catch (const std::exception &e)
@ -28,15 +42,11 @@ std::any HooCore::compile(std::string &input, std::string &moduleName)
}
}
antlr4::tree::ParseTree *HooCore::parse(std::string &input)
antlr4::tree::ParseTree *HooCore::parse()
{
try
{
antlr4::ANTLRInputStream input_stream(input);
HooLexer lexer(&input_stream);
antlr4::CommonTokenStream tokens(&lexer);
HooParser parser(&tokens);
antlr4::tree::ParseTree *tree = parser.unit();
antlr4::tree::ParseTree *tree = _parser->unit();
return tree;
}
catch (const std::exception &e)

View File

@ -1,16 +1,31 @@
#pragma once
#include "antlr4-runtime.h"
#include "HooLexer.h"
#include "HooParser.h"
#include "Visitor.hpp"
#include <string>
class HooCore
{
public:
HooCore() {}
~HooCore() {}
private:
const std::string &_input;
const std::string &_moduleName;
antlr4::ANTLRInputStream *_input_stream;
HooLexer *_lexer;
antlr4::CommonTokenStream *_tokens;
HooParser *_parser;
Visitor *_visitor;
public:
static std::any compile(std::string &input, std::string &moduleName);
static antlr4::tree::ParseTree *parse(std::string &input);
HooCore(const std::string &input, const std::string &moduleName);
~HooCore();
public:
std::any compile();
antlr4::tree::ParseTree *parse();
public:
std::shared_ptr<llvm::LLVMContext> getContext() const { return _visitor->getContext(); }
};

View File

@ -5,10 +5,10 @@
#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))
Visitor::Visitor(const 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))
{
}
@ -65,7 +65,31 @@ std::any Visitor::visitLiteral(HooParser::LiteralContext *ctx)
return std::any{Node(NODE_IGNORE, DATATYPE_VOID, nullptr)};
}
std::any Visitor::visitLiteralStatement(HooParser::LiteralStatementContext *ctx)
{
auto literal_context = ctx->literal();
auto result = visitLiteral(literal_context);
return result;
}
std::any Visitor::visitStatement(HooParser::StatementContext *ctx)
{
auto listeral_stmt_ctx = ctx->literalStatement();
if (listeral_stmt_ctx != nullptr)
{
auto result = visitLiteralStatement(listeral_stmt_ctx);
return result;
}
return nullptr;
}
std::any Visitor::visitUnit(HooParser::UnitContext *ctx)
{
return visitChildren(ctx);
auto stmt_ctx = ctx->statement();
if (stmt_ctx != nullptr)
{
auto result = visitStatement(stmt_ctx);
return result;
}
return nullptr;
}

View File

@ -11,15 +11,20 @@
class Visitor : public HooBaseVisitor
{
private:
std::string &_moduleName;
const std::string &_moduleName;
std::shared_ptr<llvm::LLVMContext> _context;
std::shared_ptr<llvm::Module> _module;
std::shared_ptr<llvm::IRBuilder<>> _builder;
public:
Visitor(std::string &moduleName);
Visitor(const std::string &moduleName);
public:
std::any visitLiteral(HooParser::LiteralContext *ctx) override;
std::any visitLiteralStatement(HooParser::LiteralStatementContext *ctx) override;
std::any visitStatement(HooParser::StatementContext *ctx) override;
std::any visitUnit(HooParser::UnitContext *ctx) override;
public:
std::shared_ptr<llvm::LLVMContext> getContext() const { return _context; }
};

5
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,5 @@
FIND_PACKAGE (GTest REQUIRED)
ADD_EXECUTABLE(literal_tests literal_tests.cpp)
TARGET_LINK_LIBRARIES(literal_tests GTest::GTest GTest::Main hoocore antlr4-runtime LLVMCore LLVMSupport)
TARGET_INCLUDE_DIRECTORIES(literal_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)

15
tests/literal_tests.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "HooCore.hpp"
#include "Node.hpp"
#include <gtest/gtest.h>
TEST(IntegerTest, Literal)
{
auto compiler = new HooCore("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;
}