From 2112bdedbf1ce7abbdd09799b67970584e805791 Mon Sep 17 00:00:00 2001 From: cat Date: Mon, 9 Feb 2026 18:11:45 +0200 Subject: [PATCH] First working machine --- CMakeLists.txt | 39 ----------------- headers/machine.cpp | 8 ---- machine.hpp | 101 ++++++++++++++++++++++++++++++++++++++++++++ source/machine.hpp | 8 ---- 4 files changed, 101 insertions(+), 55 deletions(-) delete mode 100644 CMakeLists.txt delete mode 100644 headers/machine.cpp create mode 100644 machine.hpp delete mode 100644 source/machine.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index d2cc0f8..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,39 +0,0 @@ -# SPDX-FileCopyrightText: Dora "cat" -# SPDX-License-Identifier: MPL-2.0 -# -# This Source Code Form is subject to the terms of the Mozilla Public License, -# v. 2.0. If a copy of the MPL was not distributed with this file, You can -# obtain one at http://mozilla.org/MPL/2.0/. - -cmake_minimum_required(VERSION 3.10) - -project(VariadicMachine VERSION 1) - -set(CMAKE_CXX_STANDARD 23) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") -if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address") -endif() - -include(GNUInstallDirs) - -add_library(${PROJECT_NAME} SHARED - "source/machine.cpp") - -set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) -target_include_directories(${PROJECT_NAME} - PUBLIC - $ - $ - $ -) - -install( - TARGETS ${PROJECT_NAME} - EXPORT ${PROJECT_NAME}Targets - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib - INCLUDES DESTINATION include -) - -install(DIRECTORY headers/ DESTINATION include/${PROJECT_NAME}) diff --git a/headers/machine.cpp b/headers/machine.cpp deleted file mode 100644 index f546a8b..0000000 --- a/headers/machine.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/* - * SPDX-FileCopyrightText: Dora "cat" - * SPDX-License-Identifier: MPL-2.0 - * - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. - */ diff --git a/machine.hpp b/machine.hpp new file mode 100644 index 0000000..54fd372 --- /dev/null +++ b/machine.hpp @@ -0,0 +1,101 @@ +/* + * SPDX-FileCopyrightText: Dora "cat" + * SPDX-License-Identifier: MPL-2.0 + * + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef GUARD_VARIADICMACHINE_MACHINE_H +#define GUARD_VARIADICMACHINE_MACHINE_H + +#include +#include +#include +#include +#include +#include + +namespace VariadicMachine { +enum Command : int16_t { + // Single Argument + NoCommand, + MoveLeft, + MoveRight, + Increment, + Decrement, + PrintAsCharacter, + + // Two Arguments + IncrementBy, + DecrementBy +}; + +template class VMachine { +public: + template constexpr void Do(Commands &&...commands) { + (Execute(commands), ...); + } + +private: + Command State = Command::NoCommand; + std::array memory; + std::int16_t W = 0, X = 0, Y = 0, Z = 0; + std::size_t maxCellLimit = maxCells, currentPosition = 0; + + void Execute(int16_t command) { + switch (State) { + case Command::IncrementBy: + memory[currentPosition] += command; + State = Command::NoCommand; + return; + + case Command::DecrementBy: + memory[currentPosition] -= command; + State = Command::NoCommand; + return; + + default: + std::terminate(); + break; + + case Command::NoCommand: + break; + } + + // State: NoCommand + switch (command) { + case Command::MoveLeft: + --currentPosition; + return; + case Command::MoveRight: + ++currentPosition; + return; + + case Command::Increment: + ++memory[currentPosition]; + return; + case Command::Decrement: + --memory[currentPosition]; + return; + + case Command::PrintAsCharacter: + std::wcout << static_cast(memory[currentPosition]); + return; + + // Double Argument Commands + case Command::IncrementBy: + case Command::DecrementBy: + State = static_cast(command); + return; + + default: + std::cout << "Here\n"; + std::terminate(); + return; + } + } +}; +} // namespace VariadicMachine +#endif diff --git a/source/machine.hpp b/source/machine.hpp deleted file mode 100644 index f546a8b..0000000 --- a/source/machine.hpp +++ /dev/null @@ -1,8 +0,0 @@ -/* - * SPDX-FileCopyrightText: Dora "cat" - * SPDX-License-Identifier: MPL-2.0 - * - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. - */