-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
errors.o: errors.c errors.h
$(CC) $(CFLAGS) errors.c
+dynstring.o: dynstring.c dynstring.h
+ $(CC) $(CFLAGS) dynstring.c
+
clean:
rm *.o
rm *~
#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)
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;
}
}
TAILQ_REMOVE(&b->dstrings_head, s, strings);
- if (s->p != NULL)
- free(s->p);
+ if (s->contents != NULL)
+ free(s->contents);
free(s);
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);
}
}
/*
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;
+}
#include "queue.h"
#include "macro.h"
+extern struct croma_block;
+
struct dstring {
- char *p;
+ char *contents;
int length;
TAILQ_ENTRY(dstring) strings;
};
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
TAILQ_INSERT_TAIL(&blocks_head, block, blocks);
TAILQ_INIT(&block->args_head);
- TAILQ_INIT(&block->strings_head);
+ TAILQ_INIT(&block->dstrings_head);
return block;
}
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)
#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;
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;
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);
void init_parser(void)
{
TAILQ_INIT(&blocks_head);
- init_dynstring();
}
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);
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.
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;
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; */
+/* } */
}
char *buf = malloc(8192);
struct croma_block *b;
+ char *name;
int i = 0;
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;
+
}