]> khamidou — GitHub mirrors - croma.git/commitdiff
still refactoring...
authorkhamidou <karim@lambda-expr.org>
Tue, 10 Mar 2009 06:20:06 +0000 (07:20 +0100)
committerkhamidou <karim@lambda-expr.org>
Tue, 10 Mar 2009 06:20:06 +0000 (07:20 +0100)
macro.c
macro.h
parse.c
parse.h

diff --git a/macro.c b/macro.c
index bcafbae7e1f9764156c9b0b0c1ecfea050e0ebaf..760920be556caba75185da3745e6462a2801c047 100644 (file)
--- 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 a3caca4991dee20d34209d7b83441f93ca4ff345..34cfbcad18fd9a7959213a157fc88566f2e8fc1c 100644 (file)
--- 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 0f1d45a5c53bb8df25ec86865b3d3a346d25c0c8..d6b2a7b969eec5d3a25405259bb72f89f476c851 100644 (file)
--- 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 635fae5bdd2cb17ba311ba43b14f5bca925d7e62..f9ac5775ee1595912723d0b83da7c043562eadce 100644 (file)
--- 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);