X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lex.c;h=9f0201319dfa47818beeef380a1786f45e8992a5;hb=f7469b506efca7aa155899a9398b7ce490dd4ba1;hp=ba892dbdf29789b18e073cd936e85a415c279a11;hpb=fa9f360626f91ca9fd6945aace8c90c9ec4bf077;p=umpf.git diff --git a/lex.c b/lex.c index ba892db..9f02013 100644 --- a/lex.c +++ b/lex.c @@ -5,8 +5,7 @@ #include #include "cond.tab.h" - -#define BUFSIZE 4096 +#include "umpf.h" #define KLEN 10 struct keys { @@ -14,16 +13,24 @@ struct keys { enum yytokentype keytoks; }; -static int line; - -static struct keys k[] = +static struct keys kwds[] = { {"copy", KW_COPY}, {"else", KW_ELSE}, {"if", KW_IF}, {"mail", KW_MAIL}, - {"pipe", KW_PIPE} + {"pipe", KW_PIPE}, + {"discard", KW_DISCARD} }; +void +read_conf(char* filename) +{ + conf = fopen(filename, "r"); + + if (! conf) + die("read_conf: %m"); +} + void __attribute__ ((noreturn)) die(char* msg, ...) { @@ -47,6 +54,17 @@ xmalloc(size_t size) return ret; } +void* +xrealloc(void* buf, size_t size) +{ + buf = realloc(buf, size); + + if (!buf) + die("Low memory"); + + return buf; +} + static void __attribute__ ((noreturn)) parse_err(char* msg, ...) { @@ -60,8 +78,8 @@ parse_err(char* msg, ...) exit(1); } -static char* -xstrdup(char* s) +char* +xstrdup(const char* s) { void* ret; @@ -79,7 +97,7 @@ get_string_out(int delim) int c; char buf[BUFSIZE]; - while ((c = getchar()) != delim || last == '\\'){ + while ((c = getc(conf)) != delim || last == '\\'){ if (last=='\\' && c != delim) buf[i-1] = c; else { @@ -117,7 +135,7 @@ yylex(void) { int c; - while ((c = getchar ()) == ' ' || c == '\t' || c =='\n'){ + while ((c = getc(conf)) == ' ' || c == '\t' || c =='\n'){ if (c == '\n') line++; } @@ -125,19 +143,18 @@ yylex(void) if (c == EOF) return 0; -#define CC(a,b) ((a<<8)|b) - int d = getchar(); + int d = getc(conf); if (d >= 0) { switch (CC(c,d)) { - case CC('!','='): return NEQ; - case CC('!','~'): return NRE; - case CC('<','='): return LE; - case CC('>','='): return GE; - case CC('=','='): return EQ; - case CC('~','~'): return RE; - case CC('-','>'): return ARROW; + case CC('!','='): yylval.n = CC('!','='); return NEQ; + case CC('!','~'): yylval.n = CC('!','='); return NRE; + case CC('<','='): yylval.n = CC('<','='); return LE; + case CC('>','='): yylval.n = CC('>','='); return GE; + case CC('=','='): yylval.n = CC('=','='); return EQ; + case CC('~','~'): yylval.n = CC('~','~'); return RE; + case CC('-','>'): yylval.n = CC('-','>'); return ARROW; } - ungetc(d,stdin); + ungetc(d,conf); } switch (c) { @@ -154,6 +171,11 @@ yylex(void) case '>': case '=': case ';': + case '.': + case '|': + case '&': + case '^': + yylval.n = c; return c; case '"': @@ -163,21 +185,34 @@ yylex(void) } if (c >= '0' && c <= '9'){ - ungetc(c,stdin); - scanf("%d",&yylval.n); - return NUM; + ungetc(c,conf); + int i = 0; + char buf[BUFSIZE]; + + while ((c = getc(conf))>= '0' && c<= '9'){ + buf[i] = c; + i++; + if (i >= BUFSIZE-1) + parse_err("Too long number"); + } + ungetc(c,conf); + buf[i] = 0; + yylval.str = xstrdup(buf); + + return CONST; } if (c == '$'){ int i = 0; char buf[BUFSIZE]; - while (is_var_id(c = getchar())){ + while (is_var_id(c = getc(conf))){ buf[i]=c; i++; if (i >= BUFSIZE-1) parse_err("Too long identifier, max allowed length is %d",BUFSIZE-1); } + ungetc(c,conf); buf[i] = 0; yylval.str = xstrdup(buf); @@ -188,18 +223,19 @@ yylex(void) char buf[KLEN]; int n, i = 0; - ungetc(c,stdin); - while (is_alpha(c = getchar())){ + ungetc(c,conf); + while (is_alpha(c = getc(conf))){ buf[i++] = c; if (i >= KLEN) parse_err("Keyword too long"); } buf[i] = 0; + ungetc(c,conf); - n = (sizeof(k)/sizeof(struct keys)); + n = (sizeof(kwds)/sizeof(struct keys)); for (i = 0; i < n; i++){ - if (!strcmp(buf,k[i].keywords)) - return k[i].keytoks; + if (!strcmp(buf,kwds[i].keywords)) + return kwds[i].keytoks; } parse_err("Unknown keyword %s", buf);