remove bernstein
[barvinok.git] / iscc.c
blob6814e5cbfddd9c10bca347e8eae786029540ee47
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <isl_obj.h>
5 #include <isl_stream.h>
6 #include <isl_obj_list.h>
7 #include <barvinok/barvinok.h>
9 #include "config.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},
22 {"C", ISL_FORMAT_C},
23 {0}
26 struct iscc_options {
27 struct barvinok_options *barvinok;
28 unsigned format;
31 struct isl_arg iscc_options_arg[] = {
32 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", barvinok_options_arg,
33 "barvinok options")
34 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
35 iscc_format, ISL_FORMAT_ISL, "output format")
36 ISL_ARG_END
39 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_arg)
41 static void *isl_obj_bool_copy(void *v)
43 return 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,
51 void *v)
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");
57 else
58 return isl_printer_print_str(p, "Error");
61 static void *isl_obj_bool_add(void *v1, void *v2)
63 return v1;
66 struct isl_obj_vtable isl_obj_bool_vtable = {
67 isl_obj_bool_copy,
68 isl_obj_bool_add,
69 isl_obj_bool_print,
70 isl_obj_bool_free
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);
82 isl_map_free(map1);
83 isl_map_free(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);
94 isl_map_free(map1);
95 isl_map_free(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);
106 isl_map_free(map1);
107 isl_map_free(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);
137 struct isc_bin_op {
138 enum isl_token_type op;
139 isl_obj_type lhs;
140 isl_obj_type rhs;
141 isl_obj_type res;
142 isc_bin_op_fn fn;
144 struct isc_named_bin_op {
145 char *name;
146 struct isc_bin_op op;
149 struct iscc_at {
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;
157 isl_qpolynomial *qp;
158 isl_set *set;
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));
166 return 0;
169 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_at(
170 __isl_take isl_pw_qpolynomial *pwqp, __isl_take isl_set *set)
172 struct iscc_at at;
174 at.pwqp = pwqp;
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);
180 isl_set_free(set);
182 return at.res;
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;
193 isl_qpolynomial *qp;
194 isl_set *set;
196 set = isl_set_from_point(isl_point_copy(pnt));
197 qp = isl_pw_qpolynomial_fold_eval(isl_pw_qpolynomial_fold_copy(at->pwf),
198 pnt);
200 at->res = isl_pw_qpolynomial_add_disjoint(at->res,
201 isl_pw_qpolynomial_alloc(set, qp));
203 return 0;
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;
211 at.pwf = pwf;
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);
217 isl_set_free(set);
219 return at.res;
222 struct isc_bin_op bin_ops[] = {
223 { '+', isl_obj_set, isl_obj_set,
224 isl_obj_set,
225 (isc_bin_op_fn) &isl_set_union },
226 { '+', isl_obj_map, isl_obj_map,
227 isl_obj_map,
228 (isc_bin_op_fn) &isl_map_union },
229 { '-', isl_obj_set, isl_obj_set,
230 isl_obj_set,
231 (isc_bin_op_fn) &isl_set_subtract },
232 { '-', isl_obj_map, isl_obj_map,
233 isl_obj_map,
234 (isc_bin_op_fn) &isl_map_subtract },
235 { '*', isl_obj_set, isl_obj_set,
236 isl_obj_set,
237 (isc_bin_op_fn) &isl_set_intersect },
238 { '*', isl_obj_map, isl_obj_map,
239 isl_obj_map,
240 (isc_bin_op_fn) &isl_map_intersect },
241 { '*', isl_obj_map, isl_obj_set,
242 isl_obj_map,
243 (isc_bin_op_fn) &isl_map_intersect_domain },
244 { '.', isl_obj_map, isl_obj_map,
245 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,
291 isl_obj_set,
292 (isc_bin_op_fn) &isl_set_gist },
293 { '%', isl_obj_map, isl_obj_map,
294 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 } },
309 NULL
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)
335 isl_ctx *ctx;
336 struct isl_list *list;
337 int tight;
339 ctx = isl_pw_qpolynomial_get_ctx(pwqp);
340 list = isl_list_alloc(ctx, 2);
341 if (!list)
342 goto error;
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)
349 goto error;
351 return list;
352 error:
353 isl_list_free(list);
354 return NULL;
357 typedef void *(*isc_un_op_fn)(void *arg);
358 struct isc_un_op {
359 enum isl_token_type op;
360 isl_obj_type arg;
361 isl_obj_type res;
362 isc_un_op_fn fn;
364 struct isc_named_un_op {
365 char *name;
366 struct isc_un_op 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 } },
412 NULL
415 struct isl_named_obj {
416 char *name;
417 struct isl_obj 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;
436 uint32_t name_hash;
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);
441 if (!entry)
442 goto error;
443 if (entry->data) {
444 named = entry->data;
445 free_obj(named->obj);
446 free(name);
447 } else {
448 named = isl_alloc_type(ctx, struct isl_named_obj);
449 if (!named)
450 goto error;
451 named->name = name;
452 entry->data = named;
454 named->obj = obj;
456 return 0;
457 error:
458 free_obj(obj);
459 free(name);
460 return -1;
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;
468 uint32_t name_hash;
470 name_hash = isl_hash_string(isl_hash_init(), name);
471 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
472 if (entry) {
473 struct isl_named_obj *named;
474 named = entry->data;
475 obj = named->obj;
478 free(name);
479 obj.v = obj.type->copy(obj.v);
480 return obj;
483 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
484 isl_obj_type lhs)
486 int i;
487 struct isl_token *tok;
489 tok = isl_stream_next_token(s);
490 if (!tok)
491 return NULL;
493 for (i = 0; ; ++i) {
494 if (!bin_ops[i].op)
495 break;
496 if (bin_ops[i].op != tok->type)
497 continue;
498 if (bin_ops[i].lhs != lhs)
499 continue;
501 isl_token_free(tok);
502 return &bin_ops[i];
505 for (i = 0; ; ++i) {
506 if (!named_bin_ops[i].name)
507 break;
508 if (named_bin_ops[i].op.op != tok->type)
509 continue;
510 if (named_bin_ops[i].op.lhs != lhs)
511 continue;
513 isl_token_free(tok);
514 return &named_bin_ops[i].op;
517 isl_stream_push_token(s, tok);
519 return NULL;
522 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
524 int i;
525 struct isl_token *tok;
527 tok = isl_stream_next_token(s);
528 if (!tok)
529 return NULL;
531 for (i = 0; ; ++i) {
532 if (!named_un_ops[i].name)
533 break;
534 if (named_un_ops[i].op.op != tok->type)
535 continue;
537 isl_token_free(tok);
538 return &named_un_ops[i].op;
541 isl_stream_push_token(s, tok);
543 return NULL;
546 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
547 isl_obj_type arg)
549 int i;
551 for (i = 0; ; ++i) {
552 if (!named_un_ops[i].name)
553 break;
554 if (named_un_ops[i].op.op != like->op)
555 continue;
556 if (named_un_ops[i].op.arg != arg)
557 continue;
559 return &named_un_ops[i].op;
562 return NULL;
565 static int is_assign(struct isl_stream *s)
567 struct isl_token *tok;
568 struct isl_token *tok2;
569 int assign;
571 tok = isl_stream_next_token(s);
572 if (!tok)
573 return 0;
574 if (tok->type != ISL_TOKEN_IDENT) {
575 isl_stream_push_token(s, tok);
576 return 0;
579 tok2 = isl_stream_next_token(s);
580 if (!tok2) {
581 isl_stream_push_token(s, tok);
582 return 0;
584 assign = tok2->type == ISL_TOKEN_DEF;
585 isl_stream_push_token(s, tok2);
586 isl_stream_push_token(s, tok);
588 return assign;
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);
607 obj.type = op->res;
609 return obj;
610 error:
611 free_obj(obj);
612 obj.type = isl_obj_none;
613 obj.v = NULL;
614 return obj;
617 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
619 struct isl_list *list;
620 int exact;
622 isl_assert(ctx, obj.type == isl_obj_map, goto error);
623 list = isl_list_alloc(ctx, 2);
624 if (!list)
625 goto error;
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;
631 obj.v = list;
632 obj.type = isl_obj_list;
633 if (exact < 0 || !list->obj[0].v)
634 goto error;
636 return obj;
637 error:
638 free_obj(obj);
639 obj.type = isl_obj_none;
640 obj.v = NULL;
641 return obj;
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;
648 int i;
650 tok = isl_stream_next_token(s);
651 if (!tok || tok->type != ISL_TOKEN_VALUE) {
652 isl_stream_error(s, tok, "expecting index");
653 if (tok)
654 isl_stream_push_token(s, tok);
655 goto error;
657 i = isl_int_get_si(tok->u.v);
658 isl_token_free(tok);
659 isl_assert(s, i < list->n, goto error);
660 if (isl_stream_eat(s, ']'))
661 goto error;
663 obj = list->obj[i];
664 obj.v = obj.type->copy(obj.v);
666 isl_list_free(list);
668 return obj;
669 error:
670 free_obj(obj);
671 obj.type = isl_obj_none;
672 obj.v = NULL;
673 return obj;
676 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_map *map,
677 struct isl_hash_table *table)
679 struct isl_obj obj;
681 obj = read_expr(s, table);
682 isl_assert(s->ctx, obj.type == isl_obj_set || obj.type == isl_obj_map,
683 goto error);
685 if (obj.type == isl_obj_set) {
686 obj.v = isl_set_apply(obj.v, map);
687 } else
688 obj.v = isl_map_apply_range(obj.v, map);
689 if (!obj.v)
690 goto error;
692 if (isl_stream_eat(s, ')'))
693 goto error;
695 return obj;
696 error:
697 free_obj(obj);
698 obj.type = isl_obj_none;
699 obj.v = NULL;
700 return obj;
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");
713 if (tok)
714 isl_stream_push_token(s, tok);
715 goto error;
717 isl_token_free(tok);
718 isl_assert(s->ctx, obj.type == isl_obj_map, goto error);
720 obj.v = isl_map_reverse(obj.v);
721 if (!obj.v)
722 goto error;
724 return obj;
725 error:
726 free_obj(obj);
727 obj.type = isl_obj_none;
728 obj.v = NULL;
729 return obj;
732 static struct isl_obj read_from_file(struct isl_stream *s)
734 struct isl_obj obj;
735 struct isl_token *tok;
736 struct isl_stream *s_file;
737 FILE *file;
739 tok = isl_stream_next_token(s);
740 if (!tok || tok->type != ISL_TOKEN_STRING) {
741 isl_stream_error(s, tok, "expecting filename");
742 isl_token_free(tok);
743 goto error;
746 file = fopen(tok->u.s, "r");
747 isl_token_free(tok);
748 isl_assert(s->ctx, file, goto error);
750 s_file = isl_stream_new_file(s->ctx, file);
751 if (!s_file) {
752 fclose(file);
753 goto error;
756 obj = isl_stream_read_obj(s_file);
758 isl_stream_free(s_file);
759 fclose(file);
761 return obj;
762 error:
763 obj.type = isl_obj_none;
764 obj.v = NULL;
765 return obj;
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 };
772 char *name = 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, ')'))
778 goto error;
779 } else {
780 op = read_prefix_un_op_if_available(s);
781 if (op)
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);
788 if (name) {
789 obj = stored_obj(s->ctx, table, name);
790 } else {
791 obj = isl_stream_read_obj(s);
792 assert(obj.v);
796 if (isl_stream_eat_if_available(s, '^'))
797 obj = power(s, obj);
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);
803 return obj;
804 error:
805 free_obj(obj);
806 obj.type = isl_obj_none;
807 obj.v = NULL;
808 return obj;
811 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
812 isl_obj_type lhs, isl_obj_type rhs)
814 int i;
816 for (i = 0; ; ++i) {
817 if (!bin_ops[i].op)
818 break;
819 if (bin_ops[i].op != like->op)
820 continue;
821 if (bin_ops[i].lhs != lhs)
822 continue;
823 if (bin_ops[i].rhs != rhs)
824 continue;
826 return &bin_ops[i];
829 for (i = 0; ; ++i) {
830 if (!named_bin_ops[i].name)
831 break;
832 if (named_bin_ops[i].op.op != like->op)
833 continue;
834 if (named_bin_ops[i].op.lhs != lhs)
835 continue;
836 if (named_bin_ops[i].op.rhs != rhs)
837 continue;
839 return &named_bin_ops[i].op;
842 return NULL;
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);
852 for (;;) {
853 struct isc_bin_op *op = NULL;
855 op = read_bin_op_if_available(s, obj.type);
856 if (!op)
857 break;
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);
865 obj.type = op->res;
868 return obj;
869 error:
870 free_obj(right_obj);
871 free_obj(obj);
872 obj.type = isl_obj_none;
873 obj.v = NULL;
874 return obj;
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 };
881 char *lhs = NULL;
882 int assign = 0;
883 struct isc_bin_op *op = NULL;
885 if (!p)
886 return NULL;
887 if (isl_stream_is_empty(s))
888 return p;
890 assign = is_assign(s);
891 if (assign) {
892 lhs = isl_stream_read_ident_if_available(s);
893 if (isl_stream_eat(s, ISL_TOKEN_DEF))
894 goto error;
897 obj = read_expr(s, table);
898 if (obj.type == isl_obj_none || obj.v == NULL)
899 goto error;
900 if (isl_stream_eat(s, ';'))
901 goto error;
903 if (assign) {
904 if (do_assign(s->ctx, table, lhs, obj))
905 return;
906 } else {
907 p = obj.type->print(p, obj.v);
908 p = isl_printer_end_line(p);
909 free_obj(obj);
912 return p;
913 error:
914 free(lhs);
915 free_obj(obj);
916 return p;
919 int free_cb(void *entry)
921 struct isl_named_obj *named = entry;
923 free_obj(named->obj);
924 free(named->name);
925 free(named);
927 return 0;
930 static void register_named_ops(struct isl_stream *s)
932 int i;
934 read_op = isl_stream_register_keyword(s, "read");
935 assert(read_op != ISL_TOKEN_ERROR);
937 for (i = 0; ; ++i) {
938 if (!named_un_ops[i].name)
939 break;
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);
945 for (i = 0; ; ++i) {
946 if (!named_bin_ops[i].name)
947 break;
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)
956 struct isl_ctx *ctx;
957 struct isl_stream *s;
958 struct isl_hash_table *table;
959 struct iscc_options *options;
960 isl_printer *p;
962 options = iscc_options_new_with_defaults();
963 assert(options);
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);
968 assert(s);
969 table = isl_hash_table_alloc(ctx, 10);
970 assert(table);
971 p = isl_printer_to_file(ctx, stdout);
972 p = isl_printer_set_output_format(p, options->format);
973 assert(p);
975 register_named_ops(s);
977 while (!s->eof) {
978 p = read_line(s, table, p);
981 isl_printer_free(p);
982 isl_hash_table_foreach(ctx, table, free_cb);
983 isl_hash_table_free(ctx, table);
984 isl_stream_free(s);
985 isl_ctx_free(ctx);
987 return 0;