pass all expr tests
[ozulis.git] / src / ast / generator-ast.cc
blobda3d1c93d291f66f99cb92cbbdd681012a835f67
1 #include "generator.hh"
2 #include <core/assert.hh>
3 #include <iostream>
4 #include <fstream>
6 namespace ag = ast::generator;
8 void
9 generateAstHeader(const char * working_directory)
11 assert(working_directory);
13 std::fstream out;
14 std::string path(working_directory);
16 path.append("/ast.hh");
17 out.open(path.c_str(), std::fstream::out | std::fstream::trunc);
18 assert(out.is_open() && out.good());
20 const ag::Node * node;
21 const ag::Attribute * attr;
23 ag::appendGeneratedWarning(out);
24 out << "#ifndef AST_AST_HH" NL
25 << "# define AST_AST_HH" NL NL
26 << "# include <gc/gc_cpp.h>" NL
27 << "# include <vector>" NL
28 << "# include <string>" NL NL
29 << "# include <core/id.hh>" NL NL
30 << "namespace ast" NL
31 << "{" NL NL
32 << " /// @defgroup AST" NL;
34 out << " struct Visitor;" NL
35 << " struct ConstVisitor;" NL
36 << " struct Symbol;" NL
37 << " struct Scope;" NL NL;
39 for (node = ag::nodes; node->name.length() > 0; node++)
40 out << " struct " << node->name << ";" NL;
42 out NL;
44 for (node = ag::nodes; !node->name.empty() > 0; node++)
46 out << " /// @ingroup AST" NL
47 << " /// @brief " << node->description NL
48 << " struct " << node->name;
49 if (!node->parent.empty())
50 out << " : public " << node->parent;
51 out NL << " {" NL
52 << " public:" NL NL
53 << " " << node->name << "();" NL
54 << " virtual ~" << node->name << "();" NL NL
55 << " /// The accept method for the visitor design pattern." NL
56 << " virtual void accept(Visitor & visitor);" NL
57 << " /// The accept method for the const visitor design pattern." NL
58 << " virtual void accept(ConstVisitor & visitor) const;" NL NL
59 << " /// Returns the node type" NL
60 << " static inline core::id_t nodeTypeId() { return core::Id<ast::Node, ast::"
61 << node->name << ">::id(); }" NL NL;
63 if (!node->attrs->type.empty())
64 out NL;
66 for (attr = node->attrs; !attr->type.empty(); attr++)
67 out << " " << attr->type << " " << attr->name << ";" NL;
68 out << " };" NL NL;
71 out << "} // namespace ast" NL NL
72 << "extern template class std::vector<ast::Node *>;" NL
73 << "extern template class std::vector<ast::Type *>;" NL
74 << "extern template class std::vector<ast::VarDecl *>;" NL
75 << "#endif // !AST_AST_HH" NL;
76 out.close();
79 static void
80 generateAstSource(const char * working_directory)
82 assert(working_directory);
84 std::fstream out;
85 std::string path(working_directory);
87 path.append("/ast.cc");
88 out.open(path.c_str(), std::fstream::out | std::fstream::trunc);
89 assert(out.is_open() && out.good());
91 ag::appendGeneratedWarning(out);
92 out << "#include <ast/ast.hh>" NL
93 << "#include <ast/visitor.hh>" NL
94 << "#include <core/id.hh>" NL NL
95 << "#define NL << std::endl" NL NL
96 << "namespace ast" NL
97 << "{" NL;
99 const ag::Node *node;
100 for (node = ag::nodes; !node->name.empty() > 0; node++)
102 bool hasParent = !node->parent.empty();
103 out << " " << node->name << "::" << node->name << "()";
104 if (hasParent)
105 out NL << " : " << node->parent << "()";
106 for (const ag::Attribute * attr = node->attrs; !attr->type.empty(); ++attr)
107 if (hasParent++)
108 out << ", " << attr->name << "()";
109 else
110 out << ": " << attr->name << "()";
112 out NL
113 << " {" NL
114 << " nodeType = core::Id<ast::Node, ast::" << node->name
115 << ">::id();" NL
116 << " }" NL NL;
118 out << " " << node->name << "::~" << node->name << "()" NL
119 << " {" NL
120 << " }" NL NL;
122 out << " void " << node->name << "::accept(Visitor & visitor)" NL
123 << " {" NL
124 << " visitor.visit(*this);" NL
125 << " }" NL NL;
127 out << " void " << node->name << "::accept(ConstVisitor & visitor) const" NL
128 << " {" NL
129 << " visitor.visit(*this);" NL
130 << " }" NL NL;
133 out << "} // namespace ast" NL
134 << "template class std::vector<ast::Node *>;" NL
135 << "template class std::vector<ast::Type *>;" NL
136 << "template class std::vector<ast::VarDecl *>;" NL;
137 out.close();
141 int main(int argc, char **argv)
143 assert(argc == 2);
144 generateAstHeader(argv[1]);
145 generateAstSource(argv[1]);
146 return 0;