changeset 69:dba8d111b7a0

c output
author kono
date Mon, 24 Feb 2003 10:13:29 +0900
parents 0266905063b5
children 2e84590720a6
files Changes conv/c.c mc-parse.c mc-tree.c
diffstat 4 files changed, 133 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/Changes	Mon Feb 24 02:15:15 2003 +0900
+++ b/Changes	Mon Feb 24 10:13:29 2003 +0900
@@ -1724,3 +1724,9 @@
 
 このタイプの印刷だと再帰型は印刷が終了しないんじゃないか?
 しないね。
+
+Mon Feb 24 02:31:06 JST 2003
+
+strings の\nなどを元に戻す必要がある。
+
+なんか括弧がわやになってるな。
--- a/conv/c.c	Mon Feb 24 02:15:15 2003 +0900
+++ b/conv/c.c	Mon Feb 24 10:13:29 2003 +0900
@@ -7,7 +7,8 @@
 extern void type_print(int type,NMTBL *nptr,FILE *out);
 extern void sym_print(int type,FILE *out);
 
-static FILE *vout;
+static FILE *vout,*svout;
+static FILE *null;
 
 static void
 comment_(int c)
@@ -26,11 +27,29 @@
 	    break;
 	}
     }
+#if 1
     vout = fopen(p,"w");
+#else
+    vout = stderr;p=p;
+#endif
+    null=fopen("/dev/null","w");
     if (!vout) error(-1);
 }
 
 static void
+conv_()
+{
+    vout=svout;
+}
+
+static void
+noconv_(int c)
+{
+    svout=vout;
+    vout=c?null:vout;
+}
+
+static void
 print_(char *s)
 {
     fprintf(vout,"c: %s\n",s);
@@ -52,7 +71,7 @@
     if (def)
 	fprintf(vout,"default:");
     else
-	fprintf(vout,":");
+	fprintf(vout,"%d:",car(cases));
 }
 
 void
@@ -84,12 +103,12 @@
 
 void
 cond1_(){
-
+    fprintf(vout,"?");
 }
 
 void
 cond2_(){
-
+    fprintf(vout,":");
 }
 
 void
@@ -152,16 +171,11 @@
 }
 
 void
-for3_(){
+for_body_(){
     fprintf(vout,")");
 }
 
 void
-for_body_(){
-
-}
-
-void
 for_end_(){
 
 }
@@ -206,12 +220,21 @@
 
 void
 id_(int sym,NMTBL *nptr){
-    fprintf(vout," %s ",nptr->nm);
+    fprintf(vout,"%s",nptr->nm);
 }
 
 void
 string_(char *s){
-    fprintf(vout,"\"%s\"",s);
+    int c;
+    fprintf(vout,"\"");
+    while((c=*s++)) {
+	if(c=='\n') fprintf(vout,"\\n");
+	else if(c=='\r') fprintf(vout,"\\r");
+	else if(c=='\e') fprintf(vout,"\\e");
+	else if(!(' '<=c&&c<=0x7f)) fprintf(vout,"\\%03o",c);
+	else fprintf(vout,"%c",c);
+    }
+    fprintf(vout,"\"");
 }
 
 void
@@ -241,17 +264,16 @@
 
 void
 if_else_(){
-    fprintf(vout,"} else {");
+    fprintf(vout," else ");
 }
 
 void
 if_endif_(){
-    fprintf(vout,"}");
 }
 
 void
 if_then_(){
-    fprintf(vout,") {");
+    fprintf(vout,")");
 }
 
 void
@@ -306,12 +328,12 @@
 
 void
 postfix_(int sym){
-    /* sym_print(sym,vout); */
+    sym_print(sym,vout);
 }
 
 void
 prefix_(int sym){
-    /* sym_print(sym,vout); */
+    sym_print(sym,vout);
 }
 
 void
@@ -321,12 +343,11 @@
 
 void
 return_(){
-    fprintf(vout,"return (");
+    fprintf(vout,"return ");
 }
 
 void
 return_end_(){
-    fprintf(vout,")");
 }
 
 
@@ -382,12 +403,29 @@
 
 void
 while_body_(){
-    fprintf(vout,") {");
+    fprintf(vout,")");
 }
 
 void
 while_end_(){
-    fprintf(vout,"}");
+}
+
+void
+decl_data_()
+{
+    fprintf(vout," = ");
+}
+
+void
+break_()
+{
+    fprintf(vout,"break;");
+}
+
+void
+continue_()
+{
+    fprintf(vout,"continue;");
 }
 
 /* end */
--- a/mc-parse.c	Mon Feb 24 02:15:15 2003 +0900
+++ b/mc-parse.c	Mon Feb 24 10:13:29 2003 +0900
@@ -17,7 +17,7 @@
 static int cexpr(int e);
 static int decl_data(int t, NMTBL *n,int offset);
 static int digit(char c);
-static int expr(void);
+static int expr(int);
 static int expr0(void);
 static int expr1(void);
 static int expr10(void);
@@ -358,33 +358,35 @@
     int t;
 
     if(sym==STATIC) {
-	conv->static_();
 	if(mode==LDECL) {	
 		getsym();
+		conv->static_();
 		mode=STADECL;
 		stmode=LDECL;
 	} else if(mode==GDECL) {	
 		getsym();
+		conv->static_();
 		stmode=STATIC;
 	} else 
 	    error(DCERR);
     } else if(sym==REGISTER) {
 	if(mode!=LDECL)  
 	    error(DCERR);
-	conv->register_();
 	stmode=REGISTER;
 	getsym();
+	conv->register_();
     } else if(sym==EXTRN) {
+	getsym();
 	conv->extern_();
-	getsym();
 	stmode=EXTRN;
     } else if(sym==TYPEDEF) {
-	conv->typedef_();
 	if(mode==GDECL) {	
 		getsym();
+		conv->typedef_();
 		mode=GTDECL;
 	} else if(mode==LDECL) {	
 		getsym();
+		conv->typedef_();
 		mode=LTDECL;
 	} else 
 		error(DCERR);
@@ -408,8 +410,8 @@
 	    fdecl(n); return;
 	} else error(TYERR);
     }
