Implement AST Observer and AST printer
This commit is contained in:
23
src/codegen/PrintObserver.cpp
Normal file
23
src/codegen/PrintObserver.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by christoph on 21.03.23.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "PrintObserver.h"
|
||||
|
||||
PrintObserver::PrintObserver(const Node &node) : PrefixObserver(node) {}
|
||||
|
||||
void PrintObserver::action(const Node &node) {
|
||||
// Print a simple indent guide
|
||||
std::string depth_padding(depth * 2, '|');
|
||||
if (depth > 0) {
|
||||
for (uint32_t i = 0; i < depth_padding.length(); ++i) {
|
||||
if (i % 2 == 1) {
|
||||
depth_padding[i] = ' ';
|
||||
}
|
||||
}
|
||||
depth_padding[(depth * 2) - 1] = '-';
|
||||
}
|
||||
|
||||
std::cout << depth_padding << typeid(node).name() << std::endl;
|
||||
}
|
||||
20
src/codegen/PrintObserver.h
Normal file
20
src/codegen/PrintObserver.h
Normal file
@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by christoph on 21.03.23.
|
||||
//
|
||||
|
||||
#ifndef LOGISIMASSEMBLER_PRINTOBSERVER_H
|
||||
#define LOGISIMASSEMBLER_PRINTOBSERVER_H
|
||||
|
||||
#include "../ast/PrefixObserver.h"
|
||||
|
||||
class PrintObserver : public PrefixObserver {
|
||||
public:
|
||||
PrintObserver(const Node &node);
|
||||
|
||||
~PrintObserver() override = default;
|
||||
|
||||
protected:
|
||||
void action(const Node &node) override;
|
||||
};
|
||||
|
||||
#endif //LOGISIMASSEMBLER_PRINTOBSERVER_H
|
||||
Reference in New Issue
Block a user