Compare commits
5 Commits
5538e911a6
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c8d565d8b2 | |||
| e8850f37bc | |||
| 19b6306927 | |||
| f38a131146 | |||
| c0e416e030 |
111
machine.hpp
111
machine.hpp
@@ -15,6 +15,7 @@
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
namespace VariadicMachine {
|
||||
@@ -30,10 +31,23 @@ enum : int {
|
||||
// Two Arguments
|
||||
IncrementBy,
|
||||
DecrementBy,
|
||||
MultiplyBy,
|
||||
DivideBy,
|
||||
ModulusBy,
|
||||
SetTo,
|
||||
JumpTo,
|
||||
Print,
|
||||
|
||||
// Registry
|
||||
StoreValueIn,
|
||||
PutValueIn,
|
||||
SetValueIn, // Three Argumens!!
|
||||
|
||||
// Type
|
||||
W,
|
||||
X,
|
||||
Y,
|
||||
Z,
|
||||
AsDigit,
|
||||
AsUnsignedDigit,
|
||||
AsCharacter,
|
||||
@@ -49,39 +63,73 @@ public:
|
||||
|
||||
private:
|
||||
Command State = NoCommand;
|
||||
int selectedRegistry = 0;
|
||||
std::array<int, maxCells> memory;
|
||||
int W = 0, X = 0, Y = 0, Z = 0;
|
||||
int RW = 0, RX = 0, RY = 0, RZ = 0;
|
||||
std::size_t currentPosition = 0;
|
||||
|
||||
void Execute(int command) {
|
||||
switch (State) {
|
||||
case NoCommand:
|
||||
break;
|
||||
|
||||
case IncrementBy:
|
||||
memory[currentPosition] += command;
|
||||
State = NoCommand;
|
||||
processCommand(command);
|
||||
return;
|
||||
|
||||
case DecrementBy:
|
||||
memory[currentPosition] -= command;
|
||||
State = NoCommand;
|
||||
return;
|
||||
// Arithmetic
|
||||
case SetTo:
|
||||
memory[currentPosition] = command;
|
||||
State = NoCommand;
|
||||
break;
|
||||
case IncrementBy:
|
||||
memory[currentPosition] += command;
|
||||
break;
|
||||
case DecrementBy:
|
||||
memory[currentPosition] -= command;
|
||||
break;
|
||||
case MultiplyBy:
|
||||
memory[currentPosition] *= command;
|
||||
break;
|
||||
case DivideBy:
|
||||
memory[currentPosition] /= command;
|
||||
break;
|
||||
case ModulusBy:
|
||||
memory[currentPosition] %= command;
|
||||
break;
|
||||
|
||||
case JumpTo:
|
||||
currentPosition = command;
|
||||
break;
|
||||
|
||||
case StoreValueIn:
|
||||
whichRegistry(command) = memory[currentPosition];
|
||||
break;
|
||||
case PutValueIn:
|
||||
memory[currentPosition] = whichRegistry(command);
|
||||
break;
|
||||
case SetValueIn:
|
||||
if (selectedRegistry == 0) {
|
||||
selectedRegistry = command;
|
||||
return;
|
||||
}
|
||||
whichRegistry(selectedRegistry) = command;
|
||||
selectedRegistry = 0;
|
||||
break;
|
||||
|
||||
case Print:
|
||||
handlePrint(command);
|
||||
State = NoCommand;
|
||||
return;
|
||||
break;
|
||||
|
||||
default:
|
||||
std::terminate();
|
||||
return;
|
||||
}
|
||||
|
||||
// State: NoCommand
|
||||
// This is only called after multi-argument functions are done
|
||||
if (State != NoCommand && selectedRegistry == 0) {
|
||||
State = NoCommand;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void processCommand(int command) {
|
||||
switch (command) {
|
||||
case MoveLeft:
|
||||
// Underflow wrap
|
||||
@@ -103,10 +151,20 @@ private:
|
||||
--memory[currentPosition];
|
||||
return;
|
||||
|
||||
// Double Argument Commands
|
||||
// Multi Argument Commands
|
||||
case Print:
|
||||
case JumpTo:
|
||||
|
||||
case StoreValueIn:
|
||||
case PutValueIn:
|
||||
case SetValueIn:
|
||||
|
||||
case SetTo:
|
||||
case IncrementBy:
|
||||
case DecrementBy:
|
||||
case MultiplyBy:
|
||||
case DivideBy:
|
||||
case ModulusBy:
|
||||
State = static_cast<Command>(command);
|
||||
return;
|
||||
|
||||
@@ -117,6 +175,22 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
int &whichRegistry(int Type) {
|
||||
switch (Type) {
|
||||
case W:
|
||||
return RW;
|
||||
case X:
|
||||
return RX;
|
||||
case Y:
|
||||
return RY;
|
||||
case Z:
|
||||
return RZ;
|
||||
|
||||
default:
|
||||
std::terminate();
|
||||
}
|
||||
}
|
||||
|
||||
void handlePrint(int Type) {
|
||||
switch (Type) {
|
||||
case AsDigit:
|
||||
@@ -135,8 +209,13 @@ private:
|
||||
std::cout << std::bitset<sizeof(int) * 8>(memory[currentPosition]);
|
||||
return;
|
||||
|
||||
// Ugh hate this, I really should use C++23
|
||||
case AsHex:
|
||||
std::cout << std::hex << memory[currentPosition] << std::dec;
|
||||
std::cout << "0x" << std::setfill('0') << std::setw(sizeof(int))
|
||||
<< std::hex << memory[currentPosition];
|
||||
|
||||
// Stupid reset because cout is a dumb dumb thing
|
||||
std::cout << std::dec << std::setfill(' ') << std::setw(0);
|
||||
return;
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user