changeset 540:fe37f32b552b

include_next, macro_function eat 1 char.
author kono
date Sun, 01 Jan 2006 11:14:34 +0900
parents 00da98433e63
children 4d853ee76ad6
files Changes mc-macro.c mc-parse.c test/macro.c test/tstdarg.c
diffstat 5 files changed, 72 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/Changes	Sat Dec 31 19:24:19 2005 +0900
+++ b/Changes	Sun Jan 01 11:14:34 2006 +0900
@@ -7737,4 +7737,16 @@
 arg offset のネストを「まだ」処理してなかったのか.... 引数の
 中の引数宣言は「名前をつけない」ってのが普通だったからなぁ。
 
-
+## の廻りのスペースを取る方法がわからない。なのと、; を一つ余計に食ってしまう。この手のバグは取りにくいよ。
+
+## の次の置き換えの繰り返しに対してもspaceを取らないといけないので、
+depth first には解決できないね。## で mconcat のflag を立てて、
+mappend した後、## を取り除くのが良いのではないか? (もっさいが...)
+
+Sun Jan  1 10:59:19 JST 2006
+
+inlucde_next で、重複したinclude_pathにひかかってしまって、
+同じのを開けてしまう。
+
+#include の時にchptrsave stackは? top_init でclearするのは
+変だとしても。#define \n hoge \n #include とか? 冗談だろ。
--- a/mc-macro.c	Sat Dec 31 19:24:19 2005 +0900
+++ b/mc-macro.c	Sun Jan 01 11:14:34 2006 +0900
@@ -39,7 +39,8 @@
     int i = mode;
     int macrop = 0;
     int slfree = lfree;
-    char *macropp;
+    int c;
+    char *macropp,*s,*t;
     struct cheap scheap;
     mode = STAT;
 
@@ -56,11 +57,24 @@
     mappend(reverse0(macrop),&macropp);
     cheap->ptr[-1] ='\n';
     cheap->ptr[0] =0;
+    cheap = increment_cheap(cheap,&macropp);
     while (mconcat) {
         // ## re-eval macro
-        printf("## %s",macropp);
+// if (lsrc) printf("## before %s",macropp);
         mconcat = 0;
         macrop = 0;
+	// remove \s**##\s*
+	for(s=t=macropp;*s;) {
+	    if ((c=*s++)=='#'&&*s=='#') {
+		if (t>s-3) t=s-2; else t--;
+		while(*t<=' '&&t>macropp) t--; t++;
+		for(s++;*s && *s<=' ';) s++;
+		continue;
+	    }
+	    *t++=c;
+	}
+	*t++=0;
+if (lsrc) printf("### %s",macropp);
         macrop=macro_eval(macrop,macropp,0);
 	cheap = reset_cheap(&scheap);
 	macropp = cheap->ptr;
@@ -68,6 +82,7 @@
         mappend(reverse0(macrop),&macropp);
 	cheap->ptr[-1] ='\n';
 	cheap->ptr[0] =0;
+	cheap = increment_cheap(cheap,&macropp);
     }
     cheap = reset_cheap(&scheap);
     mconcat = 0;
@@ -111,11 +126,21 @@
                         include_path / name
  */
 
