roza/lib/Node.cpp

61 lines
1.0 KiB
C++

#include "Node.hpp"
namespace roza
{
/*explicit*/ Node::Node(NodeType type, std::string const& repr, SrcLoc loc)
: m_type { type }
, m_repr { repr }
, m_loc { loc }
{
}
/*virtual*/ Node::~Node()
{
}
std::string Node::string() const
{
std::stringstream ss;
ss << std::string(NodeTypeStr[m_type]).substr(std::string("NODE_").size());
if (m_repr != "")
{
ss << "[" << m_repr << "]";
}
if (size() > 0)
{
ss << "(";
std::string sep = "";
for (auto const& child: m_children)
{
ss << sep << child->string();
sep = ",";
}
ss << ")";
}
return ss.str();
}
void Node::add_child(std::shared_ptr<Node> child)
{
assert(child);
m_children.push_back(std::move(child));
}
std::shared_ptr<Node> Node::child(size_t index)
{
assert(index < size());
return m_children[index];
}
Node const& Node::child(size_t index) const
{
assert(index < size());
return *m_children[index];
}
}