57 lines
994 B
C++
57 lines
994 B
C++
|
#include "Node.hpp"
|
||
|
|
||
|
namespace muz
|
||
|
{
|
||
|
/*explicit*/ Node::Node(NodeType type,
|
||
|
std::string const& value)
|
||
|
: m_type { type }
|
||
|
, m_value { value }
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/*virtual*/ Node::~Node()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void Node::add_child(std::shared_ptr<Node> child)
|
||
|
{
|
||
|
m_children.push_back(child);
|
||
|
}
|
||
|
|
||
|
std::shared_ptr<Node> Node::child(size_t index) const
|
||
|
{
|
||
|
if (index >= m_children.size())
|
||
|
{
|
||
|
throw node_error {"cannot get node child: bad index"};
|
||
|
}
|
||
|
|
||
|
return m_children[index];
|
||
|
}
|
||
|
|
||
|
std::string Node::string() const
|
||
|
{
|
||
|
std::stringstream ss;
|
||
|
ss << NodeTypeStr[type()] + strlen("NODE_");
|
||
|
|
||
|
if (m_value.empty() == false)
|
||
|
{
|
||
|
ss << "[" << m_value << "]";
|
||
|
}
|
||
|
|
||
|
if (m_children.empty() == false)
|
||
|
{
|
||
|
std::string sep;
|
||
|
|
||
|
ss << "(";
|
||
|
for (auto& c: m_children)
|
||
|
{
|
||
|
ss << sep << c->string();
|
||
|
sep = ",";
|
||
|
}
|
||
|
ss << ")";
|
||
|
}
|
||
|
|
||
|
return ss.str();
|
||
|
}
|
||
|
}
|