-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
133 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 << """; | ||
break; | ||
case '\'': | ||
ss << "'"; | ||
break; | ||
case '<': | ||
ss << "<"; | ||
break; | ||
case '>': | ||
ss << ">"; | ||
break; | ||
case '&': | ||
ss << "&"; | ||
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
42
mllif/Frontend/MLIR/include/mllif/Frontend/MLIR/symboltree.h
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "mllif/Frontend/MLIR/Tree.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.