Skip to content
Snippets Groups Projects
Commit 85569909 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Use numbers instead of pointers to define node ids in dot file

parent da9265a5
No related branches found
No related tags found
1 merge request!37Feature/language
......@@ -7,20 +7,21 @@ void
ASTDotPrinter::_print(std::ostream& os, const ASTNode& node) const
{
if (node.is_root()) {
os << " x" << &node << " [ label=\"root \\n" << dataTypeName(node.m_data_type) << "\" ]\n";
os << " x" << m_node_number_map.at(&node) << " [ label=\"root \\n" << dataTypeName(node.m_data_type) << "\" ]\n";
} else {
if (node.has_content()) {
os << " x" << &node << " [ label=\"" << node.name() << "\\n"
os << " x" << m_node_number_map.at(&node) << " [ label=\"" << node.name() << "\\n"
<< node.string_view() << "\\n"
<< dataTypeName(node.m_data_type) << "\" ]\n";
} else {
os << " x" << &node << " [ label=\"" << node.name() << "\\n" << dataTypeName(node.m_data_type) << "\" ]\n";
os << " x" << m_node_number_map.at(&node) << " [ label=\"" << node.name() << "\\n"
<< dataTypeName(node.m_data_type) << "\" ]\n";
}
}
if (!node.children.empty()) {
os << " x" << &node << " -> { ";
os << " x" << m_node_number_map.at(&node) << " -> { ";
for (auto& child : node.children) {
os << "x" << child.get() << ((child == node.children.back()) ? " }\n" : ", ");
os << "x" << m_node_number_map.at(child.get()) << ((child == node.children.back()) ? " }\n" : ", ");
}
for (auto& child : node.children) {
this->_print(os, *child);
......@@ -37,7 +38,17 @@ operator<<(std::ostream& os, const ASTDotPrinter& ast_printer)
return os;
}
void
ASTDotPrinter::_buildNodeNumberMap(const ASTNode& node)
{
m_node_number_map[&node] = m_node_number_map.size();
for (auto& child : node.children) {
this->_buildNodeNumberMap(*child);
}
}
ASTDotPrinter::ASTDotPrinter(const ASTNode& node) : m_node{node}
{
Assert(node.is_root());
this->_buildNodeNumberMap(node);
}
......@@ -2,14 +2,19 @@
#define AST_DOT_PRINTER_HPP
#include <ASTNode.hpp>
#include <map>
class ASTDotPrinter
{
private:
const ASTNode& m_node;
std::map<const ASTNode*, uint32_t> m_node_number_map;
void _print(std::ostream& os, const ASTNode& node) const;
void _buildNodeNumberMap(const ASTNode& node);
public:
friend std::ostream& operator<<(std::ostream& os, const ASTDotPrinter& ast_printer);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment