Compare commits
5 Commits
5538e911a6
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c8d565d8b2 | |||
| e8850f37bc | |||
| 19b6306927 | |||
| f38a131146 | |||
| c0e416e030 |
113
machine.hpp
113
machine.hpp
@@ -15,6 +15,7 @@
|
|||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace VariadicMachine {
|
namespace VariadicMachine {
|
||||||
@@ -30,10 +31,23 @@ enum : int {
|
|||||||
// Two Arguments
|
// Two Arguments
|
||||||
IncrementBy,
|
IncrementBy,
|
||||||
DecrementBy,
|
DecrementBy,
|
||||||
|
MultiplyBy,
|
||||||
|
DivideBy,
|
||||||
|
ModulusBy,
|
||||||
SetTo,
|
SetTo,
|
||||||
|
JumpTo,
|
||||||
Print,
|
Print,
|
||||||
|
|
||||||
|
// Registry
|
||||||
|
StoreValueIn,
|
||||||
|
PutValueIn,
|
||||||
|
SetValueIn, // Three Argumens!!
|
||||||
|
|
||||||
// Type
|
// Type
|
||||||
|
W,
|
||||||
|
X,
|
||||||
|
Y,
|
||||||
|
Z,
|
||||||
AsDigit,
|
AsDigit,
|
||||||
AsUnsignedDigit,
|
AsUnsignedDigit,
|
||||||
AsCharacter,
|
AsCharacter,
|
||||||
@@ -49,39 +63,73 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Command State = NoCommand;
|
Command State = NoCommand;
|
||||||
|
int selectedRegistry = 0;
|
||||||
std::array<int, maxCells> memory;
|
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;
|
std::size_t currentPosition = 0;
|
||||||
|
|
||||||
void Execute(int command) {
|
void Execute(int command) {
|
||||||
switch (State) {
|
switch (State) {
|
||||||
case NoCommand:
|
case NoCommand:
|
||||||
break;
|
processCommand(command);
|
||||||
|
|
||||||
case IncrementBy:
|
|
||||||
memory[currentPosition] += command;
|
|
||||||
State = NoCommand;
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case DecrementBy:
|
// Arithmetic
|
||||||
memory[currentPosition] -= command;
|
|
||||||
State = NoCommand;
|
|
||||||
return;
|
|
||||||
case SetTo:
|
case SetTo:
|
||||||
memory[currentPosition] = command;
|
memory[currentPosition] = command;
|
||||||
State = NoCommand;
|
break;
|
||||||
return;
|
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:
|
case Print:
|
||||||
handlePrint(command);
|
handlePrint(command);
|
||||||
State = NoCommand;
|
break;
|
||||||
return;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
std::terminate();
|
std::terminate();
|
||||||
return;
|
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) {
|
switch (command) {
|
||||||
case MoveLeft:
|
case MoveLeft:
|
||||||
// Underflow wrap
|
// Underflow wrap
|
||||||
@@ -103,10 +151,20 @@ private:
|
|||||||
--memory[currentPosition];
|
--memory[currentPosition];
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Double Argument Commands
|
// Multi Argument Commands
|
||||||
case Print:
|
case Print:
|
||||||
|
case JumpTo:
|
||||||
|
|
||||||
|
case StoreValueIn:
|
||||||
|
case PutValueIn:
|
||||||
|
case SetValueIn:
|
||||||
|
|
||||||
|
case SetTo:
|
||||||
case IncrementBy:
|
case IncrementBy:
|
||||||
case DecrementBy:
|
case DecrementBy:
|
||||||
|
case MultiplyBy:
|
||||||
|
case DivideBy:
|
||||||
|
case ModulusBy:
|
||||||
State = static_cast<Command>(command);
|
State = static_cast<Command>(command);
|
||||||
return;
|
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) {
|
void handlePrint(int Type) {
|
||||||
switch (Type) {
|
switch (Type) {
|
||||||
case AsDigit:
|
case AsDigit:
|
||||||
@@ -135,8 +209,13 @@ private:
|
|||||||
std::cout << std::bitset<sizeof(int) * 8>(memory[currentPosition]);
|
std::cout << std::bitset<sizeof(int) * 8>(memory[currentPosition]);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// Ugh hate this, I really should use C++23
|
||||||
case AsHex:
|
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;
|
return;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user