Implement AST Observer and AST printer
This commit is contained in:
11
src/ast/Observer.cpp
Normal file
11
src/ast/Observer.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
//
|
||||||
|
// Created by christoph on 21.03.23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Observer.h"
|
||||||
|
|
||||||
|
Observer::Observer(const Node &root) : root(root) {}
|
||||||
|
|
||||||
|
void Observer::traverse() {
|
||||||
|
traverse(root);
|
||||||
|
}
|
||||||
28
src/ast/Observer.h
Normal file
28
src/ast/Observer.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
//
|
||||||
|
// Created by christoph on 21.03.23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef LOGISIMASSEMBLER_OBSERVER_H
|
||||||
|
#define LOGISIMASSEMBLER_OBSERVER_H
|
||||||
|
|
||||||
|
#include "Node.h"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
class Observer {
|
||||||
|
public:
|
||||||
|
Observer(const Node &root);
|
||||||
|
|
||||||
|
virtual ~Observer() = default;
|
||||||
|
|
||||||
|
void traverse();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void traverse(const Node &node) = 0;
|
||||||
|
|
||||||
|
virtual void action(const Node &node) = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const Node &root;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //LOGISIMASSEMBLER_OBSERVER_H
|
||||||
18
src/ast/PrefixObserver.cpp
Normal file
18
src/ast/PrefixObserver.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Created by christoph on 20.03.23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "PrefixObserver.h"
|
||||||
|
|
||||||
|
PrefixObserver::PrefixObserver(const Node &root) : Observer(root) {}
|
||||||
|
|
||||||
|
// TODO: Shouldn't be recursive
|
||||||
|
void PrefixObserver::traverse(const Node &node) {
|
||||||
|
action(node);
|
||||||
|
|
||||||
|
for (const auto &child : node.getChildren()) {
|
||||||
|
depth++;
|
||||||
|
traverse(*child);
|
||||||
|
depth--;
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/ast/PrefixObserver.h
Normal file
23
src/ast/PrefixObserver.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
//
|
||||||
|
// Created by christoph on 20.03.23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef LOGISIMASSEMBLER_PREFIXOBSERVER_H
|
||||||
|
#define LOGISIMASSEMBLER_PREFIXOBSERVER_H
|
||||||
|
|
||||||
|
#include "Observer.h"
|
||||||
|
|
||||||
|
class PrefixObserver : public Observer {
|
||||||
|
public:
|
||||||
|
PrefixObserver(const Node &root);
|
||||||
|
|
||||||
|
~PrefixObserver() override = default;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void traverse(const Node &node) override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
uint32_t depth = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //LOGISIMASSEMBLER_PREFIXOBSERVER_H
|
||||||
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