Update the CodegenObserver to return a vector
This commit is contained in:
@ -6,15 +6,19 @@
|
|||||||
#include <boost/format.hpp>
|
#include <boost/format.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
CodegenObserver::CodegenObserver(const Node &node, std::string &output_string)
|
CodegenObserver::CodegenObserver(const Node &node, std::vector<std::string> &output_string)
|
||||||
: PostfixObserver(node), output_string(output_string) {}
|
: PostfixObserver(node), output_string(output_string) {}
|
||||||
|
|
||||||
void CodegenObserver::action(const Node &node) {
|
void CodegenObserver::action(const Node &node) {
|
||||||
const uint8_t opcode = node.compile();
|
const uint8_t dec = node.compile();
|
||||||
const uint8_t INVALID = -1;
|
const uint8_t INVALID = -1;
|
||||||
|
|
||||||
if (opcode != INVALID) {
|
if (dec != INVALID) {
|
||||||
output_string += (boost::format("%x") % static_cast<uint32_t>(opcode)).str(); // uint8_t is always interpreted as char
|
// uint8_t is always interpreted as char, so cast to uint32_t
|
||||||
output_string += ' ';
|
const std::string hex = (boost::format("%x") % static_cast<uint32_t>(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
class CodegenObserver : public PostfixObserver {
|
class CodegenObserver : public PostfixObserver {
|
||||||
public:
|
public:
|
||||||
CodegenObserver(const Node &node, std::string &output_string);
|
CodegenObserver(const Node &node, std::vector<std::string> &output_string);
|
||||||
|
|
||||||
~CodegenObserver() override = default;
|
~CodegenObserver() override = default;
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ protected:
|
|||||||
void action(const Node &node) override;
|
void action(const Node &node) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string &output_string;
|
std::vector<std::string> &output_string;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //LOGISIMASSEMBLER_CODEGENOBSERVER_H
|
#endif //LOGISIMASSEMBLER_CODEGENOBSERVER_H
|
||||||
|
|||||||
Reference in New Issue
Block a user