Compare commits

...

5 Commits

Author SHA1 Message Date
cat
c8d565d8b2 Setting values on registries 2026-02-10 02:41:29 +02:00
cat
e8850f37bc Added storing and outputting values in registry 2026-02-10 02:27:12 +02:00
cat
19b6306927 Added JumpTo 2026-02-10 02:17:08 +02:00
cat
f38a131146 More Arithmetic, Better sectioning of code 2026-02-10 02:10:58 +02:00
cat
c0e416e030 Fixed width hex output 2026-02-10 02:01:30 +02:00

View File

@@ -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;
return;
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: