pass all expr tests
[ozulis.git] / src / ast / generator-default-visitor.cc
blob2a5c00c17425101e587d8d40d7648dc4133e08fd
1 #include <iostream>
2 #include <fstream>
4 #include <core/assert.hh>
5 #include "generator.hh"
7 namespace ag = ast::generator;
9 static void
10 generateDefaultVisitorHeader(const char * working_directory)
12 assert(working_directory);
14 std::fstream out;
15 std::string path(working_directory);
17 path.append("/default-visitor.hh");
18 out.open(path.c_str(), std::fstream::out | std::fstream::trunc);
19 assert(out.is_open() && out.good());
21 const ag::Node *node;
23 ag::appendGeneratedWarning(out);
24 out << "#ifndef AST_DEFAULT_VISITOR_HH" NL
25 << "# define AST_DEFAULT_VISITOR_HH" NL
27 << "#include <ast/visitor.hh>" NL
29 << "namespace ast" NL
30 << "{" NL;
32 #define DEFAULT_VISITOR_HH(Prefix, Const) \
33 out << " /** @defgroup Visitors */" NL \
34 << " /**" NL \
35 << " * @brief The default visitor" NL \
36 << " * @ingroup Visitors" NL \
37 << " */" NL \
38 << " class "Prefix"DefaultVisitor : public "Prefix"Visitor" NL \
39 << " {" NL \
40 << " public:" NL \
41 << " virtual ~"Prefix"DefaultVisitor();" NL \
42 NL; \
44 for (node = ag::nodes; !node->name.empty() > 0; node++) \
45 out << " virtual void visit("Const << node->name << " & node);" \
46 NL; \
47 out << " };" NL NL;
49 DEFAULT_VISITOR_HH("", "");
50 DEFAULT_VISITOR_HH("Const", "const ");
52 out << "} // namespace ast" NL
53 << "#endif // !AST_DEFAULT_VISITOR_HH" NL;
54 out.close();
57 static void
58 generateDefaultVisitorSource(const char * working_directory)
60 assert(working_directory);
62 std::fstream out;
63 std::string path(working_directory);
65 path.append("/default-visitor.cc");
66 out.open(path.c_str(), std::fstream::out | std::fstream::trunc);
67 assert(out.is_open() && out.good());
69 const ag::Node *node;
71 ag::appendGeneratedWarning(out);
72 out << "#include <ast/default-visitor.hh>" NL NL
73 << "#define NL << std::endl" NL NL
74 << "namespace ast" NL
75 << "{" NL;
77 #define DEFAULT_VISITOR_CC(Prefix, Const) \
78 out << " "Prefix"DefaultVisitor::~"Prefix"DefaultVisitor() {}" NL; \
80 for (node = ag::nodes; !node->name.empty() > 0; node++) \
81 out << " void "Prefix"DefaultVisitor::visit("Const << node->name \
82 << " &) {}" NL NL;
84 DEFAULT_VISITOR_CC("", "");
85 DEFAULT_VISITOR_CC("Const", "const ");
87 out << "} // namespace ast" NL;
88 out.close();
91 int main(int argc, char **argv)
93 assert(argc == 2);
94 generateDefaultVisitorHeader(argv[1]);
95 generateDefaultVisitorSource(argv[1]);
96 return 0;