150 lines
2.9 KiB
C++
150 lines
2.9 KiB
C++
/*
|
|
* 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 <bitset>
|
|
#include <cstdarg>
|
|
#include <cstddef>
|
|
#include <exception>
|
|
#include <iostream>
|
|
|
|
namespace VariadicMachine {
|
|
using Command = int;
|
|
enum : int {
|
|
// Single Argument
|
|
NoCommand,
|
|
MoveLeft,
|
|
MoveRight,
|
|
Increment,
|
|
Decrement,
|
|
|
|
// Two Arguments
|
|
IncrementBy,
|
|
DecrementBy,
|
|
SetTo,
|
|
Print,
|
|
|
|
// Type
|
|
AsDigit,
|
|
AsUnsignedDigit,
|
|
AsCharacter,
|
|
AsBinary,
|
|
AsHex,
|
|
};
|
|
|
|
template <std::size_t maxCells = 2048> class VMachine {
|
|
public:
|
|
template <typename... Commands> constexpr void Do(Commands &&...commands) {
|
|
(Execute(commands), ...);
|
|
}
|
|
|
|
private:
|
|
Command State = NoCommand;
|
|
std::array<int, maxCells> memory;
|
|
int W = 0, X = 0, Y = 0, Z = 0;
|
|
std::size_t currentPosition = 0;
|
|
|
|
void Execute(int command) {
|
|
switch (State) {
|
|
case NoCommand:
|
|
break;
|
|
|
|
case IncrementBy:
|
|
memory[currentPosition] += command;
|
|
State = NoCommand;
|
|
return;
|
|
|
|
case DecrementBy:
|
|
memory[currentPosition] -= command;
|
|
State = NoCommand;
|
|
return;
|
|
case SetTo:
|
|
memory[currentPosition] = command;
|
|
State = NoCommand;
|
|
return;
|
|
case Print:
|
|
handlePrint(command);
|
|
State = NoCommand;
|
|
return;
|
|
|
|
default:
|
|
std::terminate();
|
|
return;
|
|
}
|
|
|
|
// State: NoCommand
|
|
switch (command) {
|
|
case MoveLeft:
|
|
// Underflow wrap
|
|
if (--currentPosition > maxCells) {
|
|
currentPosition = maxCells;
|
|
}
|
|
return;
|
|
case MoveRight:
|
|
// Overflow wrap
|
|
if (++currentPosition > maxCells) {
|
|
currentPosition = 0;
|
|
}
|
|
return;
|
|
|
|
case Increment:
|
|
++memory[currentPosition];
|
|
return;
|
|
case Decrement:
|
|
--memory[currentPosition];
|
|
return;
|
|
|
|
// Double Argument Commands
|
|
case Print:
|
|
case IncrementBy:
|
|
case DecrementBy:
|
|
State = static_cast<Command>(command);
|
|
return;
|
|
|
|
default:
|
|
std::cout << "Here\n";
|
|
std::terminate();
|
|
return;
|
|
}
|
|
}
|
|
|
|
void handlePrint(int Type) {
|
|
switch (Type) {
|
|
case AsDigit:
|
|
std::cout << memory[currentPosition];
|
|
return;
|
|
|
|
case AsUnsignedDigit:
|
|
std::cout << static_cast<unsigned int>(memory[currentPosition]);
|
|
return;
|
|
|
|
case AsCharacter:
|
|
std::wcout << static_cast<wchar_t>(memory[currentPosition]);
|
|
return;
|
|
|
|
case AsBinary:
|
|
std::cout << std::bitset<sizeof(int) * 8>(memory[currentPosition]);
|
|
return;
|
|
|
|
case AsHex:
|
|
std::cout << std::hex << memory[currentPosition] << std::dec;
|
|
return;
|
|
|
|
default:
|
|
std::terminate();
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
} // namespace VariadicMachine
|
|
#endif
|