From 2ab33ed85bb0c6b6e201cb8409c1c19b6579ccad Mon Sep 17 00:00:00 2001 From: ChUrl Date: Tue, 21 Mar 2023 16:27:55 +0100 Subject: [PATCH] Update the CodegenObserver to return a vector --- src/codegen/CodegenObserver.cpp | 14 +++++++++----- src/codegen/CodegenObserver.h | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/codegen/CodegenObserver.cpp b/src/codegen/CodegenObserver.cpp index 1b9640c..93d80b0 100644 --- a/src/codegen/CodegenObserver.cpp +++ b/src/codegen/CodegenObserver.cpp @@ -6,15 +6,19 @@ #include #include -CodegenObserver::CodegenObserver(const Node &node, std::string &output_string) +CodegenObserver::CodegenObserver(const Node &node, std::vector &output_string) : PostfixObserver(node), output_string(output_string) {} void CodegenObserver::action(const Node &node) { - const uint8_t opcode = node.compile(); + const uint8_t dec = node.compile(); const uint8_t INVALID = -1; - if (opcode != INVALID) { - output_string += (boost::format("%x") % static_cast(opcode)).str(); // uint8_t is always interpreted as char - output_string += ' '; + if (dec != INVALID) { + // uint8_t is always interpreted as char, so cast to uint32_t + const std::string hex = (boost::format("%x") % static_cast(dec)).str(); + if (hex.empty() || hex.size() > 2) { + throw "Compile Error: Resulting instruction has invalid size!"; + } + output_string.push_back(hex.length() == 2 ? hex : "0" + hex); } } diff --git a/src/codegen/CodegenObserver.h b/src/codegen/CodegenObserver.h index 9f916d9..551f8bd 100644 --- a/src/codegen/CodegenObserver.h +++ b/src/codegen/CodegenObserver.h @@ -9,7 +9,7 @@ class CodegenObserver : public PostfixObserver { public: - CodegenObserver(const Node &node, std::string &output_string); + CodegenObserver(const Node &node, std::vector &output_string); ~CodegenObserver() override = default; @@ -17,7 +17,7 @@ protected: void action(const Node &node) override; private: - std::string &output_string; + std::vector &output_string; }; #endif //LOGISIMASSEMBLER_CODEGENOBSERVER_H