From 584492d1efad2ff1cc0357b2f1024899ad5cbd4d Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 7 Feb 2021 17:28:19 +0100 Subject: [PATCH] Implement ZCG CPP workflow basis --- .../CLI.cpp => Generating/CodeGenerator.cpp} | 0 .../CLI.h => Generating/CodeGenerator.h} | 0 .../Interface/PrettyPrinter.cpp | 0 .../Interface/PrettyPrinter.h | 0 .../Interface/Session.cpp | 10 -- src/ZoneCodeGeneratorNew/Interface/Session.h | 11 -- .../Parsing/Commands/CommandsFileReader.cpp | 15 +++ .../Parsing/Commands/CommandsFileReader.h | 17 +++ .../Parsing/Header/HeaderFileReader.cpp | 15 +++ .../Parsing/Header/HeaderFileReader.h | 17 +++ .../Persistence/IDataRepository.h | 7 ++ .../InMemory/InMemoryRepository.cpp | 1 + .../Persistence/InMemory/InMemoryRepository.h | 8 ++ .../Printing/PrettyPrinter.cpp | 8 ++ .../Printing/PrettyPrinter.h | 8 ++ .../ZoneCodeGenerator.cpp | 107 ++++++++++++++++++ src/ZoneCodeGeneratorNew/ZoneCodeGenerator.h | 17 +++ .../ZoneCodeGeneratorArguments.cpp | 69 ++++++----- .../ZoneCodeGeneratorArguments.h | 4 +- src/ZoneCodeGeneratorNew/main.cpp | 17 +-- 20 files changed, 268 insertions(+), 63 deletions(-) rename src/ZoneCodeGeneratorNew/{Interface/CLI.cpp => Generating/CodeGenerator.cpp} (100%) rename src/ZoneCodeGeneratorNew/{Interface/CLI.h => Generating/CodeGenerator.h} (100%) delete mode 100644 src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.cpp delete mode 100644 src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.h delete mode 100644 src/ZoneCodeGeneratorNew/Interface/Session.cpp delete mode 100644 src/ZoneCodeGeneratorNew/Interface/Session.h create mode 100644 src/ZoneCodeGeneratorNew/Parsing/Commands/CommandsFileReader.cpp create mode 100644 src/ZoneCodeGeneratorNew/Parsing/Commands/CommandsFileReader.h create mode 100644 src/ZoneCodeGeneratorNew/Parsing/Header/HeaderFileReader.cpp create mode 100644 src/ZoneCodeGeneratorNew/Parsing/Header/HeaderFileReader.h create mode 100644 src/ZoneCodeGeneratorNew/Persistence/IDataRepository.h create mode 100644 src/ZoneCodeGeneratorNew/Persistence/InMemory/InMemoryRepository.cpp create mode 100644 src/ZoneCodeGeneratorNew/Persistence/InMemory/InMemoryRepository.h create mode 100644 src/ZoneCodeGeneratorNew/Printing/PrettyPrinter.cpp create mode 100644 src/ZoneCodeGeneratorNew/Printing/PrettyPrinter.h create mode 100644 src/ZoneCodeGeneratorNew/ZoneCodeGenerator.cpp create mode 100644 src/ZoneCodeGeneratorNew/ZoneCodeGenerator.h rename src/ZoneCodeGeneratorNew/{Interface => }/ZoneCodeGeneratorArguments.cpp (72%) rename src/ZoneCodeGeneratorNew/{Interface => }/ZoneCodeGeneratorArguments.h (89%) diff --git a/src/ZoneCodeGeneratorNew/Interface/CLI.cpp b/src/ZoneCodeGeneratorNew/Generating/CodeGenerator.cpp similarity index 100% rename from src/ZoneCodeGeneratorNew/Interface/CLI.cpp rename to src/ZoneCodeGeneratorNew/Generating/CodeGenerator.cpp diff --git a/src/ZoneCodeGeneratorNew/Interface/CLI.h b/src/ZoneCodeGeneratorNew/Generating/CodeGenerator.h similarity index 100% rename from src/ZoneCodeGeneratorNew/Interface/CLI.h rename to src/ZoneCodeGeneratorNew/Generating/CodeGenerator.h diff --git a/src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.cpp b/src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.h b/src/ZoneCodeGeneratorNew/Interface/PrettyPrinter.h deleted file mode 100644 index e69de29b..00000000 diff --git a/src/ZoneCodeGeneratorNew/Interface/Session.cpp b/src/ZoneCodeGeneratorNew/Interface/Session.cpp deleted file mode 100644 index dfc77fbe..00000000 --- a/src/ZoneCodeGeneratorNew/Interface/Session.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "Session.h" - -Session::Session() -= default; - -Session::Session(ZoneCodeGeneratorArguments args) - : m_args(std::move(args)) -{ - -} diff --git a/src/ZoneCodeGeneratorNew/Interface/Session.h b/src/ZoneCodeGeneratorNew/Interface/Session.h deleted file mode 100644 index da71ed39..00000000 --- a/src/ZoneCodeGeneratorNew/Interface/Session.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include "ZoneCodeGeneratorArguments.h" - -class Session -{ - ZoneCodeGeneratorArguments m_args; - -public: - Session(); - explicit Session(ZoneCodeGeneratorArguments args); -}; diff --git a/src/ZoneCodeGeneratorNew/Parsing/Commands/CommandsFileReader.cpp b/src/ZoneCodeGeneratorNew/Parsing/Commands/CommandsFileReader.cpp new file mode 100644 index 00000000..8f695ddc --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Parsing/Commands/CommandsFileReader.cpp @@ -0,0 +1,15 @@ +#include "CommandsFileReader.h" + +#include + +CommandsFileReader::CommandsFileReader(const ZoneCodeGeneratorArguments* args, std::string filename) + : m_args(args), + m_filename(std::move(filename)) +{ +} + +bool CommandsFileReader::ReadCommandsFile(IDataRepository* repository) +{ + std::cout << "Reading commands file: " << m_filename << std::endl; + return true; +} diff --git a/src/ZoneCodeGeneratorNew/Parsing/Commands/CommandsFileReader.h b/src/ZoneCodeGeneratorNew/Parsing/Commands/CommandsFileReader.h new file mode 100644 index 00000000..df6cc6dd --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Parsing/Commands/CommandsFileReader.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include "ZoneCodeGeneratorArguments.h" +#include "Persistence/IDataRepository.h" + +class CommandsFileReader +{ + const ZoneCodeGeneratorArguments* m_args; + std::string m_filename; + +public: + explicit CommandsFileReader(const ZoneCodeGeneratorArguments* args, std::string filename); + + bool ReadCommandsFile(IDataRepository* repository); +}; diff --git a/src/ZoneCodeGeneratorNew/Parsing/Header/HeaderFileReader.cpp b/src/ZoneCodeGeneratorNew/Parsing/Header/HeaderFileReader.cpp new file mode 100644 index 00000000..d5aff5d5 --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Parsing/Header/HeaderFileReader.cpp @@ -0,0 +1,15 @@ +#include "HeaderFileReader.h" + +#include + +HeaderFileReader::HeaderFileReader(const ZoneCodeGeneratorArguments* args, std::string filename) + : m_args(args), + m_filename(std::move(filename)) +{ +} + +bool HeaderFileReader::ReadHeaderFile(IDataRepository* repository) +{ + std::cout << "Reading header file: " << m_filename << std::endl; + return true; +} diff --git a/src/ZoneCodeGeneratorNew/Parsing/Header/HeaderFileReader.h b/src/ZoneCodeGeneratorNew/Parsing/Header/HeaderFileReader.h new file mode 100644 index 00000000..ac82abcf --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Parsing/Header/HeaderFileReader.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include "ZoneCodeGeneratorArguments.h" +#include "Persistence/IDataRepository.h" + +class HeaderFileReader +{ + const ZoneCodeGeneratorArguments* m_args; + std::string m_filename; + +public: + HeaderFileReader(const ZoneCodeGeneratorArguments* args, std::string filename); + + bool ReadHeaderFile(IDataRepository* repository); +}; diff --git a/src/ZoneCodeGeneratorNew/Persistence/IDataRepository.h b/src/ZoneCodeGeneratorNew/Persistence/IDataRepository.h new file mode 100644 index 00000000..aa3f510f --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Persistence/IDataRepository.h @@ -0,0 +1,7 @@ +#pragma once + +class IDataRepository +{ +public: + virtual ~IDataRepository() = default; +}; \ No newline at end of file diff --git a/src/ZoneCodeGeneratorNew/Persistence/InMemory/InMemoryRepository.cpp b/src/ZoneCodeGeneratorNew/Persistence/InMemory/InMemoryRepository.cpp new file mode 100644 index 00000000..b0ab2a7d --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Persistence/InMemory/InMemoryRepository.cpp @@ -0,0 +1 @@ +#include "InMemoryRepository.h" \ No newline at end of file diff --git a/src/ZoneCodeGeneratorNew/Persistence/InMemory/InMemoryRepository.h b/src/ZoneCodeGeneratorNew/Persistence/InMemory/InMemoryRepository.h new file mode 100644 index 00000000..c70fbb5b --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Persistence/InMemory/InMemoryRepository.h @@ -0,0 +1,8 @@ +#pragma once +#include "Persistence/IDataRepository.h" + +class InMemoryRepository final : public IDataRepository +{ +public: + +}; diff --git a/src/ZoneCodeGeneratorNew/Printing/PrettyPrinter.cpp b/src/ZoneCodeGeneratorNew/Printing/PrettyPrinter.cpp new file mode 100644 index 00000000..dca4e1ec --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Printing/PrettyPrinter.cpp @@ -0,0 +1,8 @@ +#include "PrettyPrinter.h" + +#include + +void PrettyPrinter::Print(const IDataRepository* repository) +{ + std::cout << "Pretty printing..." << std::endl; +} diff --git a/src/ZoneCodeGeneratorNew/Printing/PrettyPrinter.h b/src/ZoneCodeGeneratorNew/Printing/PrettyPrinter.h new file mode 100644 index 00000000..b5ed6660 --- /dev/null +++ b/src/ZoneCodeGeneratorNew/Printing/PrettyPrinter.h @@ -0,0 +1,8 @@ +#pragma once +#include "Persistence/IDataRepository.h" + +class PrettyPrinter +{ +public: + void Print(const IDataRepository* repository); +}; diff --git a/src/ZoneCodeGeneratorNew/ZoneCodeGenerator.cpp b/src/ZoneCodeGeneratorNew/ZoneCodeGenerator.cpp new file mode 100644 index 00000000..9a48ccf2 --- /dev/null +++ b/src/ZoneCodeGeneratorNew/ZoneCodeGenerator.cpp @@ -0,0 +1,107 @@ +#include "ZoneCodeGenerator.h" + +#include +#include +#include +#include + +#include "ZoneCodeGeneratorArguments.h" +#include "Parsing/Commands/CommandsFileReader.h" +#include "Parsing/Header/HeaderFileReader.h" +#include "Persistence/IDataRepository.h" +#include "Persistence/InMemory/InMemoryRepository.h" +#include "Printing/PrettyPrinter.h" + +class ZoneCodeGenerator::Impl +{ + ZoneCodeGeneratorArguments m_args; + std::unique_ptr m_repository; + + bool ReadHeaderData() + { + for (const auto& headerFile : m_args.m_header_paths) + { + HeaderFileReader headerFileReader(&m_args, headerFile); + + if (!headerFileReader.ReadHeaderFile(m_repository.get())) + return false; + } + + return true; + } + + bool ReadCommandsData() + { + for (const auto& commandsFile : m_args.m_command_paths) + { + CommandsFileReader commandsFileReader(&m_args, commandsFile); + + if (!commandsFileReader.ReadCommandsFile(m_repository.get())) + return false; + } + + return true; + } + + void PrintData() const + { + PrettyPrinter prettyPrinter; + prettyPrinter.Print(m_repository.get()); + } + + bool GenerateCode() + { + for(const auto& generationTask : m_args.m_generation_tasks) + { + // TODO: Implement + std::cout << "Generating code for asset \"" << generationTask.m_asset_name << "\" and preset \"" << generationTask.m_preset_name << "\" ..." << std::endl; + } + + return true; + } + +public: + Impl() + { + m_repository = std::make_unique(); + } + + int Run(const int argc, const char** argv) + { + if (!m_args.Parse(argc, argv)) + return 1; + + if (!ReadHeaderData() || !ReadCommandsData()) + return 1; + + switch(m_args.m_task) + { + case ZoneCodeGeneratorArguments::ProcessingTask::PRINT_DATA: + PrintData(); + return 0; + + case ZoneCodeGeneratorArguments::ProcessingTask::GENERATE_CODE: + return GenerateCode() ? 0 : 1; + + default: + std::cout << "Unknown task: " << static_cast(m_args.m_task) << std::endl; + return 2; + } + } +}; + +ZoneCodeGenerator::ZoneCodeGenerator() +{ + m_impl = new Impl(); +} + +ZoneCodeGenerator::~ZoneCodeGenerator() +{ + delete m_impl; + m_impl = nullptr; +} + +int ZoneCodeGenerator::Run(const int argc, const char** argv) const +{ + return m_impl->Run(argc, argv); +} diff --git a/src/ZoneCodeGeneratorNew/ZoneCodeGenerator.h b/src/ZoneCodeGeneratorNew/ZoneCodeGenerator.h new file mode 100644 index 00000000..61f5b341 --- /dev/null +++ b/src/ZoneCodeGeneratorNew/ZoneCodeGenerator.h @@ -0,0 +1,17 @@ +#pragma once + +class ZoneCodeGenerator +{ + class Impl; + Impl* m_impl; + +public: + ZoneCodeGenerator(); + ~ZoneCodeGenerator(); + ZoneCodeGenerator(const ZoneCodeGenerator& other) = delete; + ZoneCodeGenerator(ZoneCodeGenerator&& other) noexcept = default; + ZoneCodeGenerator& operator=(const ZoneCodeGenerator& other) = delete; + ZoneCodeGenerator& operator=(ZoneCodeGenerator&& other) noexcept = default; + + int Run(int argc, const char** argv) const; +}; \ No newline at end of file diff --git a/src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.cpp b/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.cpp similarity index 72% rename from src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.cpp rename to src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.cpp index 8569692d..1b1e3f16 100644 --- a/src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.cpp +++ b/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.cpp @@ -1,5 +1,7 @@ #include "ZoneCodeGeneratorArguments.h" +#include + #include "Utils/Arguments/CommandLineOption.h" #include "Utils/Arguments/UsageInformation.h" @@ -21,26 +23,23 @@ const CommandLineOption* const OPTION_VERBOSE = CommandLineOption::Builder::Crea constexpr const char* CATEGORY_INPUT = "Input"; -const CommandLineOption* const OPTION_CREATE = CommandLineOption::Builder::Create() +const CommandLineOption* const OPTION_HEADER = CommandLineOption::Builder::Create() .WithShortName("h") .WithLongName("header") - .WithDescription("Create a new database from the specified header file.") + .WithDescription("Reads from the specified header file.") .WithCategory(CATEGORY_INPUT) .WithParameter("headerFile") + .Reusable() .Build(); -// ------ -// EDITING -// ------ -constexpr const char* CATEGORY_EDITING = "Editing"; - -const CommandLineOption* const OPTION_EDITING_COMMANDS = CommandLineOption::Builder::Create() - .WithShortName("e") - .WithLongName("editing-commands") - .WithDescription("Specifies the editing command file. Defaults to stdin.") - .WithCategory(CATEGORY_EDITING) - .WithParameter("commandFile") - .Build(); +const CommandLineOption* const OPTION_COMMANDS_FILE = CommandLineOption::Builder::Create() + .WithShortName("c") + .WithLongName("commands-file") + .WithDescription("Specifies the commands file. Defaults to stdin.") + .WithCategory(CATEGORY_INPUT) + .WithParameter("commandFile") + .Reusable() + .Build(); // ------ // OUTPUT @@ -65,7 +64,8 @@ const CommandLineOption* const OPTION_PRINT = CommandLineOption::Builder::Create const CommandLineOption* const OPTION_GENERATE = CommandLineOption::Builder::Create() .WithShortName("g") .WithLongName("generate") - .WithDescription("Generates a specified asset/preset combination. Can be used multiple times. Available presets: " + .WithDescription( + "Generates a specified asset/preset combination. Can be used multiple times. Available presets: " "ZoneLoad, ZoneWrite, AssetStructTests") .WithCategory(CATEGORY_OUTPUT) .WithParameter("assetName") @@ -77,8 +77,8 @@ const CommandLineOption* const COMMAND_LINE_OPTIONS[] { OPTION_HELP, OPTION_VERBOSE, - OPTION_CREATE, - OPTION_EDITING_COMMANDS, + OPTION_HEADER, + OPTION_COMMANDS_FILE, OPTION_OUTPUT_FOLDER, OPTION_PRINT, OPTION_GENERATE @@ -137,32 +137,49 @@ bool ZoneCodeGeneratorArguments::Parse(const int argc, const char** argv) // -o; --output if (m_argument_parser.IsOptionSpecified(OPTION_OUTPUT_FOLDER)) m_output_directory = m_argument_parser.GetValueForOption(OPTION_OUTPUT_FOLDER); + else + m_output_directory = "."; // -h; --header - if (m_argument_parser.IsOptionSpecified(OPTION_CREATE)) - m_header_path = m_argument_parser.GetValueForOption(OPTION_CREATE); + if (m_argument_parser.IsOptionSpecified(OPTION_HEADER)) + { + for (const auto& arg : m_argument_parser.GetParametersForOption(OPTION_HEADER)) + m_header_paths.push_back(arg); + } + else + { + std::cout << "At least one header file must be specified via -h / --header." << std::endl; + return false; + } - // -e; --editing-commands - if (m_argument_parser.IsOptionSpecified(OPTION_EDITING_COMMANDS)) - m_commands_path = m_argument_parser.GetValueForOption(OPTION_EDITING_COMMANDS); + // -c; --commands-file + if (m_argument_parser.IsOptionSpecified(OPTION_COMMANDS_FILE)) + { + for (const auto& arg : m_argument_parser.GetParametersForOption(OPTION_COMMANDS_FILE)) + m_command_paths.push_back(arg); + } + else + { + std::cout << "At least one commands file must be specified via -c / --commands-file." << std::endl; + return false; + } if (m_task == ProcessingTask::GENERATE_CODE) { if (!m_argument_parser.IsOptionSpecified(OPTION_GENERATE)) { - printf("A generate parameter needs to be specified when generating code\n"); + std::cout << "A generate parameter needs to be specified when generating code" << std::endl; PrintUsage(); return false; } const auto generateParameterValues = m_argument_parser.GetParametersForOption(OPTION_GENERATE); - const auto generateCount = generateParameterValues.size() / 2; - for(auto i = 0u; i < generateCount; i++) + for (auto i = 0u; i < generateParameterValues.size(); i+=2) m_generation_tasks.emplace_back(generateParameterValues[i], generateParameterValues[i + 1]); } else if (m_argument_parser.IsOptionSpecified(OPTION_GENERATE)) { - printf("Cannot specify generate parameter when not generating code\n"); + std::cout << "Cannot specify generate parameter when not generating code" << std::endl; PrintUsage(); return false; } diff --git a/src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.h b/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.h similarity index 89% rename from src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.h rename to src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.h index 73bf03e4..fd0e7dbf 100644 --- a/src/ZoneCodeGeneratorNew/Interface/ZoneCodeGeneratorArguments.h +++ b/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.h @@ -31,8 +31,8 @@ public: bool m_verbose; - std::string m_header_path; - std::string m_commands_path; + std::vector m_header_paths; + std::vector m_command_paths; std::string m_output_directory; ProcessingTask m_task; diff --git a/src/ZoneCodeGeneratorNew/main.cpp b/src/ZoneCodeGeneratorNew/main.cpp index 74a6be51..9025da72 100644 --- a/src/ZoneCodeGeneratorNew/main.cpp +++ b/src/ZoneCodeGeneratorNew/main.cpp @@ -1,18 +1,7 @@ -#include -#include - -#include "Interface/Session.h" -#include "Interface/ZoneCodeGeneratorArguments.h" +#include "ZoneCodeGenerator.h" int main(const int argc, const char** argv) { - ZoneCodeGeneratorArguments args; - - if(!args.Parse(argc, argv)) - return 1; - - Session session(args); - - const std::string asdf = "Hello World"; - printf("%s\n", asdf.c_str()); + const ZoneCodeGenerator zoneCodeGenerator; + return zoneCodeGenerator.Run(argc, argv); }