5 #include <isl_stream.h>
6 #include <isl_obj_list.h>
7 #include <barvinok/barvinok.h>
11 static int isl_bool_false
= 0;
12 static int isl_bool_true
= 1;
13 static int isl_bool_error
= -1;
15 static enum isl_token_type read_op
;
17 struct isl_arg_choice iscc_format
[] = {
18 {"isl", ISL_FORMAT_ISL
},
19 {"omega", ISL_FORMAT_OMEGA
},
20 {"polylib", ISL_FORMAT_POLYLIB
},
21 {"latex", ISL_FORMAT_LATEX
},
27 struct barvinok_options
*barvinok
;
31 struct isl_arg iscc_options_arg
[] = {
32 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", barvinok_options_arg
,
34 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
35 iscc_format
, ISL_FORMAT_ISL
, "output format")
39 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
41 static void *isl_obj_bool_copy(void *v
)
46 static void isl_obj_bool_free(void *v
)
50 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
53 if (v
== &isl_bool_true
)
54 return isl_printer_print_str(p
, "True");
55 else if (v
== &isl_bool_false
)
56 return isl_printer_print_str(p
, "False");
58 return isl_printer_print_str(p
, "Error");
61 static void *isl_obj_bool_add(void *v1
, void *v2
)
66 struct isl_obj_vtable isl_obj_bool_vtable
= {
72 #define isl_obj_bool (&isl_obj_bool_vtable)
74 int *isl_bool_from_int(int res
)
76 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
79 int *map_is_equal(__isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
81 int res
= isl_map_is_equal(map1
, map2
);
84 return isl_bool_from_int(res
);
86 int *set_is_equal(__isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
88 return map_is_equal((isl_map
*)set1
, (isl_map
*)set2
);
91 int *map_is_subset(__isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
93 int res
= isl_map_is_subset(map1
, map2
);
96 return isl_bool_from_int(res
);
98 int *set_is_subset(__isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
100 return map_is_subset((isl_map
*)set1
, (isl_map
*)set2
);
103 int *map_is_strict_subset(__isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
105 int res
= isl_map_is_strict_subset(map1
, map2
);
108 return isl_bool_from_int(res
);
110 int *set_is_strict_subset(__isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
112 return map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
115 int *map_is_superset(__isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
117 return map_is_subset(map2
, map1
);
119 int *set_is_superset(__isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
121 return set_is_subset(set2
, set1
);
124 int *map_is_strict_superset(__isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
126 return map_is_strict_subset(map2
, map1
);
128 int *set_is_strict_superset(__isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
130 return set_is_strict_subset(set2
, set1
);
133 extern struct isl_obj_vtable isl_obj_list_vtable
;
134 #define isl_obj_list (&isl_obj_list_vtable)
136 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
138 enum isl_token_type op
;
144 struct isc_named_bin_op
{
146 struct isc_bin_op op
;
150 isl_pw_qpolynomial
*pwqp
;
151 isl_pw_qpolynomial
*res
;
154 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
156 struct iscc_at
*at
= (struct iscc_at
*) user
;
160 set
= isl_set_from_point(isl_point_copy(pnt
));
161 qp
= isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(at
->pwqp
), pnt
);
163 at
->res
= isl_pw_qpolynomial_add_disjoint(at
->res
,
164 isl_pw_qpolynomial_alloc(set
, qp
));
169 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_at(
170 __isl_take isl_pw_qpolynomial
*pwqp
, __isl_take isl_set
*set
)
175 at
.res
= isl_pw_qpolynomial_zero(isl_set_get_dim(set
));
177 isl_set_foreach_point(set
, eval_at
, &at
);
179 isl_pw_qpolynomial_free(pwqp
);
185 struct iscc_fold_at
{
186 isl_pw_qpolynomial_fold
*pwf
;
187 isl_pw_qpolynomial
*res
;
190 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
192 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
196 set
= isl_set_from_point(isl_point_copy(pnt
));
197 qp
= isl_pw_qpolynomial_fold_eval(isl_pw_qpolynomial_fold_copy(at
->pwf
),
200 at
->res
= isl_pw_qpolynomial_add_disjoint(at
->res
,
201 isl_pw_qpolynomial_alloc(set
, qp
));
206 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_fold_at(
207 __isl_take isl_pw_qpolynomial_fold
*pwf
, __isl_take isl_set
*set
)
209 struct iscc_fold_at at
;
212 at
.res
= isl_pw_qpolynomial_zero(isl_set_get_dim(set
));
214 isl_set_foreach_point(set
, eval_fold_at
, &at
);
216 isl_pw_qpolynomial_fold_free(pwf
);
222 struct isc_bin_op bin_ops
[] = {
223 { '+', isl_obj_set
, isl_obj_set
,
225 (isc_bin_op_fn
) &isl_set_union
},
226 { '+', isl_obj_map
, isl_obj_map
,
228 (isc_bin_op_fn
) &isl_map_union
},
229 { '-', isl_obj_set
, isl_obj_set
,
231 (isc_bin_op_fn
) &isl_set_subtract
},
232 { '-', isl_obj_map
, isl_obj_map
,
234 (isc_bin_op_fn
) &isl_map_subtract
},
235 { '*', isl_obj_set
, isl_obj_set
,
237 (isc_bin_op_fn
) &isl_set_intersect
},
238 { '*', isl_obj_map
, isl_obj_map
,
240 (isc_bin_op_fn
) &isl_map_intersect
},
241 { '*', isl_obj_map
, isl_obj_set
,
243 (isc_bin_op_fn
) &isl_map_intersect_domain
},
244 { '.', isl_obj_map
, isl_obj_map
,
246 (isc_bin_op_fn
) &isl_map_apply_range
},
247 { ISL_TOKEN_TO
, isl_obj_set
, isl_obj_set
, isl_obj_map
,
248 (isc_bin_op_fn
) &isl_map_from_domain_and_range
},
249 { '=', isl_obj_set
, isl_obj_set
, isl_obj_bool
,
250 (isc_bin_op_fn
) &set_is_equal
},
251 { '=', isl_obj_map
, isl_obj_map
, isl_obj_bool
,
252 (isc_bin_op_fn
) &map_is_equal
},
253 { ISL_TOKEN_LE
, isl_obj_set
, isl_obj_set
, isl_obj_bool
,
254 (isc_bin_op_fn
) &set_is_subset
},
255 { ISL_TOKEN_LE
, isl_obj_map
, isl_obj_map
, isl_obj_bool
,
256 (isc_bin_op_fn
) &map_is_subset
},
257 { ISL_TOKEN_LT
, isl_obj_set
, isl_obj_set
, isl_obj_bool
,
258 (isc_bin_op_fn
) &set_is_strict_subset
},
259 { ISL_TOKEN_LT
, isl_obj_map
, isl_obj_map
, isl_obj_bool
,
260 (isc_bin_op_fn
) &map_is_strict_subset
},
261 { ISL_TOKEN_GE
, isl_obj_set
, isl_obj_set
, isl_obj_bool
,
262 (isc_bin_op_fn
) &set_is_superset
},
263 { ISL_TOKEN_GE
, isl_obj_map
, isl_obj_map
, isl_obj_bool
,
264 (isc_bin_op_fn
) &map_is_superset
},
265 { ISL_TOKEN_GT
, isl_obj_set
, isl_obj_set
, isl_obj_bool
,
266 (isc_bin_op_fn
) &set_is_strict_superset
},
267 { ISL_TOKEN_GT
, isl_obj_map
, isl_obj_map
, isl_obj_bool
,
268 (isc_bin_op_fn
) &map_is_strict_superset
},
269 { '+', isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial
,
270 isl_obj_pw_qpolynomial
,
271 (isc_bin_op_fn
) &isl_pw_qpolynomial_add
},
272 { '-', isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial
,
273 isl_obj_pw_qpolynomial
,
274 (isc_bin_op_fn
) &isl_pw_qpolynomial_sub
},
275 { '*', isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial
,
276 isl_obj_pw_qpolynomial
,
277 (isc_bin_op_fn
) &isl_pw_qpolynomial_mul
},
278 { '*', isl_obj_pw_qpolynomial
, isl_obj_set
,
279 isl_obj_pw_qpolynomial
,
280 (isc_bin_op_fn
) &isl_pw_qpolynomial_intersect_domain
},
281 { '*', isl_obj_pw_qpolynomial_fold
, isl_obj_set
,
282 isl_obj_pw_qpolynomial_fold
,
283 (isc_bin_op_fn
) &isl_pw_qpolynomial_fold_intersect_domain
},
284 { '@', isl_obj_pw_qpolynomial
, isl_obj_set
,
285 isl_obj_pw_qpolynomial
,
286 (isc_bin_op_fn
) &isl_pw_qpolynomial_at
},
287 { '@', isl_obj_pw_qpolynomial_fold
, isl_obj_set
,
288 isl_obj_pw_qpolynomial
,
289 (isc_bin_op_fn
) &isl_pw_qpolynomial_fold_at
},
290 { '%', isl_obj_set
, isl_obj_set
,
292 (isc_bin_op_fn
) &isl_set_gist
},
293 { '%', isl_obj_map
, isl_obj_map
,
295 (isc_bin_op_fn
) &isl_map_gist
},
296 { '%', isl_obj_pw_qpolynomial
, isl_obj_set
,
297 isl_obj_pw_qpolynomial
,
298 (isc_bin_op_fn
) &isl_pw_qpolynomial_gist
},
299 { '%', isl_obj_pw_qpolynomial_fold
, isl_obj_set
,
300 isl_obj_pw_qpolynomial_fold
,
301 (isc_bin_op_fn
) &isl_pw_qpolynomial_fold_gist
},
304 struct isc_named_bin_op named_bin_ops
[] = {
305 { "cross", { -1, isl_obj_set
, isl_obj_set
, isl_obj_set
,
306 (isc_bin_op_fn
) &isl_set_product
} },
307 { "cross", { -1, isl_obj_map
, isl_obj_map
, isl_obj_map
,
308 (isc_bin_op_fn
) &isl_map_product
} },
312 __isl_give isl_set
*set_sample(__isl_take isl_set
*set
)
314 return isl_set_from_basic_set(isl_set_sample(set
));
317 __isl_give isl_map
*map_sample(__isl_take isl_map
*map
)
319 return isl_map_from_basic_map(isl_map_sample(map
));
322 static __isl_give isl_set
*set_affine_hull(__isl_take isl_set
*set
)
324 return isl_set_from_basic_set(isl_set_affine_hull(set
));
327 static __isl_give isl_map
*map_affine_hull(__isl_take isl_map
*map
)
329 return isl_map_from_basic_map(isl_map_affine_hull(map
));
332 static __isl_give
struct isl_list
*pw_qpolynomial_upper_bound(
333 __isl_take isl_pw_qpolynomial
*pwqp
)
336 struct isl_list
*list
;
339 ctx
= isl_pw_qpolynomial_get_ctx(pwqp
);
340 list
= isl_list_alloc(ctx
, 2);
344 list
->obj
[0].type
= isl_obj_pw_qpolynomial_fold
;
345 list
->obj
[0].v
= isl_pw_qpolynomial_bound(pwqp
, isl_fold_max
, &tight
);
346 list
->obj
[1].type
= isl_obj_bool
;
347 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
348 if (tight
< 0 || !list
->obj
[0].v
)
357 typedef void *(*isc_un_op_fn
)(void *arg
);
359 enum isl_token_type op
;
364 struct isc_named_un_op
{
368 struct isc_named_un_op named_un_ops
[] = {
369 {"aff", { -1, isl_obj_map
, isl_obj_map
,
370 (isc_un_op_fn
) &map_affine_hull
} },
371 {"aff", { -1, isl_obj_set
, isl_obj_set
,
372 (isc_un_op_fn
) &set_affine_hull
} },
373 {"card", { -1, isl_obj_set
, isl_obj_pw_qpolynomial
,
374 (isc_un_op_fn
) &isl_set_card
} },
375 {"card", { -1, isl_obj_map
, isl_obj_pw_qpolynomial
,
376 (isc_un_op_fn
) &isl_map_card
} },
377 {"coalesce", { -1, isl_obj_set
, isl_obj_set
,
378 (isc_un_op_fn
) &isl_set_coalesce
} },
379 {"coalesce", { -1, isl_obj_map
, isl_obj_map
,
380 (isc_un_op_fn
) &isl_map_coalesce
} },
381 {"coalesce", { -1, isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial
,
382 (isc_un_op_fn
) &isl_pw_qpolynomial_coalesce
} },
383 {"coalesce", { -1, isl_obj_pw_qpolynomial_fold
,
384 isl_obj_pw_qpolynomial_fold
,
385 (isc_un_op_fn
) &isl_pw_qpolynomial_fold_coalesce
} },
386 {"deltas", { -1, isl_obj_map
, isl_obj_set
,
387 (isc_un_op_fn
) &isl_map_deltas
} },
388 {"dom", { -1, isl_obj_map
, isl_obj_set
,
389 (isc_un_op_fn
) &isl_map_domain
} },
390 {"dom", { -1, isl_obj_pw_qpolynomial
, isl_obj_set
,
391 (isc_un_op_fn
) &isl_pw_qpolynomial_domain
} },
392 {"dom", { -1, isl_obj_pw_qpolynomial_fold
, isl_obj_set
,
393 (isc_un_op_fn
) &isl_pw_qpolynomial_fold_domain
} },
394 {"ran", { -1, isl_obj_map
, isl_obj_set
,
395 (isc_un_op_fn
) &isl_map_range
} },
396 {"lexmin", { -1, isl_obj_map
, isl_obj_map
,
397 (isc_un_op_fn
) &isl_map_lexmin
} },
398 {"lexmax", { -1, isl_obj_map
, isl_obj_map
,
399 (isc_un_op_fn
) &isl_map_lexmax
} },
400 {"lexmin", { -1, isl_obj_set
, isl_obj_set
,
401 (isc_un_op_fn
) &isl_set_lexmin
} },
402 {"lexmax", { -1, isl_obj_set
, isl_obj_set
,
403 (isc_un_op_fn
) &isl_set_lexmax
} },
404 {"sample", { -1, isl_obj_set
, isl_obj_set
,
405 (isc_un_op_fn
) &set_sample
} },
406 {"sample", { -1, isl_obj_map
, isl_obj_map
,
407 (isc_un_op_fn
) &map_sample
} },
408 {"sum", { -1, isl_obj_pw_qpolynomial
, isl_obj_pw_qpolynomial
,
409 (isc_un_op_fn
) &isl_pw_qpolynomial_sum
} },
410 {"ub", { -1, isl_obj_pw_qpolynomial
, isl_obj_list
,
411 (isc_un_op_fn
) &pw_qpolynomial_upper_bound
} },
415 struct isl_named_obj
{
420 static void free_obj(struct isl_obj obj
)
422 obj
.type
->free(obj
.v
);
425 static int same_name(const void *entry
, const void *val
)
427 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
429 return !strcmp(named
->name
, val
);
432 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
433 char *name
, struct isl_obj obj
)
435 struct isl_hash_table_entry
*entry
;
437 struct isl_named_obj
*named
;
439 name_hash
= isl_hash_string(isl_hash_init(), name
);
440 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
445 free_obj(named
->obj
);
448 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
463 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
464 struct isl_hash_table
*table
, char *name
)
466 struct isl_obj obj
= { isl_obj_none
, NULL
};
467 struct isl_hash_table_entry
*entry
;
470 name_hash
= isl_hash_string(isl_hash_init(), name
);
471 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
473 struct isl_named_obj
*named
;
479 obj
.v
= obj
.type
->copy(obj
.v
);
483 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
487 struct isl_token
*tok
;
489 tok
= isl_stream_next_token(s
);
496 if (bin_ops
[i
].op
!= tok
->type
)
498 if (bin_ops
[i
].lhs
!= lhs
)
506 if (!named_bin_ops
[i
].name
)
508 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
510 if (named_bin_ops
[i
].op
.lhs
!= lhs
)
514 return &named_bin_ops
[i
].op
;
517 isl_stream_push_token(s
, tok
);
522 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
525 struct isl_token
*tok
;
527 tok
= isl_stream_next_token(s
);
532 if (!named_un_ops
[i
].name
)
534 if (named_un_ops
[i
].op
.op
!= tok
->type
)
538 return &named_un_ops
[i
].op
;
541 isl_stream_push_token(s
, tok
);
546 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
552 if (!named_un_ops
[i
].name
)
554 if (named_un_ops
[i
].op
.op
!= like
->op
)
556 if (named_un_ops
[i
].op
.arg
!= arg
)
559 return &named_un_ops
[i
].op
;
565 static int is_assign(struct isl_stream
*s
)
567 struct isl_token
*tok
;
568 struct isl_token
*tok2
;
571 tok
= isl_stream_next_token(s
);
574 if (tok
->type
!= ISL_TOKEN_IDENT
) {
575 isl_stream_push_token(s
, tok
);
579 tok2
= isl_stream_next_token(s
);
581 isl_stream_push_token(s
, tok
);
584 assign
= tok2
->type
== ISL_TOKEN_DEF
;
585 isl_stream_push_token(s
, tok2
);
586 isl_stream_push_token(s
, tok
);
591 static struct isl_obj
read_obj(struct isl_stream
*s
,
592 struct isl_hash_table
*table
);
593 static struct isl_obj
read_expr(struct isl_stream
*s
,
594 struct isl_hash_table
*table
);
596 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
597 struct isl_hash_table
*table
, struct isc_un_op
*op
)
599 struct isl_obj obj
= { isl_obj_none
, NULL
};
601 obj
= read_obj(s
, table
);
603 op
= find_matching_un_op(op
, obj
.type
);
605 isl_assert(s
->ctx
, op
, goto error
);
606 obj
.v
= op
->fn(obj
.v
);
612 obj
.type
= isl_obj_none
;
617 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
619 struct isl_list
*list
;
622 isl_assert(ctx
, obj
.type
== isl_obj_map
, goto error
);
623 list
= isl_list_alloc(ctx
, 2);
627 list
->obj
[0].type
= isl_obj_map
;
628 list
->obj
[0].v
= isl_map_transitive_closure(obj
.v
, &exact
);
629 list
->obj
[1].type
= isl_obj_bool
;
630 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
632 obj
.type
= isl_obj_list
;
633 if (exact
< 0 || !list
->obj
[0].v
)
639 obj
.type
= isl_obj_none
;
644 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
646 struct isl_list
*list
= obj
.v
;
647 struct isl_token
*tok
;
650 tok
= isl_stream_next_token(s
);
651 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
652 isl_stream_error(s
, tok
, "expecting index");
654 isl_stream_push_token(s
, tok
);
657 i
= isl_int_get_si(tok
->u
.v
);
659 isl_assert(s
, i
< list
->n
, goto error
);
660 if (isl_stream_eat(s
, ']'))
664 obj
.v
= obj
.type
->copy(obj
.v
);
671 obj
.type
= isl_obj_none
;
676 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_map
*map
,
677 struct isl_hash_table
*table
)
681 obj
= read_expr(s
, table
);
682 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
|| obj
.type
== isl_obj_map
,
685 if (obj
.type
== isl_obj_set
) {
686 obj
.v
= isl_set_apply(obj
.v
, map
);
688 obj
.v
= isl_map_apply_range(obj
.v
, map
);
692 if (isl_stream_eat(s
, ')'))
698 obj
.type
= isl_obj_none
;
703 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
705 struct isl_token
*tok
;
707 if (isl_stream_eat_if_available(s
, '+'))
708 return transitive_closure(s
->ctx
, obj
);
710 tok
= isl_stream_next_token(s
);
711 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
712 isl_stream_error(s
, tok
, "expecting -1");
714 isl_stream_push_token(s
, tok
);
718 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
, goto error
);
720 obj
.v
= isl_map_reverse(obj
.v
);
727 obj
.type
= isl_obj_none
;
732 static struct isl_obj
read_from_file(struct isl_stream
*s
)
735 struct isl_token
*tok
;
736 struct isl_stream
*s_file
;
739 tok
= isl_stream_next_token(s
);
740 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
741 isl_stream_error(s
, tok
, "expecting filename");
746 file
= fopen(tok
->u
.s
, "r");
748 isl_assert(s
->ctx
, file
, goto error
);
750 s_file
= isl_stream_new_file(s
->ctx
, file
);
756 obj
= isl_stream_read_obj(s_file
);
758 isl_stream_free(s_file
);
763 obj
.type
= isl_obj_none
;
768 static struct isl_obj
read_obj(struct isl_stream
*s
,
769 struct isl_hash_table
*table
)
771 struct isl_obj obj
= { isl_obj_none
, NULL
};
773 struct isc_un_op
*op
= NULL
;
775 if (isl_stream_eat_if_available(s
, '(')) {
776 obj
= read_expr(s
, table
);
777 if (isl_stream_eat(s
, ')'))
780 op
= read_prefix_un_op_if_available(s
);
782 return read_un_op_expr(s
, table
, op
);
784 if (isl_stream_eat_if_available(s
, read_op
))
785 return read_from_file(s
);
787 name
= isl_stream_read_ident_if_available(s
);
789 obj
= stored_obj(s
->ctx
, table
, name
);
791 obj
= isl_stream_read_obj(s
);
796 if (isl_stream_eat_if_available(s
, '^'))
798 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
799 obj
= obj_at_index(s
, obj
);
800 else if (obj
.type
== isl_obj_map
&& isl_stream_eat_if_available(s
, '('))
801 obj
= apply(s
, obj
.v
, table
);
806 obj
.type
= isl_obj_none
;
811 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
812 isl_obj_type lhs
, isl_obj_type rhs
)
819 if (bin_ops
[i
].op
!= like
->op
)
821 if (bin_ops
[i
].lhs
!= lhs
)
823 if (bin_ops
[i
].rhs
!= rhs
)
830 if (!named_bin_ops
[i
].name
)
832 if (named_bin_ops
[i
].op
.op
!= like
->op
)
834 if (named_bin_ops
[i
].op
.lhs
!= lhs
)
836 if (named_bin_ops
[i
].op
.rhs
!= rhs
)
839 return &named_bin_ops
[i
].op
;
845 static struct isl_obj
read_expr(struct isl_stream
*s
,
846 struct isl_hash_table
*table
)
848 struct isl_obj obj
= { isl_obj_none
, NULL
};
849 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
851 obj
= read_obj(s
, table
);
853 struct isc_bin_op
*op
= NULL
;
855 op
= read_bin_op_if_available(s
, obj
.type
);
859 right_obj
= read_obj(s
, table
);
861 op
= find_matching_bin_op(op
, obj
.type
, right_obj
.type
);
863 isl_assert(s
->ctx
, op
, goto error
);
864 obj
.v
= op
->fn(obj
.v
, right_obj
.v
);
872 obj
.type
= isl_obj_none
;
877 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
878 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
880 struct isl_obj obj
= { isl_obj_none
, NULL
};
883 struct isc_bin_op
*op
= NULL
;
887 if (isl_stream_is_empty(s
))
890 assign
= is_assign(s
);
892 lhs
= isl_stream_read_ident_if_available(s
);
893 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
897 obj
= read_expr(s
, table
);
898 if (obj
.type
== isl_obj_none
|| obj
.v
== NULL
)
900 if (isl_stream_eat(s
, ';'))
904 if (do_assign(s
->ctx
, table
, lhs
, obj
))
907 p
= obj
.type
->print(p
, obj
.v
);
908 p
= isl_printer_end_line(p
);
919 int free_cb(void *entry
)
921 struct isl_named_obj
*named
= entry
;
923 free_obj(named
->obj
);
930 static void register_named_ops(struct isl_stream
*s
)
934 read_op
= isl_stream_register_keyword(s
, "read");
935 assert(read_op
!= ISL_TOKEN_ERROR
);
938 if (!named_un_ops
[i
].name
)
940 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
941 named_un_ops
[i
].name
);
942 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
946 if (!named_bin_ops
[i
].name
)
948 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
949 named_bin_ops
[i
].name
);
950 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
954 int main(int argc
, char **argv
)
957 struct isl_stream
*s
;
958 struct isl_hash_table
*table
;
959 struct iscc_options
*options
;
962 options
= iscc_options_new_with_defaults();
964 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
966 ctx
= isl_ctx_alloc_with_options(iscc_options_arg
, options
);
967 s
= isl_stream_new_file(ctx
, stdin
);
969 table
= isl_hash_table_alloc(ctx
, 10);
971 p
= isl_printer_to_file(ctx
, stdout
);
972 p
= isl_printer_set_output_format(p
, options
->format
);
975 register_named_ops(s
);
978 p
= read_line(s
, table
, p
);
982 isl_hash_table_foreach(ctx
, table
, free_cb
);
983 isl_hash_table_free(ctx
, table
);