5 #include <isl_stream.h>
6 #include <isl_obj_list.h>
7 #include <barvinok/barvinok.h>
11 #include <barvinok/bernstein.h>
14 static int isl_bool_false
= 0;
15 static int isl_bool_true
= 1;
16 static int isl_bool_error
= -1;
18 static enum isl_token_type read_op
;
20 struct isl_arg_choice iscc_format
[] = {
21 {"isl", ISL_FORMAT_ISL
},
22 {"omega", ISL_FORMAT_OMEGA
},
23 {"polylib", ISL_FORMAT_POLYLIB
},
24 {"latex", ISL_FORMAT_LATEX
},
30 struct isl_options
*isl
;
34 struct isl_arg iscc_options_arg
[] = {
35 ISL_ARG_CHILD(struct iscc_options
, isl
, "isl", isl_options_arg
)
36 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
37 iscc_format
, ISL_FORMAT_ISL
)
41 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
43 static void *isl_obj_bool_copy(void *v
)
48 static void isl_obj_bool_free(void *v
)
52 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
55 if (v
== &isl_bool_true
)
56 return isl_printer_print_str(p
, "True");
57 else if (v
== &isl_bool_false
)
58 return isl_printer_print_str(p
, "False");
60 return isl_printer_print_str(p
, "Error");
63 static void *isl_obj_bool_add(void *v1
, void *v2
)
68 struct isl_obj_vtable isl_obj_bool_vtable
= {
74 #define isl_obj_bool (&isl_obj_bool_vtable)
76 int *isl_bool_from_int(int res
)
78 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
81 int *map_is_equal(__isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
83 int res
= isl_map_is_equal(map1
, map2
);
86 return isl_bool_from_int(res
);
88 int *set_is_equal(__isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
90 return map_is_equal((isl_map
*)set1
, (isl_map
*)set2
);
93 int *map_is_subset(__isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
95 int res
= isl_map_is_subset(map1
, map2
);
98 return isl_bool_from_int(res
);
100 int *set_is_subset(__isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
102 return map_is_subset((isl_map
*)set1
, (isl_map
*)set2
);
105 int *map_is_strict_subset(__isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
107 int res
= isl_map_is_strict_subset(map1
, map2
);
110 return isl_bool_from_int(res
);
112 int *set_is_strict_subset(__isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
114 return map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
117 int *map_is_superset(__isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
119 return map_is_subset(map2
, map1
);
121 int *set_is_superset(__isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
123 return set_is_subset(set2
, set1
);
126 int *map_is_strict_superset(__isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
128 return map_is_strict_subset(map2
, map1
);
130 int *set_is_strict_superset(__isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
132 return set_is_strict_subset(set2
, set1
);
135 extern struct isl_obj_vtable isl_obj_list_vtable
;
136 #define isl_obj_list (&isl_obj_list_vtable)
138 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
140 enum isl_token_type op
;
146 struct isc_named_bin_op
{
148 struct isc_bin_op op
;
152 isl_pw_qpolynomial
*pwqp
;
153 isl_pw_qpolynomial
*res
;
156 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
158 struct iscc_at
*at
= (struct iscc_at
*) user
;
162 set
= isl_set_from_point(isl_point_copy(pnt
));
163 qp
= isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(at
->pwqp
), pnt
);
165 at
->res
= isl_pw_qpolynomial_add_disjoint(at
->res
,
166 isl_pw_qpolynomial_alloc(set
, qp
));
171 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_at(
172 __isl_take isl_pw_qpolynomial
*pwqp
, __isl_take isl_set
*set
)
177 at
.res
= isl_pw_qpolynomial_zero(isl_set_get_dim(set
));
179 isl_set_foreach_point(set
, eval_at
, &at
);
181 isl_pw_qpolynomial_free(pwqp
);
187 struct iscc_fold_at
{
188 isl_pw_qpolynomial_fold
*pwf
;
189 isl_pw_qpolynomial
*res
;
192 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
194 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
198 set
= isl_set_from_point(isl_point_copy(pnt
));
199 qp
= isl_pw_qpolynomial_fold_eval(isl_pw_qpolynomial_fold_copy(at
->pwf
),
202 at
->res
= isl_pw_qpolynomial_add_disjoint(at
->res
,
203 isl_pw_qpolynomial_alloc(set
, qp
));
208 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_fold_at(
209 __isl_take isl_pw_qpolynomial_fold
*pwf
, __isl_take isl_set
*set
)
211 struct iscc_fold_at at
;
214 at
.res
= isl_pw_qpolynomial_zero(isl_set_get_dim(set
));
216 isl_set_foreach_point(set
, eval_fold_at
, &at
);
218 isl_pw_qpolynomial_fold_free(pwf
);
224 struct isc_bin_op bin_ops
[] = {
225 { '+', isl_obj_set
, isl_obj_set
,
227 (isc_bin_op_fn
) &isl_set_union
},
228 { '+', isl_obj_map
, isl_obj_map
,
230 (isc_bin_op_fn
) &isl_map_union
},
231 { '-', isl_obj_set
, isl_obj_set
,
233 (isc_bin_op_fn
) &isl_set_subtract
},
234 { '-', isl_obj_map
, isl_obj_map
,
236 (isc_bin_op_fn
) &isl_map_subtract
},
237 { '*', isl_obj_set
, isl_obj_set
,
239 (isc_bin_op_fn
) &isl_set_intersect
},
240 { '*', isl_obj_map
, isl_obj_map
,
242 (isc_bin_op_fn
) &isl_map_intersect
},
243 { '*', isl_obj_map
, isl_obj_set
,
245 (isc_bin_op_fn
) &isl_map_intersect_domain
},
246 { '.', isl_obj_map
, isl_obj_map
,
248 (isc_bin_op_fn
) &isl_map_apply_range
},
249 { ISL_TOKEN_TO
, isl_obj_set
, isl_obj_set
, isl_obj_map
,
250 (isc_bin_op_fn
) &isl_map_from_domain_and_range
},
251 { '=', isl_obj_set
, isl_obj_set
, isl_obj_bool
,
252 (isc_bin_op_fn
) &set_is_equal
},
253 { '=', isl_obj_map
, isl_obj_map
, isl_obj_bool
,
254 (isc_bin_op_fn
) &map_is_equal
},
255 { ISL_TOKEN_LE
, isl_obj_set
, isl_obj_set
, isl_obj_bool
,
256 (isc_bin_op_fn
) &set_is_subset
},
257 { ISL_TOKEN_LE
, isl_obj_map
, isl_obj_map
, isl_obj_bool
,
258 (isc_bin_op_fn
) &map_is_subset
},
259 { ISL_TOKEN_LT
, isl_obj_set
, isl_obj_set
, isl_obj_bool
,
260 (isc_bin_op_fn
) &set_is_strict_subset
},
261 { ISL_TOKEN_LT
, isl_obj_map
, isl_obj_map
, isl_obj_bool
,
262 (isc_bin_op_fn
) &map_is_strict_subset
},
263 { ISL_TOKEN_GE
, isl_obj_set
, isl_obj_set
, isl_obj_bool
,
264 (isc_bin_op_fn
) &set_is_superset
},
265 { ISL_TOKEN_GE
, isl_obj_map
, isl_obj_map
, isl_obj_bool
,
266 (isc_bin_op_fn
) &map_is_superset
},
267 { ISL_TOKEN_GT
, isl_obj_set
, isl_obj_set
, isl_obj_bool
,
268 (isc_bin_op_fn
) &set_is_strict_superset
},
269 { ISL_TOKEN_GT
, isl_obj_map
, isl_obj_map
, isl_obj_bool
,
270 (isc_bin_op_fn
) &map_is_strict_superset
},
271 { '+', isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial
,
272 isl_obj_pw_qpolynomial
,
273 (isc_bin_op_fn
) &isl_pw_qpolynomial_add
},
274 { '-', isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial
,
275 isl_obj_pw_qpolynomial
,
276 (isc_bin_op_fn
) &isl_pw_qpolynomial_sub
},
277 { '*', isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial
,
278 isl_obj_pw_qpolynomial
,
279 (isc_bin_op_fn
) &isl_pw_qpolynomial_mul
},
280 { '*', isl_obj_pw_qpolynomial
, isl_obj_set
,
281 isl_obj_pw_qpolynomial
,
282 (isc_bin_op_fn
) &isl_pw_qpolynomial_intersect_domain
},
283 { '*', isl_obj_pw_qpolynomial_fold
, isl_obj_set
,
284 isl_obj_pw_qpolynomial_fold
,
285 (isc_bin_op_fn
) &isl_pw_qpolynomial_fold_intersect_domain
},
286 { '@', isl_obj_pw_qpolynomial
, isl_obj_set
,
287 isl_obj_pw_qpolynomial
,
288 (isc_bin_op_fn
) &isl_pw_qpolynomial_at
},
289 { '@', isl_obj_pw_qpolynomial_fold
, isl_obj_set
,
290 isl_obj_pw_qpolynomial
,
291 (isc_bin_op_fn
) &isl_pw_qpolynomial_fold_at
},
292 { '%', isl_obj_set
, isl_obj_set
,
294 (isc_bin_op_fn
) &isl_set_gist
},
295 { '%', isl_obj_map
, isl_obj_map
,
297 (isc_bin_op_fn
) &isl_map_gist
},
298 { '%', isl_obj_pw_qpolynomial
, isl_obj_set
,
299 isl_obj_pw_qpolynomial
,
300 (isc_bin_op_fn
) &isl_pw_qpolynomial_gist
},
301 { '%', isl_obj_pw_qpolynomial_fold
, isl_obj_set
,
302 isl_obj_pw_qpolynomial_fold
,
303 (isc_bin_op_fn
) &isl_pw_qpolynomial_fold_gist
},
306 struct isc_named_bin_op named_bin_ops
[] = {
307 { "cross", { -1, isl_obj_set
, isl_obj_set
, isl_obj_set
,
308 (isc_bin_op_fn
) &isl_set_product
} },
309 { "cross", { -1, isl_obj_map
, isl_obj_map
, isl_obj_map
,
310 (isc_bin_op_fn
) &isl_map_product
} },
314 __isl_give isl_set
*set_sample(__isl_take isl_set
*set
)
316 return isl_set_from_basic_set(isl_set_sample(set
));
319 __isl_give isl_map
*map_sample(__isl_take isl_map
*map
)
321 return isl_map_from_basic_map(isl_map_sample(map
));
324 static __isl_give isl_set
*set_affine_hull(__isl_take isl_set
*set
)
326 return isl_set_from_basic_set(isl_set_affine_hull(set
));
329 static __isl_give isl_map
*map_affine_hull(__isl_take isl_map
*map
)
331 return isl_map_from_basic_map(isl_map_affine_hull(map
));
334 typedef void *(*isc_un_op_fn
)(void *arg
);
336 enum isl_token_type op
;
341 struct isc_named_un_op
{
345 struct isc_named_un_op named_un_ops
[] = {
346 {"aff", { -1, isl_obj_map
, isl_obj_map
,
347 (isc_un_op_fn
) &map_affine_hull
} },
348 {"aff", { -1, isl_obj_set
, isl_obj_set
,
349 (isc_un_op_fn
) &set_affine_hull
} },
350 {"card", { -1, isl_obj_set
, isl_obj_pw_qpolynomial
,
351 (isc_un_op_fn
) &isl_set_card
} },
352 {"card", { -1, isl_obj_map
, isl_obj_pw_qpolynomial
,
353 (isc_un_op_fn
) &isl_map_card
} },
354 {"coalesce", { -1, isl_obj_set
, isl_obj_set
,
355 (isc_un_op_fn
) &isl_set_coalesce
} },
356 {"coalesce", { -1, isl_obj_map
, isl_obj_map
,
357 (isc_un_op_fn
) &isl_map_coalesce
} },
358 {"coalesce", { -1, isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial
,
359 (isc_un_op_fn
) &isl_pw_qpolynomial_coalesce
} },
360 {"coalesce", { -1, isl_obj_pw_qpolynomial_fold
,
361 isl_obj_pw_qpolynomial_fold
,
362 (isc_un_op_fn
) &isl_pw_qpolynomial_fold_coalesce
} },
363 {"deltas", { -1, isl_obj_map
, isl_obj_set
,
364 (isc_un_op_fn
) &isl_map_deltas
} },
365 {"dom", { -1, isl_obj_map
, isl_obj_set
,
366 (isc_un_op_fn
) &isl_map_domain
} },
367 {"dom", { -1, isl_obj_pw_qpolynomial
, isl_obj_set
,
368 (isc_un_op_fn
) &isl_pw_qpolynomial_domain
} },
369 {"dom", { -1, isl_obj_pw_qpolynomial_fold
, isl_obj_set
,
370 (isc_un_op_fn
) &isl_pw_qpolynomial_fold_domain
} },
371 {"ran", { -1, isl_obj_map
, isl_obj_set
,
372 (isc_un_op_fn
) &isl_map_range
} },
373 {"lexmin", { -1, isl_obj_map
, isl_obj_map
,
374 (isc_un_op_fn
) &isl_map_lexmin
} },
375 {"lexmax", { -1, isl_obj_map
, isl_obj_map
,
376 (isc_un_op_fn
) &isl_map_lexmax
} },
377 {"lexmin", { -1, isl_obj_set
, isl_obj_set
,
378 (isc_un_op_fn
) &isl_set_lexmin
} },
379 {"lexmax", { -1, isl_obj_set
, isl_obj_set
,
380 (isc_un_op_fn
) &isl_set_lexmax
} },
381 {"sample", { -1, isl_obj_set
, isl_obj_set
,
382 (isc_un_op_fn
) &set_sample
} },
383 {"sample", { -1, isl_obj_map
, isl_obj_map
,
384 (isc_un_op_fn
) &map_sample
} },
385 {"sum", { -1, isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial
,
386 (isc_un_op_fn
) &isl_pw_qpolynomial_sum
} },
388 {"ub", { -1, isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial_fold
,
389 (isc_un_op_fn
) &isl_pw_qpolynomial_upper_bound
} },
394 struct isl_named_obj
{
399 static void free_obj(struct isl_obj obj
)
401 obj
.type
->free(obj
.v
);
404 static int same_name(const void *entry
, const void *val
)
406 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
408 return !strcmp(named
->name
, val
);
411 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
412 char *name
, struct isl_obj obj
)
414 struct isl_hash_table_entry
*entry
;
416 struct isl_named_obj
*named
;
418 name_hash
= isl_hash_string(isl_hash_init(), name
);
419 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
424 free_obj(named
->obj
);
427 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
442 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
443 struct isl_hash_table
*table
, char *name
)
445 struct isl_obj obj
= { isl_obj_none
, NULL
};
446 struct isl_hash_table_entry
*entry
;
449 name_hash
= isl_hash_string(isl_hash_init(), name
);
450 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
452 struct isl_named_obj
*named
;
458 obj
.v
= obj
.type
->copy(obj
.v
);
462 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
466 struct isl_token
*tok
;
468 tok
= isl_stream_next_token(s
);
475 if (bin_ops
[i
].op
!= tok
->type
)
477 if (bin_ops
[i
].lhs
!= lhs
)
485 if (!named_bin_ops
[i
].name
)
487 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
489 if (named_bin_ops
[i
].op
.lhs
!= lhs
)
493 return &named_bin_ops
[i
].op
;
496 isl_stream_push_token(s
, tok
);
501 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
504 struct isl_token
*tok
;
506 tok
= isl_stream_next_token(s
);
511 if (!named_un_ops
[i
].name
)
513 if (named_un_ops
[i
].op
.op
!= tok
->type
)
517 return &named_un_ops
[i
].op
;
520 isl_stream_push_token(s
, tok
);
525 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
531 if (!named_un_ops
[i
].name
)
533 if (named_un_ops
[i
].op
.op
!= like
->op
)
535 if (named_un_ops
[i
].op
.arg
!= arg
)
538 return &named_un_ops
[i
].op
;
544 static int is_assign(struct isl_stream
*s
)
546 struct isl_token
*tok
;
547 struct isl_token
*tok2
;
550 tok
= isl_stream_next_token(s
);
553 if (tok
->type
!= ISL_TOKEN_IDENT
) {
554 isl_stream_push_token(s
, tok
);
558 tok2
= isl_stream_next_token(s
);
560 isl_stream_push_token(s
, tok
);
563 assign
= tok2
->type
== ISL_TOKEN_DEF
;
564 isl_stream_push_token(s
, tok2
);
565 isl_stream_push_token(s
, tok
);
570 static struct isl_obj
read_obj(struct isl_stream
*s
,
571 struct isl_hash_table
*table
);
572 static struct isl_obj
read_expr(struct isl_stream
*s
,
573 struct isl_hash_table
*table
);
575 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
576 struct isl_hash_table
*table
, struct isc_un_op
*op
)
578 struct isl_obj obj
= { isl_obj_none
, NULL
};
580 obj
= read_obj(s
, table
);
582 op
= find_matching_un_op(op
, obj
.type
);
584 isl_assert(s
->ctx
, op
, goto error
);
585 obj
.v
= op
->fn(obj
.v
);
591 obj
.type
= isl_obj_none
;
596 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
598 struct isl_list
*list
;
601 isl_assert(ctx
, obj
.type
== isl_obj_map
, goto error
);
602 list
= isl_list_alloc(ctx
, 2);
606 list
->obj
[0].type
= isl_obj_map
;
607 list
->obj
[0].v
= isl_map_transitive_closure(obj
.v
, &exact
);
608 list
->obj
[1].type
= isl_obj_bool
;
609 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
611 obj
.type
= isl_obj_list
;
612 if (exact
< 0 || !list
->obj
[0].v
)
618 obj
.type
= isl_obj_none
;
623 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
625 struct isl_list
*list
= obj
.v
;
626 struct isl_token
*tok
;
629 tok
= isl_stream_next_token(s
);
630 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
631 isl_stream_error(s
, tok
, "expecting index");
633 isl_stream_push_token(s
, tok
);
636 i
= isl_int_get_si(tok
->u
.v
);
638 isl_assert(s
, i
< list
->n
, goto error
);
639 if (isl_stream_eat(s
, ']'))
643 obj
.v
= obj
.type
->copy(obj
.v
);
650 obj
.type
= isl_obj_none
;
655 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_map
*map
,
656 struct isl_hash_table
*table
)
660 obj
= read_expr(s
, table
);
661 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
|| obj
.type
== isl_obj_map
,
664 if (obj
.type
== isl_obj_set
) {
665 obj
.v
= isl_set_apply(obj
.v
, map
);
667 obj
.v
= isl_map_apply_range(obj
.v
, map
);
671 if (isl_stream_eat(s
, ')'))
677 obj
.type
= isl_obj_none
;
682 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
684 struct isl_token
*tok
;
686 if (isl_stream_eat_if_available(s
, '+'))
687 return transitive_closure(s
->ctx
, obj
);
689 tok
= isl_stream_next_token(s
);
690 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
691 isl_stream_error(s
, tok
, "expecting -1");
693 isl_stream_push_token(s
, tok
);
697 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
, goto error
);
699 obj
.v
= isl_map_reverse(obj
.v
);
706 obj
.type
= isl_obj_none
;
711 static struct isl_obj
read_from_file(struct isl_stream
*s
)
714 struct isl_token
*tok
;
715 struct isl_stream
*s_file
;
718 tok
= isl_stream_next_token(s
);
719 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
720 isl_stream_error(s
, tok
, "expecting filename");
725 file
= fopen(tok
->u
.s
, "r");
727 isl_assert(s
->ctx
, file
, goto error
);
729 s_file
= isl_stream_new_file(s
->ctx
, file
);
735 obj
= isl_stream_read_obj(s_file
);
737 isl_stream_free(s_file
);
742 obj
.type
= isl_obj_none
;
747 static struct isl_obj
read_obj(struct isl_stream
*s
,
748 struct isl_hash_table
*table
)
750 struct isl_obj obj
= { isl_obj_none
, NULL
};
752 struct isc_un_op
*op
= NULL
;
754 if (isl_stream_eat_if_available(s
, '(')) {
755 obj
= read_expr(s
, table
);
756 if (isl_stream_eat(s
, ')'))
759 op
= read_prefix_un_op_if_available(s
);
761 return read_un_op_expr(s
, table
, op
);
763 if (isl_stream_eat_if_available(s
, read_op
))
764 return read_from_file(s
);
766 name
= isl_stream_read_ident_if_available(s
);
768 obj
= stored_obj(s
->ctx
, table
, name
);
770 obj
= isl_stream_read_obj(s
);
775 if (isl_stream_eat_if_available(s
, '^'))
777 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
778 obj
= obj_at_index(s
, obj
);
779 else if (obj
.type
== isl_obj_map
&& isl_stream_eat_if_available(s
, '('))
780 obj
= apply(s
, obj
.v
, table
);
785 obj
.type
= isl_obj_none
;
790 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
791 isl_obj_type lhs
, isl_obj_type rhs
)
798 if (bin_ops
[i
].op
!= like
->op
)
800 if (bin_ops
[i
].lhs
!= lhs
)
802 if (bin_ops
[i
].rhs
!= rhs
)
809 if (!named_bin_ops
[i
].name
)
811 if (named_bin_ops
[i
].op
.op
!= like
->op
)
813 if (named_bin_ops
[i
].op
.lhs
!= lhs
)
815 if (named_bin_ops
[i
].op
.rhs
!= rhs
)
818 return &named_bin_ops
[i
].op
;
824 static struct isl_obj
read_expr(struct isl_stream
*s
,
825 struct isl_hash_table
*table
)
827 struct isl_obj obj
= { isl_obj_none
, NULL
};
828 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
830 obj
= read_obj(s
, table
);
832 struct isc_bin_op
*op
= NULL
;
834 op
= read_bin_op_if_available(s
, obj
.type
);
838 right_obj
= read_obj(s
, table
);
840 op
= find_matching_bin_op(op
, obj
.type
, right_obj
.type
);
842 isl_assert(s
->ctx
, op
, goto error
);
843 obj
.v
= op
->fn(obj
.v
, right_obj
.v
);
851 obj
.type
= isl_obj_none
;
856 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
857 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
859 struct isl_obj obj
= { isl_obj_none
, NULL
};
862 struct isc_bin_op
*op
= NULL
;
866 if (isl_stream_is_empty(s
))
869 assign
= is_assign(s
);
871 lhs
= isl_stream_read_ident_if_available(s
);
872 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
876 obj
= read_expr(s
, table
);
877 if (obj
.type
== isl_obj_none
|| obj
.v
== NULL
)
879 if (isl_stream_eat(s
, ';'))
883 if (do_assign(s
->ctx
, table
, lhs
, obj
))
886 p
= obj
.type
->print(p
, obj
.v
);
887 p
= isl_printer_end_line(p
);
898 int free_cb(void *entry
)
900 struct isl_named_obj
*named
= entry
;
902 free_obj(named
->obj
);
909 static void register_named_ops(struct isl_stream
*s
)
913 read_op
= isl_stream_register_keyword(s
, "read");
914 assert(read_op
!= ISL_TOKEN_ERROR
);
917 if (!named_un_ops
[i
].name
)
919 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
920 named_un_ops
[i
].name
);
921 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
925 if (!named_bin_ops
[i
].name
)
927 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
928 named_bin_ops
[i
].name
);
929 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
933 int main(int argc
, char **argv
)
936 struct isl_stream
*s
;
937 struct isl_hash_table
*table
;
938 struct iscc_options
*options
;
941 options
= iscc_options_new_with_defaults();
943 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
945 ctx
= isl_ctx_alloc_with_options(options
->isl
);
947 s
= isl_stream_new_file(ctx
, stdin
);
949 table
= isl_hash_table_alloc(ctx
, 10);
951 p
= isl_printer_to_file(ctx
, stdout
);
952 p
= isl_printer_set_output_format(p
, options
->format
);
955 register_named_ops(s
);
958 p
= read_line(s
, table
, p
);
962 isl_hash_table_foreach(ctx
, table
, free_cb
);
963 isl_hash_table_free(ctx
, table
);