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