Compare commits
3 Commits
f38a131146
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c8d565d8b2 | |||
| e8850f37bc | |||
| 19b6306927 |
63
machine.hpp
63
machine.hpp
@@ -35,9 +35,19 @@ enum : int {
|
||||
DivideBy,
|
||||
ModulusBy,
|
||||
SetTo,
|
||||
JumpTo,
|
||||
Print,
|
||||
|
||||
// Registry
|
||||
StoreValueIn,
|
||||
PutValueIn,
|
||||
SetValueIn, // Three Argumens!!
|
||||
|
||||
// Type
|
||||
W,
|
||||
X,
|
||||
Y,
|
||||
Z,
|
||||
AsDigit,
|
||||
AsUnsignedDigit,
|
||||
AsCharacter,
|
||||
@@ -53,8 +63,9 @@ 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) {
|
||||
@@ -63,7 +74,7 @@ private:
|
||||
processCommand(command);
|
||||
return;
|
||||
|
||||
// Arithmatic
|
||||
// Arithmetic
|
||||
case SetTo:
|
||||
memory[currentPosition] = command;
|
||||
break;
|
||||
@@ -83,10 +94,28 @@ private:
|
||||
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();
|
||||
@@ -94,7 +123,7 @@ private:
|
||||
}
|
||||
|
||||
// This is only called after multi-argument functions are done
|
||||
if (State != NoCommand) {
|
||||
if (State != NoCommand && selectedRegistry == 0) {
|
||||
State = NoCommand;
|
||||
return;
|
||||
}
|
||||
@@ -122,8 +151,14 @@ 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:
|
||||
@@ -140,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:
|
||||
|
||||
Reference in New Issue
Block a user