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

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