]> khamidou — GitHub mirrors - croma.git/commitdiff
adding dynamic strings package
authorkhamidou <karim@lambda-expr.org>
Wed, 4 Mar 2009 18:54:17 +0000 (19:54 +0100)
committerkhamidou <karim@lambda-expr.org>
Wed, 4 Mar 2009 18:54:17 +0000 (19:54 +0100)
Makefile
dynstring.c [new file with mode: 0644]
dynstring.h [new file with mode: 0644]
mlexer.l [new file with mode: 0644]
parse.c
parse.h
tokens.h

index c2b59981fe8f6b4232ecfa52c512685888c5cfd9..1cbf453a4eada5acf4b48244c55be3835c30451b 100644 (file)
--- 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 (file)
index 0000000..b434157
--- /dev/null
@@ -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 (file)
index 0000000..0e7f890
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef __DYNSTRING_H__
+#define __DYNSTRING_H__
+
+#include <stdlib.h>
+#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 (file)
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;         }
+<<EOF>>                                { return -1;    }
+
+%%
\ No newline at end of file
diff --git a/parse.c b/parse.c
index 47377099f327a4cf9539126b346916690eaf0456..8a56aeae778fac8f752979b34b2f6e8c8c9853c2 100644 (file)
--- 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 df32be737c9501bfb159547817c541a733642862..635fae5bdd2cb17ba311ba43b14f5bca925d7e62 100644 (file)
--- 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);
 
index eccbe7972257f487afe907af7d6e6069f1433266..7a8dd715e5e8ea5392dfdce0123062fee7b10876 100644 (file)
--- 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,
 };