30 return prefix +
"tree_" + to_string(index) +
": if ((c+=1) >= w - 1) goto " +
31 prefix +
"break_0_" + to_string(mapping[0][index]) +
";\n";
43 return prefix +
"tree_" + to_string(index) +
": if ((c+=2) >= w - 2) { if (c > w - 2) { goto " +
44 prefix +
"break_0_" + to_string(mapping[0][index]) +
"; } else { goto " +
45 prefix +
"break_1_" + to_string(mapping[1][index]) +
"; } } \n";
50 return prefix +
"break_" + to_string(end_group_id) +
"_" + to_string(index) +
":\n";
55 return std::string(2,
'\t') +
"continue;\n";
60 return std::string(2,
'\t') +
"goto " + prefix +
";\n";
75 std::map<BinaryDrag<conact>::node*,
size_t> printed_nodes_;
80 std::map<BinaryDrag<conact>::node*,
bool> nodes_requiring_labels_;
87 with_gotos_(with_gotos),
89 printed_nodes_(printed_nodes)
94 ClearNodesRequiringLabels();
99 printed_nodes_.clear();
103 nodes_requiring_labels_.clear();
123 if (!prefix.empty() && prefix.back() !=
'_') {
126 prefix_ = std::move(prefix);
130 with_gotos_ = with_gotos;
138 return nodes_requiring_labels_;
142 return printed_nodes_;
155 auto& m = printed_nodes_;
156 auto& ml = nodes_requiring_labels_;
159 vector<uint> actions = n->data.actions();
160 os << string(tab,
'\t') <<
"ACTION_" << actions[0] <<
"\n";
162 os << string(tab,
'\t') <<
"goto " << prefix_ <<
"tree_" << n->data.next <<
";\n";
167 if (m.find(n) == end(m)) {
173 os << string(tab,
'\t') <<
"NODE_" << m[n] <<
":\n";
175 string condition = n->data.condition;
176 transform(condition.begin(), condition.end(), condition.begin(), ::toupper);
177 os << string(tab,
'\t') <<
"if (CONDITION_" << condition <<
") {\n";
178 GenerateCodeRec(os, n->right, tab + 1);
179 os << string(tab,
'\t') <<
"}\n";
180 os << string(tab,
'\t') <<
"else {\n";
181 GenerateCodeRec(os, n->left, tab + 1);
182 os << string(tab,
'\t') <<
"}\n";
186 os << string(tab,
'\t') <<
"goto NODE_" << m[n] <<
";\n";
195 auto& ml = nodes_requiring_labels_;
200 if (ml.find(n) != end(ml)) {
205 CheckNodesTraversalRec(n->left);
206 CheckNodesTraversalRec(n->right);
250 const std::string prefix,
252 const std::vector<std::vector<size_t>> mapping,
257 ofstream os(code_path);
262 return GenerateDragCode(os, bd, with_gotos, before, after, prefix, start_id, mapping, end_group_id);
270 const std::string prefix,
272 const std::vector<std::vector<size_t>> mapping,
281 for (
auto& t : bd.
roots_) {
287 for (
size_t i = 0; i < bd.
roots_.size(); ++i) {
288 os << before(i, prefix, mapping, end_group_id);
289 gcc.GenerateCodeRec(os, bd.
roots_[i], 2);
290 os << after(i, prefix, mapping, end_group_id);
A BinaryDrag is the GRAPHGEN implementation of a Binary Directed Rooted Acyclic Graph (DRAG in short)
std::vector< node * > roots_
void GenerateCodeRec(std::ostream &os, BinaryDrag< conact >::node *n, int tab)
void SetWithGotos(bool with_gotos)
std::string & GetPrefix()
GenerateCodeClass(bool with_gotos, std::string prefix, map< BinaryDrag< conact >::node *, size_t > printed_nodes)
void CheckNodesTraversalRec(BinaryDrag< conact >::node *n)
void SetPrefix(std::string prefix)
auto & GetNodeRequiringLabelsMap()
void ClearNodesRequiringLabels()
auto & GetPrintedNodeMap()
std::string BeforeMainShiftOne(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
std::string AfterEndNoLoop(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
std::string AfterEnd(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
std::string BeforeEnd(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
size_t GenerateLineForestCode(std::ostream &os, const LineForestHandler &lfh, std::string prefix, size_t start_id, std::string before_main(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id), std::string after_main(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id), std::string before_end(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id), std::string after_end(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id))
Generate the C++ code for the given Forest.
std::string DefaultEmptyFunc(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
std::string BeforeMainShiftTwo(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
bool GenerateDragCode(const BinaryDrag< conact > &bd, bool with_gotos, std::string before(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id), std::string after(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id), const std::string prefix, size_t start_id, const std::vector< std::vector< size_t >> mapping, size_t end_group_id)
Overload. No output stream required in this case.
#define BEFORE_AFTER_FUNC(func_name)
Macro used to define the base signature of before and after functions.
std::filesystem::path treecode_path_
Generates all the forests needed to handle one line of the image.
std::vector< std::vector< size_t > > main_end_tree_mapping_
std::vector< BinaryDrag< conact > > end_forests_
void SetId(size_t new_id)