Generates llvm assembly code for additions :-)
[ozulis.git] / src / ast / generator-browse-visitor.cc
blob0d6bf8947d387eaf729880d04920cb229d09a58b
1 #include <iostream>
2 #include <fstream>
3 #include <boost/foreach.hpp>
5 #include <core/assert.hh>
6 #include "generator.hh"
8 namespace ag = ast::generator;
10 static void
11 generateBrowseVisitorHeader(const char * working_directory)
13 assert(working_directory);
15 std::fstream out;
16 std::string path(working_directory);
18 path.append("/browse-visitor.hh");
19 out.open(path.c_str(), std::fstream::out | std::fstream::trunc);
20 assert(out.is_open() && out.good());
22 const ag::Node *node;
24 ag::appendGeneratedWarning(out);
25 out << "#ifndef AST_BROWSE_VISITOR_HH" NL
26 << "# define AST_BROWSE_VISITOR_HH" NL
28 << "#include <ast/visitor.hh>" NL
30 << "namespace ast" NL
31 << "{" NL;
33 #define BROWSE_VISITOR_HH(Prefix, Const) \
34 out << " /** @defgroup Visitors */" NL \
35 << " /**" NL \
36 << " * @brief A visitor which browse the AST from the top to the bottom." NL \
37 << " * @ingroup Visitors" NL \
38 << " */" NL \
39 << " class "Prefix"BrowseVisitor : public "Prefix"Visitor" NL \
40 << " {" NL \
41 << " public:" NL \
42 << " virtual ~"Prefix"BrowseVisitor();" NL \
43 NL; \
45 for (node = ag::nodes; !node->name.empty() > 0; node++) \
46 out << " virtual void visit("Const << node->name << " & node);" \
47 NL NL; \
49 out << " };" NL NL;
51 BROWSE_VISITOR_HH("", "");
52 BROWSE_VISITOR_HH("Const", "const ");
54 out << "} // namespace ast" NL
55 << "#endif // !AST_BROWSE_VISITOR_HH" NL;
56 out.close();
59 static void
60 generateBrowseVisitorSource(const char * working_directory)
62 assert(working_directory);
64 std::fstream out;
65 std::string path(working_directory);
67 path.append("/browse-visitor.cc");
68 out.open(path.c_str(), std::fstream::out | std::fstream::trunc);
69 assert(out.is_open() && out.good());
71 const ag::Node *node;
73 ag::appendGeneratedWarning(out);
74 out << "#include <boost/foreach.hpp>" NL
75 << "#include <core/assert.hh>" NL
76 << "#include <ast/browse-visitor.hh>" NL
77 << "#include <ast/ast.hh>" NL NL
78 << "#define NL << std::endl" NL NL
79 << "namespace ast" NL
80 << "{" NL;
82 #define BROWSE_VISITOR_CC(Name, Const) \
83 out << " "Name"::~"Name"() {}" NL NL; \
85 for (node = ag::nodes; !node->name.empty() > 0; node++) \
86 { \
87 out << " void "Name"::visit("Const << node->name \
88 << " & node __attribute__((unused)))" NL \
89 << " {" NL; \
91 BOOST_FOREACH(const ag::Attribute * attr, allAttributes(node)) \
92 { \
93 if (ag::isNode(attr->type)) \
94 out << " node." << attr->name << "->accept(*this);" NL; \
95 else if (ag::isVectorOfNode(attr->type)) \
96 out << " if (node." << attr->name << ")" NL \
97 << " BOOST_FOREACH(Node * n, (*node." << attr->name \
98 << ")) {" NL \
99 << " assert(n);" \
100 << " n->accept(*this);" NL \
101 << " }" NL; \
104 out << " }" NL NL; \
107 BROWSE_VISITOR_CC("BrowseVisitor", "");
108 BROWSE_VISITOR_CC("ConstBrowseVisitor", "const ");
110 out << "} // namespace ast" NL;
111 out.close();
114 int main(int argc, char **argv)
116 assert(argc == 2);
117 generateBrowseVisitorHeader(argv[1]);
118 generateBrowseVisitorSource(argv[1]);
119 return 0;