First working machine

This commit is contained in:
2026-02-09 18:11:45 +02:00
parent 554abbe8f6
commit 2112bdedbf
4 changed files with 101 additions and 55 deletions

View File

@@ -1,39 +0,0 @@
# SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
# 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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/headers>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
)
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
INCLUDES DESTINATION include
)
install(DIRECTORY headers/ DESTINATION include/${PROJECT_NAME})

View File

@@ -1,8 +0,0 @@
/*
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
* 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/.
*/

101
machine.hpp Normal file
View File

@@ -0,0 +1,101 @@
/*
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
* 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 <array>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <iostream>
namespace VariadicMachine {
enum Command : int16_t {
// Single Argument
NoCommand,
MoveLeft,
MoveRight,
Increment,
Decrement,
PrintAsCharacter,
// Two Arguments
IncrementBy,
DecrementBy
};
template <std::size_t maxCells = 2048> class VMachine {
public:
template <typename... Commands> constexpr void Do(Commands &&...commands) {
(Execute(commands), ...);
}
private:
Command State = Command::NoCommand;
std::array<std::int16_t, maxCells> 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<wchar_t>(memory[currentPosition]);
return;
// Double Argument Commands
case Command::IncrementBy:
case Command::DecrementBy:
State = static_cast<Command>(command);
return;
default:
std::cout << "Here\n";
std::terminate();
return;
}
}
};
} // namespace VariadicMachine
#endif

View File

@@ -1,8 +0,0 @@
/*
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
* 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/.
*/