From ebd3927480c05c6584695cb22a0ca698cfdbfe7b Mon Sep 17 00:00:00 2001 From: khamidou Date: Mon, 9 Mar 2009 21:14:17 +0100 Subject: [PATCH] refactoring... --- Makefile | 5 ++- dynstring.c | 69 ++++++++++++++++++++----------- dynstring.h | 7 +++- macro.c | 50 +++++++++++++++++++++- macro.h | 21 +++++----- parse.c | 117 ++++++++++++++++++++++++++++------------------------ 6 files changed, 176 insertions(+), 93 deletions(-) diff --git a/Makefile b/Makefile index 1cbf453..2f6d691 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJS=croma.o lex.yy.o lex.mlex.o parse.o macro.o errors.o +OBJS=croma.o lex.yy.o lex.mlex.o parse.o macro.o errors.o dynstring.o CFLAGS=-c -g LDFLAGS=-o croma @@ -25,6 +25,9 @@ lex.mlex.o: mlexer.l errors.o: errors.c errors.h $(CC) $(CFLAGS) errors.c +dynstring.o: dynstring.c dynstring.h + $(CC) $(CFLAGS) dynstring.c + clean: rm *.o rm *~ diff --git a/dynstring.c b/dynstring.c index 2b330f2..c9a6364 100644 --- a/dynstring.c +++ b/dynstring.c @@ -1,26 +1,20 @@ #include "dynstring.h" -void init_dynstring(struct croma_block *b) -{ - if (b == NULL) - return; - - TAILQ_INIT(&b->dstrings_head); -} - -struct dstring* alloc_and_insert_string(struct ccroma_block *b, size_t len) +struct dstring* alloc_and_insert_string(struct croma_block *b, size_t len) { if (b == NULL) return NULL; - struct dstring *s = calloc(len, sizeof(char)); + struct dstring *s = calloc(sizeof(struct dstring), 1); + s->contents = calloc(len, sizeof(char)); + s->length = len; - if (s == NULL) + if (s == NULL || s->contents == NULL) fail("Unable to alloc() memory"); TAILQ_INSERT_TAIL(&b->dstrings_head, s, strings); - return block; + return s; } struct dstring* alloc_after(struct croma_block *b, struct dstring *before) @@ -31,14 +25,14 @@ struct dstring* alloc_after(struct croma_block *b, struct dstring *before) struct dstring *ptr; TAILQ_FOREACH(ptr, &b->dstrings_head, strings) { - if (strncmp(ptr->p, before->p, 32) == 0) + if (strncmp(ptr->contents, before->contents, 32) == 0) { - struct dstring *s = calloc(len, sizeof(char)); + struct dstring *s = calloc(before->length, sizeof(char)); if (s == NULL) fail("Unable to alloc() memory"); - TAILQ_INSERT_AFTER(dstrings_head, before, s, strings); + TAILQ_INSERT_AFTER(&b->dstrings_head, before, s, strings); break; } } @@ -53,8 +47,8 @@ void free_dstring(struct croma_block *b, struct dstring *s) TAILQ_REMOVE(&b->dstrings_head, s, strings); - if (s->p != NULL) - free(s->p); + if (s->contents != NULL) + free(s->contents); free(s); @@ -68,8 +62,8 @@ void free_all_dstrings(struct croma_block *b) struct dstring *s; TAILQ_FOREACH(s, &b->dstrings_head, strings) { - TAILQ_REMOVE(&dstrings_head, s, strings); - free_dstring(s); + TAILQ_REMOVE(&b->dstrings_head, s, strings); + free_dstring(b, s); } } /* @@ -80,19 +74,44 @@ struct dstring* break_after(struct croma_block *b, struct dstring *before, char if (b == NULL || before == NULL) return NULL; - int s_len = before->length - (s - p); /* the length of the string after s */ + int s_len = before->length - (s - before->contents); /* the length of the string after s */ - before->length = s - p; + before->length = s - before->contents; - struct dstring *after = alloc_after(before); + struct dstring *after = alloc_after(b, before); *s = '\0'; /* terminate the string. FIXME: use realloc() */ - after->p = calloc(s_len, sizeof(char)); - if (after->p == NULL) + after->contents = calloc(s_len, sizeof(char)); + if (after->contents == NULL) fail("Unable to allocate memory "); - memcpy(after->p, s, s_len); + memcpy(after->contents, s, s_len); after->length = s_len; return after; } + +/* + This functions copies all the strings in b2 at the end of b1. + */ +struct dstring *paste_string(struct croma_block *b1, struct croma_block *b2) +{ + if (b1 == NULL || b2 == NULL) + return NULL; + + struct dstring *s1, *s2; + struct dstring *s2_head = NULL; /* the first element of the pasted string */ + + TAILQ_FOREACH(s2, &b2->dstrings_head, strings) { + s1 = alloc_and_insert_string(b1, 0); + + if (s2_head != NULL) + s2_head = s1; + + s1->contents = calloc(strlen(s2->contents), sizeof(char)); + memcpy(s1->contents, s2->contents, strlen(s2->contents)); + s1->length = s2->length; + } + + return s2_head; +} diff --git a/dynstring.h b/dynstring.h index 39050dd..845a6b1 100644 --- a/dynstring.h +++ b/dynstring.h @@ -5,8 +5,10 @@ #include "queue.h" #include "macro.h" +extern struct croma_block; + struct dstring { - char *p; + char *contents; int length; TAILQ_ENTRY(dstring) strings; }; @@ -17,6 +19,7 @@ struct dstring* alloc_after(struct croma_block *b, struct dstring *before); void free_dstring(struct croma_block *b, struct dstring *s); void free_all_dstrings(struct croma_block *b); -struct dstring* break_after(struct dstring *before, char *s); +struct dstring* break_after(struct croma_block *b, struct dstring *before, char *s); +struct dstring *paste_string(struct croma_block *b1, struct croma_block *b2); #endif diff --git a/macro.c b/macro.c index 475c1df..bcafbae 100644 --- a/macro.c +++ b/macro.c @@ -13,7 +13,7 @@ struct croma_block* alloc_and_insert_block(void) TAILQ_INSERT_TAIL(&blocks_head, block, blocks); TAILQ_INIT(&block->args_head); - TAILQ_INIT(&block->strings_head); + TAILQ_INIT(&block->dstrings_head); return block; } @@ -35,12 +35,58 @@ void free_block(struct croma_block *b) if (b == NULL) return; - free(b->contents); + free_all_dstrings(b); free(b->name); TAILQ_REMOVE(&blocks_head, b, blocks); free(b); } +struct croma_block* copy_block(struct croma_block *b) +{ + if (b == NULL) + return NULL; + + /* + FIXME: implement lexical scoping. + It's really simple, just make copy_block insert an element in blocks_head. + */ + struct croma_block * c = calloc(1, sizeof(struct croma_block)); + + if (c == NULL) + fail("Unable to allocate memory block"); + + TAILQ_INIT(&b->args_head); + TAILQ_INIT(&b->dstrings_head); + + if( paste_string(c, b) == NULL || + copy_args(c, b) == NULL) + fail("Unable to set up environment for macro expansion."); + + return b; + +} + +struct croma_arg* copy_args(struct croma_block *dest, struct croma_block *src) +{ + if (dest == NULL || src == NULL) + return NULL; + + struct croma_arg *a1, *a2, *a2_start; + + TAILQ_FOREACH(a2, &src->args_head, params) { + a1 = alloc_and_insert_string(dest, 0); + + if (a2_start == NULL) + a2_start = a1; + + a1->name = calloc(strlen(a2->name), sizeof(char)); + memcpy(a1->name, a2->name, strlen(a2->name)); + } + + return a2_start; + +} + void free_arg(struct croma_block *b, struct croma_arg *a) { if (b == NULL) diff --git a/macro.h b/macro.h index 820c310..a3caca4 100644 --- a/macro.h +++ b/macro.h @@ -2,17 +2,11 @@ #define __CROMA_MACRO__ #include +#include #include "errors.h" #include "queue.h" +#include "dynstring.h" -/* - A block is the memory representation of a {{ }} block, along with its - parameters. - A block has a name if it is created using a define() call. - -*/ - -#include "queue.h" struct croma_arg { char *name; @@ -20,10 +14,15 @@ struct croma_arg { TAILQ_ENTRY(croma_arg) params; }; +/* + A block is the memory representation of a {{ }} block, along with its + parameters. + A block has a name if it is created using a define() call. + +*/ + struct croma_block { char *name; - //char *contents; - int contents_length; TAILQ_HEAD(croma_args_head, croma_arg) args_head; TAILQ_HEAD(dstring_heads, dstring) dstrings_head; TAILQ_ENTRY(croma_block) blocks; @@ -41,6 +40,8 @@ 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); +struct croma_block* copy_block(struct croma_block *b); +struct croma_arg* copy_args(struct croma_block *dest, struct croma_block *src); void free_block(struct croma_block *b); void free_arg(struct croma_block *b, struct croma_arg *a); struct croma_block *lookup_symbol(char *name); diff --git a/parse.c b/parse.c index afd8ef9..0f1d45a 100644 --- a/parse.c +++ b/parse.c @@ -44,7 +44,6 @@ void expect(int t) void init_parser(void) { TAILQ_INIT(&blocks_head); - init_dynstring(); } @@ -87,26 +86,27 @@ void foreach_expr(void) parse_block(b); struct croma_arg *a; - - if (b->contents == NULL) - fail("the impossible happened : b->contents == NULL !, file: %s, line %d", __FILE__, __LINE__); + struct dstring *s; 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", a->value); - c++; /* skip the second '$' */ - } else { - printf("%c", *c); + TAILQ_FOREACH(s, &b->dstrings_head, strings) { + char *c = s->contents; + int i = 0; + + for(i = 0; i < s->length && c != NULL; i++) { + if (*c == '$' && *(c+1) == '$') { + printf("%s", a->value); + c++; /* skip the second '$' */ + + } else { + printf("%c", *c); + } + c++; } - c++; + free_arg(b, a); } - free_arg(b, a); } free_block(b); @@ -195,11 +195,11 @@ void parse_block(struct croma_block *b) if (b == NULL) fail("the impossible happened : b == NULL !, file: %s, line %d", __FILE__, __LINE__); - b->contents = malloc(8192); - b->contents_length = 0; + struct dstring *s = alloc_and_insert_string(b, 8192); - if (b->contents == NULL) - fail("Unable to alloc memory for input buffer", __FILE__, __LINE__); + struct croma_block *defblock; /* the address of a block defining a macro. */ + + s->length = 0; /* Copy the whole buffer in b->contents. FIXME: expand macros before copying contents. @@ -207,12 +207,20 @@ void parse_block(struct croma_block *b) int t = yylex(); while (t != RBRACKET && i < 8192) { - strncat(b->contents, yytext, 8192 - i); + switch(t) { + case WORD: + defblock = lookup_symbol(yytext); + + if (defblock != NULL) { + struct croma_block *env = copy_block(defblock); + } + } + strncat(s->contents, yytext, 8192 - i); i += strlen(yytext); t = yylex(); } - b->contents_length = i; + s->length = i; return; @@ -226,43 +234,43 @@ void parse_block(struct croma_block *b) char *replace_arguments(struct croma_block *b, char *t) { - if (b == NULL) - return; +/* if (b == NULL) */ +/* return; */ - char *buf = calloc(8192, sizeof(char)); - char *s = strdup(t); - char *ss = s; /* Used only to compute the number of elements copied in buf. */ - char *argname; - int buflen = s - ss; - struct croma_arg * arg; +/* char *buf = calloc(8192, sizeof(char)); */ +/* char *s = strdup(t); */ +/* char *ss = s; /\* Used only to compute the number of elements copied in buf. *\/ */ +/* char *argname; */ +/* int buflen = s - ss; */ +/* struct croma_arg * arg; */ - while(*s != '\0' && buflen < 8192) { +/* while(*s != '\0' && buflen < 8192) { */ - while(*s == ' ' || *s == '\t' || *s == '\n') - *buf++ = *s++; +/* while(*s == ' ' || *s == '\t' || *s == '\n') */ +/* *buf++ = *s++; */ - argname = extract_word(s); - arg = lookup_arg(b, argname); +/* argname = extract_word(s); */ +/* arg = lookup_arg(b, argname); */ - if (arg != NULL && arg->value != NULL) { +/* if (arg != NULL && arg->value != NULL) { */ - strncat(buf, arg->value, 8192 - buflen); +/* strncat(buf, arg->value, 8192 - buflen); */ - /* Don't forget to increment the pointers by the number - of bytes copied - */ +/* /\* Don't forget to increment the pointers by the number */ +/* of bytes copied */ +/* *\/ */ - s += strlen(arg->value); - buf += strlen(arg->value); +/* s += strlen(arg->value); */ +/* buf += strlen(arg->value); */ - } else { - /* If the value is not found, copy the word into the buffer. */ - while(*s != ' ' || *s != '\t' || *s != '\n') - *buf++ = *s++; - } +/* } else { */ +/* /\* If the value is not found, copy the word into the buffer. *\/ */ +/* while(*s != ' ' || *s != '\t' || *s != '\n') */ +/* *buf++ = *s++; */ +/* } */ - buflen = s - ss; - } +/* buflen = s - ss; */ +/* } */ } @@ -273,6 +281,7 @@ char *expand_macro(char *s) char *buf = malloc(8192); struct croma_block *b; + char *name; int i = 0; @@ -286,12 +295,14 @@ char *expand_macro(char *s) switch(c) { case WORD: - char *name = extract_word(mlextext); - b = lookup_symbol(name); - if (b != NULL) { - + /*name = extract_word(mlextext); + b = lookup_symbol(name); + if (b != NULL) { + + }*/ + break; } - break; + } -- 2.43.0