From 54da3a8b808a7454c264ce6e3c6968f9b31f6d46 Mon Sep 17 00:00:00 2001 From: khamidou Date: Tue, 10 Mar 2009 07:20:06 +0100 Subject: [PATCH] still refactoring... --- macro.c | 12 +++++++ macro.h | 1 + parse.c | 97 ++++++++++++++++++--------------------------------------- parse.h | 2 +- 4 files changed, 44 insertions(+), 68 deletions(-) diff --git a/macro.c b/macro.c index bcafbae..760920b 100644 --- a/macro.c +++ b/macro.c @@ -37,6 +37,7 @@ void free_block(struct croma_block *b) free_all_dstrings(b); free(b->name); + free_args(b); TAILQ_REMOVE(&blocks_head, b, blocks); free(b); } @@ -98,6 +99,17 @@ void free_arg(struct croma_block *b, struct croma_arg *a) free(a); } +void free_args(struct croma_block *b) +{ + if (b == NULL) + return; + + struct croma_arg *a; + TAILQ_FOREACH(a, &b->args_head, params) { + free_arg(b, a); + } +} + struct croma_block *lookup_symbol(char *name) { struct croma_block *b; diff --git a/macro.h b/macro.h index a3caca4..34cfbca 100644 --- a/macro.h +++ b/macro.h @@ -44,6 +44,7 @@ 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); +void free_args(struct croma_block *b); struct croma_block *lookup_symbol(char *name); struct croma_arg *lookup_arg(struct croma_block *b, char *name); diff --git a/parse.c b/parse.c index 0f1d45a..d6b2a7b 100644 --- a/parse.c +++ b/parse.c @@ -181,12 +181,6 @@ void save_define_arg(struct croma_block *b) fail("Unable to allocate memory"); } -void bind_macro_arg(struct croma_block *b) -{ - /* - Note : this code assumes that every new element is inserted at the tail. - */ -} void parse_block(struct croma_block *b) { @@ -201,18 +195,31 @@ void parse_block(struct croma_block *b) s->length = 0; - /* Copy the whole buffer in b->contents. - FIXME: expand macros before copying contents. - */ int t = yylex(); while (t != RBRACKET && i < 8192) { switch(t) { case WORD: defblock = lookup_symbol(yytext); - + if (defblock != NULL) { struct croma_block *env = copy_block(defblock); + if (env == NULL) + fail("Unable to allocate memory for macro environment"); + + + expect(LPAREN); + + struct croma_arg *a; + TAILQ_FOREACH(a, &env->args_head, params) { + t = yylex(); + if (t == RPAREN) + break; + + a->value = strdup(yytext); + } + + expand_macro(env); } } strncat(s->contents, yytext, 8192 - i); @@ -223,66 +230,17 @@ void parse_block(struct croma_block *b) s->length = i; return; - -} - -/* - Replace arguments replaces all the references to the arguments - of b in t by their values. - It occurs only in memory. - */ - -char *replace_arguments(struct croma_block *b, char *t) -{ -/* 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; */ - -/* while(*s != '\0' && buflen < 8192) { */ - -/* while(*s == ' ' || *s == '\t' || *s == '\n') */ -/* *buf++ = *s++; */ - -/* argname = extract_word(s); */ -/* arg = lookup_arg(b, argname); */ - -/* if (arg != NULL && arg->value != NULL) { */ - -/* strncat(buf, arg->value, 8192 - buflen); */ - -/* /\* Don't forget to increment the pointers by the number */ -/* of bytes copied */ -/* *\/ */ - -/* 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++; */ -/* } */ - -/* buflen = s - ss; */ -/* } */ - } -char *expand_macro(char *s) +char *expand_macro(struct croma_block *b) { - if (s == NULL) + if (b == NULL) return NULL; char *buf = malloc(8192); - struct croma_block *b; char *name; - + struct croma_block *ab; /* In case of a macro called inside another macro. */ + struct croma_arg *a; int i = 0; if (b == NULL) @@ -295,12 +253,17 @@ char *expand_macro(char *s) switch(c) { case WORD: - /*name = extract_word(mlextext); - b = lookup_symbol(name); - if (b != NULL) { + if ((a = lookup_arg(b, mlextext)) != NULL) { + printf("%s", a->value); + } else if ((b = lookup_symbol(mlextext)) != NULL) { - }*/ + } else { + printf("%s", mlextext); + } break; + + default: + printf("%s", mlextext); } diff --git a/parse.h b/parse.h index 635fae5..f9ac577 100644 --- a/parse.h +++ b/parse.h @@ -16,9 +16,9 @@ void yyparse(void); void foreach_expr(void); void define_expr(void); -int extract_word(char *s); void parse_arglist(struct croma_block *b, int (*tokenize)(void), void (*f) (struct croma_block *b)); void parse_block(struct croma_block *b); +char *expand_macro(struct croma_block *b); void save_foreach_arg(struct croma_block *b); void save_define_arg(struct croma_block *b); -- 2.43.0