Alexandria  2.27.0
SDC-CH common library for the Euclid project
TextReprVisitor.cpp
Go to the documentation of this file.
1 
20 #include <sstream>
21 
22 namespace Pyston {
23 
24 TextReprVisitor::TextReprVisitor(std::ostream& out_stream) : m_stream(out_stream) {}
25 
27  m_stack.push_back({});
28 }
29 
30 void TextReprVisitor::exit(const NodeBase* node) {
31  const auto& children = m_stack.back();
32  std::ostringstream self_repr;
33  std::string node_repr = node->repr();
34 
35  if (children.size() == 1 && node_repr.size() == 1) {
36  self_repr << node->repr() << children.front();
37  } else if (children.size() == 1) {
38  self_repr << node->repr() << '(' << children.front() << ')';
39  } else if (children.size() == 2 && node_repr.size() == 1) {
40  self_repr << '(' << children.front() << ' ' << node->repr() << ' ' << children.back() << ')';
41  } else if (children.size() == 2) {
42  self_repr << node->repr() << '(' << children.front() << ", " << children.back() << ')';
43  } else {
44  self_repr << node->repr();
45  }
46 
47  m_stack.pop_back();
48  if (!m_stack.empty()) {
49  m_stack.back().push_back(self_repr.str());
50  } else {
51  m_stream << self_repr.str();
52  }
53 }
54 
55 } // end of namespace Pyston
T back(T... args)
virtual std::string repr() const =0
void enter(const NodeBase *base) override
std::list< std::list< std::string > > m_stack
void exit(const NodeBase *node) override
TextReprVisitor(std::ostream &out_stream)
T empty(T... args)
T front(T... args)
T pop_back(T... args)
T push_back(T... args)
T size(T... args)
T str(T... args)