]> khamidou — GitHub mirrors - croma.git/commitdiff
changing internal data structures
authorkhamidou <karim@lambda-expr.org>
Mon, 2 Mar 2009 12:28:16 +0000 (13:28 +0100)
committerkhamidou <karim@lambda-expr.org>
Mon, 2 Mar 2009 12:28:16 +0000 (13:28 +0100)
macro.c
macro.h
parse.c
parse.h

diff --git a/macro.c b/macro.c
index 8a37531bbc3baf84a7e5353f0f7e8e90cca705e4..41c60edef9e86e1baf00d7f066e66645102c58cc 100644 (file)
--- a/macro.c
+++ b/macro.c
@@ -12,10 +12,23 @@ struct croma_block* alloc_and_insert_block(void)
                fail("Unable to alloc() memory");
 
        TAILQ_INSERT_TAIL(&blocks_head, block, blocks);
+       TAILQ_INIT(&block->args_head);
 
        return block;
 }
 
+struct croma_arg *alloc_and_insert_arg(struct croma_block *b)
+{
+       struct croma_arg *a = malloc(sizeof(struct croma_arg));
+       
+       if (a == NULL)
+               fail("Unable to alloc memory");
+
+       TAILQ_INSERT_TAIL(&b->args_head, a, params);
+
+       return a;
+}
+
 void free_block(struct croma_block *b)
 {
        if (b == NULL)
diff --git a/macro.h b/macro.h
index 1d3205026f5b204d19ce71e92e58334d35609e8e..d9f904b13b9395cec7a45bf1a13c9c11e3756108 100644 (file)
--- a/macro.h
+++ b/macro.h
 
 */
 
+#include "queue.h"
+
+struct croma_arg {
+       char *name;
+       char *value;
+       TAILQ_ENTRY(croma_arg) params;
+};
+
 struct croma_block {
        char *name;
 //     char **parameters;
-       char *parameters[16];
        char *contents;
        int contents_length;
+       TAILQ_HEAD(croma_args_head, croma_arg) args_head;
        TAILQ_ENTRY(croma_block) blocks;
 };
 
@@ -32,5 +40,7 @@ TAILQ_HEAD(croma_blocks_head, croma_block) blocks_head;
 */
 
 struct croma_block* alloc_and_insert_block(void);
+struct croma_arg *alloc_and_insert_arg(struct croma_block *b);
+void free_block(struct croma_block *b);
 
 #endif
diff --git a/parse.c b/parse.c
index 4c8a3438793a9c0a93da77f3797c8854bd38a0b8..0163b7aa2cd85df26a6fb94675ff6aeab62addf9 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -18,7 +18,6 @@ char *token_list[] = { "left parenthesis",
                       "foreach", 
                       "define", NULL};
 
-
 void expect(int t)
 {
        int c = yylex();
@@ -67,9 +66,12 @@ void yyparse(void)
 
 void foreach_expr(void)
 {
-       struct croma_block *b = alloc_and_insert_block();
-       b->name = strdup("foreach block"); /* FIXME : just to keep free_block() happy */
+       struct croma_block *b = malloc(sizeof(struct croma_block));
 
+       if (b == NULL)
+               fail("Unable to alloc() memory");
+
+       TAILQ_INIT(&b->args_head);
        expect(LPAREN);
 
        parse_arglist(b);
@@ -78,18 +80,18 @@ void foreach_expr(void)
 
        parse_block(b);
 
-       char **p = b->parameters;
+       struct croma_arg *a;
 
        if (b->contents == NULL)
                fail("the impossible happened : b->contents == NULL !, file: %s, line %d", __FILE__, __LINE__); 
 
-       while(*p != NULL) {
+       TAILQ_FOREACH(a, &b->args_head, params) {
                char *c = b->contents;
                int i = 0;
 
                for(i = 0; i < b->contents_length && c != NULL; i++) {
                        if (*c == '$' && *(c+1) == '$') {
-                               printf("%s", *p);
+                               printf("%s", a->value);
                                c++; /* skip the second '$' */
                        } else {
                                printf("%c", *c);
@@ -98,7 +100,6 @@ void foreach_expr(void)
                        c++;
                }
 
-               p++;
        }
 
        free_block(b);
@@ -106,7 +107,14 @@ void foreach_expr(void)
 
 void define_expr(void)
 {
+       struct croma_block *b = alloc_and_insert_block();
 
+       expect(WORD);
+       b->name = strdup(yytext);
+
+       expect(LPAREN);
+       parse_arglist(b);
+       
 }
 
 void parse_arglist(struct croma_block *b)
@@ -114,36 +122,23 @@ void parse_arglist(struct croma_block *b)
        if (b == NULL)
                fail("the impossible happened : b == NULL !, file: %s, line %d", __FILE__, __LINE__);
 
-//     b->parameters = malloc(16*sizeof(char *));
-
-/*     if (b->parameters == NULL)
-               fail("Unable to allocate enough memory !");
-*/
-//     char **p = b->parameters;
-
-       int i;
-
        int c = yylex();
 
-       if (c == RPAREN)
-               return;
+       struct croma_arg * arg;
 
-       /* FIXME : use realloc to implement infinite length parameter
-          lists
-       */
-       for (i = 0; i < 16; i++)
+       while(c != RPAREN)
        {
-               b->parameters[i] = strdup(yytext);
-               if (b->parameters[i] == NULL)
-                       fail("Unable to alloc memory for text %s\n", yytext);
-
-               c = yylex();
-
-               while (c == SPACES)
+               while (c == SPACES) {
                        c = yylex();
+               }
 
                switch(c) {
                case WORD:
+                       arg = alloc_and_insert_arg(b);
+                       arg->value = strdup(yytext);
+                       if (arg->value == NULL)
+                               fail("Unable to allocate memory");
+                       
                        continue;
                        break;
 
@@ -156,12 +151,9 @@ void parse_arglist(struct croma_block *b)
                        break;
                }
 
+               c = yylex();
        }
 
-//     *p++ = "\0";
-
-       expect(RPAREN);
-
 }
 
 void parse_block(struct croma_block *b)
diff --git a/parse.h b/parse.h
index 1a11473f09a8c1069c7da22b2804c8d2f814b80c..f289ff0c37eb558ae1fd975a49929e86e303048d 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -15,6 +15,7 @@ void init_parser(void);
 void yyparse(void);
 
 void foreach_expr(void);
+void define_expr(void);
 void parse_arglist(struct croma_block *b);
 void parse_block(struct croma_block *b);