From 32af6897ac42ed00f768607f549bf1a891b87b69 Mon Sep 17 00:00:00 2001 From: khamidou Date: Thu, 26 Feb 2009 19:30:23 +0100 Subject: [PATCH] fixed some idiotic bugs --- Makefile | 10 +++++++--- croma.c | 5 ++++- errors.c | 14 ++++++++++++++ errors.h | 6 +++++- lexer.l | 17 +++++++++-------- macro.c | 6 +++--- macro.h | 2 +- parse.c | 50 ++++++++++++++++++++++++++------------------------ parse.h | 3 +-- tokens.h | 11 +---------- 10 files changed, 71 insertions(+), 53 deletions(-) create mode 100644 errors.c diff --git a/Makefile b/Makefile index 798ccaf..28cccef 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -OBJS=croma.o lex.yy.o parse.o macro.o -CFLAGS=-c +OBJS=croma.o lex.yy.o parse.o macro.o errors.o +CFLAGS=-c -g LDFLAGS=-o croma croma: $(OBJS) @@ -18,6 +18,10 @@ lex.yy.o: lexer.l lex lexer.l $(CC) $(CFLAGS) lex.yy.c +errors.o: errors.c errors.h + $(CC) $(CFLAGS) errors.c + clean: rm *.o - rm *~ \ No newline at end of file + rm *~ + rm croma \ No newline at end of file diff --git a/croma.c b/croma.c index 2f5232e..1305f45 100644 --- a/croma.c +++ b/croma.c @@ -6,9 +6,12 @@ #include #include +extern FILE *yyin; int main(int argc, char **argv) { - + yyin = stdin; + init_parser(); + yyparse(); } diff --git a/errors.c b/errors.c new file mode 100644 index 0000000..ac7b305 --- /dev/null +++ b/errors.c @@ -0,0 +1,14 @@ +#include "errors.h" + +void fail(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + + fprintf(stderr, fmt, ap); + + va_end(ap); + + exit(EXIT_FAILURE); +} + diff --git a/errors.h b/errors.h index df657c7..baf0f4b 100644 --- a/errors.h +++ b/errors.h @@ -1,6 +1,10 @@ #ifndef __ERRORS_H__ #define __ERRORS_H__ -#define fatal(msg) do { printf(msg); exit(EXIT_FAILURE); } while(0) +#include +#include +#include + +void fail(char *fmt, ...); #endif diff --git a/lexer.l b/lexer.l index 2d228c1..22d6669 100644 --- a/lexer.l +++ b/lexer.l @@ -16,11 +16,12 @@ int yywrap() %% -"(" { return LPAREN; } -")" { return RPAREN; } -"{{" { return LBRACKET; } -"}}" { return RBRACKET; } -"foreach" { return FOREACH; } -[:alpha:]+ { return WORD; }; -"$$" { return DOLDOL; } -[ \t\n]+ { return SPACES; } +"(" { return LPAREN; } +")" { return RPAREN; } +"{{" { return LBRACKET; } +"}}" { return RBRACKET; } +"," { return COMMA; } +"foreach" { return FOREACH; } +[a-zA-Z0-9]+ { return WORD; } +"$$" { return DOLDOL; } +[ \t\n]+ /* { return SPACES; } */ diff --git a/macro.c b/macro.c index 3054a2e..d73d34d 100644 --- a/macro.c +++ b/macro.c @@ -6,12 +6,12 @@ struct croma_block* alloc_and_insert_block(void) { - struct croma_block *block = malloc(sizeof(croma_block)); + struct croma_block *block = malloc(sizeof(struct croma_block)); if (block == NULL) - abort("Unable to alloc() memory"); + fail("Unable to alloc() memory"); - TAILQ_INSERT_TAIL(&blocks_head, block, number); + TAILQ_INSERT_TAIL(&blocks_head, block, blocks); return block; } diff --git a/macro.h b/macro.h index 21669a2..1c159c2 100644 --- a/macro.h +++ b/macro.h @@ -2,7 +2,7 @@ #define __CROMA_MACRO__ #include - +#include "errors.h" #include "queue.h" /* diff --git a/parse.c b/parse.c index 7135de8..13a776e 100644 --- a/parse.c +++ b/parse.c @@ -1,23 +1,27 @@ +#include #include "parse.h" -void abort(char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - - fprintf(stderr, fmt, ap); +extern char *strndup (__const char *__string, size_t __n); +extern char *yytext; - va_end(ap); +char *token_list[] = { "left parenthesis", + "right parenthesis", + "{{", + "}}", + "word", + "$$ sign", + "spaces", + "comma", + "foreach", NULL}; - exit(EXIT_FAILURE); -} void expect(int t) { int c = yylex(); if (c != t) { - fprintf(stderr, "Expected token %s in input, got %s (%s).\n", token_list[t], token_list[c], yytext); - exit(EXIT_FAILURE); + fail("Expected token %s in input, got %s (%s).\n", + token_list[t], token_list[c], yytext); + fail("unexpected token : %s \n", yytext); } } @@ -39,7 +43,7 @@ void yyparse(void) break; default: - abort("garbage somewhere in input"); + fail("garbage somewhere in input.\n"); } } @@ -48,8 +52,6 @@ void foreach_expr(void) { struct croma_block *b = alloc_and_insert_block(); - int t = yylex(); - expect(LPAREN); parse_arglist(b); @@ -58,12 +60,12 @@ void foreach_expr(void) parse_block(b); - char *p = b->parameters; + char **p = b->parameters; char *c = b->contents; if (c == NULL) - abort("the impossible happened : b->contents == NULL !, file: %s, line %d", __FILE__, __LINE__); + fail("the impossible happened : b->contents == NULL !, file: %s, line %d", __FILE__, __LINE__); - while(strcmp(p, "\0") != 0) { + while(strcmp(*p, "\0") != 0) { while(c != '\0') { if (*c == '$' && *c+1 == '$') { printf("%s", p); @@ -82,13 +84,12 @@ void foreach_expr(void) void parse_arglist(struct croma_block *b) { if (b == NULL) - abort("the impossible happened : b == NULL !, file: %s, line %d", __FILE__, __LINE__); + fail("the impossible happened : b == NULL !, file: %s, line %d", __FILE__, __LINE__); b->parameters = malloc(16*sizeof(char *)); - char *s = b->parameters; + char **p = b->parameters; int i; - int t = yylex(); expect(WORD); @@ -97,12 +98,12 @@ void parse_arglist(struct croma_block *b) */ for (i = 0; i < 16; i++) { - *s++ = strndup(yytext, MAX_PARAM_LEN); + *p++ = strndup(yytext, MAX_PARAM_LEN); expect(COMMA); expect(WORD); } - *s++ = "\0"; + *p++ = "\0"; expect(RPAREN); @@ -113,7 +114,7 @@ void parse_block(struct croma_block *b) int i = 0; /* FIXME : realloc() b->contents on the fly*/ if (b == NULL) - abort("the impossible happened : b == NULL !, file: %s, line %d", __FILE__, __LINE__); + fail("the impossible happened : b == NULL !, file: %s, line %d", __FILE__, __LINE__); /* Copy the whole buffer in b->contents. FIXME: expand macros before copying contents. @@ -121,7 +122,8 @@ void parse_block(struct croma_block *b) int t = yylex(); while (t != RBRACKET && i < 8192) { - i += strncat(b->contents, yytext, 8192 - i); + strncat(b->contents, yytext, 8192 - i); + i += strlen(yytext); yylex(); } diff --git a/parse.h b/parse.h index 6216166..1a11473 100644 --- a/parse.h +++ b/parse.h @@ -4,14 +4,13 @@ #include #include #include -#include +#include "errors.h" #include "tokens.h" #include "macro.h" #define MAX_PARAM_LEN 4096 -void abort(char *fmt, ...); void init_parser(void); void yyparse(void); diff --git a/tokens.h b/tokens.h index 3631c6a..dc87c6d 100644 --- a/tokens.h +++ b/tokens.h @@ -2,19 +2,10 @@ #define __TOKENS_H__ enum { - LPAREN, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, COMMA, + LPAREN = 0, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, COMMA, FOREACH, }; -char *token_list[] = { "left parenthesis", - "right parenthesis", - "{{", - "}}", - "word", - "$$ sign", - "spaces", - "comma", - "foreach", }; extern int yylex(void); extern char *yytext; -- 2.43.0