Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Separate front-end and core code. #4

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/master-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ jobs:
generate_release_notes: true
files: |
${{github.workspace}}/build/gifscript
${{github.workspace}}/build/tpircsfig
tag_name: ${{ steps.tag_version.outputs.new_tag }}
104 changes: 52 additions & 52 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ endif()

find_package(RAGEL REQUIRED)

if(WIN32)
find_program(LEMON-PARSER "lemon" REQUIRED)
if(NOT ${LEMON-PARSER} STREQUAL LEMON-PARSER-NOTFOUND)
message("lemon parser (lemon.exe) not found. If using VCPKG install lemon-parser-generator and add `$VCPKG_ROOT\\packages\\lemon-parser-generator_x64-windows\\tools\\lemon` (or wherever lemon.exe is) to your $PATH.")
endif()
endif()
set(CORE_DIR ${CMAKE_SOURCE_DIR}/core)
set(CORE_INCLUDE ${CORE_DIR}/include)
set(CORE_SRC ${CORE_DIR}/src)

set(BACKEND_DIR ${CMAKE_SOURCE_DIR}/backends)
set(BACKEND_INCLUDE ${BACKEND_DIR}/include)
set(BACKEND_SRC ${BACKEND_DIR}/src)

set(FRONTEND_DIR ${CMAKE_SOURCE_DIR}/frontends)

include(FetchContent)

Expand All @@ -31,78 +34,75 @@ FetchContent_Declare(fmt
)
FetchContent_MakeAvailable(fmt)

if(NOT WIN32)
find_program(CLANG_TIDY_EXE NAMES "clang-tidy")
if(CLANG_TIDY_EXE AND NOT DISABLE_CLANG_TIDY AND NOT WIN32)
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,readability-*,modernize-*,performance-*,portability-*,bugprone-*,clang-analyzer-*)
endif()
if(CLANG_TIDY_EXE AND NOT DISABLE_CLANG_TIDY)
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,readability-*,modernize-*,performance-*,portability-*,bugprone-*,clang-analyzer-*)
endif()

RAGEL_TARGET(gifscript
${CMAKE_SOURCE_DIR}/gifscript.rl
${FRONTEND_DIR}/gifscript.rl
${CMAKE_CURRENT_BINARY_DIR}/gifscript.cpp
COMPILE_FLAGS -G2
)

set(GENERATED_SOURCES
parser.h
parser.c
${CMAKE_CURRENT_BINARY_DIR}/parser.h
${CMAKE_CURRENT_BINARY_DIR}/parser.c
${RAGEL_gifscript_OUTPUTS}
)

set(BACKEND_SOURCES
backend/backend.hpp
backend/c_code.cpp
backend/c_code.h
${BACKEND_INCLUDE}/backend.hpp
${BACKEND_INCLUDE}/c_code.hpp
${BACKEND_INCLUDE}/gifscript_backend.hpp
${BACKEND_SRC}/c_code.cpp
${BACKEND_SRC}/gifscript_backend.cpp
)

set(CORE_SOURCES
version.h
logger.h
machine.h
machine.cpp
registers.h
registers.cpp
)
${CORE_INCLUDE}/version.hpp
${CORE_INCLUDE}/logger.hpp
${CORE_INCLUDE}/machine.hpp
${CORE_INCLUDE}/registers.hpp
${CORE_SRC}/machine.cpp
${CORE_SRC}/registers.cpp
)

add_library(gifscript_core ${BACKEND_SOURCES} ${CORE_SOURCES} ${GENERATED_SOURCES})
target_include_directories(gifscript_core PRIVATE ${CORE_INCLUDE} ${BACKEND_INCLUDE})

add_executable(gifscript ${BACKEND_SOURCES} ${CORE_SOURCES} ${GENERATED_SOURCES})
add_executable(gifscript gifscript.cpp)
target_include_directories(gifscript PRIVATE ${CORE_INCLUDE} ${BACKEND_INCLUDE})
target_link_libraries(gifscript PRIVATE gifscript_core)

