/*
* Copyright 2008-2009 Katholieke Universiteit Leuven
*
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, K.U.Leuven, Departement
* Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
#include <string.h>
#include <strings.h>
#include <isl/ctx.h>
-#include <isl/stream.h>
+#include <isl_stream_private.h>
+#include <isl/map.h>
+#include <isl/aff.h>
struct isl_keyword {
char *name;
return keyword->type;
}
-static struct isl_token *isl_token_new(struct isl_ctx *ctx,
+struct isl_token *isl_token_new(isl_ctx *ctx,
int line, int col, unsigned on_new_line)
{
struct isl_token *tok = isl_alloc_type(ctx, struct isl_token);
tok->col = col;
tok->on_new_line = on_new_line;
tok->is_keyword = 0;
+ tok->u.s = NULL;
return tok;
}
return;
if (tok->type == ISL_TOKEN_VALUE)
isl_int_clear(tok->u.v);
- else if (tok->type == ISL_TOKEN_IDENT ||
- tok->type == ISL_TOKEN_STRING ||
- tok->is_keyword)
+ else if (tok->type == ISL_TOKEN_MAP)
+ isl_map_free(tok->u.map);
+ else if (tok->type == ISL_TOKEN_AFF)
+ isl_pw_aff_free(tok->u.pwaff);
+ else
free(tok->u.s);
free(tok);
}
fprintf(stderr, "got ident '%s'\n", tok->u.s);
else if (tok->is_keyword)
fprintf(stderr, "got keyword '%s'\n", tok->u.s);
+ else if (tok->type == ISL_TOKEN_VALUE) {
+ fprintf(stderr, "got value '");
+ isl_int_print(stderr, tok->u.v, 0);
+ fprintf(stderr, "'\n");
+ } else if (tok->type == ISL_TOKEN_MAP) {
+ isl_printer *p;
+ fprintf(stderr, "got map '");
+ p = isl_printer_to_file(s->ctx, stderr);
+ p = isl_printer_print_map(p, tok->u.map);
+ isl_printer_free(p);
+ fprintf(stderr, "'\n");
+ } else if (tok->type == ISL_TOKEN_AFF) {
+ isl_printer *p;
+ fprintf(stderr, "got affine expression '");
+ p = isl_printer_to_file(s->ctx, stderr);
+ p = isl_printer_print_pw_aff(p, tok->u.pwaff);
+ isl_printer_free(p);
+ fprintf(stderr, "'\n");
+ } else if (tok->u.s)
+ fprintf(stderr, "got token '%s'\n", tok->u.s);
else
fprintf(stderr, "got token type %d\n", tok->type);
}
s->col = 0;
s->eof = 0;
s->c = -1;
+ s->n_un = 0;
for (i = 0; i < 5; ++i)
s->tokens[i] = NULL;
s->n_token = 0;
struct isl_stream* isl_stream_new_str(struct isl_ctx *ctx, const char *str)
{
- struct isl_stream *s = isl_stream_new(ctx);
+ struct isl_stream *s;
+ if (!str)
+ return NULL;
+ s = isl_stream_new(ctx);
if (!s)
return NULL;
s->str = str;
return s;
}
-static int isl_stream_getc(struct isl_stream *s)
+static int stream_getc(struct isl_stream *s)
{
int c;
if (s->eof)
return -1;
+ if (s->n_un)
+ return s->c = s->un[--s->n_un];
if (s->file)
c = fgetc(s->file);
else {
static void isl_stream_ungetc(struct isl_stream *s, int c)
{
- if (s->file)
- ungetc(c, s->file);
- else
- --s->str;
+ isl_assert(s->ctx, s->n_un < 5, return);
+ s->un[s->n_un++] = c;
s->c = -1;
}
+static int isl_stream_getc(struct isl_stream *s)
+{
+ int c;
+
+ do {
+ c = stream_getc(s);
+ if (c != '\\')
+ return c;
+ c = stream_getc(s);
+ } while (c == '\n');
+
+ isl_stream_ungetc(s, c);
+
+ return '\\';
+}
+
static int isl_stream_push_char(struct isl_stream *s, int c)
{
if (s->len >= s->size) {
+ char *buffer;
s->size = (3*s->size)/2;
- s->buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
- if (!s->buffer)
+ buffer = isl_realloc_array(s->ctx, s->buffer, char, s->size);
+ if (!buffer)
return -1;
+ s->buffer = buffer;
}
s->buffer[s->len++] = c;
return 0;
return ISL_TOKEN_AND;
if (!strcasecmp(s->buffer, "or"))
return ISL_TOKEN_OR;
+ if (!strcasecmp(s->buffer, "not"))
+ return ISL_TOKEN_NOT;
if (!strcasecmp(s->buffer, "infty"))
return ISL_TOKEN_INFTY;
if (!strcasecmp(s->buffer, "infinity"))
return ISL_TOKEN_INFTY;
if (!strcasecmp(s->buffer, "NaN"))
return ISL_TOKEN_NAN;
+ if (!strcasecmp(s->buffer, "min"))
+ return ISL_TOKEN_MIN;
if (!strcasecmp(s->buffer, "max"))
return ISL_TOKEN_MAX;
+ if (!strcasecmp(s->buffer, "rat"))
+ return ISL_TOKEN_RAT;
+ if (!strcasecmp(s->buffer, "true"))
+ return ISL_TOKEN_TRUE;
+ if (!strcasecmp(s->buffer, "false"))
+ return ISL_TOKEN_FALSE;
+ if (!strcasecmp(s->buffer, "ceild"))
+ return ISL_TOKEN_CEILD;
+ if (!strcasecmp(s->buffer, "floord"))
+ return ISL_TOKEN_FLOORD;
+ if (!strcasecmp(s->buffer, "mod"))
+ return ISL_TOKEN_MOD;
if (!s->keywords)
return ISL_TOKEN_IDENT;
if (c == '(' ||
c == ')' ||
c == '+' ||
- c == '/' ||
c == '*' ||
c == '%' ||
+ c == '?' ||
c == '^' ||
- c == '=' ||
c == '@' ||
+ c == '$' ||
c == ',' ||
c == '.' ||
c == ';' ||
tok = isl_token_new(s->ctx, line, col, old_line != line);
if (!tok)
return NULL;
+ tok->u.s = strdup("->");
tok->type = ISL_TOKEN_TO;
return tok;
}
}
}
if (c == '-' || isdigit(c)) {
+ int minus = c == '-';
tok = isl_token_new(s->ctx, line, col, old_line != line);
if (!tok)
return NULL;
isl_stream_ungetc(s, c);
isl_stream_push_char(s, '\0');
isl_int_read(tok->u.v, s->buffer);
+ if (minus && isl_int_is_zero(tok->u.v)) {
+ tok->col++;
+ tok->on_new_line = 0;
+ isl_stream_push_token(s, tok);
+ tok = isl_token_new(s->ctx, line, col, old_line != line);
+ if (!tok)
+ return NULL;
+ tok->type = (enum isl_token_type) '-';
+ }
return tok;
}
if (isalpha(c) || c == '_') {
tok->u.s = strdup(s->buffer);
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->u.s = strdup("==");
+ tok->type = ISL_TOKEN_EQ_EQ;
+ return tok;
+ }
+ if (c != -1)
+ isl_stream_ungetc(s, c);
+ tok->type = (enum isl_token_type) '=';
+ 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->u.s = strdup(":=");
tok->type = ISL_TOKEN_DEF;
return tok;
}
if (!tok)
return NULL;
if ((c = isl_stream_getc(s)) == '=') {
+ tok->u.s = strdup(">=");
tok->type = ISL_TOKEN_GE;
return tok;
} else if (c == '>') {
if ((c = isl_stream_getc(s)) == '=') {
+ tok->u.s = strdup(">>=");
tok->type = ISL_TOKEN_LEX_GE;
return tok;
}
+ tok->u.s = strdup(">>");
tok->type = ISL_TOKEN_LEX_GT;
- } else
+ } else {
+ tok->u.s = strdup(">");
tok->type = ISL_TOKEN_GT;
+ }
if (c != -1)
isl_stream_ungetc(s, c);
return tok;
if (!tok)
return NULL;
if ((c = isl_stream_getc(s)) == '=') {
+ tok->u.s = strdup("<=");
tok->type = ISL_TOKEN_LE;
return tok;
} else if (c == '<') {
if ((c = isl_stream_getc(s)) == '=') {
+ tok->u.s = strdup("<<=");
tok->type = ISL_TOKEN_LEX_LE;
return tok;
}
+ tok->u.s = strdup("<<");
tok->type = ISL_TOKEN_LEX_LT;
- } else
+ } else {
+ tok->u.s = strdup("<");
tok->type = ISL_TOKEN_LT;
+ }
if (c != -1)
isl_stream_ungetc(s, c);
return tok;
if (!tok)
return NULL;
tok->type = ISL_TOKEN_AND;
- if ((c = isl_stream_getc(s)) != '&' && c != -1)
+ if ((c = isl_stream_getc(s)) != '&' && c != -1) {
+ tok->u.s = strdup("&");
isl_stream_ungetc(s, c);
+ } else
+ tok->u.s = strdup("&&");
return tok;
}
if (c == '|') {
if (!tok)
return NULL;
tok->type = ISL_TOKEN_OR;
- if ((c = isl_stream_getc(s)) != '|' && c != -1)
+ if ((c = isl_stream_getc(s)) != '|' && c != -1) {
+ tok->u.s = strdup("|");
+ isl_stream_ungetc(s, c);
+ } else
+ tok->u.s = strdup("||");
+ return tok;
+ }
+ if (c == '/') {
+ tok = isl_token_new(s->ctx, line, col, old_line != line);
+ if (!tok)
+ return NULL;
+ if ((c = isl_stream_getc(s)) != '\\' && c != -1) {
+ tok->type = (enum isl_token_type) '/';
+ isl_stream_ungetc(s, c);
+ } else {
+ tok->u.s = strdup("/\\");
+ tok->type = ISL_TOKEN_AND;
+ }
+ return tok;
+ }
+ if (c == '\\') {
+ tok = isl_token_new(s->ctx, line, col, old_line != line);
+ if (!tok)
+ return NULL;
+ if ((c = isl_stream_getc(s)) != '/' && c != -1) {
+ tok->type = (enum isl_token_type) '\\';
+ isl_stream_ungetc(s, c);
+ } else {
+ tok->u.s = strdup("\\/");
+ tok->type = ISL_TOKEN_OR;
+ }
+ return tok;
+ }
+ if (c == '!') {
+ tok = isl_token_new(s->ctx, line, col, old_line != line);
+ if (!tok)
+ return NULL;
+ if ((c = isl_stream_getc(s)) == '=') {
+ tok->u.s = strdup("!=");
+ tok->type = ISL_TOKEN_NE;
+ return tok;
+ } else {
+ tok->type = ISL_TOKEN_NOT;
+ tok->u.s = strdup("!");
+ }
+ if (c != -1)
isl_stream_ungetc(s, c);
return tok;
}