isl_map_read: use more uniform way of reading in polylib constraint coefficients
[platform/upstream/isl.git] / isl_input.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
11  */
12
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <strings.h>
17 #include <isl_set.h>
18 #include <isl_seq.h>
19 #include "isl_stream.h"
20 #include "isl_map_private.h"
21
22 struct variable {
23         char                    *name;
24         int                      pos;
25         struct variable         *next;
26 };
27
28 struct vars {
29         struct isl_ctx  *ctx;
30         int              n;
31         struct variable *v;
32 };
33
34 static struct vars *vars_new(struct isl_ctx *ctx)
35 {
36         struct vars *v;
37         v = isl_alloc_type(ctx, struct vars);
38         if (!v)
39                 return NULL;
40         v->ctx = ctx;
41         v->n = 0;
42         v->v = NULL;
43         return v;
44 }
45
46 static void variable_free(struct variable *var)
47 {
48         while (var) {
49                 struct variable *next = var->next;
50                 free(var->name);
51                 free(var);
52                 var = next;
53         }
54 }
55
56 static void vars_free(struct vars *v)
57 {
58         if (!v)
59                 return;
60         variable_free(v->v);
61         free(v);
62 }
63
64 static struct variable *variable_new(struct vars *v, const char *name, int len,
65                                 int pos)
66 {
67         struct variable *var;
68         var = isl_alloc_type(v->ctx, struct variable);
69         if (!var)
70                 goto error;
71         var->name = strdup(name);
72         var->name[len] = '\0';
73         var->pos = pos;
74         var->next = v->v;
75         return var;
76 error:
77         variable_free(v->v);
78         return NULL;
79 }
80
81 static int vars_pos(struct vars *v, const char *s, int len)
82 {
83         int pos;
84         struct variable *q;
85
86         if (len == -1)
87                 len = strlen(s);
88         for (q = v->v; q; q = q->next) {
89                 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
90                         break;
91         }
92         if (q)
93                 pos = q->pos;
94         else {
95                 pos = v->n;
96                 v->v = variable_new(v, s, len, v->n);
97                 if (!v->v)
98                         return -1;
99                 v->n++;
100         }
101         return pos;
102 }
103
104 static struct vars *read_var_list(struct isl_stream *s, struct vars *v)
105 {
106         struct isl_token *tok;
107
108         while ((tok = isl_stream_next_token(s)) != NULL) {
109                 int p;
110                 int n = v->n;
111
112                 if (tok->type != ISL_TOKEN_IDENT)
113                         break;
114
115                 p = vars_pos(v, tok->u.s, -1);
116                 if (p < 0)
117                         goto error;
118                 if (p < n) {
119                         isl_stream_error(s, tok, "expecting unique identifier");
120                         goto error;
121                 }
122                 isl_token_free(tok);
123                 tok = isl_stream_next_token(s);
124                 if (!tok || tok->type != ',')
125                         break;
126
127                 isl_token_free(tok);
128         }
129         if (tok)
130                 isl_stream_push_token(s, tok);
131
132         return v;
133 error:
134         isl_token_free(tok);
135         vars_free(v);
136         return NULL;
137 }
138
139 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
140 {
141         struct isl_token *tok = NULL;
142         struct isl_vec *aff;
143         int sign = 1;
144
145         aff = isl_vec_alloc(v->ctx, 1 + v->n);
146         isl_seq_clr(aff->el, aff->size);
147         if (!aff)
148                 return NULL;
149
150         for (;;) {
151                 tok = isl_stream_next_token(s);
152                 if (!tok) {
153                         isl_stream_error(s, NULL, "unexpected EOF");
154                         goto error;
155                 }
156                 if (tok->type == ISL_TOKEN_IDENT) {
157                         int n = v->n;
158                         int pos = vars_pos(v, tok->u.s, -1);
159                         if (pos < 0)
160                                 goto error;
161                         if (pos >= n) {
162                                 isl_stream_error(s, tok, "unknown identifier");
163                                 goto error;
164                         }
165                         if (sign > 0)
166                                 isl_int_add_ui(aff->el[1 + pos],
167                                                aff->el[1 + pos], 1);
168                         else
169                                 isl_int_sub_ui(aff->el[1 + pos],
170                                                aff->el[1 + pos], 1);
171                         sign = 1;
172                 } else if (tok->type == ISL_TOKEN_VALUE) {
173                         struct isl_token *tok2;
174                         int n = v->n;
175                         int pos = -1;
176                         tok2 = isl_stream_next_token(s);
177                         if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
178                                 pos = vars_pos(v, tok2->u.s, -1);
179                                 if (pos < 0)
180                                         goto error;
181                                 if (pos >= n) {
182                                         isl_stream_error(s, tok2,
183                                                 "unknown identifier");
184                                         isl_token_free(tok2);
185                                         goto error;
186                                 }
187                                 isl_token_free(tok2);
188                         } else if (tok2)
189                                 isl_stream_push_token(s, tok2);
190                         if (sign < 0)
191                                 isl_int_neg(tok->u.v, tok->u.v);
192                         isl_int_add(aff->el[1 + pos],
193                                         aff->el[1 + pos], tok->u.v);
194                         sign = 1;
195                 } else if (tok->type == '-') {
196                         sign = -sign;
197                 } else if (tok->type == '+') {
198                         /* nothing */
199                 } else {
200                         isl_stream_push_token(s, tok);
201                         break;
202                 }
203                 isl_token_free(tok);
204         }
205
206         return aff;
207 error:
208         isl_vec_free(aff);
209         return NULL;
210 }
211
212 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
213         struct vars *v, struct isl_basic_map *bmap, int k)
214 {
215         struct isl_token *tok;
216         int seen_paren = 0;
217         struct isl_vec *aff;
218
219         if (isl_stream_eat(s, '['))
220                 goto error;
221
222         tok = isl_stream_next_token(s);
223         if (!tok)
224                 goto error;
225         if (tok->type == '(') {
226                 seen_paren = 1;
227                 isl_token_free(tok);
228         } else
229                 isl_stream_push_token(s, tok);
230
231         aff = accept_affine(s, v);
232         if (!aff)
233                 goto error;
234
235         isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
236
237         isl_vec_free(aff);
238
239         if (seen_paren && isl_stream_eat(s, ')'))
240                 goto error;
241         if (isl_stream_eat(s, '/'))
242                 goto error;
243
244         tok = isl_stream_next_token(s);
245         if (!tok)
246                 goto error;
247         if (tok->type != ISL_TOKEN_VALUE) {
248                 isl_stream_error(s, tok, "expected denominator");
249                 isl_stream_push_token(s, tok);
250                 goto error;
251         }
252         isl_int_set(bmap->div[k][0], tok->u.v);
253         isl_token_free(tok);
254
255         if (isl_stream_eat(s, ']'))
256                 goto error;
257
258         if (isl_basic_map_add_div_constraints(bmap, k) < 0)
259                 goto error;
260
261         return bmap;
262 error:
263         isl_basic_map_free(bmap);
264         return NULL;
265 }
266
267 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
268         struct vars *v, struct isl_basic_map *bmap)
269 {
270         struct isl_token *tok;
271
272         while ((tok = isl_stream_next_token(s)) != NULL) {
273                 int k;
274                 int p;
275                 int n = v->n;
276                 unsigned total = isl_basic_map_total_dim(bmap);
277
278                 if (tok->type != ISL_TOKEN_IDENT)
279                         break;
280
281                 p = vars_pos(v, tok->u.s, -1);
282                 if (p < 0)
283                         goto error;
284                 if (p < n) {
285                         isl_stream_error(s, tok, "expecting unique identifier");
286                         goto error;
287                 }
288                 isl_token_free(tok);
289
290                 bmap = isl_basic_map_cow(bmap);
291                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
292                                                 1, 0, 2);
293
294                 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
295                         goto error;
296                 isl_seq_clr(bmap->div[k], 1 + 1 + total);
297
298                 tok = isl_stream_next_token(s);
299                 if (tok && tok->type == '=') {
300                         isl_token_free(tok);
301                         bmap = add_div_definition(s, v, bmap, k);
302                         tok = isl_stream_next_token(s);
303                 }
304
305                 if (!tok || tok->type != ',')
306                         break;
307
308                 isl_token_free(tok);
309         }
310         if (tok)
311                 isl_stream_push_token(s, tok);
312
313         return bmap;
314 error:
315         isl_token_free(tok);
316         isl_basic_map_free(bmap);
317         return NULL;
318 }
319
320 static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
321 {
322         struct isl_token *tok;
323
324         tok = isl_stream_next_token(s);
325         if (!tok || tok->type != '[') {
326                 isl_stream_error(s, tok, "expecting '['");
327                 goto error;
328         }
329         isl_token_free(tok);
330         v = read_var_list(s, v);
331         tok = isl_stream_next_token(s);
332         if (!tok || tok->type != ']') {
333                 isl_stream_error(s, tok, "expecting ']'");
334                 goto error;
335         }
336         isl_token_free(tok);
337
338         return v;
339 error:
340         if (tok)
341                 isl_token_free(tok);
342         vars_free(v);
343         return NULL;
344 }
345
346 static struct isl_basic_map *add_constraints(struct isl_stream *s,
347         struct vars **v, struct isl_basic_map *bmap);
348
349 static struct isl_basic_map *add_exists(struct isl_stream *s,
350         struct vars **v, struct isl_basic_map *bmap)
351 {
352         struct isl_token *tok;
353         int n = (*v)->n;
354         int extra;
355         int seen_paren = 0;
356         int i;
357         unsigned total;
358
359         tok = isl_stream_next_token(s);
360         if (!tok)
361                 goto error;
362         if (tok->type == '(') {
363                 seen_paren = 1;
364                 isl_token_free(tok);
365         } else
366                 isl_stream_push_token(s, tok);
367
368         bmap = read_defined_var_list(s, *v, bmap);
369         if (!bmap)
370                 goto error;
371
372         if (isl_stream_eat(s, ':'))
373                 goto error;
374         bmap = add_constraints(s, v, bmap);
375         if (seen_paren && isl_stream_eat(s, ')'))
376                 goto error;
377         return bmap;
378 error:
379         isl_basic_map_free(bmap);
380         return NULL;
381 }
382
383 static struct isl_basic_map *add_constraint(struct isl_stream *s,
384         struct vars **v, struct isl_basic_map *bmap)
385 {
386         unsigned total = isl_basic_map_total_dim(bmap);
387         int k;
388         struct isl_token *tok = NULL;
389         struct isl_vec *aff1 = NULL, *aff2 = NULL;
390
391         tok = isl_stream_next_token(s);
392         if (!tok)
393                 goto error;
394         if (tok->type == ISL_TOKEN_EXISTS) {
395                 isl_token_free(tok);
396                 return add_exists(s, v, bmap);
397         }
398         isl_stream_push_token(s, tok);
399         tok = NULL;
400
401         bmap = isl_basic_map_cow(bmap);
402         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
403         k = isl_basic_map_alloc_inequality(bmap);
404         if (k < 0)
405                 goto error;
406
407         aff1 = accept_affine(s, *v);
408         if (!aff1)
409                 goto error;
410         tok = isl_stream_next_token(s);
411         switch (tok->type) {
412         case ISL_TOKEN_LT:
413         case ISL_TOKEN_GT:
414         case ISL_TOKEN_LE:
415         case ISL_TOKEN_GE:
416         case '=':
417                 break;
418         default:
419                 isl_stream_error(s, tok, "missing operator");
420                 isl_stream_push_token(s, tok);
421                 tok = NULL;
422                 goto error;
423         }
424         aff2 = accept_affine(s, *v);
425         if (!aff2)
426                 goto error;
427         isl_assert(aff1->ctx, aff1->size == 1 + total, goto error);
428         isl_assert(aff2->ctx, aff2->size == 1 + total, goto error);
429
430         if (tok->type == ISL_TOKEN_LE)
431                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
432                                                (*v)->ctx->one, aff2->el,
433                                                aff1->size);
434         else if (tok->type == ISL_TOKEN_GE)
435                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
436                                                (*v)->ctx->negone, aff2->el,
437                                                aff1->size);
438         else if (tok->type == ISL_TOKEN_LT) {
439                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
440                                                (*v)->ctx->one, aff2->el,
441                                                aff1->size);
442                 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
443         } else if (tok->type == ISL_TOKEN_GT) {
444                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
445                                                (*v)->ctx->negone, aff2->el,
446                                                aff1->size);
447                 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
448         } else {
449                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
450                                                (*v)->ctx->negone, aff2->el,
451                                                aff1->size);
452                 isl_basic_map_inequality_to_equality(bmap, k);
453         }
454         isl_token_free(tok);
455         isl_vec_free(aff1);
456         isl_vec_free(aff2);
457
458         return bmap;
459 error:
460         if (tok)
461                 isl_token_free(tok);
462         isl_vec_free(aff1);
463         isl_vec_free(aff2);
464         isl_basic_map_free(bmap);
465         return NULL;
466 }
467
468 static struct isl_basic_map *add_constraints(struct isl_stream *s,
469         struct vars **v, struct isl_basic_map *bmap)
470 {
471         struct isl_token *tok;
472
473         for (;;) {
474                 bmap = add_constraint(s, v, bmap);
475                 if (!bmap)
476                         return NULL;
477                 tok = isl_stream_next_token(s);
478                 if (!tok) {
479                         isl_stream_error(s, NULL, "unexpected EOF");
480                         goto error;
481                 }
482                 if (tok->type != ISL_TOKEN_AND)
483                         break;
484                 isl_token_free(tok);
485         }
486         isl_stream_push_token(s, tok);
487
488         return bmap;
489 error:
490         if (tok)
491                 isl_token_free(tok);
492         isl_basic_map_free(bmap);
493         return NULL;
494 }
495
496 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
497         struct vars **v, __isl_take isl_dim *dim)
498 {
499         struct isl_basic_map *bmap;
500
501         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
502         if (!bmap)
503                 return NULL;
504
505         bmap = add_constraints(s, v, bmap);
506         bmap = isl_basic_map_simplify(bmap);
507         bmap = isl_basic_map_finalize(bmap);
508         return bmap;
509 }
510
511 static struct isl_map *read_disjuncts(struct isl_stream *s,
512         struct vars **v, __isl_take isl_dim *dim)
513 {
514         struct isl_token *tok;
515         struct isl_map *map;
516
517         tok = isl_stream_next_token(s);
518         if (!tok) {
519                 isl_stream_error(s, NULL, "unexpected EOF");
520                 goto error;
521         }
522         if (tok->type == '}') {
523                 isl_stream_push_token(s, tok);
524                 return isl_map_universe(dim);
525         }
526         isl_stream_push_token(s, tok);
527
528         map = isl_map_empty(isl_dim_copy(dim));
529         for (;;) {
530                 struct isl_basic_map *bmap;
531
532                 bmap = read_disjunct(s, v, isl_dim_copy(dim));
533                 map = isl_map_union(map, isl_map_from_basic_map(bmap));
534
535                 tok = isl_stream_next_token(s);
536                 if (!tok || tok->type != ISL_TOKEN_OR)
537                         break;
538                 isl_token_free(tok);
539         }
540         if (tok)
541                 isl_stream_push_token(s, tok);
542
543         isl_dim_free(dim);
544         return map;
545 error:
546         isl_dim_free(dim);
547         return NULL;
548 }
549
550 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
551 {
552         if (pos < isl_basic_map_dim(bmap, isl_dim_out))
553                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
554                            isl_basic_map_dim(bmap, isl_dim_in) + pos;
555         pos -= isl_basic_map_dim(bmap, isl_dim_out);
556
557         if (pos < isl_basic_map_dim(bmap, isl_dim_in))
558                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
559         pos -= isl_basic_map_dim(bmap, isl_dim_in);
560
561         if (pos < isl_basic_map_dim(bmap, isl_dim_div))
562                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
563                            isl_basic_map_dim(bmap, isl_dim_in) +
564                            isl_basic_map_dim(bmap, isl_dim_out) + pos;
565         pos -= isl_basic_map_dim(bmap, isl_dim_div);
566
567         if (pos < isl_basic_map_dim(bmap, isl_dim_param))
568                 return 1 + pos;
569
570         return 0;
571 }
572
573 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
574         struct isl_stream *s, __isl_take isl_basic_map *bmap)
575 {
576         int j;
577         struct isl_token *tok;
578         int type;
579         int k;
580         isl_int *c;
581         unsigned nparam;
582         unsigned dim;
583
584         if (!bmap)
585                 return NULL;
586
587         nparam = isl_basic_map_dim(bmap, isl_dim_param);
588         dim = isl_basic_map_dim(bmap, isl_dim_out);
589
590         tok = isl_stream_next_token(s);
591         if (!tok || tok->type != ISL_TOKEN_VALUE) {
592                 isl_stream_error(s, tok, "expecting coefficient");
593                 if (tok)
594                         isl_stream_push_token(s, tok);
595                 goto error;
596         }
597         if (!tok->on_new_line) {
598                 isl_stream_error(s, tok, "coefficient should appear on new line");
599                 isl_stream_push_token(s, tok);
600                 goto error;
601         }
602
603         type = isl_int_get_si(tok->u.v);
604         isl_token_free(tok);
605
606         isl_assert(s->ctx, type == 0 || type == 1, goto error);
607         if (type == 0) {
608                 k = isl_basic_map_alloc_equality(bmap);
609                 c = bmap->eq[k];
610         } else {
611                 k = isl_basic_map_alloc_inequality(bmap);
612                 c = bmap->ineq[k];
613         }
614         if (k < 0)
615                 goto error;
616
617         for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
618                 int pos;
619                 tok = isl_stream_next_token(s);
620                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
621                         isl_stream_error(s, tok, "expecting coefficient");
622                         if (tok)
623                                 isl_stream_push_token(s, tok);
624                         goto error;
625                 }
626                 pos = polylib_pos_to_isl_pos(bmap, j);
627                 isl_int_set(c[pos], tok->u.v);
628                 isl_token_free(tok);
629         }
630
631         return bmap;
632 error:
633         isl_basic_map_free(bmap);
634         return NULL;
635 }
636
637 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
638         int nparam)
639 {
640         int i;
641         struct isl_token *tok;
642         struct isl_token *tok2;
643         int n_row, n_col;
644         int on_new_line;
645         unsigned dim;
646         struct isl_basic_map *bmap = NULL;
647
648         if (nparam < 0)
649                 nparam = 0;
650
651         tok = isl_stream_next_token(s);
652         if (!tok) {
653                 isl_stream_error(s, NULL, "unexpected EOF");
654                 return NULL;
655         }
656         tok2 = isl_stream_next_token(s);
657         if (!tok2) {
658                 isl_token_free(tok);
659                 isl_stream_error(s, NULL, "unexpected EOF");
660                 return NULL;
661         }
662         n_row = isl_int_get_si(tok->u.v);
663         n_col = isl_int_get_si(tok2->u.v);
664         on_new_line = tok2->on_new_line;
665         isl_token_free(tok2);
666         isl_token_free(tok);
667         isl_assert(s->ctx, !on_new_line, return NULL);
668         isl_assert(s->ctx, n_row >= 0, return NULL);
669         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
670         dim = n_col - 2 - nparam;
671         bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
672         if (!bmap)
673                 return NULL;
674
675         for (i = 0; i < n_row; ++i)
676                 bmap = basic_map_read_polylib_constraint(s, bmap);
677
678         bmap = isl_basic_map_simplify(bmap);
679         bmap = isl_basic_map_finalize(bmap);
680         return bmap;
681 }
682
683 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
684 {
685         struct isl_token *tok;
686         struct isl_token *tok2;
687         int i, n;
688         struct isl_map *map;
689
690         tok = isl_stream_next_token(s);
691         if (!tok) {
692                 isl_stream_error(s, NULL, "unexpected EOF");
693                 return NULL;
694         }
695         tok2 = isl_stream_next_token(s);
696         if (!tok2) {
697                 isl_token_free(tok);
698                 isl_stream_error(s, NULL, "unexpected EOF");
699                 return NULL;
700         }
701         if (!tok2->on_new_line) {
702                 isl_stream_push_token(s, tok2);
703                 isl_stream_push_token(s, tok);
704                 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
705         }
706         isl_stream_push_token(s, tok2);
707         n = isl_int_get_si(tok->u.v);
708         isl_token_free(tok);
709
710         isl_assert(s->ctx, n >= 1, return NULL);
711
712         map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
713
714         for (i = 1; i < n; ++i)
715                 map = isl_map_union(map,
716                         isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
717
718         return map;
719 }
720
721 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
722         enum isl_dim_type type, int offset, int n)
723 {
724         int i;
725         struct variable *v;
726
727         for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
728                 ;
729         for (i = n - 1; i >= 0; --i, v = v->next)
730                 dim = isl_dim_set_name(dim, type, i, v->name);
731
732         return dim;
733 }
734
735 static struct isl_dim *dim_from_vars(struct vars *vars,
736         int nparam, int n_in, int n_out)
737 {
738         struct isl_dim *dim;
739
740         dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
741         dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
742         dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
743         dim = set_names(dim, vars, isl_dim_out, 0, n_out);
744
745         return dim;
746 }
747
748 static struct isl_map *map_read(struct isl_stream *s, int nparam)
749 {
750         struct isl_dim *dim = NULL;
751         struct isl_map *map = NULL;
752         struct isl_token *tok;
753         struct vars *v = NULL;
754         int n1;
755         int n2;
756
757         tok = isl_stream_next_token(s);
758         if (!tok) {
759                 isl_stream_error(s, NULL, "unexpected EOF");
760                 goto error;
761         }
762         if (tok->type == ISL_TOKEN_VALUE) {
763                 isl_stream_push_token(s, tok);
764                 return map_read_polylib(s, nparam);
765         }
766         v = vars_new(s->ctx);
767         if (tok->type == '[') {
768                 isl_stream_push_token(s, tok);
769                 v = read_tuple(s, v);
770                 if (!v)
771                         return NULL;
772                 if (nparam >= 0)
773                         isl_assert(s->ctx, nparam == v->n, goto error);
774                 nparam = v->n;
775                 tok = isl_stream_next_token(s);
776                 if (!tok || tok->type != ISL_TOKEN_TO) {
777                         isl_stream_error(s, tok, "expecting '->'");
778                         if (tok)
779                                 isl_stream_push_token(s, tok);
780                         goto error;
781                 }
782                 isl_token_free(tok);
783                 tok = isl_stream_next_token(s);
784         }
785         if (nparam < 0)
786                 nparam = 0;
787         if (!tok || tok->type != '{') {
788                 isl_stream_error(s, tok, "expecting '{'");
789                 if (tok)
790                         isl_stream_push_token(s, tok);
791                 goto error;
792         }
793         isl_token_free(tok);
794         v = read_tuple(s, v);
795         if (!v)
796                 return NULL;
797         n1 = v->n - nparam;
798         tok = isl_stream_next_token(s);
799         if (tok && tok->type == ISL_TOKEN_TO) {
800                 isl_token_free(tok);
801                 v = read_tuple(s, v);
802                 if (!v)
803                         return NULL;
804                 n2 = v->n - n1 - nparam;
805         } else {
806                 if (tok)
807                         isl_stream_push_token(s, tok);
808                 n2 = n1;
809                 n1 = 0;
810         }
811         dim = dim_from_vars(v, nparam, n1, n2);
812         tok = isl_stream_next_token(s);
813         if (!tok) {
814                 isl_stream_error(s, NULL, "unexpected EOF");
815                 goto error;
816         }
817         if (tok->type == ':') {
818                 isl_token_free(tok);
819                 map = read_disjuncts(s, &v, isl_dim_copy(dim));
820                 tok = isl_stream_next_token(s);
821         } else
822                 map = isl_map_universe(isl_dim_copy(dim));
823         if (tok && tok->type == '}') {
824                 isl_token_free(tok);
825         } else {
826                 isl_stream_error(s, tok, "unexpected isl_token");
827                 if (tok)
828                         isl_token_free(tok);
829                 goto error;
830         }
831         vars_free(v);
832         isl_dim_free(dim);
833
834         return map;
835 error:
836         isl_dim_free(dim);
837         isl_map_free(map);
838         if (v)
839                 vars_free(v);
840         return NULL;
841 }
842
843 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
844 {
845         struct isl_map *map;
846         struct isl_basic_map *bmap;
847
848         map = map_read(s, nparam);
849         if (!map)
850                 return NULL;
851
852         isl_assert(map->ctx, map->n <= 1, goto error);
853
854         if (map->n == 0)
855                 bmap = isl_basic_map_empty_like_map(map);
856         else
857                 bmap = isl_basic_map_copy(map->p[0]);
858
859         isl_map_free(map);
860
861         return bmap;
862 error:
863         isl_map_free(map);
864         return NULL;
865 }
866
867 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
868                 FILE *input, int nparam)
869 {
870         struct isl_basic_map *bmap;
871         struct isl_stream *s = isl_stream_new_file(ctx, input);
872         if (!s)
873                 return NULL;
874         bmap = basic_map_read(s, nparam);
875         isl_stream_free(s);
876         return bmap;
877 }
878
879 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
880                 FILE *input, int nparam)
881 {
882         struct isl_basic_map *bmap;
883         bmap = isl_basic_map_read_from_file(ctx, input, nparam);
884         if (!bmap)
885                 return NULL;
886         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
887         return (struct isl_basic_set *)bmap;
888 error:
889         isl_basic_map_free(bmap);
890         return NULL;
891 }
892
893 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
894                 const char *str, int nparam)
895 {
896         struct isl_basic_map *bmap;
897         struct isl_stream *s = isl_stream_new_str(ctx, str);
898         if (!s)
899                 return NULL;
900         bmap = basic_map_read(s, nparam);
901         isl_stream_free(s);
902         return bmap;
903 }
904
905 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
906                 const char *str, int nparam)
907 {
908         struct isl_basic_map *bmap;
909         bmap = isl_basic_map_read_from_str(ctx, str, nparam);
910         if (!bmap)
911                 return NULL;
912         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
913         return (struct isl_basic_set *)bmap;
914 error:
915         isl_basic_map_free(bmap);
916         return NULL;
917 }
918
919 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
920                 FILE *input, int nparam)
921 {
922         struct isl_map *map;
923         struct isl_stream *s = isl_stream_new_file(ctx, input);
924         if (!s)
925                 return NULL;
926         map = map_read(s, nparam);
927         isl_stream_free(s);
928         return map;
929 }
930
931 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
932                 const char *str, int nparam)
933 {
934         struct isl_map *map;
935         struct isl_stream *s = isl_stream_new_str(ctx, str);
936         if (!s)
937                 return NULL;
938         map = map_read(s, nparam);
939         isl_stream_free(s);
940         return map;
941 }
942
943 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
944                 FILE *input, int nparam)
945 {
946         struct isl_map *map;
947         map = isl_map_read_from_file(ctx, input, nparam);
948         if (!map)
949                 return NULL;
950         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
951         return (struct isl_set *)map;
952 error:
953         isl_map_free(map);
954         return NULL;
955 }
956
957 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
958                 const char *str, int nparam)
959 {
960         struct isl_map *map;
961         map = isl_map_read_from_str(ctx, str, nparam);
962         if (!map)
963                 return NULL;
964         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
965         return (struct isl_set *)map;
966 error:
967         isl_map_free(map);
968         return NULL;
969 }
970
971 static char *next_line(FILE *input, char *line, unsigned len)
972 {
973         char *p;
974
975         do {
976                 if (!(p = fgets(line, len, input)))
977                         return NULL;
978                 while (isspace(*p) && *p != '\n')
979                         ++p;
980         } while (*p == '#' || *p == '\n');
981
982         return p;
983 }
984
985 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
986                 FILE *input)
987 {
988         struct isl_vec *vec = NULL;
989         char line[1024];
990         char val[1024];
991         char *p;
992         unsigned size;
993         int j;
994         int n;
995         int offset;
996
997         isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
998         isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
999
1000         vec = isl_vec_alloc(ctx, size);
1001
1002         p = next_line(input, line, sizeof(line));
1003         isl_assert(ctx, p, goto error);
1004
1005         for (j = 0; j < size; ++j) {
1006                 n = sscanf(p, "%s%n", val, &offset);
1007                 isl_assert(ctx, n != 0, goto error);
1008                 isl_int_read(vec->el[j], val);
1009                 p += offset;
1010         }
1011
1012         return vec;
1013 error:
1014         isl_vec_free(vec);
1015         return NULL;
1016 }
1017
1018 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1019                 FILE *input, unsigned input_format)
1020 {
1021         if (input_format == ISL_FORMAT_POLYLIB)
1022                 return isl_vec_read_from_file_polylib(ctx, input);
1023         else
1024                 isl_assert(ctx, 0, return NULL);
1025 }