set_source_files_properties(${GENERATED_SOURCES} PROPERTIES
add_executable(tpircsfig ${FRONTEND_DIR}/tpircsfig.cpp)
target_include_directories(tpircsfig PRIVATE ${CORE_INCLUDE} ${BACKEND_INCLUDE} ${CMAKE_BINARY_DIR})
target_link_libraries(tpircsfig PRIVATE gifscript_core)

# DISABLING LINTING FOR WIP TPIRCSFIG FRONTEND FOR NOW
set_source_files_properties(${GENERATED_SOURCES} ${FRONTEND_DIR}/tpircsfig.cpp PROPERTIES
SKIP_LINTING ON
)

if(WIN32)
target_compile_options(gifscript PRIVATE /std:c++latest)
else()
execute_process(
COMMAND git describe --tags --abbrev=4 --always
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
execute_process(
COMMAND git describe --tags --abbrev=4 --always
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(CONCAT GIT_VERSION "\"" ${GIT_VERSION} "\"")
message("git version: ${GIT_VERSION}")
target_compile_options(gifscript PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})
endif()

target_include_directories(gifscript PRIVATE ${CMAKE_SOURCE_DIR})

target_include_directories(gifscript PRIVATE ${fmt_SOURCE_DIR}/include)
target_link_libraries(gifscript PRIVATE fmt::fmt)
string(CONCAT GIT_VERSION "\"" ${GIT_VERSION} "\"")
message("git version: ${GIT_VERSION}")
target_compile_options(gifscript_core PRIVATE -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})
target_compile_options(gifscript PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})
target_compile_options(tpircsfig PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})

# CMake on windows causes issues with the lemon parser template file
# We need to get the location of the current lemon parser executable
# And (on vcpkg at least) the location of the template file is in the same
# discord as the lemon executable

if(WIN32)
get_filename_component(LEMON_PARSER_DIR ${LEMON-PARSER} DIRECTORY)
set(LEMON_PARSER_TEMPLATE "-T${LEMON_PARSER_DIR}/lempar.c")
endif()
target_include_directories(gifscript PUBLIC ${fmt_SOURCE_DIR}/include)
target_link_libraries(gifscript_core PUBLIC fmt::fmt)

add_custom_command(
OUTPUT parser.c parser.h
COMMAND lemon -q ${CMAKE_SOURCE_DIR}/parser.y -d${CMAKE_CURRENT_BINARY_DIR} ${LEMON_PARSER_TEMPLATE}
DEPENDS ${CMAKE_SOURCE_DIR}/parser.y
COMMAND lemon -q ${CORE_SRC}/parser.y -d${CMAKE_CURRENT_BINARY_DIR} ${LEMON_PARSER_TEMPLATE}
DEPENDS ${CORE_SRC}/parser.y
USES_TERMINAL
)
2 changes: 1 addition & 1 deletion backend/backend.hpp → backends/include/backend.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "registers.h"
#include "registers.hpp"

class Backend
{
Expand Down
File renamed without changes.
57 changes: 57 additions & 0 deletions backends/include/gifscript_backend.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include "backend.hpp"

#include <functional>
#include <unordered_map>

class gifscript_backend : public Backend
{

public:
gifscript_backend() = default;
~gifscript_backend();

bool arg_parse(int argc, char** argv) override;

void set_output(const std::string_view& output) override
{
this->output = output;
};

void print_help() const override;

void emit(GIFBlock& block) override;

// Primitive dispatching
static std::string emit_primitive(gifscript_backend*, const GifRegister&);
static std::string emit_rgbaq(gifscript_backend*, const GifRegister&);
static std::string emit_uv(gifscript_backend*, const GifRegister&);
static std::string emit_xyz2(gifscript_backend*, const GifRegister&);
static std::string emit_tex0(gifscript_backend*, const GifRegister&);
static std::string emit_fog(gifscript_backend*, const GifRegister&);
static std::string emit_fogcol(gifscript_backend*, const GifRegister&);
static std::string emit_scissor(gifscript_backend*, const GifRegister&);
static std::string emit_signal(gifscript_backend*, const GifRegister&);
static std::string emit_finish(gifscript_backend*, const GifRegister&);
static std::string emit_label(gifscript_backend*, const GifRegister&);

std::unordered_map<uint32_t, std::function<std::string(gifscript_backend*, const GifRegister&)>> dispatch_table =
{
{0x00, gifscript_backend::emit_primitive},
{0x01, gifscript_backend::emit_rgbaq},
{0x03, gifscript_backend::emit_uv},
{0x05, gifscript_backend::emit_xyz2},
{0x06, gifscript_backend::emit_tex0},
{0x0A, gifscript_backend::emit_fog},
{0x3D, gifscript_backend::emit_fogcol},
{0x40, gifscript_backend::emit_scissor},
{0x60, gifscript_backend::emit_signal},
{0x61, gifscript_backend::emit_finish},
{0x62, gifscript_backend::emit_label}};

private:
std::string output = "";
FILE* file = nullptr;
bool first_emit = true;
};
6 changes: 3 additions & 3 deletions backend/c_code.cpp → backends/src/c_code.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "c_code.h"
#include "registers.h"
#include "c_code.hpp"
#include "registers.hpp"
#include <fmt/core.h>
#include <functional>
#include "logger.h"
#include "logger.hpp"

auto c_code_backend::arg_parse(int argc, char** argv) -> bool
{
Expand Down
Loading
Loading