From a969707ddbf4e5dc32152cc608459fa1e400167c Mon Sep 17 00:00:00 2001 From: Benoy Bose Date: Sat, 2 Nov 2024 19:55:24 +0530 Subject: [PATCH] Initial commit --- .devcontainer/devcontainer.json | 36 +++++++++++++++++++++++++ .gitignore | 4 +++ .vscode/c_cpp_properties.json | 18 +++++++++++++ CMakeLists.txt | 48 +++++++++++++++++++++++++++++++++ Dockerfile | 40 +++++++++++++++++++++++++++ Hoo.cpp | 3 +++ Hoo.g4 | 13 +++++++++ Hoo.hpp | 5 ++++ HooLexer.g4 | 40 +++++++++++++++++++++++++++ Visitor.cpp | 36 +++++++++++++++++++++++++ Visitor.hpp | 23 ++++++++++++++++ 11 files changed, 266 insertions(+) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitignore create mode 100644 .vscode/c_cpp_properties.json create mode 100644 CMakeLists.txt create mode 100644 Dockerfile create mode 100644 Hoo.cpp create mode 100644 Hoo.g4 create mode 100644 Hoo.hpp create mode 100644 HooLexer.g4 create mode 100644 Visitor.cpp create mode 100644 Visitor.hpp diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..b338e18 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,36 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile +{ + "name": "Existing Dockerfile", + "build": { + // Sets the run context to one level up instead of the .devcontainer folder. + "context": "..", + // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename. + "dockerfile": "../Dockerfile" + }, + + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Uncomment the next line to run commands after the container is created. + // "postCreateCommand": "cat /etc/os-release", + + // Configure tool-specific properties. + "customizations": { + "vscode": { + "extensions": [ + "twxs.cmake", + "ms-vscode.cmake-tools", + "codeium.codeium", + "mike-lischke.vscode-antlr4", + "ms-vscode.cpptools-extension-pack" + ] + } + } + + // Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "devcontainer" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4e44dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/ +CMakeCache.txt +**/CMakeCache.txt +Makefile diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..0c97623 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/local/include/antlr4-runtime", + "${workspaceFolder}/build/antlr4/generated" + ], + "defines": [], + "compilerPath": "/usr/bin/clang-19", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "linux-clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6c18f17 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,48 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.30 FATAL_ERROR) +PROJECT(HOOLANG) + +SET (CMAKE_CXX_STANDARD 17) +SET (ANTLR_JAR /usr/local/lib/antlr4-4.13.2-complete.jar) +SET (ANTLR_INCLUDE_DIR /usr/local/include/antlr4-runtime/) +SET (ANTLR_GENERATED_DIR "${CMAKE_BINARY_DIR}/antlr4/generated") +SET (GRAMMAR_FILE "${CMAKE_SOURCE_DIR}/Hoo.g4") +SET (LLVM_LIBRARY_DIRS "/usr/lib/llvm-19/lib") +# SET (LLVM_LIBS +# ${LLVM_LIBRARY_DIRS}/libLLVMCore.a +# ${LLVM_LIBRARY_DIRS}/libLLVMIRReader.a +# ${LLVM_LIBRARY_DIRS}/libLLVMSupport.a +# ) + +FIND_PACKAGE(LLVM REQUIRED CONFIG) + +MESSAGE (STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") +MESSAGE (${LLVM_DEFINITIONS}) + +LLVM_MAP_COMPONENTS_TO_LIBNAMES(llvm_libs support core irreader) +MESSAGE (${llvm_libs}) + +# ADD_CUSTOM_COMMAND(OUTPUT ${ANTLR_GENERATED_DIR}/HooBaseVisitor.cpp +# COMMAND java -jar ${ANTLR_JAR} -no-listener -visitor -o ${ANTLR_GENERATED_DIR} -Dlanguage=Cpp ${GRAMMAR_FILE} +# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} +# DEPENDS ${GRAMMAR_FILE}) + +EXECUTE_PROCESS( + COMMAND java -jar ${ANTLR_JAR} -no-listener -visitor -o ${ANTLR_GENERATED_DIR} -Dlanguage=Cpp ${GRAMMAR_FILE} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_FILE ${ANTLR_GENERATED_DIR}/HooBaseVisitor.cpp +) + +ADD_CUSTOM_TARGET(HooBaseVisitor ALL DEPENDS ${ANTLR_GENERATED_DIR}/HooBaseVisitor.cpp) + +INCLUDE_DIRECTORIES(${ANTLR_GENERATED_DIR} ${ANTLR_INCLUDE_DIR}) + +ADD_EXECUTABLE(hoo + Hoo.cpp Visitor.cpp + ${ANTLR_GENERATED_DIR}/HooBaseVisitor.cpp + ${ANTLR_GENERATED_DIR}/HooVisitor.cpp + ${ANTLR_GENERATED_DIR}/HooLexer.cpp + ${ANTLR_GENERATED_DIR}/HooParser.cpp) + +ADD_DEPENDENCIES(hoo HooBaseVisitor) + +TARGET_LINK_LIBRARIES(hoo antlr4-runtime ${LLVM_LIBS}) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fefb54d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,40 @@ +FROM ubuntu:oracular AS build +RUN apt-get update +RUN apt-get install -y git +RUN apt-get install -y cmake clang-19 +RUN apt-get install -y openjdk-21-jdk-headless maven + +RUN mkdir /builds +WORKDIR /builds +RUN git clone https://github.com/antlr/antlr4.git +WORKDIR /builds/antlr4 +RUN git checkout 4.13.2 +ENV MAVEN_OPTS="-Xmx1G" + +RUN mvn clean +RUN mvn -DskipTests install +RUN echo "if [ ! -f /root/.m2/repository/org/antlr/antlr4/4.13.2/antlr4-4.13.2-complete.jar ]; then exit 1; fi" > verify.sh +RUN chmod +x verify.sh +RUN ./verify.sh + +RUN mkdir -p /custom/antlr4 +RUN cp /root/.m2/repository/org/antlr/antlr4/4.13.2/antlr4-4.13.2-complete.jar /custom/antlr4/antlr4-4.13.2-complete.jar +RUN mkdir -p /custom/antlr4-cpp-runtime +WORKDIR /builds/antlr4/runtime/Cpp +RUN mkdir build +WORKDIR /builds/antlr4/runtime/Cpp/build +ENV CXX=/usr/bin/clang++-19 +RUN cmake .. -DANTLR4_INSTALL=ON +RUN make +RUN DESTDIR=/custom/antlr4-cpp-runtime make install + +FROM ubuntu:oracular AS runtime +RUN apt-get update +RUN apt-get install -y git +RUN apt-get install -y cmake clang-19 +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 +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/Hoo.cpp b/Hoo.cpp new file mode 100644 index 0000000..0373b85 --- /dev/null +++ b/Hoo.cpp @@ -0,0 +1,3 @@ +int main(int argc, char* argv[]) { + return 0; +} \ No newline at end of file diff --git a/Hoo.g4 b/Hoo.g4 new file mode 100644 index 0000000..735311e --- /dev/null +++ b/Hoo.g4 @@ -0,0 +1,13 @@ +grammar Hoo; + +import hoolexer; + +literal: + DECIMAL_LITERAL | + FLOAT_LITERAL | + BOOL_LITERAL | + CHAR_LITERAL | + STRING_LITERAL | + TEXT_BLOCK; + +unit: literal; \ No newline at end of file diff --git a/Hoo.hpp b/Hoo.hpp new file mode 100644 index 0000000..ca6607f --- /dev/null +++ b/Hoo.hpp @@ -0,0 +1,5 @@ +#include "Visitor.hpp" + +int main(int argc, char *argv[]) { + return 0; +} \ No newline at end of file diff --git a/HooLexer.g4 b/HooLexer.g4 new file mode 100644 index 0000000..f97ea2d --- /dev/null +++ b/HooLexer.g4 @@ -0,0 +1,40 @@ +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]?; + +FLOAT_LITERAL: (Digits '.' Digits? | '.' Digits); + +BOOL_LITERAL: 'true' | 'false'; + +CHAR_LITERAL: '\'' (~['\\\r\n] | EscapeSequence) '\''; + +STRING_LITERAL: '"' (~["\\\r\n] | EscapeSequence)* '"'; + +TEXT_BLOCK: '"""' [ \t]* [\r\n] (. | EscapeSequence)*? '"""'; + +NULL_LITERAL: 'null'; + +WS: [ \t\r\n\u000C]+ -> channel(HIDDEN); +COMMENT: '/*' .*? '*/' -> channel(HIDDEN); +LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN); + +fragment EscapeSequence: + '\\' 'u005c'? [btnfr"'\\] + | '\\' 'u005c'? ([0-3]? [0-7])? [0-7] + | '\\' 'u'+ HexDigit HexDigit HexDigit HexDigit; + +fragment HexDigit: [0-9a-fA-F]; + +fragment Digits: [0-9]; + +fragment LetterOrDigit: Letter | [0-9]; + +fragment Letter: + [a-zA-Z$_] + | ~[\u0000-\u007F\uD800-\uDBFF] + | [\uD800-\uDBFF] [\uDC00-\uDFFF]; \ No newline at end of file diff --git a/Visitor.cpp b/Visitor.cpp new file mode 100644 index 0000000..961f47e --- /dev/null +++ b/Visitor.cpp @@ -0,0 +1,36 @@ +#include "Visitor.hpp" + +#include +#include +#include + +Visitor::Visitor(std::string &moduleName) : _moduleName(moduleName), + _context(std::make_shared()), + _module(std::make_shared(moduleName, *_context)), + _builder(std::make_shared>(*_context)) +{ +} + +std::any Visitor::visitLiteral(HooParser::LiteralContext *ctx) +{ + // Implement your custom logic for visiting a LiteralContext here + // Example: if it's a decimal literal, return its value as a string + if (ctx->DECIMAL_LITERAL()) + { + return ctx->DECIMAL_LITERAL()->getText(); + } + // Add handling for other literal types as needed + return visitChildren(ctx); +} + +std::any Visitor::visitUnit(HooParser::UnitContext *ctx) +{ + // Implement your custom logic for visiting a UnitContext here + // Example: if it's a literal, return its value as a string + if (ctx->literal()) + { + return visit(ctx->literal()); + } + // Add handling for other unit types as needed + return visitChildren(ctx); +} \ No newline at end of file diff --git a/Visitor.hpp b/Visitor.hpp new file mode 100644 index 0000000..803be66 --- /dev/null +++ b/Visitor.hpp @@ -0,0 +1,23 @@ +#include "HooVisitor.h" + +#include +#include +#include +#include +#include + +class Visitor : public HooVisitor +{ +private: + std::string &_moduleName; + std::shared_ptr _context; + std::shared_ptr _module; + std::shared_ptr> _builder; + +public: + Visitor(std::string &moduleName); + +public: + std::any visitLiteral(HooParser::LiteralContext *ctx) override; + std::any visitUnit(HooParser::UnitContext *ctx) override; +};