Implement codegen using an Observer
This commit is contained in:
20
src/codegen/CodegenObserver.cpp
Normal file
20
src/codegen/CodegenObserver.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by christoph on 21.03.23.
|
||||
//
|
||||
|
||||
#include "CodegenObserver.h"
|
||||
#include <boost/format.hpp>
|
||||
#include <iostream>
|
||||
|
||||
CodegenObserver::CodegenObserver(const Node &node, std::string &output_string)
|
||||
: PostfixObserver(node), output_string(output_string) {}
|
||||
|
||||
void CodegenObserver::action(const Node &node) {
|
||||
const uint8_t opcode = node.compile();
|
||||
const uint8_t INVALID = -1;
|
||||
|
||||
if (opcode != INVALID) {
|
||||
output_string += (boost::format("%x") % static_cast<uint32_t>(opcode)).str(); // uint8_t is always interpreted as char
|
||||
output_string += ' ';
|
||||
}
|
||||
}
|
||||
23
src/codegen/CodegenObserver.h
Normal file
23
src/codegen/CodegenObserver.h
Normal file
@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by christoph on 21.03.23.
|
||||
//
|
||||
|
||||
#ifndef LOGISIMASSEMBLER_CODEGENOBSERVER_H
|
||||
#define LOGISIMASSEMBLER_CODEGENOBSERVER_H
|
||||
|
||||
#include "../ast/PostfixObserver.h"
|
||||
|
||||
class CodegenObserver : public PostfixObserver {
|
||||
public:
|
||||
CodegenObserver(const Node &node, std::string &output_string);
|
||||
|
||||
~CodegenObserver() override = default;
|
||||
|
||||
protected:
|
||||
void action(const Node &node) override;
|
||||
|
||||
private:
|
||||
std::string &output_string;
|
||||
};
|
||||
|
||||
#endif //LOGISIMASSEMBLER_CODEGENOBSERVER_H
|
||||
Reference in New Issue
Block a user