6 static int emit_string(yaml_emitter_t
*emitter
, const char *str
)
10 if (!yaml_scalar_event_initialize(&event
, NULL
, NULL
,
11 (yaml_char_t
*) str
, strlen(str
),
12 1, 1, YAML_PLAIN_SCALAR_STYLE
))
14 if (!yaml_emitter_emit(emitter
, &event
))
20 static int emit_int(yaml_emitter_t
*emitter
, int i
)
24 snprintf(buffer
, sizeof(buffer
), "%d", i
);
25 return emit_string(emitter
, buffer
);
28 static int emit_double(yaml_emitter_t
*emitter
, double d
)
32 snprintf(buffer
, sizeof(buffer
), "%g", d
);
33 return emit_string(emitter
, buffer
);
36 static int emit_map(yaml_emitter_t
*emitter
, __isl_keep isl_map
*map
)
38 isl_ctx
*ctx
= isl_map_get_ctx(map
);
43 p
= isl_printer_to_str(ctx
);
44 p
= isl_printer_print_map(p
, map
);
45 str
= isl_printer_get_str(p
);
47 r
= emit_string(emitter
, str
);
52 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
54 isl_ctx
*ctx
= isl_set_get_ctx(set
);
59 p
= isl_printer_to_str(ctx
);
60 p
= isl_printer_print_set(p
, set
);
61 str
= isl_printer_get_str(p
);
63 r
= emit_string(emitter
, str
);
68 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
72 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
73 YAML_BLOCK_MAPPING_STYLE
))
75 if (!yaml_emitter_emit(emitter
, &event
))
78 if (emit_string(emitter
, "context") < 0)
80 if (emit_set(emitter
, array
->context
) < 0)
83 if (emit_string(emitter
, "extent") < 0)
85 if (emit_set(emitter
, array
->extent
) < 0)
88 if (array
->value_bounds
) {
89 if (emit_string(emitter
, "value_bounds") < 0)
91 if (emit_set(emitter
, array
->value_bounds
) < 0)
95 if (emit_string(emitter
, "element_type") < 0)
97 if (emit_string(emitter
, array
->element_type
) < 0)
100 if (!yaml_mapping_end_event_initialize(&event
))
102 if (!yaml_emitter_emit(emitter
, &event
))
108 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
109 struct pet_array
**arrays
)
114 if (emit_string(emitter
, "arrays") < 0)
116 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
117 YAML_BLOCK_SEQUENCE_STYLE
))
119 if (!yaml_emitter_emit(emitter
, &event
))
122 for (i
= 0; i
< n_array
; ++i
)
123 if (emit_array(emitter
, arrays
[i
]) < 0)
126 if (!yaml_sequence_end_event_initialize(&event
))
128 if (!yaml_emitter_emit(emitter
, &event
))
134 static int emit_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
136 if (emit_string(emitter
, pet_type_str(type
)) < 0)
141 static int emit_expr(yaml_emitter_t
*emitter
, struct pet_expr
*expr
)
145 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
146 YAML_BLOCK_MAPPING_STYLE
))
148 if (!yaml_emitter_emit(emitter
, &event
))
151 if (emit_string(emitter
, "type") < 0)
153 if (emit_type(emitter
, expr
->type
) < 0)
156 switch (expr
->type
) {
157 case pet_expr_double
:
158 if (emit_string(emitter
, "value") < 0)
160 if (emit_double(emitter
, expr
->d
) < 0)
163 case pet_expr_access
:
164 if (emit_string(emitter
, "relation") < 0)
166 if (emit_map(emitter
, expr
->acc
.access
) < 0)
168 if (emit_string(emitter
, "read") < 0)
170 if (emit_int(emitter
, expr
->acc
.read
) < 0)
172 if (emit_string(emitter
, "write") < 0)
174 if (emit_int(emitter
, expr
->acc
.write
) < 0)
178 case pet_expr_binary
:
179 if (emit_string(emitter
, "operation") < 0)
181 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
184 case pet_expr_ternary
:
187 if (emit_string(emitter
, "name") < 0)
189 if (emit_string(emitter
, expr
->name
) < 0)
194 if (expr
->n_arg
> 0) {
197 if (emit_string(emitter
, "arguments") < 0)
199 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
200 YAML_BLOCK_SEQUENCE_STYLE
))
202 if (!yaml_emitter_emit(emitter
, &event
))
205 for (i
= 0; i
< expr
->n_arg
; ++i
)
206 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
209 if (!yaml_sequence_end_event_initialize(&event
))
211 if (!yaml_emitter_emit(emitter
, &event
))
215 if (!yaml_mapping_end_event_initialize(&event
))
217 if (!yaml_emitter_emit(emitter
, &event
))
223 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
227 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
228 YAML_BLOCK_MAPPING_STYLE
))
230 if (!yaml_emitter_emit(emitter
, &event
))
233 if (emit_string(emitter
, "line") < 0)
235 if (emit_int(emitter
, stmt
->line
) < 0)
238 if (emit_string(emitter
, "domain") < 0)
240 if (emit_set(emitter
, stmt
->domain
) < 0)
243 if (emit_string(emitter
, "schedule") < 0)
245 if (emit_map(emitter
, stmt
->schedule
) < 0)
248 if (emit_string(emitter
, "body") < 0)
250 if (emit_expr(emitter
, stmt
->body
) < 0)
253 if (!yaml_mapping_end_event_initialize(&event
))
255 if (!yaml_emitter_emit(emitter
, &event
))
261 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
262 struct pet_stmt
**stmts
)
267 if (emit_string(emitter
, "statements") < 0)
269 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
270 YAML_BLOCK_SEQUENCE_STYLE
))
272 if (!yaml_emitter_emit(emitter
, &event
))
275 for (i
= 0; i
< n_stmt
; ++i
)
276 if (emit_stmt(emitter
, stmts
[i
]) < 0)
279 if (!yaml_sequence_end_event_initialize(&event
))
281 if (!yaml_emitter_emit(emitter
, &event
))
287 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
291 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
292 YAML_BLOCK_MAPPING_STYLE
))
294 if (!yaml_emitter_emit(emitter
, &event
))
297 if (emit_string(emitter
, "context") < 0)
299 if (emit_set(emitter
, scop
->context
) < 0)
302 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
305 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
308 if (!yaml_mapping_end_event_initialize(&event
))
310 if (!yaml_emitter_emit(emitter
, &event
))
316 /* Print a YAML serialization of "scop" to "out".
318 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
320 yaml_emitter_t emitter
;
323 yaml_emitter_initialize(&emitter
);
325 yaml_emitter_set_output_file(&emitter
, out
);
327 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
328 if (!yaml_emitter_emit(&emitter
, &event
))
331 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
333 if (!yaml_emitter_emit(&emitter
, &event
))
336 if (emit_scop(&emitter
, scop
) < 0)
339 if (!yaml_document_end_event_initialize(&event
, 1))
341 if (!yaml_emitter_emit(&emitter
, &event
))
344 yaml_stream_end_event_initialize(&event
);
345 if (!yaml_emitter_emit(&emitter
, &event
))
348 yaml_emitter_delete(&emitter
);
351 yaml_emitter_delete(&emitter
);