From: khamidou Date: Tue, 3 Mar 2009 09:04:11 +0000 (+0100) Subject: implementing macro expansion... X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=05783c3b7ee4f6ae1fb2178caca627f45698eb2e;p=croma.git implementing macro expansion... --- diff --git a/Makefile b/Makefile index 28cccef..c2b5998 100644 --- 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 46aeacf..3cc964e 100644 --- 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; } +<> { return -1; } diff --git a/macro.c b/macro.c index 9786d46..aad891b 100644 --- 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 1a1a827..6d07919 100644 --- 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 89a3835..fee6fcb 100644 --- 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"); + } + + +} diff --git a/tokens.h b/tokens.h index 7cf95f4..eccbe79 100644 --- 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, };