--- /dev/null
+#include "ASTPrinter.h"
+
+using namespace std;
+
+string ASTPrinter::str()
+{
+ return stream.str();
+}
+
+void ASTPrinter::beforeVisit(AST* cur, int depth)
+{
+}
+
+void ASTPrinter::afterVisit(AST* cur, int depth)
+{
+ stream << endl;
+}
+
+void ASTPrinter::beforeChildren(AST* cur, int depth)
+{
+ stream << "(" << cur->type() << " " << cur->text();
+}
+
+void ASTPrinter::afterChildren(AST* cur, int depth)
+{
+ stream << ")";
+}
+
+void ASTPrinter::beforeChild(AST* cur, int depth)
+{
+ stream << endl;
+ for(int i = 0; i< depth; i++)
+ {
+ stream << " ";
+ }
+}
+
+void ASTPrinter::afterChild(AST* cur, int depth)
+{
+}
+
--- /dev/null
+#ifndef ASTPRINTER_H
+#define ASTPRINTER_H
+
+#include "ivisitor.h"
+#include <iostream>
+#include <sstream>
+
+class ASTPrinter : public IVisitor {
+ protected:
+ ostringstream stream;
+ public:
+ ASTPrinter(AST* root) : IVisitor(root) {};
+ string str();
+ private:
+ void beforeVisit(AST* cur, int depth);
+ void afterVisit(AST* cur, int depth);
+ void beforeChildren(AST* cur, int depth);
+ void afterChildren(AST* cur, int depth);
+ void beforeChild(AST* cur, int depth);
+ void afterChild(AST* cur, int depth);
+};
+
+#endif