From f687b32cb5a84026121522ad5b2bac2ac20f835c Mon Sep 17 00:00:00 2001 From: Benoy Bose Date: Thu, 28 Nov 2024 01:37:29 +0530 Subject: [PATCH] Tested literal statement --- .vscode/launch.json | 32 ++++++++++++++++++++++++++++++++ CMakeLists.txt | 4 ++++ Dockerfile | 1 + src/CMakeLists.txt | 11 ++++++++++- src/HooCore.cpp | 36 +++++++++++++++++++++++------------- src/HooCore.hpp | 25 ++++++++++++++++++++----- src/Visitor.cpp | 34 +++++++++++++++++++++++++++++----- src/Visitor.hpp | 9 +++++++-- tests/CMakeLists.txt | 5 +++++ tests/literal_tests.cpp | 15 +++++++++++++++ 10 files changed, 146 insertions(+), 26 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 tests/CMakeLists.txt create mode 100644 tests/literal_tests.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a7c5817 --- /dev/null +++ b/.vscode/launch.json @@ -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 + } + ] + } + ] +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index ff12059..4263dc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/Dockerfile b/Dockerfile index 460f565..e564275 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a1e0a76..389764f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/HooCore.cpp b/src/HooCore.cpp index af66f07..78cdb99 100644 --- a/src/HooCore.cpp +++ b/src/HooCore.cpp @@ -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) diff --git a/src/HooCore.hpp b/src/HooCore.hpp index 7e9b682..3ba2946 100644 --- a/src/HooCore.hpp +++ b/src/HooCore.hpp @@ -1,16 +1,31 @@ #pragma once #include "antlr4-runtime.h" +#include "HooLexer.h" +#include "HooParser.h" +#include "Visitor.hpp" #include 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 getContext() const { return _visitor->getContext(); } }; diff --git a/src/Visitor.cpp b/src/Visitor.cpp index 3799c91..10c976b 100644 --- a/src/Visitor.cpp +++ b/src/Visitor.cpp @@ -5,10 +5,10 @@ #include #include -Visitor::Visitor(std::string &moduleName) : _moduleName(moduleName), - _context(std::make_shared()), - _module(std::make_shared(moduleName, *_context)), - _builder(std::make_shared>(*_context)) +Visitor::Visitor(const std::string &moduleName) : _moduleName(moduleName), + _context(std::make_shared()), + _module(std::make_shared(moduleName, *_context)), + _builder(std::make_shared>(*_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; } \ No newline at end of file diff --git a/src/Visitor.hpp b/src/Visitor.hpp index 3087a8c..f86ebc3 100644 --- a/src/Visitor.hpp +++ b/src/Visitor.hpp @@ -11,15 +11,20 @@ class Visitor : public HooBaseVisitor { private: - std::string &_moduleName; + const std::string &_moduleName; std::shared_ptr _context; std::shared_ptr _module; std::shared_ptr> _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 getContext() const { return _context; } }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..1cd5d77 --- /dev/null +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/literal_tests.cpp b/tests/literal_tests.cpp new file mode 100644 index 0000000..f53d449 --- /dev/null +++ b/tests/literal_tests.cpp @@ -0,0 +1,15 @@ +#include "HooCore.hpp" +#include "Node.hpp" + +#include + +TEST(IntegerTest, Literal) +{ + auto compiler = new HooCore("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; +}