1 #include "generator.hh"
2 #include <core/assert.hh>
6 namespace ag
= ast::generator
;
9 generateAstHeader(const char * working_directory
)
11 assert(working_directory
);
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
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
;
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
;
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())
66 for (attr
= node
->attrs
; !attr
->type
.empty(); attr
++)
67 out
<< " " << attr
->type
<< " " << attr
->name
<< ";" 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
;
80 generateAstSource(const char * working_directory
)
82 assert(working_directory
);
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
100 for (node
= ag::nodes
; !node
->name
.empty() > 0; node
++)
102 bool hasParent
= !node
->parent
.empty();
103 out
<< " " << node
->name
<< "::" << node
->name
<< "()";
105 out NL
<< " : " << node
->parent
<< "()";
106 for (const ag::Attribute
* attr
= node
->attrs
; !attr
->type
.empty(); ++attr
)
108 out
<< ", " << attr
->name
<< "()";
110 out
<< ": " << attr
->name
<< "()";
114 << " nodeType = core::Id<ast::Node, ast::" << node
->name
118 out
<< " " << node
->name
<< "::~" << node
->name
<< "()" NL
122 out
<< " void " << node
->name
<< "::accept(Visitor & visitor)" NL
124 << " visitor.visit(*this);" NL
127 out
<< " void " << node
->name
<< "::accept(ConstVisitor & visitor) const" NL
129 << " visitor.visit(*this);" 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
;
141 int main(int argc
, char **argv
)
144 generateAstHeader(argv
[1]);
145 generateAstSource(argv
[1]);