]> khamidou — GitHub mirrors - croma.git/commitdiff
refactoring...
authorkhamidou <karim@lambda-expr.org>
Mon, 9 Mar 2009 20:14:17 +0000 (21:14 +0100)
committerkhamidou <karim@lambda-expr.org>
Mon, 9 Mar 2009 20:14:17 +0000 (21:14 +0100)
Makefile
dynstring.c
dynstring.h
macro.c
macro.h
parse.c

index 1cbf453a4eada5acf4b48244c55be3835c30451b..2f6d69133afa6330f83c0312e30d04af2eb3e2c6 100644 (file)
--- 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 *~
index 2b330f2feb1fbd02f3c55f4e00342b0902af8f29..c9a636480deb0dfeafc86f9b3d3854c1056d62b8 100644 (file)
@@ -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;
+}
index 39050dd778d360878327c654803c1c48d6f7f55c..845a6b1acae79b177acec7281fb755d9992431ef 100644 (file)
@@ -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 475c1df0e24b6674d3d65d46548e2803b53f0ec2..bcafbae7e1f9764156c9b0b0c1ecfea050e0ebaf 100644 (file)
--- 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 820c310d4df5fcc800aa904a1beaac25dc7e77a4..a3caca4991dee20d34209d7b83441f93ca4ff345 100644 (file)
--- a/macro.h
+++ b/macro.h
@@ -2,17 +2,11 @@
 #define __CROMA_MACRO__
 
 #include <stdlib.h>
+#include <string.h>
 #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 afd8ef953d78d9bae09951d4c20cbba46ae1106d..0f1d45a5c53bb8df25ec86865b3d3a346d25c0c8 100644 (file)
--- 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;
+
 
        }