]> khamidou — GitHub mirrors - croma.git/commitdiff
fixed some idiotic bugs
authorkhamidou <karim@lambda-expr.org>
Thu, 26 Feb 2009 18:30:23 +0000 (19:30 +0100)
committerkhamidou <karim@lambda-expr.org>
Thu, 26 Feb 2009 18:30:23 +0000 (19:30 +0100)
Makefile
croma.c
errors.c [new file with mode: 0644]
errors.h
lexer.l
macro.c
macro.h
parse.c
parse.h
tokens.h

index 798ccaf31c97a0d1d9ce69adccd0e76e3ee95d51..28cccefd9f79a43ccf42cd1539903e7a9f11a161 100644 (file)
--- 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 2f5232ec993068f0c3d07f1c046300d35bb61905..1305f452261033116e012ac3848d4de40859a3d7 100644 (file)
--- a/croma.c
+++ b/croma.c
@@ -6,9 +6,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+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 (file)
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);
+}
+
index df657c71dfe3a0e7c6ebe58e6692a4c426b9df3a..baf0f4b4ecf1f580a7f326dd25a52a0e9cfc37c4 100644 (file)
--- 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 <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+void fail(char *fmt, ...);
 
 #endif
diff --git a/lexer.l b/lexer.l
index 2d228c15640bf6d4685ad3b7dd4080ebcd8670f9..22d6669a0b516f19be99fea669936fa22a4f7cdb 100644 (file)
--- 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 3054a2e8498d9c0d72c4fb4a25ea504454c953e1..d73d34db5c5bd1f56a9330c2a709c6024ec5a54e 100644 (file)
--- 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 21669a277b4fbb484ad4ca910c5aaa23bebecfa6..1c159c20f921a0951108d61dc3e84782cfe912d2 100644 (file)
--- a/macro.h
+++ b/macro.h
@@ -2,7 +2,7 @@
 #define __CROMA_MACRO__
 
 #include <stdlib.h>
-
+#include "errors.h"
 #include "queue.h"
 
 /*
diff --git a/parse.c b/parse.c
index 7135de8794d90a9406839810502c6e17f47b39cd..13a776ea162ff728eaf2e2c2e4ed366dae506e87 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -1,23 +1,27 @@
+#include <string.h>
 #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 6216166a884f324b35f5e079af060fad4e3088ee..1a11473f09a8c1069c7da22b2804c8d2f814b80c 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -4,14 +4,13 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <stdarg.h>
 
+#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);
 
index 3631c6a9b422afb71cbe816b7528609d78d71b70..dc87c6d0548e6ab25f85ccfd83f38e38a1142a40 100644 (file)
--- 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;