From d7df0c04e78d8f31848fc3f1e590239fbb212225 Mon Sep 17 00:00:00 2001 From: Michael Kuc Date: Sat, 23 Nov 2019 21:29:00 +0000 Subject: [PATCH] Improved display code. Refactored display code into its own function. --- src/main.cpp | 11 +++-------- src/register.cpp | 18 ++++++++++++++++++ src/register.hpp | 3 ++- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 631215a..b877657 100644 --- a/src/main.cpp +++ b/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) { diff --git a/src/register.cpp b/src/register.cpp index 1da8f93..bdc8c7f 100644 --- a/src/register.cpp +++ b/src/register.cpp @@ -1,4 +1,5 @@ #include "register.hpp" +#include template <> std::pair toPair(Register unsplit) { @@ -41,3 +42,20 @@ std::vector toList(Register unsplit) { return list; } + +std::ostream& printInstruction(std::ostream& sout, Register instruction) { + if (instruction == 0) + return sout << "BREAK"; + + const auto splitInstruction = toPair(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(splitInstruction.second); + + return sout << "R" << command << "\u207b \u21a3 " << labels.first << ", " << labels.second; +} diff --git a/src/register.hpp b/src/register.hpp index 5acf69c..f04e407 100644 --- a/src/register.hpp +++ b/src/register.hpp @@ -4,11 +4,12 @@ #include #include #include +#include using Register = mpz_class; using RegIndex = uint16_t; template std::pair toPair(Register unsplit); - std::vector toList(Register unsplit); +std::ostream& printInstruction(std::ostream& sout, Register instruction);