Skip to content

Commit

Permalink
♻️ Reimplement tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharp0802 committed Jan 8, 2025
1 parent f001b12 commit 0028558
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 111 deletions.
123 changes: 123 additions & 0 deletions mllif/Frontend/MLIR/include/mllif/Frontend/MLIR/Tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#pragma once

#include <deque>

namespace mllif::mlir {

class Node final {
std::string _tag;
std::string _name;
std::vector<std::pair<std::string, std::string>> _attributes;
std::vector<Node> _children;

static std::string Escape(std::string s) {
std::stringstream ss;
for (const auto c : s) {
if (!std::isprint(c)) {
ss << "&#" << static_cast<int>(c) << ';';
}

switch (c) {
case '"':
ss << "&quot;";
break;
case '\'':
ss << "&apos;";
break;
case '<':
ss << "&lt;";
break;
case '>':
ss << "&gt;";
break;
case '&':
ss << "&amp;";
break;
default:
ss << c;
break;
}
}
return ss.str();
}

public:
explicit Node(std::string tag, std::string name) : _tag(std::move(tag)), _name(std::move(name)) {}

std::string &tag() { return _tag; }
std::string tag() const { return _tag; }

std::string name() const { return _name; }

std::vector<std::pair<std::string, std::string>> &attributes() { return _attributes; }
const std::vector<std::pair<std::string, std::string>> &attributes() const { return _attributes; }

std::vector<Node> &children() { return _children; }
const std::vector<Node> &children() const { return _children; }

void print(llvm::raw_ostream &os) const {
os << '<' << tag();

if (name().size()) {
os << " id=\"" << Escape(name()) << '"';
}

if (attributes().size()) {
for (const auto &[key, value] : _attributes) {
os << ' ' << key << "=\"" << Escape(value) << '"';
}
}

if (children().size()) {
os << "/>";
return;
}

os << '>';
for (const auto &child : children()) {
child.print(os);
}
os << "</" << tag() << '>';
}

Node *insert_inplace(std::deque<std::string> &path, const std::string &tag) {
if (path.empty()) {
return nullptr;
}

for (auto &child : children()) {
if (const auto p = child.insert(path, tag))
return p;
}

auto &node = children().emplace_back(path.size() > 1 ? "namespace" : tag, path.front());
path.pop_front();
node.insert_inplace(path, tag);

return &node;
}

Node *insert(std::deque<std::string> &path, const std::string &tag) {
if (path.empty() || path.front() != name()) {
return nullptr;
}

path.pop_front();
if (path.empty()) {
return this;
}

return insert_inplace(path, tag);
}
};

class Tree {
Node _root;

public:
Tree() : _root("assembly", "") {}

Node &root() { return _root; }
};

} // namespace mllif::mlir
42 changes: 0 additions & 42 deletions mllif/Frontend/MLIR/include/mllif/Frontend/MLIR/symboltree.h

This file was deleted.

1 change: 1 addition & 0 deletions mllif/Frontend/MLIR/lib/Tree.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "mllif/Frontend/MLIR/Tree.h"
17 changes: 9 additions & 8 deletions mllif/Frontend/MLIR/lib/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/SourceMgr.h>
#include <mlir/Parser/Parser.h>
#include <mllif/Frontend/MLIR/symboltree.h>
#include <mllif/Frontend/MLIR/Tree.h>
#include <mllif/Frontend/annotation.h>

namespace {
Expand Down Expand Up @@ -66,7 +66,7 @@ auto main(int argc, char **argv) -> int {
const std::string output = argv[1];


mllif::mlir::SymbolTree tree;
mllif::mlir::Tree tree;

for (auto i = 2; i < argc; ++i) {
auto module = LoadModule(context, std::string(argv[i]));
Expand Down Expand Up @@ -146,18 +146,19 @@ auto main(int argc, char **argv) -> int {
return;
}

auto fnSym = tree.insert(path);
fnSym.tag = tag;
const auto fnSym = tree.root().insert_inplace(path, tag);

for (auto iParm = 0; iParm < args.size(); ++iParm) {
std::string buffer;
llvm::raw_string_ostream os(buffer);
args[iParm].getType().print(os);
os.flush();

auto parmSym = mllif::mlir::Symbol("param", argNames[iParm]);
parmSym.attributes.emplace_back("type", buffer);
fnSym.children.push_back(parmSym);
fnSym
->children()
.emplace_back("param", argNames[iParm])
.attributes()
.emplace_back("type", buffer);
}
});
}
Expand All @@ -169,7 +170,7 @@ auto main(int argc, char **argv) -> int {
return 1;
}

tree.print(os);
tree.root().print(os);

return 0;
}
61 changes: 0 additions & 61 deletions mllif/Frontend/MLIR/lib/symboltree.cxx

This file was deleted.

0 comments on commit 0028558

Please sign in to comment.