Improved display code.
Refactored display code into its own function.
This commit is contained in:
11
src/main.cpp
11
src/main.cpp
@@ -25,7 +25,8 @@ int main() {
|
||||
const auto registerState = registerStates[i];
|
||||
|
||||
registers.insert(std::make_pair(i, registerState));
|
||||
std::cout << std::setw(registerIndexPadding) << i << ": " << registerState << "\n";
|
||||
std::cout << std::setw(registerIndexPadding) << i << ": " << registerState << " => ";
|
||||
printInstruction(std::cout, registerState) << std::endl;
|
||||
}
|
||||
|
||||
std::cout << std::setw(registerIndexPadding) << i << ": 0...\n\n";
|
||||
@@ -36,9 +37,9 @@ int main() {
|
||||
while (executing) {
|
||||
std::cout << "L" << pc << "\t";
|
||||
Register instruction = getOrDefault(registers, pc);
|
||||
printInstruction(std::cout, instruction) << std::endl;
|
||||
|
||||
if (instruction == 0) { /* HALT instruction. */
|
||||
std::cout << "HALT" << std::endl;
|
||||
executing = false;
|
||||
continue;
|
||||
}
|
||||
@@ -49,9 +50,6 @@ int main() {
|
||||
RegIndex sourceRegisterIndex = splitInstruction.first.get_ui();
|
||||
RegIndex destinationLabel = splitInstruction.second.get_ui();
|
||||
|
||||
std::cout << "R" << sourceRegisterIndex << u8"\u207a \u21a3 " << destinationLabel
|
||||
<< "\n";
|
||||
|
||||
getOrDefault(registers, sourceRegisterIndex)++;
|
||||
pc = destinationLabel;
|
||||
} else {
|
||||
@@ -61,9 +59,6 @@ int main() {
|
||||
RegIndex trueLabel = branches.first.get_ui();
|
||||
RegIndex falseLabel = branches.second.get_ui();
|
||||
|
||||
std::cout << "R" << sourceRegisterIndex << "\u207b \u21a3 " << trueLabel << ", "
|
||||
<< falseLabel << "\n";
|
||||
|
||||
auto& sourceRegister = getOrDefault(registers, sourceRegisterIndex);
|
||||
|
||||
if (sourceRegister > 0) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "register.hpp"
|
||||
#include <iostream>
|
||||
|
||||
template <>
|
||||
std::pair<Register, Register> toPair<false>(Register unsplit) {
|
||||
@@ -41,3 +42,20 @@ std::vector<Register> toList(Register unsplit) {
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
std::ostream& printInstruction(std::ostream& sout, Register instruction) {
|
||||
if (instruction == 0)
|
||||
return sout << "BREAK";
|
||||
|
||||
const auto splitInstruction = toPair<false>(instruction);
|
||||
const auto command = splitInstruction.first / 2;
|
||||
|
||||
if (splitInstruction.first % 2 == 0) {
|
||||
/* We have an increment instruction, so second parameter is just label. */
|
||||
return sout << "R" << command << "\u207a \u21a3 " << splitInstruction.second;
|
||||
}
|
||||
|
||||
const auto labels = toPair<true>(splitInstruction.second);
|
||||
|
||||
return sout << "R" << command << "\u207b \u21a3 " << labels.first << ", " << labels.second;
|
||||
}
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
#include <gmpxx.h>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <ios>
|
||||
|
||||
using Register = mpz_class;
|
||||
using RegIndex = uint16_t;
|
||||
|
||||
template <bool mapZero>
|
||||
std::pair<Register, Register> toPair(Register unsplit);
|
||||
|
||||
std::vector<Register> toList(Register unsplit);
|
||||
std::ostream& printInstruction(std::ostream& sout, Register instruction);
|
||||
|
||||
Reference in New Issue
Block a user