From d0782ca08f38de03e045dacc78ef290f3fa91104 Mon Sep 17 00:00:00 2001 From: khamidou Date: Mon, 2 Mar 2009 13:28:16 +0100 Subject: [PATCH] changing internal data structures --- macro.c | 13 +++++++++++++ macro.h | 12 +++++++++++- parse.c | 58 +++++++++++++++++++++++++-------------------------------- parse.h | 1 + 4 files changed, 50 insertions(+), 34 deletions(-) diff --git a/macro.c b/macro.c index 8a37531..41c60ed 100644 --- 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 1d32050..d9f904b 100644 --- a/macro.h +++ b/macro.h @@ -12,12 +12,20 @@ */ +#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 4c8a343..0163b7a 100644 --- 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 1a11473..f289ff0 100644 --- 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); -- 2.43.0