]> khamidou — GitHub mirrors - croma.git/commitdiff
implementing macro expansion...
authorkhamidou <karim@lambda-expr.org>
Tue, 3 Mar 2009 09:04:11 +0000 (10:04 +0100)
committerkhamidou <karim@lambda-expr.org>
Tue, 3 Mar 2009 09:04:11 +0000 (10:04 +0100)
Makefile
lexer.l
macro.c
macro.h
parse.c
tokens.h

index 28cccefd9f79a43ccf42cd1539903e7a9f11a161..c2b59981fe8f6b4232ecfa52c512685888c5cfd9 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -24,4 +24,8 @@ errors.o: errors.c errors.h
 clean:
        rm *.o
        rm *~
-       rm croma
\ No newline at end of file
+       rm croma
+
+tests: croma
+       ./croma tests/simple
+       ./croma tests/test.c
\ No newline at end of file
diff --git a/lexer.l b/lexer.l
index 46aeacffb3fecd9acce71b2dc84d60fa6b2a09d9..3cc964ea8b63da4a8b59ca28d7a10d45a4b534f9 100644 (file)
--- a/lexer.l
+++ b/lexer.l
@@ -21,12 +21,11 @@ int yywrap()
 ")"            { return RPAREN;        }
 "{{"           { return LBRACKET;      }
 "}}"           { return RBRACKET;      }
-","             { return COMMA;                }
 "'"|"\\"               { return QUOTE;         }
 "foreach"       { return FOREACH;      }
 "define"       { return DEFINE;        }
 "$$"           { return DOLDOL;        }
 [ \t\n]+       { return SPACES;        }
-[a-zA-Z0-9,?!%*:;$^]+  { return WORD;          }
-
+[a-zA-Z0-9,?!%*:,;#$^]+        { return WORD;          }
+<<EOF>>                { return -1;    }
 
diff --git a/macro.c b/macro.c
index 9786d46bab814cd41b80386e69215c84e6041af6..aad891b17409d560e0abd3b5a398b0f63650a8db 100644 (file)
--- a/macro.c
+++ b/macro.c
@@ -50,3 +50,30 @@ void free_arg(struct croma_block *b, struct croma_arg *a)
        free(a->value);
        free(a);
 }
+
+struct croma_block *lookup_symbol(char *name)
+{
+       struct croma_block *b;
+       TAILQ_FOREACH(b, &blocks_head, blocks) {
+               if (strcmp(name, b->name) == 0)
+                       return b;
+       }
+
+       return NULL;
+}
+
+struct croma_arg *lookup_arg(struct croma_block *b, char *name)
+{
+       if (b == NULL)
+               return NULL;
+       
+       struct croma_arg *a;
+
+       TAILQ_FOREACH(a, &b->args_head, params) {
+               if (strcmp(name, a->name) == 0)
+                       return a;
+       }
+
+       return NULL;
+       
+}
diff --git a/macro.h b/macro.h
index 1a1a8270e3c902b8fa0493799a62ba4e711bef8f..6d07919d8245eb913973bbbc9fcf0b71a274d892 100644 (file)
--- a/macro.h
+++ b/macro.h
@@ -43,5 +43,7 @@ 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);
 void free_arg(struct croma_block *b, struct croma_arg *a);
+struct croma_block *lookup_symbol(char *name);
+struct croma_arg *lookup_arg(struct croma_block *b, char *name);
 
 #endif
diff --git a/parse.c b/parse.c
index 89a38359457da9c14fda4366086671ac2c11a8e7..fee6fcb19993c4eac486a6f391f8e0fb3064371b 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -45,8 +45,9 @@ void init_parser(void)
 
 void yyparse(void)
 {
-       while(!feof(yyin)) {
-               int t = yylex();
+       int t;
+       while((t = yylex()) != -1) {
+
                switch(t) {
                        
                case FOREACH:
@@ -59,6 +60,8 @@ void yyparse(void)
 
                default:
                        printf("%s", yytext);
+                       continue;
+                       break;
                }
        }
 }
@@ -67,6 +70,7 @@ void yyparse(void)
 void foreach_expr(void)
 {
        struct croma_block *b = alloc_and_insert_block();
+       b->name = strdup("Another anonymous foreach block");
 
        TAILQ_INIT(&b->args_head);
        expect(LPAREN);
@@ -181,3 +185,100 @@ void parse_block(struct croma_block *b)
        return;
        
 }
+
+/*
+  This routine extracts a word from a string, without modifying it.
+ */
+char *extract_word(char *s)
+{
+       while(*s == ' ' || *s == '\t' || *s == '\n')
+               s++;
+
+       char *wend = s;
+
+       while(*wend != ' ' || *wend != '\t' || wend != '\n')
+               wend++;
+
+       char *r = malloc((wend - s) * sizeof(char));
+
+       wend = s;
+
+       /* Copy the contents of the word in r. */
+       while(*wend != ' ' || *wend != '\t' || wend != '\n')
+               *r++ = *wend++;
+
+       return r;
+}
+
+/*
+  Replace arguments replaces all the references to the arguments
+  of b in t by their values.
+ */
+
+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 *text)
+{
+       if (text == NULL)
+               return NULL;
+
+       char *buf = malloc(8192);
+       char *s = strdup(text);
+       char *p = strtok(s," \t\n");
+
+       struct croma_block *b;
+
+       int i = 0;
+
+       if (b == NULL)
+               fail("Unable to allocate memory");
+
+       
+       while(p != NULL) {
+               b = lookup_symbol(p);
+               /* FIXME : implement argument list parsing */
+               p = strtok(NULL, " \t\n");
+       }
+           
+       
+}
index 7cf95f4881442b2a4dd100c109e277f93db1193e..eccbe7972257f487afe907af7d6e6069f1433266 100644 (file)
--- a/tokens.h
+++ b/tokens.h
@@ -2,8 +2,8 @@
 #define __TOKENS_H__
 
 enum {
-       LPAREN = 0, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, COMMA, QUOTE,
-       FOREACH, DEFINE,
+       LPAREN = 0, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, 
+       QUOTE, FOREACH, DEFINE,
 };