From: khamidou Date: Wed, 4 Mar 2009 18:54:17 +0000 (+0100) Subject: adding dynamic strings package X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=61ac7c43a8b83ab55ffd4e5d13889afb8ae4c254;p=croma.git adding dynamic strings package --- diff --git a/Makefile b/Makefile index c2b5998..1cbf453 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJS=croma.o lex.yy.o parse.o macro.o errors.o +OBJS=croma.o lex.yy.o lex.mlex.o parse.o macro.o errors.o CFLAGS=-c -g LDFLAGS=-o croma @@ -18,6 +18,10 @@ lex.yy.o: lexer.l lex lexer.l $(CC) $(CFLAGS) lex.yy.c +lex.mlex.o: mlexer.l + lex --prefix=mlex mlexer.l + $(CC) $(CFLAGS) lex.mlex.c + errors.o: errors.c errors.h $(CC) $(CFLAGS) errors.c diff --git a/dynstring.c b/dynstring.c new file mode 100644 index 0000000..b434157 --- /dev/null +++ b/dynstring.c @@ -0,0 +1,38 @@ +#include "dynstring.h" + +void init_dynstring(void) +{ + TAILQ_INIT(&dstrings_head); +} + +struct dstring* alloc_and_insert_string(size_t len) +{ + struct dstring *s = calloc(len, sizeof(char)); + + if (s == NULL) + fail("Unable to alloc() memory"); + + TAILQ_INSERT_TAIL(&dstrings_head, s, strings); + + return block; +} + +struct dstring* alloc_after(struct dstring *before) +{ + if (before == NULL) + return NULL; + + struct dstring *ptr; + + TAILQ_FOREACH(ptr, &dstrings_head, strings) { + if (strncmp(ptr->p, before->p, 32) == 0) + { + struct dstring *s = calloc(len, sizeof(char)); + + if (s == NULL) + fail("Unable to alloc() memory"); + + TAILQ_INSERT_AFTER(dstrings_head, dstring, dstring, p) + } + } +} diff --git a/dynstring.h b/dynstring.h new file mode 100644 index 0000000..0e7f890 --- /dev/null +++ b/dynstring.h @@ -0,0 +1,15 @@ +#ifndef __DYNSTRING_H__ +#define __DYNSTRING_H__ + +#include +#include "queue.h" + +struct dstring { + char *p; + int length; + TAILQ_ENTRY(dstring) strings; +}; + +TAILQ_HEAD(dstring_heads, dstring) dstrings_head; + +#endif diff --git a/mlexer.l b/mlexer.l new file mode 100644 index 0000000..e4c1e2f --- /dev/null +++ b/mlexer.l @@ -0,0 +1,40 @@ +%{ +/* + mlexer.l - a lexer used to parse the strings in memory. +*/ + +#undef YY_INPUT + +#include "tokens.h" + +char *mlexer_ptr; + +int mlexwrap() +{ + return(1); +} + +void init_mlexer(char *ptr) +{ + mlexer_ptr = ptr; +} + +#define YY_INPUT(buf,result,max_size) \ +{ \ +int c = *mlexer_ptr++; \ +if (c == '\0')\ + result = YY_NULL;\ +else{\ + result = 1;\ + buf[0] = c;\ + }\ +} +%} + +%% + +[:word:]+ { return WORD; } +[:print:]+ { return OTHER; } +<> { return -1; } + +%% \ No newline at end of file diff --git a/parse.c b/parse.c index 4737709..8a56aea 100644 --- a/parse.c +++ b/parse.c @@ -6,6 +6,9 @@ extern char *yytext; extern int yylineno; extern FILE *yyin; +extern int mlexlex(); +extern char *mlextext; + char *token_list[] = { "left parenthesis", "right parenthesis", "{{", @@ -176,6 +179,12 @@ 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) { @@ -207,43 +216,10 @@ void parse_block(struct croma_block *b) } -/* - 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 !='(' && *wend != ')') - wend++; - - wend--; /* Get rid of the last (unwanted) character */ - - if (wend == s) - return NULL; - - int len = (wend - s); - char *r = calloc(len + 1, sizeof(char)); - char *p = r; - - wend = s; - - int i; - /* Copy the contents of the word in r. */ - for(i = 0; i <= len; i++) - *p++ = *wend++; - - return r; -} - /* 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) @@ -294,30 +270,27 @@ char *expand_macro(char *s) return NULL; char *buf = malloc(8192); - char *ss = s; /* Used only to compute the length of the buffer */ - int buflen = s - ss; - struct croma_block *b; - int i = 0; if (b == NULL) fail("Unable to allocate memory"); - - while(*s != '\0' && buflen < 8192) { + int c = mlexlex(); - while(*s == ' ' || *s == '\t' || *s == '\n') - *buf++ = *s++; + while(c != -1 && i < 8192) { + + switch(c) { - char *name = extract_word(s); + case WORD: + char *name = extract_word(mlextext); b = lookup_symbol(name); if (b != NULL) { } - /* FIXME : implement argument list parsing */ - buflen = s - ss; + break; + } diff --git a/parse.h b/parse.h index df32be7..635fae5 100644 --- a/parse.h +++ b/parse.h @@ -16,6 +16,7 @@ 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); diff --git a/tokens.h b/tokens.h index eccbe79..7a8dd71 100644 --- a/tokens.h +++ b/tokens.h @@ -3,7 +3,7 @@ enum { LPAREN = 0, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, - QUOTE, FOREACH, DEFINE, + QUOTE, FOREACH, DEFINE, OTHER, };