+    conv->return_type_(type,n,0);
     def(n);
-    conv->return_type_(type,n,0);
     while(sym==COMMA) {	
 	conv->comma_();
 	getsym();
@@ -418,8 +420,8 @@
 	reverse(t);
 	if(n == &null_nptr) error(DCERR); 
 	/* if(args) error(DCERR); */
+	conv->return_type_(type,n,1);
 	def(n);
-	conv->return_type_(type,n,1);
     }
     if(sym!=SM) error(DCERR);
     conv->sm_();
@@ -531,7 +533,7 @@
 		}
 	    } else {	
 		t=type;
-		i=cexpr(expr());
+		i=cexpr(expr(1));
 		checksym(RBRA);
 		type=list3(ARRAY,t,i);
 	    }
@@ -863,6 +865,7 @@
 {
     int t1,e,i,mode_save;
 
+    conv->decl_data_();
     mode_save = mode;
     mode=STAT;
     getsym();
@@ -1210,11 +1213,13 @@
 	docomp();
 	return;
     case BREAK:
+	conv->break_();
 	jmp(blabel);
 	getsym();
 	checksym(SM);
 	return;
     case CONTINUE:
+	conv->continue_();
 	jmp(clabel);
 	getsym();
 	checksym(SM);
@@ -1239,8 +1244,9 @@
 	    statement();
 	} else {	
 	    slfree=lfree;
-	    gexpr(expr());
+	    gexpr(expr(0));
 	    lfree=slfree;
+	    conv->sm_();
 	    checksym(SM);
 	}
     }
@@ -1254,7 +1260,7 @@
     checksym(LPAR);
     conv->if_();
     slfree=lfree;
-    bexpr(expr(),0,l1=fwdlabel());
+    bexpr(expr(0),0,l1=fwdlabel());
     lfree=slfree;
     checksym(RPAR);
     conv->if_then_();
@@ -1287,7 +1293,7 @@
     getsym();
     checksym(LPAR);
     slfree=lfree;
