add isl_map_read_from_str
[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
144         aff = isl_vec_alloc(v->ctx, 1 + v->n);
145         isl_seq_clr(aff->el, aff->size);
146         if (!aff)
147                 return NULL;
148
149         for (;;) {
150                 tok = isl_stream_next_token(s);
151                 if (!tok) {
152                         isl_stream_error(s, NULL, "unexpected EOF");
153                         goto error;
154                 }
155                 if (tok->type == ISL_TOKEN_IDENT) {
156                         int n = v->n;
157                         int pos = vars_pos(v, tok->u.s, -1);
158                         if (pos < 0)
159                                 goto error;
160                         if (pos >= n) {
161                                 isl_stream_error(s, tok, "unknown identifier");
162                                 goto error;
163                         }
164                         isl_int_add_ui(aff->el[1 + pos], aff->el[1 + pos], 1);
165                 } else if (tok->type == ISL_TOKEN_VALUE) {
166                         struct isl_token *tok2;
167                         int n = v->n;
168                         int pos = -1;
169                         tok2 = isl_stream_next_token(s);
170                         if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
171                                 pos = vars_pos(v, tok2->u.s, -1);
172                                 if (pos < 0)
173                                         goto error;
174                                 if (pos >= n) {
175                                         isl_stream_error(s, tok2,
176                                                 "unknown identifier");
177                                         isl_token_free(tok2);
178                                         goto error;
179                                 }
180                                 isl_token_free(tok2);
181                         } else if (tok2)
182                                 isl_stream_push_token(s, tok2);
183                         isl_int_add(aff->el[1 + pos],
184                                         aff->el[1 + pos], tok->u.v);
185                 } else if (tok->type == '+') {
186                         /* nothing */
187                 } else {
188                         isl_stream_push_token(s, tok);
189                         break;
190                 }
191                 isl_token_free(tok);
192         }
193
194         return aff;
195 error:
196         isl_vec_free(aff);
197         return NULL;
198 }
199
200 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
201         struct vars *v, struct isl_basic_map *bmap, int k)
202 {
203         struct isl_token *tok;
204         int seen_paren = 0;
205         struct isl_vec *aff;
206
207         if (isl_stream_eat(s, '['))
208                 goto error;
209
210         tok = isl_stream_next_token(s);
211         if (!tok)
212                 goto error;
213         if (tok->type == '(') {
214                 seen_paren = 1;
215                 isl_token_free(tok);
216         } else
217                 isl_stream_push_token(s, tok);
218
219         aff = accept_affine(s, v);
220         if (!aff)
221                 goto error;
222
223         isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
224
225         isl_vec_free(aff);
226
227         if (seen_paren && isl_stream_eat(s, ')'))
228                 goto error;
229         if (isl_stream_eat(s, '/'))
230                 goto error;
231
232         tok = isl_stream_next_token(s);
233         if (!tok)
234                 goto error;
235         if (tok->type != ISL_TOKEN_VALUE) {
236                 isl_stream_error(s, tok, "expected denominator");
237                 isl_stream_push_token(s, tok);
238                 goto error;
239         }
240         isl_int_set(bmap->div[k][0], tok->u.v);
241         isl_token_free(tok);
242
243         if (isl_stream_eat(s, ']'))
244                 goto error;
245
246         if (isl_basic_map_add_div_constraints(bmap, k) < 0)
247                 goto error;
248
249         return bmap;
250 error:
251         isl_basic_map_free(bmap);
252         return NULL;
253 }
254
255 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
256         struct vars *v, struct isl_basic_map *bmap)
257 {
258         struct isl_token *tok;
259
260         while ((tok = isl_stream_next_token(s)) != NULL) {
261                 int k;
262                 int p;
263                 int n = v->n;
264                 unsigned total = isl_basic_map_total_dim(bmap);
265
266                 if (tok->type != ISL_TOKEN_IDENT)
267                         break;
268
269                 p = vars_pos(v, tok->u.s, -1);
270                 if (p < 0)
271                         goto error;
272                 if (p < n) {
273                         isl_stream_error(s, tok, "expecting unique identifier");
274                         goto error;
275                 }
276                 isl_token_free(tok);
277
278                 bmap = isl_basic_map_cow(bmap);
279                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
280                                                 1, 0, 2);
281
282                 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
283                         goto error;
284                 isl_seq_clr(bmap->div[k], 1 + 1 + total);
285
286                 tok = isl_stream_next_token(s);
287                 if (tok && tok->type == '=') {
288                         isl_token_free(tok);
289                         bmap = add_div_definition(s, v, bmap, k);
290                         tok = isl_stream_next_token(s);
291                 }
292
293                 if (!tok || tok->type != ',')
294                         break;
295
296                 isl_token_free(tok);
297         }
298         if (tok)
299                 isl_stream_push_token(s, tok);
300
301         return bmap;
302 error:
303         isl_token_free(tok);
304         isl_basic_map_free(bmap);
305         return NULL;
306 }
307
308 static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
309 {
310         struct isl_token *tok;
311
312         tok = isl_stream_next_token(s);
313         if (!tok || tok->type != '[') {
314                 isl_stream_error(s, tok, "expecting '['");
315                 goto error;
316         }
317         isl_token_free(tok);
318         v = read_var_list(s, v);
319         tok = isl_stream_next_token(s);
320         if (!tok || tok->type != ']') {
321                 isl_stream_error(s, tok, "expecting ']'");
322                 goto error;
323         }
324         isl_token_free(tok);
325
326         return v;
327 error:
328         if (tok)
329                 isl_token_free(tok);
330         vars_free(v);
331         return NULL;
332 }
333
334 static struct isl_basic_map *add_constraints(struct isl_stream *s,
335         struct vars **v, struct isl_basic_map *bmap);
336
337 static struct isl_basic_map *add_exists(struct isl_stream *s,
338         struct vars **v, struct isl_basic_map *bmap)
339 {
340         struct isl_token *tok;
341         int n = (*v)->n;
342         int extra;
343         int seen_paren = 0;
344         int i;
345         unsigned total;
346
347         tok = isl_stream_next_token(s);
348         if (!tok)
349                 goto error;
350         if (tok->type == '(') {
351                 seen_paren = 1;
352                 isl_token_free(tok);
353         } else
354                 isl_stream_push_token(s, tok);
355
356         bmap = read_defined_var_list(s, *v, bmap);
357         if (!bmap)
358                 goto error;
359
360         if (isl_stream_eat(s, ':'))
361                 goto error;
362         bmap = add_constraints(s, v, bmap);
363         if (seen_paren && isl_stream_eat(s, ')'))
364                 goto error;
365         return bmap;
366 error:
367         isl_basic_map_free(bmap);
368         return NULL;
369 }
370
371 static struct isl_basic_map *add_constraint(struct isl_stream *s,
372         struct vars **v, struct isl_basic_map *bmap)
373 {
374         unsigned total = isl_basic_map_total_dim(bmap);
375         int k;
376         struct isl_token *tok = NULL;
377         struct isl_vec *aff1 = NULL, *aff2 = NULL;
378
379         tok = isl_stream_next_token(s);
380         if (!tok)
381                 goto error;
382         if (tok->type == ISL_TOKEN_EXISTS) {
383                 isl_token_free(tok);
384                 return add_exists(s, v, bmap);
385         }
386         isl_stream_push_token(s, tok);
387         tok = NULL;
388
389         bmap = isl_basic_map_cow(bmap);
390         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
391         k = isl_basic_map_alloc_inequality(bmap);
392         if (k < 0)
393                 goto error;
394
395         aff1 = accept_affine(s, *v);
396         if (!aff1)
397                 goto error;
398         tok = isl_stream_next_token(s);
399         switch (tok->type) {
400         case ISL_TOKEN_LE:
401         case ISL_TOKEN_GE:
402         case '=':
403                 break;
404         default:
405                 isl_stream_error(s, tok, "missing operator");
406                 isl_stream_push_token(s, tok);
407                 goto error;
408         }
409         aff2 = accept_affine(s, *v);
410         if (!aff2)
411                 goto error;
412         isl_assert(aff1->ctx, aff1->size == 1 + total, goto error);
413         isl_assert(aff2->ctx, aff2->size == 1 + total, goto error);
414
415         if (tok->type == ISL_TOKEN_LE)
416                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
417                                                (*v)->ctx->one, aff2->el,
418                                                aff1->size);
419         else if (tok->type == ISL_TOKEN_GE)
420                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
421                                                (*v)->ctx->negone, aff2->el,
422                                                aff1->size);
423         else {
424                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
425                                                (*v)->ctx->negone, aff2->el,
426                                                aff1->size);
427                 isl_basic_map_inequality_to_equality(bmap, k);
428         }
429         isl_token_free(tok);
430         isl_vec_free(aff1);
431         isl_vec_free(aff2);
432
433         return bmap;
434 error:
435         if (tok)
436                 isl_token_free(tok);
437         isl_vec_free(aff1);
438         isl_vec_free(aff2);
439         isl_basic_map_free(bmap);
440         return NULL;
441 }
442
443 static struct isl_basic_map *add_constraints(struct isl_stream *s,
444         struct vars **v, struct isl_basic_map *bmap)
445 {
446         struct isl_token *tok;
447
448         for (;;) {
449                 bmap = add_constraint(s, v, bmap);
450                 if (!bmap)
451                         return NULL;
452                 tok = isl_stream_next_token(s);
453                 if (!tok) {
454                         isl_stream_error(s, NULL, "unexpected EOF");
455                         goto error;
456                 }
457                 if (tok->type != ISL_TOKEN_AND)
458                         break;
459                 isl_token_free(tok);
460         }
461         isl_stream_push_token(s, tok);
462
463         return bmap;
464 error:
465         if (tok)
466                 isl_token_free(tok);
467         isl_basic_map_free(bmap);
468         return NULL;
469 }
470
471 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
472         struct vars **v, __isl_take isl_dim *dim)
473 {
474         struct isl_basic_map *bmap;
475
476         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
477         if (!bmap)
478                 return NULL;
479
480         bmap = add_constraints(s, v, bmap);
481         bmap = isl_basic_map_simplify(bmap);
482         bmap = isl_basic_map_finalize(bmap);
483         return bmap;
484 }
485
486 static struct isl_map *read_disjuncts(struct isl_stream *s,
487         struct vars **v, __isl_take isl_dim *dim)
488 {
489         struct isl_token *tok;
490         struct isl_map *map;
491
492         tok = isl_stream_next_token(s);
493         if (!tok) {
494                 isl_stream_error(s, NULL, "unexpected EOF");
495                 goto error;
496         }
497         if (tok->type == '}') {
498                 isl_stream_push_token(s, tok);
499                 return isl_map_universe(dim);
500         }
501         isl_stream_push_token(s, tok);
502
503         map = isl_map_empty(isl_dim_copy(dim));
504         for (;;) {
505                 struct isl_basic_map *bmap;
506
507                 bmap = read_disjunct(s, v, isl_dim_copy(dim));
508                 map = isl_map_union(map, isl_map_from_basic_map(bmap));
509
510                 tok = isl_stream_next_token(s);
511                 if (!tok || tok->type != ISL_TOKEN_OR)
512                         break;
513                 isl_token_free(tok);
514         }
515         if (tok)
516                 isl_stream_push_token(s, tok);
517
518         isl_dim_free(dim);
519         return map;
520 error:
521         isl_dim_free(dim);
522         return NULL;
523 }
524
525 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
526         struct isl_stream *s, __isl_take isl_basic_map *bmap)
527 {
528         int j;
529         struct isl_token *tok;
530         int type;
531         int k;
532         isl_int *c;
533         unsigned nparam;
534         unsigned dim;
535
536         if (!bmap)
537                 return NULL;
538
539         nparam = isl_basic_map_dim(bmap, isl_dim_param);
540         dim = isl_basic_map_dim(bmap, isl_dim_out);
541
542         tok = isl_stream_next_token(s);
543         if (!tok || tok->type != ISL_TOKEN_VALUE) {
544                 isl_stream_error(s, tok, "expecting coefficient");
545                 if (tok)
546                         isl_stream_push_token(s, tok);
547                 goto error;
548         }
549         if (!tok->on_new_line) {
550                 isl_stream_error(s, tok, "coefficient should appear on new line");
551                 isl_stream_push_token(s, tok);
552                 goto error;
553         }
554
555         type = isl_int_get_si(tok->u.v);
556         isl_token_free(tok);
557
558         isl_assert(s->ctx, type == 0 || type == 1, goto error);
559         if (type == 0) {
560                 k = isl_basic_map_alloc_equality(bmap);
561                 c = bmap->eq[k];
562         } else {
563                 k = isl_basic_map_alloc_inequality(bmap);
564                 c = bmap->ineq[k];
565         }
566         if (k < 0)
567                 goto error;
568
569         for (j = 0; j < dim; ++j) {
570                 tok = isl_stream_next_token(s);
571                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
572                         isl_stream_error(s, tok, "expecting coefficient");
573                         if (tok)
574                                 isl_stream_push_token(s, tok);
575                         goto error;
576                 }
577                 isl_int_set(c[1 + nparam + j], tok->u.v);
578                 isl_token_free(tok);
579         }
580         for (j = 0; j < nparam; ++j) {
581                 tok = isl_stream_next_token(s);
582                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
583                         isl_stream_error(s, tok, "expecting coefficient");
584                         if (tok)
585                                 isl_stream_push_token(s, tok);
586                         goto error;
587                 }
588                 isl_int_set(c[1 + j], tok->u.v);
589                 isl_token_free(tok);
590         }
591         tok = isl_stream_next_token(s);
592         if (!tok || tok->type != ISL_TOKEN_VALUE) {
593                 isl_stream_error(s, tok, "expecting coefficient");
594                 if (tok)
595                         isl_stream_push_token(s, tok);
596                 goto error;
597         }
598         isl_int_set(c[0], tok->u.v);
599         isl_token_free(tok);
600
601         return bmap;
602 error:
603         isl_basic_map_free(bmap);
604         return NULL;
605 }
606
607 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
608         int nparam)
609 {
610         int i;
611         struct isl_token *tok;
612         struct isl_token *tok2;
613         int n_row, n_col;
614         int on_new_line;
615         unsigned dim;
616         struct isl_basic_map *bmap = NULL;
617
618         if (nparam < 0)
619                 nparam = 0;
620
621         tok = isl_stream_next_token(s);
622         if (!tok) {
623                 isl_stream_error(s, NULL, "unexpected EOF");
624                 return NULL;
625         }
626         tok2 = isl_stream_next_token(s);
627         if (!tok2) {
628                 isl_token_free(tok);
629                 isl_stream_error(s, NULL, "unexpected EOF");
630                 return NULL;
631         }
632         n_row = isl_int_get_si(tok->u.v);
633         n_col = isl_int_get_si(tok2->u.v);
634         on_new_line = tok2->on_new_line;
635         isl_token_free(tok2);
636         isl_token_free(tok);
637         isl_assert(s->ctx, !on_new_line, return NULL);
638         isl_assert(s->ctx, n_row >= 0, return NULL);
639         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
640         dim = n_col - 2 - nparam;
641         bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
642         if (!bmap)
643                 return NULL;
644
645         for (i = 0; i < n_row; ++i)
646                 bmap = basic_map_read_polylib_constraint(s, bmap);
647
648         bmap = isl_basic_map_simplify(bmap);
649         bmap = isl_basic_map_finalize(bmap);
650         return bmap;
651 }
652
653 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
654 {
655         struct isl_token *tok;
656         struct isl_token *tok2;
657         int i, n;
658         struct isl_map *map;
659
660         tok = isl_stream_next_token(s);
661         if (!tok) {
662                 isl_stream_error(s, NULL, "unexpected EOF");
663                 return NULL;
664         }
665         tok2 = isl_stream_next_token(s);
666         if (!tok2) {
667                 isl_token_free(tok);
668                 isl_stream_error(s, NULL, "unexpected EOF");
669                 return NULL;
670         }
671         if (!tok2->on_new_line) {
672                 isl_stream_push_token(s, tok2);
673                 isl_stream_push_token(s, tok);
674                 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
675         }
676         isl_stream_push_token(s, tok2);
677         n = isl_int_get_si(tok->u.v);
678         isl_token_free(tok);
679
680         isl_assert(s->ctx, n >= 1, return NULL);
681
682         map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
683
684         for (i = 1; i < n; ++i)
685                 map = isl_map_union(map,
686                         isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
687
688         return map;
689 }
690
691 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
692         enum isl_dim_type type, int offset, int n)
693 {
694         int i;
695         struct variable *v;
696
697         for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
698                 ;
699         for (i = n - 1; i >= 0; --i, v = v->next)
700                 dim = isl_dim_set_name(dim, type, i, v->name);
701
702         return dim;
703 }
704
705 static struct isl_dim *dim_from_vars(struct vars *vars,
706         int nparam, int n_in, int n_out)
707 {
708         struct isl_dim *dim;
709
710         dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
711         dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
712         dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
713         dim = set_names(dim, vars, isl_dim_out, 0, n_out);
714
715         return dim;
716 }
717
718 static struct isl_map *map_read(struct isl_stream *s, int nparam)
719 {
720         struct isl_dim *dim = NULL;
721         struct isl_map *map = NULL;
722         struct isl_token *tok;
723         struct vars *v = NULL;
724         int n1;
725         int n2;
726
727         tok = isl_stream_next_token(s);
728         if (!tok) {
729                 isl_stream_error(s, NULL, "unexpected EOF");
730                 goto error;
731         }
732         if (tok->type == ISL_TOKEN_VALUE) {
733                 isl_stream_push_token(s, tok);
734                 return map_read_polylib(s, nparam);
735         }
736         v = vars_new(s->ctx);
737         if (tok->type == '[') {
738                 isl_stream_push_token(s, tok);
739                 v = read_tuple(s, v);
740                 if (!v)
741                         return NULL;
742                 if (nparam >= 0)
743                         isl_assert(s->ctx, nparam == v->n, goto error);
744                 nparam = v->n;
745                 tok = isl_stream_next_token(s);
746                 if (!tok || tok->type != ISL_TOKEN_TO) {
747                         isl_stream_error(s, tok, "expecting '->'");
748                         if (tok)
749                                 isl_stream_push_token(s, tok);
750                         goto error;
751                 }
752                 isl_token_free(tok);
753                 tok = isl_stream_next_token(s);
754         }
755         if (nparam < 0)
756                 nparam = 0;
757         if (!tok || tok->type != '{') {
758                 isl_stream_error(s, tok, "expecting '{'");
759                 if (tok)
760                         isl_stream_push_token(s, tok);
761                 goto error;
762         }
763         isl_token_free(tok);
764         v = read_tuple(s, v);
765         if (!v)
766                 return NULL;
767         n1 = v->n - nparam;
768         tok = isl_stream_next_token(s);
769         if (tok && tok->type == ISL_TOKEN_TO) {
770                 isl_token_free(tok);
771                 v = read_tuple(s, v);
772                 if (!v)
773                         return NULL;
774                 n2 = v->n - n1 - nparam;
775         } else {
776                 if (tok)
777                         isl_stream_push_token(s, tok);
778                 n2 = n1;
779                 n1 = 0;
780         }
781         dim = dim_from_vars(v, nparam, n1, n2);
782         tok = isl_stream_next_token(s);
783         if (!tok) {
784                 isl_stream_error(s, NULL, "unexpected EOF");
785                 goto error;
786         }
787         if (tok->type == ':') {
788                 isl_token_free(tok);
789                 map = read_disjuncts(s, &v, isl_dim_copy(dim));
790                 tok = isl_stream_next_token(s);
791         } else
792                 map = isl_map_universe(isl_dim_copy(dim));
793         if (tok && tok->type == '}') {
794                 isl_token_free(tok);
795         } else {
796                 isl_stream_error(s, tok, "unexpected isl_token");
797                 if (tok)
798                         isl_token_free(tok);
799                 goto error;
800         }
801         vars_free(v);
802         isl_dim_free(dim);
803
804         return map;
805 error:
806         isl_dim_free(dim);
807         isl_map_free(map);
808         if (v)
809                 vars_free(v);
810         return NULL;
811 }
812
813 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
814 {
815         struct isl_map *map;
816         struct isl_basic_map *bmap;
817
818         map = map_read(s, nparam);
819         if (!map)
820                 return NULL;
821
822         isl_assert(map->ctx, map->n <= 1, goto error);
823
824         if (map->n == 0)
825                 bmap = isl_basic_map_empty_like_map(map);
826         else
827                 bmap = isl_basic_map_copy(map->p[0]);
828
829         isl_map_free(map);
830
831         return bmap;
832 error:
833         isl_map_free(map);
834         return NULL;
835 }
836
837 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
838                 FILE *input, int nparam)
839 {
840         struct isl_basic_map *bmap;
841         struct isl_stream *s = isl_stream_new_file(ctx, input);
842         if (!s)
843                 return NULL;
844         bmap = basic_map_read(s, nparam);
845         isl_stream_free(s);
846         return bmap;
847 }
848
849 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
850                 FILE *input, int nparam)
851 {
852         struct isl_basic_map *bmap;
853         bmap = isl_basic_map_read_from_file(ctx, input, nparam);
854         if (!bmap)
855                 return NULL;
856         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
857         return (struct isl_basic_set *)bmap;
858 error:
859         isl_basic_map_free(bmap);
860         return NULL;
861 }
862
863 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
864                 const char *str, int nparam)
865 {
866         struct isl_basic_map *bmap;
867         struct isl_stream *s = isl_stream_new_str(ctx, str);
868         if (!s)
869                 return NULL;
870         bmap = basic_map_read(s, nparam);
871         isl_stream_free(s);
872         return bmap;
873 }
874
875 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
876                 const char *str, int nparam)
877 {
878         struct isl_basic_map *bmap;
879         bmap = isl_basic_map_read_from_str(ctx, str, nparam);
880         if (!bmap)
881                 return NULL;
882         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
883         return (struct isl_basic_set *)bmap;
884 error:
885         isl_basic_map_free(bmap);
886         return NULL;
887 }
888
889 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
890                 FILE *input, int nparam)
891 {
892         struct isl_map *map;
893         struct isl_stream *s = isl_stream_new_file(ctx, input);
894         if (!s)
895                 return NULL;
896         map = map_read(s, nparam);
897         isl_stream_free(s);
898         return map;
899 }
900
901 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
902                 const char *str, int nparam)
903 {
904         struct isl_map *map;
905         struct isl_stream *s = isl_stream_new_str(ctx, str);
906         if (!s)
907                 return NULL;
908         map = map_read(s, nparam);
909         isl_stream_free(s);
910         return map;
911 }
912
913 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
914                 FILE *input, int nparam)
915 {
916         struct isl_map *map;
917         map = isl_map_read_from_file(ctx, input, nparam);
918         if (!map)
919                 return NULL;
920         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
921         return (struct isl_set *)map;
922 error:
923         isl_map_free(map);
924         return NULL;
925 }
926
927 static char *next_line(FILE *input, char *line, unsigned len)
928 {
929         char *p;
930
931         do {
932                 if (!(p = fgets(line, len, input)))
933                         return NULL;
934                 while (isspace(*p) && *p != '\n')
935                         ++p;
936         } while (*p == '#' || *p == '\n');
937
938         return p;
939 }
940
941 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
942                 FILE *input)
943 {
944         struct isl_vec *vec = NULL;
945         char line[1024];
946         char val[1024];
947         char *p;
948         unsigned size;
949         int j;
950         int n;
951         int offset;
952
953         isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
954         isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
955
956         vec = isl_vec_alloc(ctx, size);
957
958         p = next_line(input, line, sizeof(line));
959         isl_assert(ctx, p, goto error);
960
961         for (j = 0; j < size; ++j) {
962                 n = sscanf(p, "%s%n", val, &offset);
963                 isl_assert(ctx, n != 0, goto error);
964                 isl_int_read(vec->el[j], val);
965                 p += offset;
966         }
967
968         return vec;
969 error:
970         isl_vec_free(vec);
971         return NULL;
972 }
973
974 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
975                 FILE *input, unsigned input_format)
976 {
977         if (input_format == ISL_FORMAT_POLYLIB)
978                 return isl_vec_read_from_file_polylib(ctx, input);
979         else
980                 isl_assert(ctx, 0, return NULL);
981 }