ready to test more expr
[ozulis.git] / src / ast / generator-ast.cc
blob7e230a7849a5e1848428d2ac8dd171e440fe3a94
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
43 << "# define AST_IS_FLOATING_POINT_TYPE(Type) \\" NL
44 << " ((Type)->nodeType == ::ast::FloatType::nodeTypeId() || \\" NL
45 << " (Type)->nodeType == ::ast::DoubleType::nodeTypeId())" NL NL;
47 for (node = ag::nodes; !node->name.empty() > 0; node++)
49 out << " /// @ingroup AST" NL
50 << " /// @brief " << node->description NL
51 << " struct " << node->name;
52 if (!node->parent.empty())
53 out << " : public " << node->parent;
54 out NL << " {" NL
55 << " public:" NL NL
56 << " " << node->name << "();" NL
57 << " virtual ~" << node->name << "();" NL NL
58 << " /// The accept method for the visitor design pattern." NL
59 << " virtual void accept(Visitor & visitor);" NL
60 << " /// The accept method for the const visitor design pattern." NL
61 << " virtual void accept(ConstVisitor & visitor) const;" NL NL
62 << " /// Returns the node type" NL
63 << " static inline core::id_t nodeTypeId() { return core::Id<ast::Node, ast::"
64 << node->name << ">::id(); }" NL NL;
66 if (!node->attrs->type.empty())
67 out NL;
69 for (attr = node->attrs; !attr->type.empty(); attr++)
70 out << " " << attr->type << " " << attr->name << ";" NL;
71 out << " };" NL NL;
74 out << "} // namespace ast" NL NL
75 << "extern template class std::vector<ast::Node *>;" NL
76 << "extern template class std::vector<ast::Type *>;" NL
77 << "extern template class std::vector<ast::VarDecl *>;" NL
78 << "#endif // !AST_AST_HH" NL;
79 out.close();
82 static void
83 generateAstSource(const char * working_directory)
85 assert(working_directory);
87 std::fstream out;
88 std::string path(working_directory);
90 path.append("/ast.cc");
91 out.open(path.c_str(), std::fstream::out | std::fstream::trunc);
92 assert(out.is_open() && out.good());
94 ag::appendGeneratedWarning(out);
95 out << "#include <ast/ast.hh>" NL
96 << "#include <ast/visitor.hh>" NL
97 << "#include <core/id.hh>" NL NL
98 << "#define NL << std::endl" NL NL
99 << "namespace ast" NL
100 << "{" NL;
102 const ag::Node *node;
103 for (node = ag::nodes; !node->name.empty() > 0; node++)
105 bool hasParent = !node->parent.empty();
106 out << " " << node->name << "::" << node->name << "()";
107 if (hasParent)
108 out NL << " : " << node->parent << "()";
109 for (const ag::Attribute * attr = node->attrs; !attr->type.empty(); ++attr)
110 if (hasParent++)
111 out << ", " << attr->name << "()";
112 else
113 out << ": " << attr->name << "()";
115 out NL
116 << " {" NL
117 << " nodeType = core::Id<ast::Node, ast::" << node->name
118 << ">::id();" NL
119 << " }" NL NL;
121 out << " " << node->name << "::~" << node->name << "()" NL
122 << " {" NL
123 << " }" NL NL;
125 out << " void " << node->name << "::accept(Visitor & visitor)" NL
126 << " {" NL
127 << " visitor.visit(*this);" NL
128 << " }" NL NL;
130 out << " void " << node->name << "::accept(ConstVisitor & visitor) const" NL
131 << " {" NL
132 << " visitor.visit(*this);" NL
133 << " }" NL NL;
136 out << "} // namespace ast" NL
137 << "template class std::vector<ast::Node *>;" NL
138 << "template class std::vector<ast::Type *>;" NL
139 << "template class std::vector<ast::VarDecl *>;" NL;
140 out.close();
144 int main(int argc, char **argv)
146 assert(argc == 2);
147 generateAstHeader(argv[1]);
148 generateAstSource(argv[1]);
149 return 0;