-    e=expr();
+    e=expr(0);
     checksym(RPAR);
     conv->while_body_();
     if(sym==SM) {	
@@ -1327,7 +1333,7 @@
     checksym(LPAR);
     slfree=lfree;
     conv->dowhile_cond_();
-    bexpr(expr(),1,l);
+    bexpr(expr(0),1,l);
     lfree=slfree;
     checksym(RPAR);
     checksym(SM);
@@ -1350,30 +1356,29 @@
     checksym(LPAR);
     slfree=lfree;
     if(sym!=SM) {	
-	gexpr(expr());
+	gexpr(expr(0));
 	checksym(SM);
+	conv->for1_();
     }
     else getsym();
-    conv->for1_();
     lfree=slfree;
     l=backdef();
     if(sym!=SM) {	
-	bexpr(expr(),0,blabel);
+	bexpr(expr(0),0,blabel);
 	checksym(SM);
+	conv->for2_();
     }
     else getsym();
-    conv->for2_();
     lfree=slfree;
     if(sym==RPAR) {	
 	clabel=l;
+	conv->for_body_();
 	getsym();
-	conv->for_body_();
 	statement();
 	checkret();
     } else {	
-	conv->for3_();
 	clabel=fwdlabel();
-	e=expr();
+	e=expr(0);
 	conv->for_body_();
 	checksym(RPAR);
 	statement();
@@ -1404,7 +1409,7 @@
     checksym(LPAR);
     slfree=lfree;
     svalue=csvalue1;      /* save parents switch value */
-    gexpr(expr());
+    gexpr(expr(0));
     csvalue1=csvalue() ;
     lfree=slfree;
     checksym(RPAR);
@@ -1443,7 +1448,7 @@
     while(sym==CASE) {	
 	conv->case_begin_(c,0);
 	getsym();
-	c=list2(cexpr(expr()),c);
+	c=list2(cexpr(expr(1)),c);
 	conv->case_(c,0);
 	checksym(COLON);
     }
@@ -1488,7 +1493,7 @@
     conv->return_();
     slfree=lfree;
     if (struct_return) {
-	e = expr();
+	e = expr(0);
 	if ((car(type)==STRUCT || car(type)==UNION)&&
 		size(type)==cadr(struct_return)) {
 	    if(car(e)==RSTRUCT && car(cadr(e))==FUNCTION) {
@@ -1505,13 +1510,13 @@
 	    error(TYERR); /* should check compatible */
 	}
     } else {
-	gexpr(expr());
+	gexpr(expr(0));
     }
     lfree=slfree;
+    conv->return_end_();
     checksym(SM);
     /* control = 0; still control continue until pending return emittion */
     retpending = 1;
-    conv->return_end_();
 }
 
 void
@@ -1530,7 +1535,7 @@
 
     conv->goto_();
     getsym();
-    e1 = expr();
+    e1 = expr(0);
     t=car(e1);
     if (t==FNAME) {
 	nptr0 = (NMTBL *)cadr(e1);
@@ -1614,9 +1619,10 @@
 }
 
 int
-expr(void)
+expr(int noconv)
 {
     int r;
+    conv->noconv_(noconv);
     r=rvalue(expr0());
     return r;
 }
@@ -1639,9 +1645,9 @@
 {
     int e1,e2,t,op;
     e1=expr2();
-    conv->op_(sym);
     switch (sym) {
     case ASS:
+	conv->op_(sym);
 	lcheck(e1);
 	t=type;
 	getsym();
@@ -1649,6 +1655,7 @@
 	return assign_expr(e1,e2,t,type);
     case ADD+AS: case SUB+AS: case MUL+AS: case DIV+AS: case MOD+AS:
     case RSHIFT+AS: case LSHIFT+AS: case BAND+AS: case EOR+AS: case BOR+AS:
+	conv->op_(sym);
 	op = sym-AS;
 	lcheck(e1);
 	t=type;
@@ -1688,8 +1695,8 @@
 	conv->cond1_();
 	e2=rvalue(expr2());
 	t=type;
+	conv->cond2_();
 	checksym(COLON);
-	conv->cond2_();
 	e3=rvalue(expr2());
 	conv->cond_end_();
 	if(car(e1)==CONST) {
@@ -1881,9 +1888,9 @@
 {
     int e,op;
 
-    conv->prefix_(sym);
     switch (op = sym) {
     case INC: case DEC:
+	conv->prefix_(sym);
 	getsym();
 	lcheck(e=expr13());
 	if(type==CHAR) {	
@@ -1897,10 +1904,12 @@
 	return(list3(PREINC,e,
 	    op==INC?size(cadr(type)):-size(cadr(type)) ));
     case MUL:
+	conv->prefix_(sym);
 	getsym();
 	e=rvalue(expr13());
 	return(indop(e));
     case BAND:
+	conv->prefix_(sym);
 	getsym();
 	switch(car(e=expr13())) {
 	case INDIRECT:
@@ -1917,24 +1926,28 @@
 	type=list2(POINTER,type);
 	return e;
     case SUB:
+	conv->prefix_(sym);
 	getsym();
 	e=rvalue(expr13());
 	if(!integral(type)) 
 	    error(TYERR);
 	return(car(e)==CONST?list2(CONST,-cadr(e)):list2(MINUS,e));
     case BNOT:
+	conv->prefix_(sym);
 	getsym();
 	e=rvalue(expr13());
 	if(!integral(type)) 
 	    error(TYERR);
 	return(car(e)==CONST?list2(CONST,~cadr(e)):list2(BNOT,e));
     case LNOT:
+	conv->prefix_(sym);
 	getsym();
 	e=rvalue(expr13());
 	if(!scalar(type)) 
 	    error(TYERR);
 	return(car(e)==CONST?list2(CONST,!cadr(e)):list2(LNOT,e));
     case SIZEOF:
+	conv->prefix_(sym);
 	if(getsym()==LPAR) {
 	    if(typeid(getsym())) {	
 		e=list2(CONST,size(typename()));
@@ -1959,8 +1972,8 @@
 	return e;
     }
     e=expr14();
-    conv->postfix_(sym);
     if((op=sym)==INC||op==DEC) {	
+	conv->postfix_(sym);
 	lcheck(e);
 	getsym();
 	if(type==CHAR) {	
@@ -2097,8 +2110,8 @@
 	    e1=indop(e1);
 	} else if(sym==LPAR) e1=expr15(e1);
 	else {
-	    if(sym==PERIOD) { e1=strop(e1);conv->op_(sym);
-	    } else if(sym==ARROW) { e1=strop(indop(rvalue(e1)));conv->op_(sym);
+	    if(sym==PERIOD) { conv->op_(sym);e1=strop(e1);
+	    } else if(sym==ARROW) { conv->op_(sym);e1=strop(indop(rvalue(e1)));
 	    } else break;
 	}
     }
@@ -2185,6 +2198,7 @@
 {
     getsym();
     if (sym!=IDENT||nptr->sc!=FIELD) error(TYERR);
+    conv->id_(sym,nptr);
     if (integral(type)||(car(type)!=STRUCT && car(type)!=UNION))
 	e=rvalue(e);
     type = nptr->ty;
@@ -2358,7 +2372,7 @@
 	e=rvalue(expr1());
 	arglist=list3(e,arglist,type);
 	if(sym!=COMMA) break;
-	conv->comma();
+	conv->comma_();
 	getsym();
     }
     checksym(RPAR);
@@ -2441,7 +2455,7 @@
 	if(sym==LBRA) {	
 	    getsym();
 	    t=type;
-	    i=cexpr(expr());
+	    i=cexpr(expr(1));
 	    checksym(RBRA);
 	    type=list3(ARRAY,t,i);
 	} else if(sym==LPAR) {	
@@ -2464,6 +2478,7 @@
 static int
 cexpr(int e)
 {       
+    conv->conv_();
     if (car(e) != CONST) error(CNERR);
     return (cadr(e));
 }
@@ -2825,13 +2840,20 @@
     symval++;
 }
 
+static int topspc = 0;
+
 static int
 skipspc(void)
 {
+    /* static int topspc = 0; */
+
     while(ch=='\t'||ch=='\n'||ch==' '||ch=='\r') {
-	conv->comment_(ch);
+	if (ch=='\n'||ch=='\r') topspc=1;
+	if (topspc)
+	    conv->comment_(ch);
 	getch();
     }
+    topspc=0;
     return ch;
 }
 
@@ -2965,7 +2987,7 @@
             chptr[c] = ';';  /* this can't happen in macro expression */
 	    ch= *chptr;
 	    getsym();
-	    i=cexpr(expr());
+	    i=cexpr(expr(1));
 	    macro_if_depth = macro_if_current;
 	    macro_if_skip = !i;
 	}
--- a/mc-tree.c	Mon Feb 24 02:15:15 2003 +0900
+++ b/mc-tree.c	Mon Feb 24 10:13:29 2003 +0900
@@ -103,14 +103,14 @@
     {33,">>","ee"},
     {34,"<<","ee"},
     {35,"<<","ee"},
-    {36,"gt","ee"},
-    {37,"ugt","ee"},
-    {38,"ge","ee"},
-    {39,"uge","ee"},
-    {40,"lt","ee"},
-    {41,"ult","ee"},
-    {42,"le","ee"},
-    {43,"ule","ee"},
+    {36,">","ee"},
+    {37,">","ee"},
+    {38,">=","ee"},
+    {39,">=","ee"},
+    {40,"<","ee"},
+    {41,"<","ee"},
+    {42,"<=","ee"},
+    {43,"<=","ee"},
     {44,"==","ee"},
     {45,"!=","ee"},
     {46,"&","ee"},
@@ -119,8 +119,8 @@
     {49,"&&","ee"},
     {50,"||","ee"},
     {51,"cond","eee"},
-    {52,"=","ee"},
-    {53,"=","ee"},
+    {52," = ","ee"},
+    {53," = ","ee"},
     {54,"assop","eev"},
     {55,"cassop","eev"},
     {56,",","ee"},