+static int
+nameeq(char *p, char *q)
+{
+    if (!p)
+        return 0;
+    while(*p)
+            if(*p++ != *q++) return 0;
+    return (*q==0);
+}
+
 static FILE *
 getfname(int next)
 {
     int i,end='"',err=0;
-    char *s,*p,**pp,*name;
+    char *s,*p,**pp,*name,*prev=0;
     FILE *fp;
     struct cheap scheap;
     name = cheap->ptr;
@@ -135,15 +160,16 @@
     cheap = increment_cheap(cheap,&name);
     save_cheap(&scheap,cheap);
     fp = fopen(name,"r") ;
-    if (next && fp) { fclose(fp); fp=0; next=0; }
+    if (next && fp) { fclose(fp); fp=0; next=0; prev=name; }
     if (fp) {
 	p = name; 
     } else {
 	for(pp=include_path; *pp;pp++) {  // "" and <> case
 	    p = expand_file_name(*pp,name);
+	    if(prev && nameeq(p,prev)) continue;
 	    if ((fp = fopen(p,"r"))) {
 		if (next) {
-		    fclose(fp); fp=0; next=0; 
+		    fclose(fp); fp=0; next=0; prev=p;
 		    continue;
 		} else
 		    break ;
@@ -152,9 +178,10 @@
         if (!fp && (end=='>'||filep->inc=='>')) {  // <> case only
 	    for(pp=l_include_path; *pp;pp++) {
 		p = expand_file_name(*pp,name);
+		if(prev && nameeq(p,prev)) continue;
 		if ((fp = fopen(p,"r"))) {
 		    if (next) {
-			fclose(fp); fp=0; next=0; 
+			fclose(fp); fp=0; next=0; prev=p;
 			continue;
 		    } else
 			break ;
@@ -749,8 +776,9 @@
 	} else if (c=='\'') {
 	    in_quote = 1;
 	} else if (c=='#' && *body=='#') {
-	    // name concatenation. skip ## and re-eval macro line.
-	    mconcat = 1; body++; continue;
+	    mconcat = 1; 
+	    // name concatenation. mark only. remove and re-evaluate
+	    // in the top level. (and skip space)
 	} else if (alpha(c)) {
 	    body--; // ungetc
 	    nptrm = get_name(body,&len,NONDEF);
--- a/mc-parse.c	Sat Dec 31 19:24:19 2005 +0900
+++ b/mc-parse.c	Sun Jan 01 11:14:34 2006 +0900
@@ -242,7 +242,7 @@
 static void
 top_init()
 {
-    int i;
+//    int i;
 
     mode=TOP;
     if (gfree > heapsize-HEAPSIZE) {
@@ -254,12 +254,14 @@
     }
     set_lfree(heapsize);
     codegen_decl_init();
+#if 0
     while (chptrsave!=0) {
 	i = cadr(chptrsave); free_glist2(chptrsave); chptrsave = i;
     }
     while (chsave!=0) {
 	i = cadr(chsave); free_glist2(chsave); chsave = i;
     }
+#endif
     // a in previous extern f(a) is in current scope, release it
     leave_scope();
 }
@@ -367,6 +369,9 @@
 	lim = (char*)car(chptrsave);
 	chptrsave = cadr(chptrsave);
     }
+    if (lim<linebuf||linebuf+BUFSIZ<lim) {
+	fprintf (stderr,"\n"); return;
+    }
     for (p=linebuf; p < lim;)
 	    fprintf(stderr,(*p++ == '\t') ? "\t" : " ");
     fprintf (stderr,"^\n");
@@ -765,9 +770,9 @@
     if(sym==LC || ( sym!=SM && sym!=COMMA && sym!=ASS)) {
 	/* function body */
 	if (mode!=GDECL) error(DCERR);
+        stypedecl=sd;
 	if (type<0) error(DCERR);
-        stypedecl=sd;
-	if (car(type)==CODE) {
+	else if (car(type)==CODE) {
 	    code_decl(n); return;
 	} else if (car(type)==FUNCTION) {
 	    fdecl(n); return;
@@ -3779,16 +3784,16 @@
 get_name(char *name,int *len,int mode)
 {
     /* no name copy */
-    unsigned int ch,i = 0;
+    unsigned int c,i = 0;
     unsigned int hash0 = 0;
     char *n = name;
     struct cheap scheap;
 
     save_cheap(&scheap,cheap);
-    ch = *n++;
-    for(i=0;alpha(ch) || digit(ch);i++) {
-	hash_value(hash0,ch);
-	ch = *n++;
+    c = *n++;
+    for(i=0;alpha(c) || digit(c);i++) {
+	hash_value(hash0,c);
+	c = *n++;
     }
     if (len) *len = i;
     return hash_search(name,&scheap,i,hash0,mode);
@@ -4022,6 +4027,7 @@
     NMTBL *nlist,*nptr0,*nptrm;
     char c;
 
+retry:
     if (alpha(skipspc())) {
 	nptrm=name_space_search(nlist = get_name_from_chptr(),MACRO);
 	if (mode==MDECL) {
@@ -4039,7 +4045,7 @@
 	if ((nptrm->sc==MACRO&&neqname((char *)car(nptrm->dsp),nptrm->nm)) ||
 	    (nptrm->sc==FMACRO&&skipspc()=='(')) {
 	    macro_expansion(nptrm);
-	    return getsym(0);
+	    sc=0; goto retry;
 	}
         /* global variable name table */
 	nptr0 = name_space_search(nlist,sc);
@@ -4136,7 +4142,7 @@
 	    while(ch!='\n') { getch(); conv->comment_(ch); }
 	    in_comment = 0;
 	    getch();
-	    return getsym(0);
+	    sc = 0; goto retry;
 	}
 	if(ch!='*') return postequ(DIV,DIV+AS);
 	in_comment = 1;
@@ -4146,15 +4152,15 @@
 	} while(!(c=='*'&&ch=='/'));
 	in_comment = 0;
 	getch();
-	return getsym(0);
+	sc = 0; goto retry;
     case 0:
     case '\n':
     case '\f':
     case '\\':
-	return getsym(0);
+	sc = 0; goto retry;
     default:
 	error(CHERR);
-	return getsym(0);
+	sc = 0; goto retry;
     }
 }
 
--- a/test/macro.c	Sat Dec 31 19:24:19 2005 +0900
+++ b/test/macro.c	Sun Jan 01 11:14:34 2006 +0900
@@ -29,6 +29,8 @@
 
 #define name(a,b)   name_##b(a)
 
+#define names(a,b)   name_ ## b(a)
+
 #define name_hoge(c)   (c+a)
 
 
@@ -68,6 +70,7 @@
    printf("#0067:4\n");
 #endif
     printf("#0069:%d\n",name(3,hoge));
+    printf("#0069:%d\n",names(3,hoge));
     return 0;
 }
 
--- a/test/tstdarg.c	Sat Dec 31 19:24:19 2005 +0900
+++ b/test/tstdarg.c	Sun Jan 01 11:14:34 2006 +0900
@@ -41,7 +41,7 @@
     }
 }
 
-
+int
 main()
 {