From 03474d834022c89a52b638c9b8ac3e8ef2a7581d Mon Sep 17 00:00:00 2001 From: Maksim Vlasov Date: Fri, 22 Mar 2024 10:51:51 +0300 Subject: [PATCH] Re-organize compiler modules into a `compiler` directory (#101) * All modules were moved to `compiler` directory * Preprocessor, lexer, and parser were separated into `frontend` subdirectory * AST -> LLVMIR generator was renamed to `codegen` * Semantizer and optimizer were moved to `backend/ast` subdirectory * Base error utilities were moved to `utils` subdirectory * Headers and implementation were moved ortogonally * All includes, cmake targets, tests were renamed accordingly --- .github/workflows/ci.yml | 21 +++++++------ ast/CMakeLists.txt | 22 ------------- backend/CMakeLists.txt | 23 -------------- cli/CMakeLists.txt | 31 ------------------- CMakeLists.txt => compiler/CMakeLists.txt | 26 +++++++--------- compiler/cmake/utils.cmake | 5 +++ .../include/compiler}/ast/functions_table.hpp | 2 +- .../include/compiler}/ast/node.hpp | 6 ++-- .../include/compiler}/ast/node_type.hpp | 0 .../include/compiler}/ast/syntax_tree.hpp | 4 +-- .../include/compiler}/ast/types.hpp | 0 .../include/compiler}/ast/variables_table.hpp | 2 +- .../backend/ast}/optimizer/optimizer.hpp | 6 ++-- .../ast}/optimizer/optimizer_context.hpp | 8 ++--- .../ast}/optimizer/optimizer_options.hpp | 0 .../backend/ast}/semantizer/semantizer.hpp | 10 +++--- .../ast}/semantizer/semantizer_context.hpp | 10 +++--- .../ast}/semantizer/semantizer_error.hpp | 5 ++- .../include/compiler}/cli/compiler.hpp | 0 .../include/compiler}/cli/dumping.hpp | 4 +-- .../include/compiler}/cli/logger.hpp | 0 .../compiler/codegen}/ir_generator.hpp | 2 +- .../compiler/frontend}/lexer/lexer.hpp | 6 ++-- .../compiler/frontend}/lexer/lexer_error.hpp | 2 +- .../compiler/frontend}/lexer/token.hpp | 2 +- .../compiler/frontend}/lexer/token_types.hpp | 0 .../compiler/frontend}/parser/parser.hpp | 4 +-- .../frontend}/parser/parser_context.hpp | 14 ++++----- .../frontend}/parser/parser_error.hpp | 3 +- .../frontend}/parser/type_registry.hpp | 4 +-- .../frontend}/preprocessor/preprocessor.hpp | 2 +- .../include/compiler/utils}/base_error.hpp | 2 +- .../include/compiler/utils}/error_buffer.hpp | 2 +- .../include/compiler}/utils/source_files.hpp | 2 +- .../include/compiler}/utils/source_ref.hpp | 0 .../include/compiler/utils}/stringvec.hpp | 2 +- compiler/lib/CMakeLists.txt | 15 +++++++++ compiler/lib/ast/CMakeLists.txt | 18 +++++++++++ {ast/src => compiler/lib/ast}/node.cpp | 0 {ast/src => compiler/lib/ast}/syntax_tree.cpp | 0 compiler/lib/backend/CMakeLists.txt | 3 ++ compiler/lib/backend/ast/CMakeLists.txt | 20 ++++++++++++ .../lib/backend/ast}/optimizer/optimizer.cpp | 0 .../backend/ast}/semantizer/semantizer.cpp | 0 compiler/lib/cli/CMakeLists.txt | 25 +++++++++++++++ {cli/src => compiler/lib/cli}/compiler.cpp | 31 ++++++++++--------- {cli/src => compiler/lib/cli}/dumping.cpp | 0 {cli/src => compiler/lib/cli}/logger.cpp | 0 {cli/src => compiler/lib/cli}/main.cpp | 0 .../lib/codegen}/CMakeLists.txt | 16 +++++----- .../lib/codegen}/ir_generator.cpp | 0 compiler/lib/frontend/CMakeLists.txt | 19 ++++++++++++ .../lib/frontend}/lexer/lexer.cpp | 0 .../lib/frontend}/lexer/token.cpp | 0 .../lib/frontend}/parser/parser.cpp | 0 .../lib/frontend}/parser/type_registry.cpp | 0 .../frontend}/preprocessor/preprocessor.cpp | 0 compiler/lib/utils/CMakeLists.txt | 14 +++++++++ .../src => compiler/lib/utils}/base_error.cpp | 0 .../lib/utils}/source_files.cpp | 0 compiler/tests/CMakeLists.txt | 25 +++++++++++++++ {tests => compiler/tests}/ast/CMakeLists.txt | 12 +++++-- {tests => compiler/tests}/ast/main.cpp | 0 {tests => compiler/tests}/ast/node.cpp | 2 +- {tests => compiler/tests}/ast/syntax_tree.cpp | 2 +- compiler/tests/backend/CMakeLists.txt | 3 ++ compiler/tests/backend/ast/CMakeLists.txt | 27 ++++++++++++++++ .../tests/backend/ast}/main.cpp | 0 .../tests/backend/ast}/optimizer.cpp | 10 +++--- .../tests/backend/ast}/semantizer.cpp | 8 ++--- compiler/tests/codegen/CMakeLists.txt | 24 ++++++++++++++ .../tests/codegen}/ir_generator.cpp | 0 .../tests/codegen}/main.cpp | 0 .../tests/frontend}/CMakeLists.txt | 14 ++++++--- .../tests/frontend}/lexer.cpp | 8 ++--- .../tests/frontend}/parser.cpp | 6 ++-- .../tests/frontend}/preprocessor.cpp | 4 +-- .../tests/frontend}/token.cpp | 2 +- docs/ru/tutorials/install.md | 6 ++-- tests/CMakeLists.txt | 8 ----- tests/ir_generator/CMakeLists.txt | 18 ----------- thirdparty/CMakeLists.txt | 2 +- utils/CMakeLists.txt | 20 ------------ 83 files changed, 339 insertions(+), 255 deletions(-) delete mode 100644 ast/CMakeLists.txt delete mode 100644 backend/CMakeLists.txt delete mode 100644 cli/CMakeLists.txt rename CMakeLists.txt => compiler/CMakeLists.txt (60%) create mode 100644 compiler/cmake/utils.cmake rename {ast/include => compiler/include/compiler}/ast/functions_table.hpp (96%) rename {ast/include => compiler/include/compiler}/ast/node.hpp (96%) rename {ast/include => compiler/include/compiler}/ast/node_type.hpp (100%) rename {ast/include => compiler/include/compiler}/ast/syntax_tree.hpp (86%) rename {ast/include => compiler/include/compiler}/ast/types.hpp (100%) rename {ast/include => compiler/include/compiler}/ast/variables_table.hpp (90%) rename {backend/include/backend => compiler/include/compiler/backend/ast}/optimizer/optimizer.hpp (71%) rename {backend/include/backend => compiler/include/compiler/backend/ast}/optimizer/optimizer_context.hpp (89%) rename {backend/include/backend => compiler/include/compiler/backend/ast}/optimizer/optimizer_options.hpp (100%) rename {backend/include/backend => compiler/include/compiler/backend/ast}/semantizer/semantizer.hpp (53%) rename {backend/include/backend => compiler/include/compiler/backend/ast}/semantizer/semantizer_context.hpp (81%) rename {backend/include/backend => compiler/include/compiler/backend/ast}/semantizer/semantizer_error.hpp (81%) rename {cli/include => compiler/include/compiler}/cli/compiler.hpp (100%) rename {cli/include => compiler/include/compiler}/cli/dumping.hpp (67%) rename {cli/include => compiler/include/compiler}/cli/logger.hpp (100%) rename {ir_generator/include/ir_generator => compiler/include/compiler/codegen}/ir_generator.hpp (98%) rename {backend/include/backend => compiler/include/compiler/frontend}/lexer/lexer.hpp (80%) rename {backend/include/backend => compiler/include/compiler/frontend}/lexer/lexer_error.hpp (85%) rename {backend/include/backend => compiler/include/compiler/frontend}/lexer/token.hpp (98%) rename {backend/include/backend => compiler/include/compiler/frontend}/lexer/token_types.hpp (100%) rename {backend/include/backend => compiler/include/compiler/frontend}/parser/parser.hpp (76%) rename {backend/include/backend => compiler/include/compiler/frontend}/parser/parser_context.hpp (85%) rename {backend/include/backend => compiler/include/compiler/frontend}/parser/parser_error.hpp (86%) rename {backend/include/backend => compiler/include/compiler/frontend}/parser/type_registry.hpp (80%) rename {backend/include/backend => compiler/include/compiler/frontend}/preprocessor/preprocessor.hpp (88%) rename {backend/include/backend => compiler/include/compiler/utils}/base_error.hpp (88%) rename {backend/include/backend => compiler/include/compiler/utils}/error_buffer.hpp (94%) rename {utils/include => compiler/include/compiler}/utils/source_files.hpp (97%) rename {utils/include => compiler/include/compiler}/utils/source_ref.hpp (100%) rename {backend/include/backend => compiler/include/compiler/utils}/stringvec.hpp (69%) create mode 100644 compiler/lib/CMakeLists.txt create mode 100644 compiler/lib/ast/CMakeLists.txt rename {ast/src => compiler/lib/ast}/node.cpp (100%) rename {ast/src => compiler/lib/ast}/syntax_tree.cpp (100%) create mode 100644 compiler/lib/backend/CMakeLists.txt create mode 100644 compiler/lib/backend/ast/CMakeLists.txt rename {backend/src => compiler/lib/backend/ast}/optimizer/optimizer.cpp (100%) rename {backend/src => compiler/lib/backend/ast}/semantizer/semantizer.cpp (100%) create mode 100644 compiler/lib/cli/CMakeLists.txt rename {cli/src => compiler/lib/cli}/compiler.cpp (93%) rename {cli/src => compiler/lib/cli}/dumping.cpp (100%) rename {cli/src => compiler/lib/cli}/logger.cpp (100%) rename {cli/src => compiler/lib/cli}/main.cpp (100%) rename {ir_generator => compiler/lib/codegen}/CMakeLists.txt (56%) rename {ir_generator/src => compiler/lib/codegen}/ir_generator.cpp (100%) create mode 100644 compiler/lib/frontend/CMakeLists.txt rename {backend/src => compiler/lib/frontend}/lexer/lexer.cpp (100%) rename {backend/src => compiler/lib/frontend}/lexer/token.cpp (100%) rename {backend/src => compiler/lib/frontend}/parser/parser.cpp (100%) rename {backend/src => compiler/lib/frontend}/parser/type_registry.cpp (100%) rename {backend/src => compiler/lib/frontend}/preprocessor/preprocessor.cpp (100%) create mode 100644 compiler/lib/utils/CMakeLists.txt rename {backend/src => compiler/lib/utils}/base_error.cpp (100%) rename {utils/src => compiler/lib/utils}/source_files.cpp (100%) create mode 100644 compiler/tests/CMakeLists.txt rename {tests => compiler/tests}/ast/CMakeLists.txt (56%) rename {tests => compiler/tests}/ast/main.cpp (100%) rename {tests => compiler/tests}/ast/node.cpp (97%) rename {tests => compiler/tests}/ast/syntax_tree.cpp (96%) create mode 100644 compiler/tests/backend/CMakeLists.txt create mode 100644 compiler/tests/backend/ast/CMakeLists.txt rename {tests/backend => compiler/tests/backend/ast}/main.cpp (100%) rename {tests/backend => compiler/tests/backend/ast}/optimizer.cpp (99%) rename {tests/backend => compiler/tests/backend/ast}/semantizer.cpp (99%) create mode 100644 compiler/tests/codegen/CMakeLists.txt rename {tests/ir_generator => compiler/tests/codegen}/ir_generator.cpp (100%) rename {tests/ir_generator => compiler/tests/codegen}/main.cpp (100%) rename {tests/backend => compiler/tests/frontend}/CMakeLists.txt (52%) rename {tests/backend => compiler/tests/frontend}/lexer.cpp (98%) rename {tests/backend => compiler/tests/frontend}/parser.cpp (99%) rename {tests/backend => compiler/tests/frontend}/preprocessor.cpp (95%) rename {tests/backend => compiler/tests/frontend}/token.cpp (97%) delete mode 100644 tests/CMakeLists.txt delete mode 100644 tests/ir_generator/CMakeLists.txt delete mode 100644 utils/CMakeLists.txt diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 59efb224..beff8cc1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,19 +24,18 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: DoozyX/clang-format-lint-action@v0.13 + - uses: DoozyX/clang-format-lint-action@v0.17 with: - source: '.' - exclude: './thirdparty ./docs' + source: './compiler' extensions: 'hpp,cpp' - clangFormatVersion: 12 + clangFormatVersion: 17 build-gcc: name: Build on Ubuntu GCC runs-on: ubuntu-latest needs: code-style-check container: - image: ghcr.io/vla5924-practice/compiler-project/devcontainer:1710845516 + image: ghcr.io/vla5924-practice/compiler-project/devcontainer:latest options: --user root credentials: username: ${{ github.actor }} @@ -48,7 +47,7 @@ jobs: submodules: true - name: Build run: | - cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DENABLE_IR_GENERATOR=OFF + cmake -S compiler -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DENABLE_IR_GENERATOR=OFF ninja -C build - name: Archive binaries run: tar cf binaries.tar -C build bin @@ -63,7 +62,7 @@ jobs: runs-on: ubuntu-latest needs: build-gcc container: - image: ghcr.io/vla5924-practice/compiler-project/devcontainer:1710845516 + image: ghcr.io/vla5924-practice/compiler-project/devcontainer:latest options: --user root credentials: username: ${{ github.actor }} @@ -75,7 +74,9 @@ jobs: name: binaries-ubuntu-latest-gcc - name: Extract binaries run: tar xf binaries.tar - - name: Run ast_test + - name: Run AST interface tests run: ./bin/ast_test - - name: Run backend_test - run: ./bin/backend_test + - name: Run frontend tests + run: ./bin/frontend_test + - name: Run AST-based backend tests + run: ./bin/backend_ast_test diff --git a/ast/CMakeLists.txt b/ast/CMakeLists.txt deleted file mode 100644 index 171b2b61..00000000 --- a/ast/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -set(TARGET_NAME ast) - -file(GLOB_RECURSE TARGET_HEADERS - ${CMAKE_CURRENT_SOURCE_DIR}/include/ast/*.hpp -) - -file(GLOB_RECURSE TARGET_SRC - ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp -) - -add_library(${TARGET_NAME} STATIC ${TARGET_SRC} ${TARGET_HEADERS}) - -target_include_directories(${TARGET_NAME} -PUBLIC - include -PRIVATE - include/ast -) - -target_link_libraries(${TARGET_NAME} PUBLIC utils) diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt deleted file mode 100644 index cf3e8515..00000000 --- a/backend/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -set(TARGET_NAME backend) - -file(GLOB_RECURSE TARGET_HEADERS - ${CMAKE_CURRENT_SOURCE_DIR}/include/backend/*.hpp -) - -file(GLOB_RECURSE TARGET_SRC - ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp -) - -add_library(${TARGET_NAME} STATIC ${TARGET_SRC} ${TARGET_HEADERS}) - -target_include_directories(${TARGET_NAME} PUBLIC - include/backend - include -) - -target_link_libraries(${TARGET_NAME} PUBLIC - ast - utils -) diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt deleted file mode 100644 index fdf44173..00000000 --- a/cli/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -set(TARGET_NAME cli) - -file(GLOB_RECURSE TARGET_HEADERS - ${CMAKE_CURRENT_SOURCE_DIR}/include/cli/*.hpp -) - -file(GLOB_RECURSE TARGET_SRC - ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp -) - -add_executable(${TARGET_NAME} ${TARGET_SRC} ${TARGET_HEADERS}) - -set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME compiler) - -target_include_directories(${TARGET_NAME} -PUBLIC - include -PRIVATE - include/cli -) - -target_link_libraries(${TARGET_NAME} PUBLIC - argparse - backend - utils -) -if (ENABLE_IR_GENERATOR) - target_link_libraries(${TARGET_NAME} PUBLIC ir_generator) -endif() diff --git a/CMakeLists.txt b/compiler/CMakeLists.txt similarity index 60% rename from CMakeLists.txt rename to compiler/CMakeLists.txt index 0734d247..1c5137f0 100644 --- a/CMakeLists.txt +++ b/compiler/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.22) project(Compiler LANGUAGES CXX) @@ -6,14 +6,19 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_COMPILE_WARNING_AS_ERROR ON) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/archive) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -option(ENABLE_IR_GENERATOR "Enables LLVM-based IR generation backend" ON) +set(COMPILER_CMAKE_DIR ${CMAKE_SOURCE_DIR}/cmake) +set(COMPILER_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include) +set(COMPILER_LIB_DIR ${CMAKE_SOURCE_DIR}/lib) + +option(ENABLE_CODEGEN "Enables LLVM IR generation backend" OFF) option(ENABLE_CLI "Enables command-line interface application" ON) option(ENABLE_TESTS "Enables tests for components" ON) @@ -22,19 +27,10 @@ if(WIN32) option(gtest_force_shared_crt "" TRUE) endif() -add_subdirectory(utils) -add_subdirectory(ast) -add_subdirectory(backend) -add_subdirectory(thirdparty) - -if(ENABLE_IR_GENERATOR) - add_subdirectory(ir_generator) - add_compile_definitions("ENABLE_IR_GENERATOR") -endif() +include(${COMPILER_CMAKE_DIR}/utils.cmake) -if(ENABLE_CLI) - add_subdirectory(cli) -endif() +add_subdirectory(../thirdparty thirdparty) +add_subdirectory(lib) if(ENABLE_TESTS) enable_testing() diff --git a/compiler/cmake/utils.cmake b/compiler/cmake/utils.cmake new file mode 100644 index 00000000..fb7daa7a --- /dev/null +++ b/compiler/cmake/utils.cmake @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.22) + +macro(set_target_include_dir TARGET_NAME) + set(TARGET_INCLUDE_DIR "${COMPILER_INCLUDE_DIR}/compiler/${TARGET_NAME}") +endmacro() diff --git a/ast/include/ast/functions_table.hpp b/compiler/include/compiler/ast/functions_table.hpp similarity index 96% rename from ast/include/ast/functions_table.hpp rename to compiler/include/compiler/ast/functions_table.hpp index c7619253..b6c2f0c9 100644 --- a/ast/include/ast/functions_table.hpp +++ b/compiler/include/compiler/ast/functions_table.hpp @@ -4,7 +4,7 @@ #include #include -#include "ast/types.hpp" +#include "compiler/ast/types.hpp" namespace ast { diff --git a/ast/include/ast/node.hpp b/compiler/include/compiler/ast/node.hpp similarity index 96% rename from ast/include/ast/node.hpp rename to compiler/include/compiler/ast/node.hpp index 131cc8e8..0e87bc73 100644 --- a/ast/include/ast/node.hpp +++ b/compiler/include/compiler/ast/node.hpp @@ -6,10 +6,10 @@ #include #include -#include +#include "compiler/utils/source_ref.hpp" -#include "ast/node_type.hpp" -#include "ast/variables_table.hpp" +#include "compiler/ast/node_type.hpp" +#include "compiler/ast/variables_table.hpp" namespace ast { diff --git a/ast/include/ast/node_type.hpp b/compiler/include/compiler/ast/node_type.hpp similarity index 100% rename from ast/include/ast/node_type.hpp rename to compiler/include/compiler/ast/node_type.hpp diff --git a/ast/include/ast/syntax_tree.hpp b/compiler/include/compiler/ast/syntax_tree.hpp similarity index 86% rename from ast/include/ast/syntax_tree.hpp rename to compiler/include/compiler/ast/syntax_tree.hpp index 93baa896..b34a6332 100644 --- a/ast/include/ast/syntax_tree.hpp +++ b/compiler/include/compiler/ast/syntax_tree.hpp @@ -2,8 +2,8 @@ #include -#include "ast/functions_table.hpp" -#include "ast/node.hpp" +#include "compiler/ast/functions_table.hpp" +#include "compiler/ast/node.hpp" namespace ast { diff --git a/ast/include/ast/types.hpp b/compiler/include/compiler/ast/types.hpp similarity index 100% rename from ast/include/ast/types.hpp rename to compiler/include/compiler/ast/types.hpp diff --git a/ast/include/ast/variables_table.hpp b/compiler/include/compiler/ast/variables_table.hpp similarity index 90% rename from ast/include/ast/variables_table.hpp rename to compiler/include/compiler/ast/variables_table.hpp index 0260d124..ea6f7616 100644 --- a/ast/include/ast/variables_table.hpp +++ b/compiler/include/compiler/ast/variables_table.hpp @@ -3,7 +3,7 @@ #include #include -#include "ast/types.hpp" +#include "compiler/ast/types.hpp" namespace ast { diff --git a/backend/include/backend/optimizer/optimizer.hpp b/compiler/include/compiler/backend/ast/optimizer/optimizer.hpp similarity index 71% rename from backend/include/backend/optimizer/optimizer.hpp rename to compiler/include/compiler/backend/ast/optimizer/optimizer.hpp index c0b36f8b..19b04022 100644 --- a/backend/include/backend/optimizer/optimizer.hpp +++ b/compiler/include/compiler/backend/ast/optimizer/optimizer.hpp @@ -1,9 +1,9 @@ #pragma once -#include -#include +#include "compiler/ast/syntax_tree.hpp" +#include "compiler/ast/types.hpp" -#include "backend/optimizer/optimizer_options.hpp" +#include "compiler/backend/ast/optimizer/optimizer_options.hpp" namespace optimizer { diff --git a/backend/include/backend/optimizer/optimizer_context.hpp b/compiler/include/compiler/backend/ast/optimizer/optimizer_context.hpp similarity index 89% rename from backend/include/backend/optimizer/optimizer_context.hpp rename to compiler/include/compiler/backend/ast/optimizer/optimizer_context.hpp index c54fd117..7a12f36f 100644 --- a/backend/include/backend/optimizer/optimizer_context.hpp +++ b/compiler/include/compiler/backend/ast/optimizer/optimizer_context.hpp @@ -6,11 +6,11 @@ #include #include -#include -#include -#include +#include "compiler/ast/functions_table.hpp" +#include "compiler/ast/node.hpp" +#include "compiler/ast/variables_table.hpp" -#include "backend/optimizer/optimizer_options.hpp" +#include "compiler/backend/ast/optimizer/optimizer_options.hpp" namespace optimizer { diff --git a/backend/include/backend/optimizer/optimizer_options.hpp b/compiler/include/compiler/backend/ast/optimizer/optimizer_options.hpp similarity index 100% rename from backend/include/backend/optimizer/optimizer_options.hpp rename to compiler/include/compiler/backend/ast/optimizer/optimizer_options.hpp diff --git a/backend/include/backend/semantizer/semantizer.hpp b/compiler/include/compiler/backend/ast/semantizer/semantizer.hpp similarity index 53% rename from backend/include/backend/semantizer/semantizer.hpp rename to compiler/include/compiler/backend/ast/semantizer/semantizer.hpp index 97bf625c..7042ef8e 100644 --- a/backend/include/backend/semantizer/semantizer.hpp +++ b/compiler/include/compiler/backend/ast/semantizer/semantizer.hpp @@ -1,11 +1,11 @@ #pragma once -#include -#include +#include "compiler/ast/syntax_tree.hpp" +#include "compiler/ast/types.hpp" +#include "compiler/utils/error_buffer.hpp" -#include "error_buffer.hpp" -#include "semantizer_context.hpp" -#include "semantizer_error.hpp" +#include "compiler/backend/ast/semantizer/semantizer_context.hpp" +#include "compiler/backend/ast/semantizer/semantizer_error.hpp" namespace semantizer { diff --git a/backend/include/backend/semantizer/semantizer_context.hpp b/compiler/include/compiler/backend/ast/semantizer/semantizer_context.hpp similarity index 81% rename from backend/include/backend/semantizer/semantizer_context.hpp rename to compiler/include/compiler/backend/ast/semantizer/semantizer_context.hpp index 8c190f93..6971695a 100644 --- a/backend/include/backend/semantizer/semantizer_context.hpp +++ b/compiler/include/compiler/backend/ast/semantizer/semantizer_context.hpp @@ -2,12 +2,12 @@ #include -#include "error_buffer.hpp" -#include "semantizer_error.hpp" +#include "compiler/ast/functions_table.hpp" +#include "compiler/ast/node.hpp" +#include "compiler/ast/variables_table.hpp" +#include "compiler/utils/error_buffer.hpp" -#include -#include -#include +#include "compiler/backend/ast/semantizer/semantizer_error.hpp" namespace semantizer { diff --git a/backend/include/backend/semantizer/semantizer_error.hpp b/compiler/include/compiler/backend/ast/semantizer/semantizer_error.hpp similarity index 81% rename from backend/include/backend/semantizer/semantizer_error.hpp rename to compiler/include/compiler/backend/ast/semantizer/semantizer_error.hpp index 34ef03e6..93771a40 100644 --- a/backend/include/backend/semantizer/semantizer_error.hpp +++ b/compiler/include/compiler/backend/ast/semantizer/semantizer_error.hpp @@ -1,8 +1,7 @@ #pragma once -#include - -#include "base_error.hpp" +#include "compiler/ast/node.hpp" +#include "compiler/utils/base_error.hpp" namespace semantizer { diff --git a/cli/include/cli/compiler.hpp b/compiler/include/compiler/cli/compiler.hpp similarity index 100% rename from cli/include/cli/compiler.hpp rename to compiler/include/compiler/cli/compiler.hpp diff --git a/cli/include/cli/dumping.hpp b/compiler/include/compiler/cli/dumping.hpp similarity index 67% rename from cli/include/cli/dumping.hpp rename to compiler/include/compiler/cli/dumping.hpp index 4a6d777c..98f23764 100644 --- a/cli/include/cli/dumping.hpp +++ b/compiler/include/compiler/cli/dumping.hpp @@ -2,8 +2,8 @@ #include -#include -#include +#include "compiler/frontend/lexer/token.hpp" +#include "compiler/utils/stringvec.hpp" namespace dumping { diff --git a/cli/include/cli/logger.hpp b/compiler/include/compiler/cli/logger.hpp similarity index 100% rename from cli/include/cli/logger.hpp rename to compiler/include/compiler/cli/logger.hpp diff --git a/ir_generator/include/ir_generator/ir_generator.hpp b/compiler/include/compiler/codegen/ir_generator.hpp similarity index 98% rename from ir_generator/include/ir_generator/ir_generator.hpp rename to compiler/include/compiler/codegen/ir_generator.hpp index 64b40165..0a9fc793 100644 --- a/ir_generator/include/ir_generator/ir_generator.hpp +++ b/compiler/include/compiler/codegen/ir_generator.hpp @@ -13,7 +13,7 @@ #include #pragma warning(pop) -#include +#include "compiler/ast/syntax_tree.hpp" namespace ir_generator { diff --git a/backend/include/backend/lexer/lexer.hpp b/compiler/include/compiler/frontend/lexer/lexer.hpp similarity index 80% rename from backend/include/backend/lexer/lexer.hpp rename to compiler/include/compiler/frontend/lexer/lexer.hpp index c6e7723d..4b6c7a49 100644 --- a/backend/include/backend/lexer/lexer.hpp +++ b/compiler/include/compiler/frontend/lexer/lexer.hpp @@ -5,10 +5,10 @@ #include #include -#include +#include "compiler/utils/error_buffer.hpp" +#include "compiler/utils/source_files.hpp" -#include "error_buffer.hpp" -#include "lexer/token.hpp" +#include "compiler/frontend/lexer/token.hpp" namespace lexer { diff --git a/backend/include/backend/lexer/lexer_error.hpp b/compiler/include/compiler/frontend/lexer/lexer_error.hpp similarity index 85% rename from backend/include/backend/lexer/lexer_error.hpp rename to compiler/include/compiler/frontend/lexer/lexer_error.hpp index 758229da..113b3bfc 100644 --- a/backend/include/backend/lexer/lexer_error.hpp +++ b/compiler/include/compiler/frontend/lexer/lexer_error.hpp @@ -1,6 +1,6 @@ #pragma once -#include "base_error.hpp" +#include "compiler/utils/base_error.hpp" namespace lexer { diff --git a/backend/include/backend/lexer/token.hpp b/compiler/include/compiler/frontend/lexer/token.hpp similarity index 98% rename from backend/include/backend/lexer/token.hpp rename to compiler/include/compiler/frontend/lexer/token.hpp index 008795cb..68479072 100644 --- a/backend/include/backend/lexer/token.hpp +++ b/compiler/include/compiler/frontend/lexer/token.hpp @@ -7,7 +7,7 @@ #include #include -#include +#include "compiler/utils/source_ref.hpp" namespace lexer { diff --git a/backend/include/backend/lexer/token_types.hpp b/compiler/include/compiler/frontend/lexer/token_types.hpp similarity index 100% rename from backend/include/backend/lexer/token_types.hpp rename to compiler/include/compiler/frontend/lexer/token_types.hpp diff --git a/backend/include/backend/parser/parser.hpp b/compiler/include/compiler/frontend/parser/parser.hpp similarity index 76% rename from backend/include/backend/parser/parser.hpp rename to compiler/include/compiler/frontend/parser/parser.hpp index 83da7187..199033e9 100644 --- a/backend/include/backend/parser/parser.hpp +++ b/compiler/include/compiler/frontend/parser/parser.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include "compiler/ast/syntax_tree.hpp" -#include "lexer/token.hpp" +#include "compiler/frontend/lexer/token.hpp" namespace parser { diff --git a/backend/include/backend/parser/parser_context.hpp b/compiler/include/compiler/frontend/parser/parser_context.hpp similarity index 85% rename from backend/include/backend/parser/parser_context.hpp rename to compiler/include/compiler/frontend/parser/parser_context.hpp index c5785665..d87c3053 100644 --- a/backend/include/backend/parser/parser_context.hpp +++ b/compiler/include/compiler/frontend/parser/parser_context.hpp @@ -4,14 +4,14 @@ #include #include -#include -#include -#include +#include "compiler/ast/node.hpp" +#include "compiler/ast/types.hpp" +#include "compiler/utils/error_buffer.hpp" +#include "compiler/utils/source_ref.hpp" -#include "error_buffer.hpp" -#include "lexer/token.hpp" -#include "lexer/token_types.hpp" -#include "parser/parser_error.hpp" +#include "compiler/frontend/lexer/token.hpp" +#include "compiler/frontend/lexer/token_types.hpp" +#include "compiler/frontend/parser/parser_error.hpp" namespace parser { diff --git a/backend/include/backend/parser/parser_error.hpp b/compiler/include/compiler/frontend/parser/parser_error.hpp similarity index 86% rename from backend/include/backend/parser/parser_error.hpp rename to compiler/include/compiler/frontend/parser/parser_error.hpp index 72c0fe6b..faaf21dd 100644 --- a/backend/include/backend/parser/parser_error.hpp +++ b/compiler/include/compiler/frontend/parser/parser_error.hpp @@ -1,6 +1,7 @@ #pragma once -#include "base_error.hpp" +#include "compiler/utils/base_error.hpp" + #include "lexer/token.hpp" namespace parser { diff --git a/backend/include/backend/parser/type_registry.hpp b/compiler/include/compiler/frontend/parser/type_registry.hpp similarity index 80% rename from backend/include/backend/parser/type_registry.hpp rename to compiler/include/compiler/frontend/parser/type_registry.hpp index 78486360..3df2db10 100644 --- a/backend/include/backend/parser/type_registry.hpp +++ b/compiler/include/compiler/frontend/parser/type_registry.hpp @@ -3,9 +3,9 @@ #include #include -#include +#include "compiler/ast/types.hpp" -#include "lexer/token.hpp" +#include "compiler/frontend/lexer/token.hpp" namespace parser { diff --git a/backend/include/backend/preprocessor/preprocessor.hpp b/compiler/include/compiler/frontend/preprocessor/preprocessor.hpp similarity index 88% rename from backend/include/backend/preprocessor/preprocessor.hpp rename to compiler/include/compiler/frontend/preprocessor/preprocessor.hpp index 4eccef0a..1b5dbc12 100644 --- a/backend/include/backend/preprocessor/preprocessor.hpp +++ b/compiler/include/compiler/frontend/preprocessor/preprocessor.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include "compiler/utils/source_files.hpp" namespace preprocessor { diff --git a/backend/include/backend/base_error.hpp b/compiler/include/compiler/utils/base_error.hpp similarity index 88% rename from backend/include/backend/base_error.hpp rename to compiler/include/compiler/utils/base_error.hpp index 945f63ec..72c0e5eb 100644 --- a/backend/include/backend/base_error.hpp +++ b/compiler/include/compiler/utils/base_error.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include "compiler/utils/source_ref.hpp" class BaseError : public std::exception { std::string what_str; diff --git a/backend/include/backend/error_buffer.hpp b/compiler/include/compiler/utils/error_buffer.hpp similarity index 94% rename from backend/include/backend/error_buffer.hpp rename to compiler/include/compiler/utils/error_buffer.hpp index e45f43c2..9d2de97a 100644 --- a/backend/include/backend/error_buffer.hpp +++ b/compiler/include/compiler/utils/error_buffer.hpp @@ -5,7 +5,7 @@ #include #include -#include "base_error.hpp" +#include "compiler/utils/base_error.hpp" class ErrorBuffer : public std::exception { std::list> buffer; diff --git a/utils/include/utils/source_files.hpp b/compiler/include/compiler/utils/source_files.hpp similarity index 97% rename from utils/include/utils/source_files.hpp rename to compiler/include/compiler/utils/source_files.hpp index 8fd6c152..2bfc6b27 100644 --- a/utils/include/utils/source_files.hpp +++ b/compiler/include/compiler/utils/source_files.hpp @@ -4,7 +4,7 @@ #include #include -#include "utils/source_ref.hpp" +#include "compiler/utils/source_ref.hpp" namespace utils { diff --git a/utils/include/utils/source_ref.hpp b/compiler/include/compiler/utils/source_ref.hpp similarity index 100% rename from utils/include/utils/source_ref.hpp rename to compiler/include/compiler/utils/source_ref.hpp diff --git a/backend/include/backend/stringvec.hpp b/compiler/include/compiler/utils/stringvec.hpp similarity index 69% rename from backend/include/backend/stringvec.hpp rename to compiler/include/compiler/utils/stringvec.hpp index c7bc3de4..d680f148 100644 --- a/backend/include/backend/stringvec.hpp +++ b/compiler/include/compiler/utils/stringvec.hpp @@ -1,5 +1,5 @@ #pragma once -#include +#include "compiler/utils/source_files.hpp" using StringVec = utils::SourceFile; // FIXME: remove backward compatiability alias diff --git a/compiler/lib/CMakeLists.txt b/compiler/lib/CMakeLists.txt new file mode 100644 index 00000000..bd7bfbaa --- /dev/null +++ b/compiler/lib/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.22) + +add_subdirectory(utils) +add_subdirectory(ast) +add_subdirectory(frontend) +add_subdirectory(backend) + +if(ENABLE_CODEGEN) + add_subdirectory(codegen) + add_compile_definitions("ENABLE_CODEGEN") +endif() + +if(ENABLE_CLI) + add_subdirectory(cli) +endif() diff --git a/compiler/lib/ast/CMakeLists.txt b/compiler/lib/ast/CMakeLists.txt new file mode 100644 index 00000000..6beea90e --- /dev/null +++ b/compiler/lib/ast/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.22) + +set(TARGET_NAME ast) +set_target_include_dir(${TARGET_NAME}) + +file(GLOB_RECURSE TARGET_HEADERS ${TARGET_INCLUDE_DIR}/*.hpp) +file(GLOB_RECURSE TARGET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) + +add_library(${TARGET_NAME} STATIC ${TARGET_SRC} ${TARGET_HEADERS}) + +target_include_directories(${TARGET_NAME} + PUBLIC ${COMPILER_INCLUDE_DIR} + PRIVATE ${TARGET_INCLUDE_DIR} +) + +target_link_libraries(${TARGET_NAME} PUBLIC + utils +) diff --git a/ast/src/node.cpp b/compiler/lib/ast/node.cpp similarity index 100% rename from ast/src/node.cpp rename to compiler/lib/ast/node.cpp diff --git a/ast/src/syntax_tree.cpp b/compiler/lib/ast/syntax_tree.cpp similarity index 100% rename from ast/src/syntax_tree.cpp rename to compiler/lib/ast/syntax_tree.cpp diff --git a/compiler/lib/backend/CMakeLists.txt b/compiler/lib/backend/CMakeLists.txt new file mode 100644 index 00000000..20d7cceb --- /dev/null +++ b/compiler/lib/backend/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.22) + +add_subdirectory(ast) diff --git a/compiler/lib/backend/ast/CMakeLists.txt b/compiler/lib/backend/ast/CMakeLists.txt new file mode 100644 index 00000000..fc7e4e54 --- /dev/null +++ b/compiler/lib/backend/ast/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.22) + +set(TARGET_NAME backend_ast) +set_target_include_dir("backend/ast") + +file(GLOB_RECURSE TARGET_HEADERS ${TARGET_INCLUDE_DIR}/*.hpp) +file(GLOB_RECURSE TARGET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) + +add_library(${TARGET_NAME} STATIC ${TARGET_SRC} ${TARGET_HEADERS}) + +target_include_directories(${TARGET_NAME} + PUBLIC ${COMPILER_INCLUDE_DIR} + PRIVATE ${TARGET_INCLUDE_DIR} +) + +target_link_libraries(${TARGET_NAME} PUBLIC + ast + frontend + utils +) diff --git a/backend/src/optimizer/optimizer.cpp b/compiler/lib/backend/ast/optimizer/optimizer.cpp similarity index 100% rename from backend/src/optimizer/optimizer.cpp rename to compiler/lib/backend/ast/optimizer/optimizer.cpp diff --git a/backend/src/semantizer/semantizer.cpp b/compiler/lib/backend/ast/semantizer/semantizer.cpp similarity index 100% rename from backend/src/semantizer/semantizer.cpp rename to compiler/lib/backend/ast/semantizer/semantizer.cpp diff --git a/compiler/lib/cli/CMakeLists.txt b/compiler/lib/cli/CMakeLists.txt new file mode 100644 index 00000000..02c2d43c --- /dev/null +++ b/compiler/lib/cli/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.22) + +set(TARGET_NAME cli) +set_target_include_dir(${TARGET_NAME}) + +file(GLOB_RECURSE TARGET_HEADERS ${TARGET_INCLUDE_DIR}/*.hpp) +file(GLOB_RECURSE TARGET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) + +add_executable(${TARGET_NAME} ${TARGET_SRC} ${TARGET_HEADERS}) +set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME compiler) + +target_include_directories(${TARGET_NAME} + PUBLIC ${COMPILER_INCLUDE_DIR} + PRIVATE ${TARGET_INCLUDE_DIR} +) + +target_link_libraries(${TARGET_NAME} PUBLIC + argparse + backend_ast + frontend + utils +) +if (ENABLE_CODEGEN) + target_link_libraries(${TARGET_NAME} PUBLIC codegen) +endif() diff --git a/cli/src/compiler.cpp b/compiler/lib/cli/compiler.cpp similarity index 93% rename from cli/src/compiler.cpp rename to compiler/lib/cli/compiler.cpp index f2ffb152..5770ca8e 100644 --- a/cli/src/compiler.cpp +++ b/compiler/lib/cli/compiler.cpp @@ -4,7 +4,7 @@ #include #include -#ifdef ENABLE_IR_GENERATOR +#ifdef ENABLE_CODEGEN #include #include #include @@ -12,15 +12,16 @@ #endif #include -#include -#include -#include -#include -#include -#include - -#ifdef ENABLE_IR_GENERATOR -#include + +#include "compiler/backend/ast/optimizer/optimizer.hpp" +#include "compiler/backend/ast/semantizer/semantizer.hpp" +#include "compiler/frontend/lexer/lexer.hpp" +#include "compiler/frontend/parser/parser.hpp" +#include "compiler/frontend/preprocessor/preprocessor.hpp" +#include "compiler/utils/source_files.hpp" + +#ifdef ENABLE_CODEGEN +#include "compiler/codegen/ir_generator.hpp" #endif #include "dumping.hpp" @@ -33,7 +34,7 @@ using preprocessor::Preprocessor; using semantizer::Semantizer; using utils::SourceFile; -#ifdef ENABLE_IR_GENERATOR +#ifdef ENABLE_CODEGEN using ir_generator::IRGenerator; #endif @@ -45,7 +46,7 @@ const char *const ARG_LOG = "--log"; const char *const ARG_OPTIMIZE = "--optimize"; const char *const ARG_FILES = "FILES"; -#ifdef ENABLE_IR_GENERATOR +#ifdef ENABLE_CODEGEN const char *const ARG_COMPILE = "--compile"; const char *const ARG_CLANG = "--clang"; const char *const ARG_LLC = "--llc"; @@ -58,7 +59,7 @@ argparse::ArgumentParser createArgumentParser() { parser.add_argument("-v", ARG_VERBOSE).help("print info messages").default_value(false).implicit_value(true); parser.add_argument("-l", ARG_LOG).help("log file (stages output will be saved if provided)"); parser.add_argument("-O", ARG_OPTIMIZE).help("run optimization pass").default_value(false).implicit_value(true); -#ifdef ENABLE_IR_GENERATOR +#ifdef ENABLE_CODEGEN parser.add_argument("-c", ARG_COMPILE) .help("produce an executable instead of LLVM IR code") .default_value(false) @@ -75,7 +76,7 @@ argparse::ArgumentParser createArgumentParser() { return parser; } -#ifdef ENABLE_IR_GENERATOR +#ifdef ENABLE_CODEGEN std::filesystem::path createTemporaryDirectory() { auto tempDir = std::filesystem::temp_directory_path(); std::random_device dev; @@ -185,7 +186,7 @@ int Compiler::exec(int argc, char *argv[]) { return 3; } -#ifdef ENABLE_IR_GENERATOR +#ifdef ENABLE_CODEGEN logger << std::endl << "IR GENERATOR:" << std::endl; IRGenerator generator("module"); generator.process(tree); diff --git a/cli/src/dumping.cpp b/compiler/lib/cli/dumping.cpp similarity index 100% rename from cli/src/dumping.cpp rename to compiler/lib/cli/dumping.cpp diff --git a/cli/src/logger.cpp b/compiler/lib/cli/logger.cpp similarity index 100% rename from cli/src/logger.cpp rename to compiler/lib/cli/logger.cpp diff --git a/cli/src/main.cpp b/compiler/lib/cli/main.cpp similarity index 100% rename from cli/src/main.cpp rename to compiler/lib/cli/main.cpp diff --git a/ir_generator/CMakeLists.txt b/compiler/lib/codegen/CMakeLists.txt similarity index 56% rename from ir_generator/CMakeLists.txt rename to compiler/lib/codegen/CMakeLists.txt index 4f682492..1b2cbe8b 100644 --- a/ir_generator/CMakeLists.txt +++ b/compiler/lib/codegen/CMakeLists.txt @@ -1,11 +1,12 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.22) -set(TARGET_NAME ir_generator) +set(TARGET_NAME codegen) +set_target_include_dir(${TARGET_NAME}) find_package(LLVM REQUIRED CONFIG) -file(GLOB_RECURSE TARGET_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/ir_generator/*.hpp) -file(GLOB_RECURSE TARGET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp) +file(GLOB_RECURSE TARGET_HEADERS ${TARGET_INCLUDE_DIR}/*.hpp) +file(GLOB_RECURSE TARGET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS}) add_definitions(${LLVM_DEFINITIONS_LIST}) @@ -14,11 +15,8 @@ llvm_map_components_to_libnames(LLVM_LINK_LIBRARIES core) add_library(${TARGET_NAME} STATIC ${TARGET_SRC} ${TARGET_HEADERS}) target_include_directories(${TARGET_NAME} -PUBLIC - include - ${LLVM_INCLUDE_DIRS} -PRIVATE - include/ir_generator + PUBLIC ${COMPILER_INCLUDE_DIR} ${LLVM_INCLUDE_DIRS} + PRIVATE ${TARGET_INCLUDE_DIR} ) target_link_libraries(${TARGET_NAME} PUBLIC diff --git a/ir_generator/src/ir_generator.cpp b/compiler/lib/codegen/ir_generator.cpp similarity index 100% rename from ir_generator/src/ir_generator.cpp rename to compiler/lib/codegen/ir_generator.cpp diff --git a/compiler/lib/frontend/CMakeLists.txt b/compiler/lib/frontend/CMakeLists.txt new file mode 100644 index 00000000..3be39e4f --- /dev/null +++ b/compiler/lib/frontend/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.22) + +set(TARGET_NAME frontend) +set_target_include_dir(${TARGET_NAME}) + +file(GLOB_RECURSE TARGET_HEADERS ${TARGET_INCLUDE_DIR}/*.hpp) +file(GLOB_RECURSE TARGET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) + +add_library(${TARGET_NAME} STATIC ${TARGET_SRC} ${TARGET_HEADERS}) + +target_include_directories(${TARGET_NAME} + PUBLIC ${COMPILER_INCLUDE_DIR} + PRIVATE ${TARGET_INCLUDE_DIR} +) + +target_link_libraries(${TARGET_NAME} PUBLIC + ast + utils +) diff --git a/backend/src/lexer/lexer.cpp b/compiler/lib/frontend/lexer/lexer.cpp similarity index 100% rename from backend/src/lexer/lexer.cpp rename to compiler/lib/frontend/lexer/lexer.cpp diff --git a/backend/src/lexer/token.cpp b/compiler/lib/frontend/lexer/token.cpp similarity index 100% rename from backend/src/lexer/token.cpp rename to compiler/lib/frontend/lexer/token.cpp diff --git a/backend/src/parser/parser.cpp b/compiler/lib/frontend/parser/parser.cpp similarity index 100% rename from backend/src/parser/parser.cpp rename to compiler/lib/frontend/parser/parser.cpp diff --git a/backend/src/parser/type_registry.cpp b/compiler/lib/frontend/parser/type_registry.cpp similarity index 100% rename from backend/src/parser/type_registry.cpp rename to compiler/lib/frontend/parser/type_registry.cpp diff --git a/backend/src/preprocessor/preprocessor.cpp b/compiler/lib/frontend/preprocessor/preprocessor.cpp similarity index 100% rename from backend/src/preprocessor/preprocessor.cpp rename to compiler/lib/frontend/preprocessor/preprocessor.cpp diff --git a/compiler/lib/utils/CMakeLists.txt b/compiler/lib/utils/CMakeLists.txt new file mode 100644 index 00000000..9372c2e9 --- /dev/null +++ b/compiler/lib/utils/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.22) + +set(TARGET_NAME utils) +set_target_include_dir(${TARGET_NAME}) + +file(GLOB_RECURSE TARGET_HEADERS ${TARGET_INCLUDE_DIR}/*.hpp) +file(GLOB_RECURSE TARGET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) + +add_library(${TARGET_NAME} STATIC ${TARGET_SRC} ${TARGET_HEADERS}) + +target_include_directories(${TARGET_NAME} + PUBLIC ${COMPILER_INCLUDE_DIR} + PRIVATE ${TARGET_INCLUDE_DIR} +) diff --git a/backend/src/base_error.cpp b/compiler/lib/utils/base_error.cpp similarity index 100% rename from backend/src/base_error.cpp rename to compiler/lib/utils/base_error.cpp diff --git a/utils/src/source_files.cpp b/compiler/lib/utils/source_files.cpp similarity index 100% rename from utils/src/source_files.cpp rename to compiler/lib/utils/source_files.cpp diff --git a/compiler/tests/CMakeLists.txt b/compiler/tests/CMakeLists.txt new file mode 100644 index 00000000..843fb140 --- /dev/null +++ b/compiler/tests/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.22) + +add_subdirectory(ast) +add_subdirectory(frontend) +add_subdirectory(backend) + +add_custom_target(tests +DEPENDS + ast_test + frontend_test + backend_ast_test +) + +add_custom_target(run_tests +DEPENDS + run_ast_test + run_frontend_test + run_backend_ast_test +) + +if(ENABLE_CODEGEN) + add_subdirectory(codegen) + add_dependencies(tests codegen_test) + add_dependencies(run_tests run_codegen_test) +endif() diff --git a/tests/ast/CMakeLists.txt b/compiler/tests/ast/CMakeLists.txt similarity index 56% rename from tests/ast/CMakeLists.txt rename to compiler/tests/ast/CMakeLists.txt index 754bc42d..1baec768 100644 --- a/tests/ast/CMakeLists.txt +++ b/compiler/tests/ast/CMakeLists.txt @@ -1,6 +1,6 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.22) -set(TARGET_NAME "ast_test") +set(TARGET_NAME ast_test) file(GLOB_RECURSE TARGET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp @@ -9,7 +9,7 @@ file(GLOB_RECURSE TARGET_SRC add_executable(${TARGET_NAME} ${TARGET_SRC}) target_include_directories(${TARGET_NAME} PUBLIC - ${CMAKE_SOURCE_DIR}/ast/include + ${COMPILER_INCLUDE_DIR} ) target_link_libraries(${TARGET_NAME} PUBLIC @@ -19,3 +19,9 @@ target_link_libraries(${TARGET_NAME} PUBLIC ) gtest_discover_tests(${TARGET_NAME}) + +add_custom_target(run_ast_test + COMMAND $ + DEPENDS ast_test + COMMENT "Run AST interface tests" +) diff --git a/tests/ast/main.cpp b/compiler/tests/ast/main.cpp similarity index 100% rename from tests/ast/main.cpp rename to compiler/tests/ast/main.cpp diff --git a/tests/ast/node.cpp b/compiler/tests/ast/node.cpp similarity index 97% rename from tests/ast/node.cpp rename to compiler/tests/ast/node.cpp index d39f90cb..179a2b15 100644 --- a/tests/ast/node.cpp +++ b/compiler/tests/ast/node.cpp @@ -1,6 +1,6 @@ #include -#include "ast/node.hpp" +#include "compiler/ast/node.hpp" using namespace ast; diff --git a/tests/ast/syntax_tree.cpp b/compiler/tests/ast/syntax_tree.cpp similarity index 96% rename from tests/ast/syntax_tree.cpp rename to compiler/tests/ast/syntax_tree.cpp index 8351dfc7..6222d80a 100644 --- a/tests/ast/syntax_tree.cpp +++ b/compiler/tests/ast/syntax_tree.cpp @@ -1,6 +1,6 @@ #include -#include "ast/syntax_tree.hpp" +#include "compiler/ast/syntax_tree.hpp" using namespace ast; diff --git a/compiler/tests/backend/CMakeLists.txt b/compiler/tests/backend/CMakeLists.txt new file mode 100644 index 00000000..20d7cceb --- /dev/null +++ b/compiler/tests/backend/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.22) + +add_subdirectory(ast) diff --git a/compiler/tests/backend/ast/CMakeLists.txt b/compiler/tests/backend/ast/CMakeLists.txt new file mode 100644 index 00000000..d9fb8b84 --- /dev/null +++ b/compiler/tests/backend/ast/CMakeLists.txt @@ -0,0 +1,27 @@ +cmake_minimum_required(VERSION 3.22) + +set(TARGET_NAME backend_ast_test) + +file(GLOB_RECURSE TARGET_SRC + ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp +) + +add_executable(${TARGET_NAME} ${TARGET_SRC}) + +target_include_directories(${TARGET_NAME} PUBLIC + ${COMPILER_INCLUDE_DIR} +) + +target_link_libraries(${TARGET_NAME} PUBLIC + backend_ast + gtest + gtest_main +) + +gtest_discover_tests(${TARGET_NAME}) + +add_custom_target(run_backend_ast_test + COMMAND $ + DEPENDS backend_ast_test + COMMENT "Run AST-based backend tests" +) diff --git a/tests/backend/main.cpp b/compiler/tests/backend/ast/main.cpp similarity index 100% rename from tests/backend/main.cpp rename to compiler/tests/backend/ast/main.cpp diff --git a/tests/backend/optimizer.cpp b/compiler/tests/backend/ast/optimizer.cpp similarity index 99% rename from tests/backend/optimizer.cpp rename to compiler/tests/backend/ast/optimizer.cpp index 4700d0ac..a0df0ff8 100644 --- a/tests/backend/optimizer.cpp +++ b/compiler/tests/backend/ast/optimizer.cpp @@ -1,10 +1,10 @@ #include -#include "lexer/lexer.hpp" -#include "optimizer/optimizer.hpp" -#include "parser/parser.hpp" -#include "semantizer/semantizer.hpp" -#include "stringvec.hpp" +#include "compiler/backend/ast/optimizer/optimizer.hpp" +#include "compiler/backend/ast/semantizer/semantizer.hpp" +#include "compiler/frontend/lexer/lexer.hpp" +#include "compiler/frontend/parser/parser.hpp" +#include "compiler/utils/stringvec.hpp" using namespace ast; using namespace lexer; diff --git a/tests/backend/semantizer.cpp b/compiler/tests/backend/ast/semantizer.cpp similarity index 99% rename from tests/backend/semantizer.cpp rename to compiler/tests/backend/ast/semantizer.cpp index e0004394..36cb9fa7 100644 --- a/tests/backend/semantizer.cpp +++ b/compiler/tests/backend/ast/semantizer.cpp @@ -1,9 +1,9 @@ #include -#include "lexer/lexer.hpp" -#include "parser/parser.hpp" -#include "semantizer/semantizer.hpp" -#include "stringvec.hpp" +#include "compiler/backend/ast/semantizer/semantizer.hpp" +#include "compiler/frontend/lexer/lexer.hpp" +#include "compiler/frontend/parser/parser.hpp" +#include "compiler/utils/stringvec.hpp" using namespace ast; using namespace lexer; diff --git a/compiler/tests/codegen/CMakeLists.txt b/compiler/tests/codegen/CMakeLists.txt new file mode 100644 index 00000000..714aa006 --- /dev/null +++ b/compiler/tests/codegen/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.22) + +set(TARGET_NAME codegen_test) + +file(GLOB_RECURSE TARGET_SRC + ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp +) + +add_executable(${TARGET_NAME} ${TARGET_SRC}) + +target_link_libraries(${TARGET_NAME} PUBLIC + backend_ast + codegen + gtest + gtest_main +) + +gtest_discover_tests(${TARGET_NAME}) + +add_custom_target(run_codegen_test + COMMAND $ + DEPENDS codegen_test + COMMENT "Run LLVM IR generator tests" +) diff --git a/tests/ir_generator/ir_generator.cpp b/compiler/tests/codegen/ir_generator.cpp similarity index 100% rename from tests/ir_generator/ir_generator.cpp rename to compiler/tests/codegen/ir_generator.cpp diff --git a/tests/ir_generator/main.cpp b/compiler/tests/codegen/main.cpp similarity index 100% rename from tests/ir_generator/main.cpp rename to compiler/tests/codegen/main.cpp diff --git a/tests/backend/CMakeLists.txt b/compiler/tests/frontend/CMakeLists.txt similarity index 52% rename from tests/backend/CMakeLists.txt rename to compiler/tests/frontend/CMakeLists.txt index 518468ee..e09a0578 100644 --- a/tests/backend/CMakeLists.txt +++ b/compiler/tests/frontend/CMakeLists.txt @@ -1,6 +1,6 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.22) -set(TARGET_NAME "backend_test") +set(TARGET_NAME frontend_test) file(GLOB_RECURSE TARGET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp @@ -9,13 +9,19 @@ file(GLOB_RECURSE TARGET_SRC add_executable(${TARGET_NAME} ${TARGET_SRC}) target_include_directories(${TARGET_NAME} PUBLIC - ${CMAKE_SOURCE_DIR}/backend/include + ${COMPILER_INCLUDE_DIR} ) target_link_libraries(${TARGET_NAME} PUBLIC - backend + frontend gtest gtest_main ) gtest_discover_tests(${TARGET_NAME}) + +add_custom_target(run_frontend_test + COMMAND $ + DEPENDS frontend_test + COMMENT "Run frontend tests" +) diff --git a/tests/backend/lexer.cpp b/compiler/tests/frontend/lexer.cpp similarity index 98% rename from tests/backend/lexer.cpp rename to compiler/tests/frontend/lexer.cpp index aae95796..326c178a 100644 --- a/tests/backend/lexer.cpp +++ b/compiler/tests/frontend/lexer.cpp @@ -1,9 +1,9 @@ #include -#include "lexer/lexer.hpp" -#include "lexer/lexer_error.hpp" -#include "lexer/token.hpp" -#include "stringvec.hpp" +#include "compiler/frontend/lexer/lexer.hpp" +#include "compiler/frontend/lexer/lexer_error.hpp" +#include "compiler/frontend/lexer/token.hpp" +#include "compiler/utils/stringvec.hpp" #define SINGLE_TOKEN_TEST_IMPL(TOKEN_STR, TOKEN_VALUE) \ StringVec source = {TOKEN_STR}; \ diff --git a/tests/backend/parser.cpp b/compiler/tests/frontend/parser.cpp similarity index 99% rename from tests/backend/parser.cpp rename to compiler/tests/frontend/parser.cpp index 2dbc6ed3..6771234b 100644 --- a/tests/backend/parser.cpp +++ b/compiler/tests/frontend/parser.cpp @@ -1,8 +1,8 @@ #include -#include "lexer/lexer.hpp" -#include "parser/parser.hpp" -#include "stringvec.hpp" +#include "compiler/frontend/lexer/lexer.hpp" +#include "compiler/frontend/parser/parser.hpp" +#include "compiler/utils/stringvec.hpp" using namespace ast; using namespace lexer; diff --git a/tests/backend/preprocessor.cpp b/compiler/tests/frontend/preprocessor.cpp similarity index 95% rename from tests/backend/preprocessor.cpp rename to compiler/tests/frontend/preprocessor.cpp index 8eebf59c..949ac6c5 100644 --- a/tests/backend/preprocessor.cpp +++ b/compiler/tests/frontend/preprocessor.cpp @@ -1,7 +1,7 @@ #include -#include "preprocessor/preprocessor.hpp" -#include "stringvec.hpp" +#include "compiler/frontend/preprocessor/preprocessor.hpp" +#include "compiler/utils/stringvec.hpp" using namespace preprocessor; diff --git a/tests/backend/token.cpp b/compiler/tests/frontend/token.cpp similarity index 97% rename from tests/backend/token.cpp rename to compiler/tests/frontend/token.cpp index c93245ac..3119ba79 100644 --- a/tests/backend/token.cpp +++ b/compiler/tests/frontend/token.cpp @@ -1,6 +1,6 @@ #include -#include "lexer/token.hpp" +#include "compiler/frontend/lexer/token.hpp" using namespace lexer; diff --git a/docs/ru/tutorials/install.md b/docs/ru/tutorials/install.md index dc8d263c..25881242 100644 --- a/docs/ru/tutorials/install.md +++ b/docs/ru/tutorials/install.md @@ -38,7 +38,7 @@ Вы также можете указать список опций: ```sh - cmake -DENABLE_TESTS=ON -DENABLE_IR_GENERATOR=OFF .. + cmake -DENABLE_TESTS=ON -DENABLE_CODEGEN=OFF .. ``` Доступные опции: @@ -47,9 +47,9 @@ -------- | ------------------ | --------------------- | ---------- `ENABLE_CLI` | `ON`, `OFF` | `ON` | Выполнить сборку приложения с интерфейсом командной строки (CLI) `ENABLE_TESTS` | `ON`, `OFF` | `ON` | Выполнить сборку тестировочных приложений - `ENABLE_IR_GENERATOR` | `ON`, `OFF` | `ON` | Выполнить сборку с поддержкой генерации IR (необходим LLVM) + `ENABLE_CODEGEN` | `ON`, `OFF` | `ON` | Выполнить сборку с поддержкой генерации IR (необходим LLVM) - > **Подсказка:** задайте `ENABLE_IR_GENERATOR` как `OFF`, если у вас не установлен LLVM. + > **Подсказка:** задайте `ENABLE_CODEGEN` как `OFF`, если у вас не установлен LLVM. 4. Запустите сборку проекта: ```sh cmake --build . diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt deleted file mode 100644 index e85d9562..00000000 --- a/tests/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -add_subdirectory(ast) -add_subdirectory(backend) - -if(ENABLE_IR_GENERATOR) - add_subdirectory(ir_generator) -endif() diff --git a/tests/ir_generator/CMakeLists.txt b/tests/ir_generator/CMakeLists.txt deleted file mode 100644 index 049f56c7..00000000 --- a/tests/ir_generator/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -set(TARGET_NAME "ir_generator_test") - -file(GLOB_RECURSE TARGET_SRC - ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp -) - -add_executable(${TARGET_NAME} ${TARGET_SRC}) - -target_link_libraries(${TARGET_NAME} PUBLIC - backend - ir_generator - gtest - gtest_main -) - -gtest_discover_tests(${TARGET_NAME}) diff --git a/thirdparty/CMakeLists.txt b/thirdparty/CMakeLists.txt index 35cc7ea8..788b582e 100644 --- a/thirdparty/CMakeLists.txt +++ b/thirdparty/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.22) if(ENABLE_CLI) add_subdirectory(argparse) diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt deleted file mode 100644 index 37ce4879..00000000 --- a/utils/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -set(TARGET_NAME utils) - -file(GLOB_RECURSE TARGET_HEADERS - ${CMAKE_CURRENT_SOURCE_DIR}/include/utils/*.hpp -) - -file(GLOB_RECURSE TARGET_SRC - ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp -) - -add_library(${TARGET_NAME} STATIC ${TARGET_SRC} ${TARGET_HEADERS}) - -target_include_directories(${TARGET_NAME} -PUBLIC - include -PRIVATE - include/utils -)