Vcsn  2.8
Be Rational
context-printer.cc
Go to the documentation of this file.
2 
3 #include <boost/algorithm/string/predicate.hpp>
4 
6 #include <vcsn/misc/getargs.hh>
7 #include <vcsn/misc/indent.hh>
8 
9 
10 namespace vcsn
11 {
12  namespace ast
13  {
14  void context_printer::header(const std::string& h)
15  {
16  headers_.insert(h);
17  }
18 
19  void context_printer::linkflags(const std::string& flags)
20  {
21  // We rely on this initial space when not empty.
22  linkflags_ += ' ';
23  linkflags_ += flags;
24  }
25 
26  const std::string& context_printer::linkflags() const
27  {
28  return linkflags_;
29  }
30 
31  std::ostream& context_printer::print(std::ostream& o)
32  {
33  o <<
34  "#define BUILD_LIBVCSN 1\n"
35  "#define VCSN_INSTANTIATION 1\n"
36  "#define MAYBE_EXTERN\n"
37  "\n";
38  for (const auto& h: headers_)
39  o << "#include <" << h << ">\n";
40  o << '\n';
41  for (const auto& h: headers_late_)
42  o << "#include <" << h << ">\n";
43  o << '\n'
44  << os_.str();
45  return o;
46  }
47 
48 #define DEFINE(Type) \
49  void context_printer::visit(const Type& t)
50 
52  {
53  static const auto map = getarg<std::string>
54  {
55  "automaton type",
56  {
57  {"compose_automaton" , "vcsn/algos/compose.hh"},
58  {"delay_automaton" , "vcsn/algos/is-synchronized.hh"},
59  {"derived_term_automaton" , "vcsn/algos/derived-term.hh"},
60  {"determinized_automaton" , "vcsn/algos/determinize.hh"},
61  {"expression_automaton" , "vcsn/core/expression-automaton.hh"},
62  {"filter_automaton" , "vcsn/algos/filter.hh"},
63  {"focus_automaton" , "vcsn/algos/focus.hh"},
64  {"insplit_automaton" , "vcsn/algos/insplit.hh"},
65  {"lazy_proper_automaton" , "vcsn/algos/epsilon-remover-lazy.hh"},
66  {"mutable_automaton" , "vcsn/core/mutable-automaton.hh"},
67  {"name_automaton" , "vcsn/core/name-automaton.hh"},
68  {"pair_automaton" , "vcsn/algos/synchronizing-word.hh"},
69  {"partition_automaton" , "vcsn/core/partition-automaton.hh"},
70  {"permutation_automaton" , "vcsn/core/permutation-automaton.hh"},
71  {"product_automaton" , "vcsn/algos/conjunction.hh"},
72  {"scc_automaton" , "vcsn/algos/scc.hh"},
73  {"synchronized_automaton" , "vcsn/algos/synchronize.hh"},
74  {"transpose_automaton" , "vcsn/algos/transpose.hh"},
75  {"tuple_automaton" , "vcsn/core/tuple-automaton.hh"},
76  },
77  };
78  auto type = t.get_type();
79  header(map[type]);
80  os_ << "vcsn::" << type << '<' << incendl;
81  bool first = true;
82  for (auto c: t.get_content())
83  {
84  if (!first)
85  os_ << ',' << iendl;
86  first = false;
87  c->accept(*this);
88  }
89  os_ << decendl << '>';
90  }
91 
92  DEFINE(context)
93  {
94  header("vcsn/ctx/context.hh");
95  os_ << "vcsn::context<" << incendl;
96  t.get_labelset()->accept(*this);
97  os_ << ',' << iendl;
98  t.get_weightset()->accept(*this);
99  os_ << decendl << '>';
100  }
101 
102  DEFINE(tuple)
103  {
104  header("tuple");
105  os_ << "std::tuple<" << incendl;
106  bool first = true;
107  for (auto v: t.get_sets())
108  {
109  if (!first)
110  os_ << ',' << iendl;
111  first = false;
112  v->accept(*this);
113  }
114  os_ << decendl << '>';
115  }
116 
117  DEFINE(tupleset)
118  {
119  headers_late_.insert("vcsn/labelset/tupleset.hh");
120  os_ << "vcsn::tupleset<" << incendl;
121  bool first = true;
122  for (auto v: t.get_sets())
123  {
124  if (!first)
125  os_ << ',' << iendl;
126  first = false;
127  v->accept(*this);
128  }
129  os_ << decendl << '>';
130  }
131 
132  DEFINE(nullableset)
133  {
134  header("vcsn/labelset/nullableset.hh");
135  os_ << "vcsn::nullableset<" << incendl;
136  t.get_labelset()->accept(*this);
137  os_ << decendl << ">";
138  }
139 
140  DEFINE(oneset)
141  {
142  (void) t;
143  header("vcsn/labelset/oneset.hh");
144  os_ << "vcsn::oneset";
145  }
146 
147  DEFINE(genset)
148  {
149  header("vcsn/alphabets/setalpha.hh"); // set_alphabet
150  if (t.letter_type() == "char_letters")
151  header("vcsn/alphabets/char.hh");
152  else if (t.letter_type() == "string_letters")
153  header("vcsn/alphabets/string.hh");
154  os_ << "vcsn::set_alphabet<vcsn::" << t.letter_type() << '>';
155  }
156 
157  DEFINE(letterset)
158  {
159  header("vcsn/labelset/letterset.hh");
160  os_ << "vcsn::letterset<";
161  t.genset()->accept(*this);
162  os_ << '>';
163  }
164 
165  DEFINE(expressionset)
166  {
167  header("vcsn/core/rat/expressionset.hh");
168  os_ << "vcsn::expressionset<" << incendl;
169  t.get_context()->accept(*this);
170  os_ << decendl << '>';
171  }
172 
173  DEFINE(expansionset)
174  {
175  header("vcsn/core/rat/expansionset.hh");
176  os_ << "vcsn::rat::expansionset<" << incendl;
177  t.get_expressionset()->accept(*this);
178  os_ << decendl << '>';
179  }
180 
181  DEFINE(weightset)
182  {
183  header("vcsn/weightset/" + t.get_type() + ".hh");
184  if (t.get_type() == "qmp")
185  linkflags("-lgmp -lgmpxx");
186  os_ << "vcsn::" << t.get_type();
187  }
188 
189  DEFINE(wordset)
190  {
191  header("vcsn/labelset/wordset.hh");
192  os_ << "vcsn::wordset<";
193  t.genset()->accept(*this);
194  os_ << '>';
195  }
196 
197  DEFINE(other)
198  {
199  if (boost::ends_with(t.get_type(), "_tag"))
200  os_ << "vcsn::";
201  os_ << t.get_type();
202  }
203 
204  DEFINE(polynomialset)
205  {
206  header("vcsn/weightset/polynomialset.hh");
207  os_ << "vcsn::polynomialset<" << incendl;
208  t.get_content()->accept(*this);
209  os_ << decendl << '>';
210  }
211 #undef DEFINE
212  }
213 }
std::ostream & iendl(std::ostream &o)
Print an end of line, then set the indentation.
Definition: indent.cc:49
#define DEFINE(Type)
std::set< std::string > headers_
Headers to include.
const std::string & linkflags() const
Get the link flags.
std::ostringstream & os_
Definition: a-star.hh:8
std::ostream & incendl(std::ostream &o)
Increment the indentation, print an end of line, and set the indentation.
Definition: indent.cc:54
std::string linkflags_
Flags to pass to the linker.
void header(const std::string &h)
Record that we need an include for this header.
A mapping from strings to Values.
Definition: getargs.hh:33
std::ostream & print(std::ostream &o)
Generate the code to compile on o.
Indentation relative functions.
std::set< std::string > headers_late_