2nd Iteration of the engine
int16_t to int, made all the enums anonymous, broken print, etc...
This commit is contained in:
106
machine.hpp
106
machine.hpp
@@ -11,25 +11,34 @@
|
|||||||
#define GUARD_VARIADICMACHINE_MACHINE_H
|
#define GUARD_VARIADICMACHINE_MACHINE_H
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <bitset>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace VariadicMachine {
|
namespace VariadicMachine {
|
||||||
enum Command : int16_t {
|
using Command = int;
|
||||||
|
enum : int {
|
||||||
// Single Argument
|
// Single Argument
|
||||||
NoCommand,
|
NoCommand,
|
||||||
MoveLeft,
|
MoveLeft,
|
||||||
MoveRight,
|
MoveRight,
|
||||||
Increment,
|
Increment,
|
||||||
Decrement,
|
Decrement,
|
||||||
PrintAsCharacter,
|
|
||||||
|
|
||||||
// Two Arguments
|
// Two Arguments
|
||||||
IncrementBy,
|
IncrementBy,
|
||||||
DecrementBy
|
DecrementBy,
|
||||||
|
SetTo,
|
||||||
|
Print,
|
||||||
|
|
||||||
|
// Type
|
||||||
|
AsDigit,
|
||||||
|
AsUnsignedDigit,
|
||||||
|
AsCharacter,
|
||||||
|
AsBinary,
|
||||||
|
AsHex,
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t maxCells = 2048> class VMachine {
|
template <std::size_t maxCells = 2048> class VMachine {
|
||||||
@@ -39,54 +48,65 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Command State = Command::NoCommand;
|
Command State = NoCommand;
|
||||||
std::array<std::int16_t, maxCells> memory;
|
std::array<int, maxCells> memory;
|
||||||
std::int16_t W = 0, X = 0, Y = 0, Z = 0;
|
int W = 0, X = 0, Y = 0, Z = 0;
|
||||||
std::size_t maxCellLimit = maxCells, currentPosition = 0;
|
std::size_t currentPosition = 0;
|
||||||
|
|
||||||
void Execute(int16_t command) {
|
void Execute(int command) {
|
||||||
switch (State) {
|
switch (State) {
|
||||||
case Command::IncrementBy:
|
case NoCommand:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IncrementBy:
|
||||||
memory[currentPosition] += command;
|
memory[currentPosition] += command;
|
||||||
State = Command::NoCommand;
|
State = NoCommand;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case Command::DecrementBy:
|
case DecrementBy:
|
||||||
memory[currentPosition] -= command;
|
memory[currentPosition] -= command;
|
||||||
State = Command::NoCommand;
|
State = NoCommand;
|
||||||
|
return;
|
||||||
|
case SetTo:
|
||||||
|
memory[currentPosition] = command;
|
||||||
|
State = NoCommand;
|
||||||
|
return;
|
||||||
|
case Print:
|
||||||
|
handlePrint(command);
|
||||||
|
State = NoCommand;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
std::terminate();
|
std::terminate();
|
||||||
break;
|
return;
|
||||||
|
|
||||||
case Command::NoCommand:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// State: NoCommand
|
// State: NoCommand
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case Command::MoveLeft:
|
case MoveLeft:
|
||||||
--currentPosition;
|
// Underflow wrap
|
||||||
|
if (--currentPosition > maxCells) {
|
||||||
|
currentPosition = maxCells;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
case Command::MoveRight:
|
case MoveRight:
|
||||||
++currentPosition;
|
// Overflow wrap
|
||||||
|
if (++currentPosition > maxCells) {
|
||||||
|
currentPosition = 0;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case Command::Increment:
|
case Increment:
|
||||||
++memory[currentPosition];
|
++memory[currentPosition];
|
||||||
return;
|
return;
|
||||||
case Command::Decrement:
|
case Decrement:
|
||||||
--memory[currentPosition];
|
--memory[currentPosition];
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case Command::PrintAsCharacter:
|
|
||||||
std::wcout << static_cast<wchar_t>(memory[currentPosition]);
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Double Argument Commands
|
// Double Argument Commands
|
||||||
case Command::IncrementBy:
|
case Print:
|
||||||
case Command::DecrementBy:
|
case IncrementBy:
|
||||||
|
case DecrementBy:
|
||||||
State = static_cast<Command>(command);
|
State = static_cast<Command>(command);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -96,6 +116,34 @@ private:
|
|||||||
return;
|
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
|
} // namespace VariadicMachine
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user