From: khamidou Date: Wed, 25 Feb 2009 20:18:33 +0000 (+0100) Subject: implement foreach() X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=1d78659b33b219482351aa7cefa8e85ebb608907;p=croma.git implement foreach() --- diff --git a/Makefile b/Makefile index d095ca0..798ccaf 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJS=croma.o lex.yy.o +OBJS=croma.o lex.yy.o parse.o macro.o CFLAGS=-c LDFLAGS=-o croma @@ -8,6 +8,16 @@ croma: $(OBJS) croma.o: croma.c $(CC) $(CFLAGS) croma.c +macro.o: macro.c macro.h + $(CC) $(CFLAGS) macro.c + +parse.o: parse.c parse.h + $(CC) $(CFLAGS) parse.c + lex.yy.o: lexer.l lex lexer.l - $(CC) $(CFLAGS) lex.yy.c \ No newline at end of file + $(CC) $(CFLAGS) lex.yy.c + +clean: + rm *.o + rm *~ \ No newline at end of file diff --git a/macro.c b/macro.c index 6dcd4a0..3054a2e 100644 --- a/macro.c +++ b/macro.c @@ -2,7 +2,7 @@ macro.c - helper routines for blocks. */ -#include "parse.h" +#include "macro.h" struct croma_block* alloc_and_insert_block(void) { diff --git a/macro.h b/macro.h index 7d219fc..21669a2 100644 --- a/macro.h +++ b/macro.h @@ -1,6 +1,10 @@ #ifndef __CROMA_MACRO__ #define __CROMA_MACRO__ +#include + +#include "queue.h" + /* A block is the memory representation of a {{ }} block, along with its parameters. diff --git a/parse.c b/parse.c index 755165b..c6d697d 100644 --- a/parse.c +++ b/parse.c @@ -1,13 +1,14 @@ -#include - #include "parse.h" -#include "tokens.h" -#include "macro.h" - -void abort(char *msg) +void abort(char *fmt, ...) { - fprintf(stderr, "%s\n"); + va_list ap; + va_start(ap, fmt); + + fprintf(stderr, fmt, ap); + + va_end(ap); + exit(EXIT_FAILURE); } @@ -36,6 +37,9 @@ int yyparse(void) case FOREACH: foreach_expr(); break; + + default: + abort("garbage somewhere in input"); } } @@ -51,13 +55,34 @@ void foreach_expr(void) parse_arglist(b); expect(LBRACKET); + + parse_block(b); + + char *p = b->parameters; + char *t = b->contents; + if (t == NULL) + abort("the impossible happened : b->contents == NULL !, file: %s, line %d", __FILE__, __LINE__); + + while(strcmp(p, "\0") != 0) { + while(t != '\0') { + if (*t == '$' && *t+1 == '$') { + printf("%s", p); + t++; /* skip them second '$' */ + } else { + printf("%c", *t); + } + + t++; + } + p++; + } } void parse_arglist(struct croma_block *b) { if (b == NULL) - abort("the impossible happened : b == NULL !"); + abort("the impossible happened : b == NULL !, file: %s, line %d", __FILE__, __LINE__); b->parameters = malloc(16*sizeof(char *)); int i; @@ -81,3 +106,24 @@ void parse_arglist(struct croma_block *b) expect(RPAREN); } + +void parse_block(struct croma_block *b) +{ + int i = 0; /* FIXME : realloc() b->contents on the fly*/ + + if (b == NULL) + abort("the impossible happened : b == NULL !, file: %s, line %d", __FILE__, __LINE__); + + /* Copy the whole buffer in b->contents. + FIXME: expand macros before copying contents. + */ + int t = yylex(); + + while (t != RBRACKET && i < 8192) { + i += strncat(b->contents, yytext, 8192 - i); + yylex(); + } + + return; + +} diff --git a/parse.h b/parse.h index 264485a..6216166 100644 --- a/parse.h +++ b/parse.h @@ -1,13 +1,22 @@ #ifndef __CROMA_PARSE_H__ #define __CROMA_PARSE_H__ +#include +#include +#include +#include + +#include "tokens.h" +#include "macro.h" + #define MAX_PARAM_LEN 4096 -void abort(char *msg); +void abort(char *fmt, ...); void init_parser(void); void yyparse(void); void foreach_expr(void); void parse_arglist(struct croma_block *b); +void parse_block(struct croma_block *b); #endif diff --git a/tokens.h b/tokens.h index cc7df93..bb2afc9 100644 --- a/tokens.h +++ b/tokens.h @@ -2,7 +2,7 @@ #define __TOKENS_H__ enum { - LPAREN, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, FOREACH, + LPAREN, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, COMMA, FOREACH, }; char *token_list[] = { "left parenthesis", @@ -12,8 +12,10 @@ char *token_list[] = { "left parenthesis", "word", "$$ sign", "spaces", + "comma", "foreach", }; extern int yylex(void); +extern char *yytext; #endif