isl_stream: treat "-" as operator rather than as -1
[platform/upstream/isl.git] / isl_stream.c
index 478eb4e..192546e 100644 (file)
@@ -1,3 +1,12 @@
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
 #include <ctype.h>
 #include <string.h>
 #include <strings.h>
@@ -47,6 +56,7 @@ static struct isl_stream* isl_stream_new(struct isl_ctx *ctx)
        if (!s)
                return NULL;
        s->ctx = ctx;
+       isl_ctx_ref(s->ctx);
        s->size = 256;
        s->file = NULL;
        s->str = NULL;
@@ -147,10 +157,17 @@ struct isl_token *isl_stream_next_token(struct isl_stream *s)
 
        s->len = 0;
 
-       /* skip spaces */
-       while ((c = isl_stream_getc(s)) != -1 && isspace(c))
-               /* nothing */
-               ;
+       /* skip spaces and comment lines */
+       while ((c = isl_stream_getc(s)) != -1) {
+               if (c == '#') {
+                       while ((c = isl_stream_getc(s)) != -1 && c != '\n')
+                               /* nothing */
+                               ;
+                       if (c == -1)
+                               break;
+               } else if (!isspace(c))
+                       break;
+       }
 
        line = s->line;
        col = s->col;
@@ -187,6 +204,13 @@ struct isl_token *isl_stream_next_token(struct isl_stream *s)
                }
                if (c != -1)
                        isl_stream_ungetc(s, c);
+               if (!isdigit(c)) {
+                       tok = isl_token_new(s->ctx, line, col, old_line != line);
+                       if (!tok)
+                               return NULL;
+                       tok->type = (enum isl_token_type) '-';
+                       return tok;
+               }
        }
        if (c == '-' || isdigit(c)) {
                tok = isl_token_new(s->ctx, line, col, old_line != line);
@@ -201,12 +225,8 @@ struct isl_token *isl_stream_next_token(struct isl_stream *s)
                                goto error;
                if (c != -1)
                        isl_stream_ungetc(s, c);
-               if (s->len == 1 && s->buffer[0] == '-')
-                       isl_int_set_si(tok->u.v, -1);
-               else {
-                       isl_stream_push_char(s, '\0');
-                       isl_int_read(tok->u.v, s->buffer);
-               }
+               isl_stream_push_char(s, '\0');
+               isl_int_read(tok->u.v, s->buffer);
                return tok;
        }
        if (isalpha(c)) {
@@ -221,6 +241,10 @@ struct isl_token *isl_stream_next_token(struct isl_stream *s)
                isl_stream_push_char(s, '\0');
                if (!strcasecmp(s->buffer, "exists"))
                        tok->type = ISL_TOKEN_EXISTS;
+               else if (!strcasecmp(s->buffer, "and"))
+                       tok->type = ISL_TOKEN_AND;
+               else if (!strcasecmp(s->buffer, "or"))
+                       tok->type = ISL_TOKEN_OR;
                else {
                        tok->type = ISL_TOKEN_IDENT;
                        tok->u.s = strdup(s->buffer);
@@ -229,27 +253,31 @@ struct isl_token *isl_stream_next_token(struct isl_stream *s)
        }
        if (c == '>') {
                int c;
+               tok = isl_token_new(s->ctx, line, col, old_line != line);
+               if (!tok)
+                       return NULL;
                if ((c = isl_stream_getc(s)) == '=') {
-                       tok = isl_token_new(s->ctx, line, col, old_line != line);
-                       if (!tok)
-                               return NULL;
                        tok->type = ISL_TOKEN_GE;
                        return tok;
                }
                if (c != -1)
                        isl_stream_ungetc(s, c);
+               tok->type = ISL_TOKEN_GT;
+               return tok;
        }
        if (c == '<') {
                int c;
+               tok = isl_token_new(s->ctx, line, col, old_line != line);
+               if (!tok)
+                       return NULL;
                if ((c = isl_stream_getc(s)) == '=') {
-                       tok = isl_token_new(s->ctx, line, col, old_line != line);
-                       if (!tok)
-                               return NULL;
                        tok->type = ISL_TOKEN_LE;
                        return tok;
                }
                if (c != -1)
                        isl_stream_ungetc(s, c);
+               tok->type = ISL_TOKEN_LT;
+               return tok;
        }
        if (c == '&') {
                tok = isl_token_new(s->ctx, line, col, old_line != line);
@@ -260,6 +288,15 @@ struct isl_token *isl_stream_next_token(struct isl_stream *s)
                        isl_stream_ungetc(s, c);
                return tok;
        }
+       if (c == '|') {
+               tok = isl_token_new(s->ctx, line, col, old_line != line);
+               if (!tok)
+                       return NULL;
+               tok->type = ISL_TOKEN_OR;
+               if ((c = isl_stream_getc(s)) != '|' && c != -1)
+                       isl_stream_ungetc(s, c);
+               return tok;
+       }
 
        tok = isl_token_new(s->ctx, line, col, old_line != line);
        if (!tok)
@@ -297,5 +334,6 @@ void isl_stream_free(struct isl_stream *s)
                isl_stream_error(s, tok, "unexpected token");
                isl_token_free(tok);
        }
+       isl_ctx_deref(s->ctx);
        free(s);
 }