]> khamidou — GitHub mirrors - croma.git/commitdiff
implement foreach()
authorkhamidou <karim@lambda-expr.org>
Wed, 25 Feb 2009 20:18:33 +0000 (21:18 +0100)
committerkhamidou <karim@lambda-expr.org>
Wed, 25 Feb 2009 20:18:33 +0000 (21:18 +0100)
Makefile
macro.c
macro.h
parse.c
parse.h
tokens.h

index d095ca01df849f708edd721771117fe5c8c412db..798ccaf31c97a0d1d9ce69adccd0e76e3ee95d51 100644 (file)
--- 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 6dcd4a008026b06ac7e6dd026a36d20f91e2bdac..3054a2e8498d9c0d72c4fb4a25ea504454c953e1 100644 (file)
--- 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 7d219fcc7eb130eb865833b4f922c7b032531a54..21669a277b4fbb484ad4ca910c5aaa23bebecfa6 100644 (file)
--- a/macro.h
+++ b/macro.h
@@ -1,6 +1,10 @@
 #ifndef __CROMA_MACRO__
 #define __CROMA_MACRO__
 
+#include <stdlib.h>
+
+#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 755165bfb4aa204fc786d1be2514726e6d035bed..c6d697d45b44d144d9772ddab605ebf32f2116c1 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -1,13 +1,14 @@
-#include <stdlib.h>
-
 #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 264485a479d885e7646fd72c75aed80b3676fa4a..6216166a884f324b35f5e079af060fad4e3088ee 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -1,13 +1,22 @@
 #ifndef __CROMA_PARSE_H__
 #define __CROMA_PARSE_H__
 
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+
+#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
index cc7df9323fc0bc1e20b9df08dc828ed33ba2871f..bb2afc9a4798eba86aba6c8a6dd12708933b0e64 100644 (file)
--- 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