Compare commits

..

2 Commits

Author SHA1 Message Date
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 <cstdarg>
#include <cstddef> #include <cstddef>
#include <exception> #include <exception>
#include <iomanip>
#include <iostream> #include <iostream>
namespace VariadicMachine { namespace VariadicMachine {
@@ -30,6 +31,9 @@ enum : int {
// Two Arguments // Two Arguments
IncrementBy, IncrementBy,
DecrementBy, DecrementBy,
MultiplyBy,
DivideBy,
ModulusBy,
SetTo, SetTo,
Print, Print,
@@ -56,21 +60,29 @@ private:
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: // Arithmatic
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 Print: case Print:
handlePrint(command); handlePrint(command);
State = NoCommand; State = NoCommand;
@@ -81,7 +93,14 @@ private:
return; return;
} }
// State: NoCommand // This is only called after multi-argument functions are done
if (State != NoCommand) {
State = NoCommand;
return;
}
}
void processCommand(int command) {
switch (command) { switch (command) {
case MoveLeft: case MoveLeft:
// Underflow wrap // Underflow wrap
@@ -105,8 +124,12 @@ private:
// Double Argument Commands // Double Argument Commands
case Print: case Print:
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;
@@ -135,8 +158,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: