Mercurial > hg > CbC > old > device
changeset 180:f00e3bfa1b45
nkf.c compile (not correct)
author | kono |
---|---|
date | Sun, 30 Nov 2003 18:26:36 +0900 |
parents | fde8cb5d9254 |
children | 4beb7f079055 |
files | Changes mc-parse.c test/macro.c test/tmp4.c |
diffstat | 4 files changed, 241 insertions(+), 263 deletions(-) [+] |
line wrap: on
line diff
--- a/Changes Sun Nov 30 06:41:38 2003 +0900 +++ b/Changes Sun Nov 30 18:26:36 2003 +0900 @@ -3621,3 +3621,12 @@ stat の関数名とtag名が重なっているみたい。やっぱり tag名の名前空間をわけないとだめかぁ... + +Sun Nov 30 17:41:04 JST 2003 + +マクロを登録するときには、コメントをとらないとだめ。 + +while のslfree でマクロのchptrsave が破壊されてしまう。 + +やっぱり、nkf.c は、一発では動かないみたいね。compile は +できたけどさ。
--- a/mc-parse.c Sun Nov 30 06:41:38 2003 +0900 +++ b/mc-parse.c Sun Nov 30 18:26:36 2003 +0900 @@ -5,7 +5,8 @@ #include "mc.h" #include "mc-codegen.h" -static NMTBL *decl0(void),*decl1(void),*lsearch(char *name),*gsearch(void); +static NMTBL *decl0(void),*decl1(void),*lsearch(char *name,int sc); +static NMTBL *gsearch(int sc); static NMTBL *def(NMTBL *n); static NMTBL *free_nptr(); static NMTBL *msearch(char *name); @@ -37,7 +38,7 @@ static int expr9(void); static int getch(void); static int getfree(int n); -static int getsym(void); +static int getsym(int); static int indop(int e); static int macro_args(char **pcheapp,char* max,char **pchptr); static int macro_eval(int macrop,char *body,int history); @@ -90,9 +91,6 @@ static void set_converter(char *s); static void statement(void); static int correct_type(int e,int t); -static NMTBL *get_tag(NMTBL *n) ; -static NMTBL *get_type(NMTBL *n) ; -static NMTBL * get_non_tag(NMTBL *n) ; static int struct_return = 0; @@ -162,7 +160,7 @@ mode=TOP; lfree= HEAPSIZE; chptrsave = chsave = 0; - while(getsym()==SM) conv->sm_(); + while(getsym(0)==SM) conv->sm_(); mode=GDECL; stmode=0; args=0; @@ -250,7 +248,7 @@ fprintf(stderr,"%d:%s expected.\n",lineno,p); errmsg(); } else - getsym(); + getsym(0); } static void @@ -381,7 +379,7 @@ } if (cheapp+i >= cheap+CHEAPSIZE) error(STRERR); name[i++] = 0; - (nptr = gsearch())->sc = RESERVE; + (nptr = gsearch(0))->sc = RESERVE; if (d==0) { nptr->sc = MACRO; nptr->dsp = (int)""; nptr->ty=0; @@ -391,19 +389,19 @@ } static void -strage_class() +storage_class() { if(sym==VOLATILE) { - getsym(); + getsym(0); } if(sym==STATIC) { if(mode==LDECL) { - getsym(); + getsym(0); conv->static_(); mode=STADECL; stmode=LDECL; } else if(mode==GDECL) { - getsym(); + getsym(0); conv->static_(); stmode=STATIC; } else @@ -412,19 +410,19 @@ if(mode!=LDECL) error(DCERR); stmode=REGISTER; - getsym(); + getsym(0); conv->register_(); } else if(sym==EXTRN) { - getsym(); + getsym(0); conv->extern_(); stmode=EXTRN; } else if(sym==TYPEDEF) { if(mode==GDECL) { - getsym(); + getsym(0); conv->typedef_(); mode=GTDECL; } else if(mode==LDECL) { - getsym(); + getsym(0); conv->typedef_(); mode=LTDECL; } else @@ -438,7 +436,7 @@ NMTBL *n; int t,sd; if (mode==GDECL) { typedefed=0; } - strage_class(); + storage_class(); if((t=typespec())==0) return; if(sym==SM) { conv->return_type_(t,0,stypedecl); @@ -463,7 +461,7 @@ def(n); while(sym==COMMA) { conv->comma_(); - getsym(); + getsym(0); type=t; n=decl0(); reverse(t); @@ -487,10 +485,10 @@ stypedecl = 0; while (sym==KONST) { - getsym(); + getsym(0); } if (sym==INLINE) { - getsym(); + getsym(0); } switch(sym) { case VOID: @@ -500,7 +498,7 @@ case FLOAT: case DOUBLE: t= sym; - getsym(); + getsym(0); break; case ENUM: t = edecl(); @@ -511,58 +509,58 @@ break; case UNSIGNED: t = UNSIGNED; - if(getsym()==INT) getsym(); - else if (sym==CHAR) { getsym(); t = UCHAR; } - else if (sym==SHORT) { getsym(); t = USHORT; } + if(getsym(0)==INT) getsym(0); + else if (sym==CHAR) { getsym(0); t = UCHAR; } + else if (sym==SHORT) { getsym(0); t = USHORT; } else if (sym==LONG) { - getsym(); + getsym(0); t = UNSIGNED; if(sym==LONG) { - getsym(); + getsym(0); t=ULONGLONG; // t=UNSIGNED; } else if(sym==INT) { - getsym(); + getsym(0); t=UNSIGNED; } } break; case SHORT: t=SHORT; - if(getsym()==INT) getsym(); + if(getsym(0)==INT) getsym(0); break; case LONG: t=INT; - getsym(); + getsym(0); if(sym==LONG) { - getsym(); + getsym(0); t=LONGLONG; // t=INT; } else if(sym==DOUBLE) { - getsym(); + getsym(0); t=DOUBLE; - } else if(sym==INT) getsym(); + } else if(sym==INT) getsym(0); break; default: if(sym==IDENT) { - if((nptr=get_type(nptr))->sc==TYPE) { + if(nptr->sc==TYPE) { t=nptr->ty; typedefed=glist2((int)nptr,typedefed); - getsym(); + getsym(0); break; - } else if(nptr->sc==EMPTY && (gnptr=get_type(gnptr))->sc==TYPE) { - getsym(); + } else if(nptr->sc==EMPTY && gnptr->sc==TYPE) { + getsym(0); break; } } while (sym==KONST) { - getsym(); + getsym(0); } if(mode==LDECL) return 0; t= INT; } while (sym==KONST) { - getsym(); + getsym(0); } return t; } @@ -572,7 +570,7 @@ { NMTBL *n; if(sym==MUL) { - getsym(); + getsym(0); n=decl0(); type=list2(POINTER,type); return n; @@ -588,23 +586,23 @@ int i,t; if(sym==LPAR) { - getsym(); + getsym(0); n=decl0(); checksym(RPAR); } else if (sym == IDENT) { n=nptr; - getsym(); + getsym(0); } else { /* error(DCERR); */ n= &null_nptr; } while(1) { if(sym==LBRA) { - if(getsym()==RBRA) { - getsym(); + if(getsym(0)==RBRA) { + getsym(0); if(mode==ADECL) { type=list2(POINTER,type); - } else if (mode==GDECL) { + } else if (mode==GDECL || stmode==EXTRN) { type=list3(ARRAY,type,0); } else { error(DCERR); @@ -617,9 +615,9 @@ } } else if(sym==LPAR) { if(mode==GDECL) { - mode=ADECL;getsym();mode=GDECL; /* ??? */ + mode=ADECL;getsym(0);mode=GDECL; /* ??? */ } else - getsym(); + getsym(0); n->dsp=0; if(stmode==EXTRN) n->sc=EXTRN; if(stmode==STATIC) n->sc=STATIC; @@ -627,20 +625,20 @@ if (type==CODE) { n->ty=CODE; if(sym==RPAR) { - getsym();t=0; + getsym(0);t=0; } else { t=adecl(n); } type=glist3(CODE,CODE,t); } else { if(sym==RPAR) { - getsym();t=0; + getsym(0);t=0; } else { t=adecl(n); } type=glist3(FUNCTION,type,t); } - n->ty=type; + // n->ty=type; /* in GDECL mode dsp contains real parameter, if not, it contains arg type list. Real parameter list is compatible with arg type list. See def/ADECL */ @@ -675,15 +673,15 @@ args = 0; n->dsp=0; for(;;) { - if(sym==IDENT && (nptr=get_type(nptr))->sc!=TYPE) { + if(sym==IDENT && nptr->sc!=TYPE) { type=INT; /* naked argument, old K&R C */ def(nptr); - getsym(); + getsym(0); if(sym==RPAR) break; } else { if(sym==DOTS) { argtypes=list2(DOTS,argtypes); - getsym(); + getsym(0); break; } if((t=typespec())==0) { @@ -705,7 +703,7 @@ if(sym==RPAR) break; } if (sym!=COMMA) error(DCERR); - getsym(); + getsym(0); } argtypes=reverse0(argtypes); n->dsp=reverse0(n->dsp); @@ -715,11 +713,6 @@ fnptr=sfnptr; type=stype; sdecl_f = sd; - /* Now nptr is the last of the arguments if any. - In struct_fields, nptr have to be a defined funciton - pointer body, so nptr should be set back to n. - struct { void (*error_)(char *s); } */ - nptr=n; stmode=sstmode; return argtypes; } @@ -863,6 +856,8 @@ n->nm = "_"; } nsc=ndsp=0; + if (stmode==EXTRN||mode==GDECL) + n->ty = type; if(type>0&&(car(type)==FUNCTION || car(type)==CODE)) { if (/* stmode==EXTRN ||*/ (mode==GDECL)) { fcheck(n); @@ -880,13 +875,12 @@ } else if (mode==GUDECL||mode==LUDECL) { // if (n->sc==FIELD && n->dsp==0); // else error(DCERR); - } else if (mode==GTDECL && n->sc==TAG) { - n = gnptr = get_type(n); - } else if (mode==LTDECL && n->sc==TAG) { - n = nptr = get_type(n); } else error(DCERR); } - sz = size(n->ty = type); + if (mode==GSDECL||mode==LSDECL) + sz = size(type); + else + sz = size(n->ty = type); switch(mode) { case GDECL: gen_gdecl(n->nm,gpc); @@ -920,16 +914,27 @@ case GSDECL: fields = list4(type,fields,(int)(n->nm),disp); disp += sz; - break; + return n; case GUDECL: fields = list4(type,fields,(int)(n->nm),disp); if (disp < sz) disp = sz; - break; + return n; + case LSDECL: + fields = list4(type,fields,(int)(n->nm),disp); + disp += sz; + return n; + case LUDECL: + fields = list4(type,fields,(int)(n->nm),disp); + if (disp < sz) disp = sz; + return n; case GTDECL: nsc = TYPE; if (gnptr!=n) error(-1); gtypedefed=glist2((int)gnptr,gtypedefed); break; + case LTDECL: + nsc = TYPE; + break; case ADECL: if(!integral(type)&&(car(type)==FUNCTION||car(type)==CODE)) { type=list2(POINTER,type); n->ty = type; @@ -985,17 +990,6 @@ decl_data(type,n,0); } return n; - case LSDECL: - fields = list4(type,fields,(int)(n->nm),disp); - disp += sz; - break; - case LUDECL: - fields = list4(type,fields,(int)(n->nm),disp); - if (disp < sz) disp = sz; - break; - case LTDECL: - nsc = TYPE; - break; default: error(DCERR); } @@ -1046,7 +1040,7 @@ conv->decl_data_(); mode_save = mode; mode=STAT; - getsym(); + getsym(0); if (sym==RC) { /* premature end */ conv->decl_data_end_(); mode = mode_save; @@ -1087,7 +1081,7 @@ } else if (0 && caddr(t)!=i+1) { /* size match? */ error(TYERR); } - getsym(); + getsym(0); return offset; } else { error(TYERR); @@ -1117,11 +1111,11 @@ offset = decl_data(car(t1),n,offset); /* alignment? */ t1 = cadr(t1); if ( t1 && sym==COMMA) { conv->comma_(); continue; } - if (!t1 && sym==COMMA) getsym(); /* extra comma */ + if (!t1 && sym==COMMA) getsym(0); /* extra comma */ if (sym==RC) break; // premature end } conv->decl_data_end_(); conv->rc_(); - getsym(); + getsym(0); return offset; } else if (sym==RC) { /* empty case */ conv->lc_(); @@ -1138,11 +1132,11 @@ static void sdecl_field() { - while (getsym() != RC) { + while (getsym(0) != RC) { decl(); } if (sdecl_f) conv->rc_(); - getsym(); + getsym(0); fields = reverse0(fields); } @@ -1172,15 +1166,13 @@ sdisp=disp; disp=0; if (sdecl_f) conv->sdecl_(s); - if (getsym() == IDENT) { + if (getsym(TAG) == IDENT) { nptr0 = nptr; gnptr0 = gnptr; if (sdecl_f) conv->id_(sym,nptr); - if (getsym() == LC) { + if (getsym(0) == LC) { if (sdecl_f) conv->lc_(); - nptr0 = get_tag(nptr0); if(nptr0->sc == EMPTY) nptr0=gnptr0; - nptr0 = get_tag(nptr0); if (nptr0->sc != EMPTY) error(DCERR); nptr0->sc = TAG; nptr0->ty = list4(s,-1,0,(int)nptr0); @@ -1190,16 +1182,14 @@ /* type0 = list4(s,disp,fields,0); now ... */ } else { /* struct tag name */ - nptr0 = get_tag(nptr0); if(nptr0->sc == EMPTY) nptr0=gnptr0; - nptr0 = get_tag(nptr0); if(nptr0->sc == EMPTY) nptr0->sc = TAG; if(nptr0->sc != TAG) error(TYERR); fields = caddr(nptr0->ty); disp = cadr(nptr0->ty); conv->comment_(' '); + type0 = list4(s,disp,fields,(int)nptr0); } - type0 = list4(s,disp,fields,(int)nptr0); } else if(sym==LC) { if (sdecl_f) conv->lc_(); sdecl_field(); @@ -1226,17 +1216,17 @@ mode=GEDECL; else mode=LEDECL; - if (getsym() == IDENT) { + if (getsym(0) == IDENT) { nptr->sc = TAG; - getsym(); + getsym(0); } if(sym==LC) { - while (getsym() == IDENT) { + while (getsym(0) == IDENT) { nptr->sc = ENUM; nptr->ty = INT; nptr0 = nptr; - if (getsym() == ASS) { - getsym(); + if (getsym(0) == ASS) { + getsym(0); disp = cexpr(expr1()); } nptr0->dsp = disp; @@ -1259,13 +1249,15 @@ if(!chk) code_enter(n->nm); fnptr=n; n->sc = CODE; + n->ty = type; + fcheck(n); disp = -args; mode=ADECL; if (sym!=LC) { reg_var=0; args=0; fnptr->dsp=0; while (sym!=LC) { /* argument declaration !ANSI */ - decl(); getsym(); + decl(); getsym(0); } fnptr->dsp=reverse0(fnptr->dsp); disp = -args; @@ -1284,7 +1276,7 @@ stmode=0; mode=STAT; init_vars=0; - while (typeid(getsym()) || sym==STATIC || sym==EXTRN || sym==TYPEDEF) { + while (typeid(getsym(0)) || sym==STATIC || sym==EXTRN || sym==TYPEDEF) { mode=LDECL; decl(); mode=STAT; @@ -1305,6 +1297,23 @@ static NMTBL *tmp_struct; static void +local_decl() +{ + init_vars=0; + /* local variable declaration */ + stmode=0; + mode=STAT; + while (typeid(getsym(0)) || sym==STATIC || sym==EXTRN + || sym==REGISTER || sym==TYPEDEF) { + mode=LDECL; + stmode=0; + decl(); + mode=STAT; + } + conv->localvar_end_(); +} + +static void fdecl(NMTBL *n) { int sd = stypedecl; @@ -1315,6 +1324,7 @@ tmp_struct = 0; reg_var=0; + n->ty = type; fcheck(n); n->sc = FUNCTION; mode=ADECL; @@ -1322,7 +1332,7 @@ args=0; fnptr->dsp=0; while (sym!=LC) { /* argument declaration !ANSI */ stmode=0; - decl(); getsym(); + decl(); getsym(0); } } else fnptr->dsp=reverse0(fnptr->dsp); @@ -1333,16 +1343,7 @@ conv->function_(fnptr,sd); conv->lc_(); init_vars=0; /* local variable declaration */ - stmode=0; - mode=STAT; - while (typeid(getsym()) || sym==STATIC || sym==EXTRN - || sym==REGISTER || sym==TYPEDEF) { - mode=LDECL; - stmode=0; - decl(); - mode=STAT; - } - conv->localvar_end_(); + local_decl(); control=1; if(!chk) enter1(); emit_init_vars(); @@ -1474,7 +1475,7 @@ if(sym==SM) { conv->sm_(); - getsym(); return; + getsym(0); return; } checkret(); switch(sym) { @@ -1499,13 +1500,13 @@ case BREAK: conv->break_(); jmp(blabel); - getsym(); + getsym(0); checksym(SM); return; case CONTINUE: conv->continue_(); jmp(clabel); - getsym(); + getsym(0); checksym(SM); return; case CASE: @@ -1540,7 +1541,7 @@ doif(void) { int l1,l2,slfree; - getsym(); + getsym(0); checksym(LPAR); conv->if_(); slfree=lfree; @@ -1555,7 +1556,7 @@ if ((l2 = control)) jmp(l2=fwdlabel()); fwddef(l1); - getsym(); + getsym(0); statement(); checkret(); if (l2) fwddef(l2); @@ -1574,7 +1575,7 @@ blabel=fwdlabel(); clabel=backdef(); conv->while_(); - getsym(); + getsym(0); checksym(LPAR); slfree=lfree; e=expr(0); @@ -1584,10 +1585,10 @@ bexpr(e,1,clabel); lfree=slfree; conv->sm_(); - getsym(); + getsym(0); } else { bexpr(e,0,blabel); - lfree=slfree; + // lfree=slfree; statement(); checkret(); if(control) @@ -1610,7 +1611,7 @@ clabel=fwdlabel(); l=backdef(); conv->dowhile_(); - getsym(); + getsym(0); statement(); checkret(); fwddef(clabel); @@ -1637,7 +1638,7 @@ scontinue=clabel; blabel=fwdlabel(); conv->for_(); - getsym(); + getsym(0); checksym(LPAR); slfree=lfree; if(sym!=SM) { @@ -1646,7 +1647,7 @@ conv->for1_(); } else { conv->for1_(); - getsym(); + getsym(0); } lfree=slfree; l=backdef(); @@ -1656,13 +1657,13 @@ conv->for2_(); } else { conv->for2_(); - getsym(); + getsym(0); } lfree=slfree; if(sym==RPAR) { clabel=l; conv->for_body_(); - getsym(); + getsym(0); statement(); checkret(); } else { @@ -1694,7 +1695,7 @@ dlabel=0; scase=cslabel; /* save parents next case label */ conv->switch_(); - getsym(); + getsym(0); checksym(LPAR); slfree=lfree; svalue=csvalue1; /* save parents switch value */ @@ -1721,10 +1722,11 @@ docomp(void) { conv->lc_(); - getsym(); + local_decl(); + emit_init_vars(); while(sym!=RC) { statement(); checkret();} conv->rc_(); - getsym(); + getsym(0); } static void @@ -1736,7 +1738,7 @@ slfree=lfree; while(sym==CASE) { conv->case_begin_(c,0); - getsym(); + getsym(0); c=list2(cexpr(expr(1)),c); conv->case_(c,0); checksym(COLON); @@ -1761,7 +1763,7 @@ static void dodefault(void) { - getsym(); + getsym(0); checksym(COLON); if (dlabel) error(STERR); if (!cslabel) jmp(cslabel = fwdlabel()); @@ -1774,10 +1776,10 @@ { int slfree,e,e1; - if(getsym()==SM) { + if(getsym(0)==SM) { conv->return_(); conv->return_end_(); - getsym(); + getsym(0); retpending = 1; return; } @@ -1825,7 +1827,7 @@ int t,e1,e2,env; conv->goto_(); - getsym(); + getsym(0); e1 = expr(0); t=car(e1); if (t==FNAME) { @@ -1882,7 +1884,7 @@ nptr->sc = BLABEL; nptr->dsp = backdef(); conv->label_(); - getsym(); + getsym(0); checksym(COLON); } @@ -1993,7 +1995,7 @@ e=expr1(); while(sym==COMMA) { conv->op_(sym); - getsym();e=list3(COMMA,e,rvalue(expr1())); + getsym(0);e=list3(COMMA,e,rvalue(expr1())); } return e; } @@ -2009,7 +2011,7 @@ conv->op_(sym); lcheck(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr1()); return assign_expr(e1,e2,t,type); case RSHIFT+AS: case LSHIFT+AS: case BAND+AS: case EOR+AS: case BOR+AS: case MOD+AS: @@ -2019,7 +2021,7 @@ op = sym-AS; lcheck(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr1()); if(!(integral(type)||type==FLOAT||type==DOUBLE)) error(TYERR); @@ -2069,7 +2071,7 @@ if(sym==COND) { // e1?e2:e3 conv->cond_(); e1=rvalue(e1); - getsym(); + getsym(0); conv->cond1_(); e2=rvalue(expr2()); t=type; @@ -2109,7 +2111,7 @@ while(sym==LOR) { /* || */ conv->op_(sym); e=rvalue(e); - getsym(); + getsym(0); e1=rvalue(expr4()); if(car(e)==CONST) e = cadr(e )?e:e1; else if(car(e1)==CONST) e = cadr(e1)?e1:e; @@ -2128,7 +2130,7 @@ while(sym==LAND) { /* && */ conv->op_(sym); e=rvalue(e); - getsym(); + getsym(0); e1=rvalue(expr5()); if(car(e)==CONST) e = cadr(e )?e1:e; else if(car(e1)==CONST) e = cadr(e1)?e:e1; @@ -2148,7 +2150,7 @@ conv->op_(sym); e1=rvalue(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr6()); e1=binop(BOR,e1,e2,t,type); } @@ -2165,7 +2167,7 @@ conv->op_(sym); e1=rvalue(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr7()); e1=binop(EOR,e1,e2,t,type); } @@ -2182,7 +2184,7 @@ conv->op_(sym); e1=rvalue(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr8()); e1=binop(BAND,e1,e2,t,type); } @@ -2199,7 +2201,7 @@ conv->op_(sym); e1=rvalue(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr9()); e1=binop(op,e1,e2,t,type); type= INT; @@ -2217,7 +2219,7 @@ conv->op_(sym); e1=rvalue(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr10()); if(t==DOUBLE||type==DOUBLE|| t==FLOAT||type==FLOAT) @@ -2242,7 +2244,7 @@ conv->op_(sym); e1=rvalue(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr11()); e1=binop(op,e1,e2,t,type); } @@ -2259,7 +2261,7 @@ conv->op_(sym); e1=rvalue(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr12()); e1=binop(op,e1,e2,t,type); } @@ -2276,7 +2278,7 @@ conv->op_(sym); e1=rvalue(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr13()); e1=binop(op,e1,e2,t,type); } @@ -2291,7 +2293,7 @@ switch (op = sym) { case INC: case DEC: conv->prefix_(sym); - getsym(); + getsym(0); lcheck(e=expr13()); dir = op==INC?1:-1; if(type==CHAR) { @@ -2317,12 +2319,12 @@ op==INC?size(cadr(type)):-size(cadr(type)),size_of_int )); case MUL: /* *p */ conv->prefix_(sym); - getsym(); + getsym(0); e=rvalue(expr13()); return(indop(e)); case BAND: /* &p */ conv->prefix_(sym); - getsym(); + getsym(0); switch(car(e=expr13())) { case INDIRECT: e=cadr(e); @@ -2342,7 +2344,7 @@ return e; case SUB: /* -p */ conv->prefix_(sym); - getsym(); + getsym(0); e=rvalue(expr13()); if(type==FLOAT||type==DOUBLE) { return list2(DMINUS,e); @@ -2353,14 +2355,14 @@ return(car(e)==CONST?list2(CONST,-cadr(e)):list2(MINUS,e)); case BNOT: /* ~p */ conv->prefix_(sym); - getsym(); + getsym(0); e=rvalue(expr13()); if(!integral(type)) error(TYERR); return(car(e)==CONST?list2(CONST,~cadr(e)):list2(BNOT,e)); case LNOT: /* !p */ conv->prefix_(sym); - getsym(); + getsym(0); e=rvalue(expr13()); if(type==FLOAT||type==DOUBLE) return(car(e)==DCONST?list2(CONST,!dcadr(e)): @@ -2370,8 +2372,8 @@ return(car(e)==CONST?list2(CONST,!cadr(e)):list2(LNOT,e)); case SIZEOF: conv->prefix_(sym); - if(getsym()==LPAR) { - if(typeid(getsym())) { + if(getsym(0)==LPAR) { + if(typeid(getsym(0))) { e=list2(CONST,size(typename())); type=INT; checksym(RPAR); @@ -2382,7 +2384,7 @@ expr16(e); if(sym==INC||sym==DEC) { /* after this operation, type is extended */ - getsym(); + getsym(0); if(type==CHAR) type=INT; else if(type==SHORT) type=INT; else if(type==UCHAR) type=UNSIGNED; @@ -2401,7 +2403,7 @@ if((op=sym)==INC||op==DEC) { conv->postfix_(sym); lcheck(e); - getsym(); + getsym(0); dir = op==INC?1:-1; if(type==CHAR) { @@ -2442,7 +2444,7 @@ int e1; e1=list2(FNAME,(int)nptr); type=list3(car(nptr->ty),cadr(nptr->ty),caddr(nptr->ty)); - getsym(); + getsym(0); extrn_use(nptr); return expr16(e1); } @@ -2455,7 +2457,6 @@ switch(sym) { case IDENT: conv->id_(sym,nptr); - nptr = get_non_tag(nptr); switch(nptr->sc) { case EXTRN: case EXTRN1: extrn_use(nptr); @@ -2466,7 +2467,7 @@ case GVAR: e1=list2(GVAR,(int)nptr); type=nptr->ty; - getsym(); + getsym(0); extrn_use(nptr); break; case FLABEL: case BLABEL: @@ -2475,22 +2476,22 @@ case LVAR: e1=list2(LVAR,nptr->dsp); type=nptr->ty; - getsym(); + getsym(0); break; case DREGISTER: case FREGISTER: case REGISTER: e1=list3(nptr->sc,nptr->dsp,(int)nptr); type=nptr->ty; - getsym(); + getsym(0); break; case ENUM: e1=list2(CONST,nptr->dsp); type=INT; - getsym(); + getsym(0); break; case EMPTY: - if(getsym()==LPAR) { + if(getsym(0)==LPAR) { nptr->sc = EXTRN1; type= glist3(FUNCTION,INT,0); nptr->ty= type; @@ -2512,19 +2513,19 @@ conv-> string_(sptr); e1=list3(STRING,(int)sptr,symval); type=list3(ARRAY,CHAR,symval); - getsym(); + getsym(0); break; case CONST: conv-> const_(symval); type= INT; e1=list2(CONST,symval); - getsym(); + getsym(0); break; case DCONST: conv-> const_(symval); type= DOUBLE; e1=dlist2(DCONST,dsymval); - getsym(); + getsym(0); break; case RETURN: conv-> return_f_(); @@ -2533,28 +2534,28 @@ } type=list2(POINTER,CODE); e1=list2(RETURN,(int)fnptr); - getsym(); + getsym(0); break; case DEFINED: - getsym(); + getsym(0); t = mode; mode = IFDEF; checksym(LPAR); conv-> defined_(name); mode = t; type= INT; e1=list2(CONST,symval); - getsym(); + getsym(0); checksym(RPAR); break; case ENVIRONMENT: conv-> environment_(); type=list2(POINTER,VOID); e1=list2(ENVIRONMENT,0); - getsym(); + getsym(0); break; case LPAR: conv->lpar_(); - if(typeid(getsym())) { /* cast */ + if(typeid(getsym(0))) { /* cast */ t=typename(); conv->return_type_(t,0,0); conv->rpar_(); @@ -2590,7 +2591,7 @@ conv->lbra_(sym); e1=rvalue(e1); t=type; - getsym(); + getsym(0); e2=rvalue(expr0()); checksym(RBRA); conv->rbra_(sym); @@ -2732,7 +2733,7 @@ { int dsp = 0; - getsym(); + getsym(0); if (sym!=IDENT) error(TYERR); conv->id_(sym,nptr); if (integral(type)||(car(type)!=STRUCT && car(type)!=UNION)) @@ -2763,7 +2764,7 @@ e=list2(INDIRECT,e); } } - getsym(); + getsym(0); return e; } @@ -3039,7 +3040,7 @@ argtypes = cadr(argtypes); } arglist=0; - getsym(); + getsym(0); while(sym!=RPAR) { e=rvalue(expr1()); if(argtypes==0) at=DOTS; @@ -3049,7 +3050,7 @@ arglist=list3(e,arglist,type); if(sym!=COMMA) break; conv->comma_(); - getsym(); + getsym(0); } checksym(RPAR); conv->funcall_args_(); @@ -3109,7 +3110,7 @@ ndecl0(void) { if(sym==MUL) { - getsym(); + getsym(0); return type=list2(POINTER,ndecl0()); } return ndecl1(); @@ -3121,8 +3122,8 @@ int i,t,arglist; if(sym==LPAR) { - if(getsym()==RPAR) { - type=list3(FUNCTION,type,0); getsym(); + if(getsym(0)==RPAR) { + type=list3(FUNCTION,type,0); getsym(0); } else { ndecl0(); checksym(RPAR); @@ -3130,20 +3131,20 @@ } while(1) { if(sym==LBRA) { - getsym(); + getsym(0); t=type; i=cexpr(expr(1)); checksym(RBRA); type=list3(ARRAY,t,i); } else if(sym==LPAR) { t = type; - getsym(); + getsym(0); arglist=0; while(sym!=RPAR) { ndecl0(); arglist=list2(type,arglist); if(sym!=COMMA) break; - getsym(); + getsym(0); } checksym(RPAR); type=list3(FUNCTION,t,arglist); @@ -3165,7 +3166,7 @@ extern double strtod(const char *nptr, char **endptr); static int -getsym(void) +getsym(int sc) { NMTBL *nptr0,*nptr1,*nptrm; int i,slfree,macrop,d; @@ -3216,26 +3217,26 @@ macropp[-1] =0; if (macro_buf[0]==0) { mode = i; - return getsym(); + return getsym(0); } chptrsave = list2((int)chptr,chptrsave); chsave = list2(chptr[-1],chsave); chptr = macro_buf; ch = *chptr++; mode = i; - return getsym(); + return getsym(0); } - nptr0 = gsearch(); + nptr0 = gsearch(sc); if (nptr0->sc == RESERVE) return sym = nptr0->dsp; sym = IDENT; gnptr=nptr=nptr0; - if (mode==ADECL && (nptr0=get_type(nptr0))->sc ==TYPE) return sym; + if (mode==ADECL && nptr0->sc ==TYPE) return sym; if (mode==GDECL || mode==GSDECL || mode==GUDECL || mode==GTDECL || mode==TOP || mode==GEDECL) { return sym; } - nptr1=lsearch(nptr0->nm); + nptr1=lsearch(nptr0->nm,sc); if (mode==STAT) { if (nptr1->sc == EMPTY) return sym; } @@ -3253,7 +3254,7 @@ return sym=DOTS; } error(CHERR); - return getsym(); + return getsym(0); } else if (!digit(ch)) return sym=PERIOD; d=1; @@ -3310,7 +3311,7 @@ } else if(ch=='"') { getstring(); return sym= STRING; - } + } c=ch; getch(); switch(c) { @@ -3359,12 +3360,12 @@ return sym=RC; case ',': return sym=COMMA; - case ';': - return sym=SM; case ':': return sym=COLON; case '?': return sym=COND; + case ';': + return sym=SM; case '/': if(ch=='/') { in_comment = 1; @@ -3372,7 +3373,7 @@ while(ch!='\n') { getch(); conv->comment_(ch); } in_comment = 0; getch(); - return getsym(); + return getsym(0); } if(ch!='*') return postequ(DIV,DIV+AS); in_comment = 1; @@ -3382,67 +3383,16 @@ } while(!(c=='*'&&ch=='/')); in_comment = 0; getch(); - return getsym(); + return getsym(0); case 0: case '\n': - return getsym(); + return getsym(0); default: error(CHERR); - return getsym(); + return getsym(0); } } -static NMTBL * -other_sym(NMTBL *n) -{ - NMTBL *nptr,*iptr; - - nptr = n+1; - if (ntable <= n && n < ntable + GSYMS) { /* global */ - while(nptr->sc!=0 && neqname(nptr->nm,n->nm)) { - if (++nptr== &ntable[GSYMS]) - nptr=ntable; - if (nptr==iptr) error(GSERR); - } - } else if (ntable+GSYMS <= n && n <= ntable + GSYMS + LSYMS) { /* local */ - while(nptr->sc!=0 && neqname(nptr->nm,n->nm)) { - if (++nptr== &ntable[LSYMS+GSYMS]) - nptr= &ntable[GSYMS]; - if (nptr==iptr) error(LSERR); - } - } else error(-1); - if (nptr->sc == 0) { - copy(nptr,name); - nptr->sc=EMPTY; - } - return nptr; -} - - -static NMTBL * -get_tag(NMTBL *n) -{ - if (n->sc == EMPTY) return n; - if (n->sc == TAG) return n; - if (n->sc != TYPE) return n; - return other_sym(n); -} - -static NMTBL * -get_type(NMTBL *n) -{ - if (n->sc == EMPTY) return n; - if (n->sc == TYPE) return n; - if (n->sc != TAG) return n; - return other_sym(n); -} - -static NMTBL * -get_non_tag(NMTBL *n) -{ - if (n->sc != TAG) return n; - return other_sym(n); -} static int postequ(int s1, int s2) @@ -3487,12 +3437,13 @@ } static NMTBL * -gsearch(void) +gsearch(int sc) { NMTBL *nptr,*iptr; iptr=nptr= &ntable[hash % GSYMS]; - while(nptr->sc!=0 && neqname(nptr->nm,name)) { + while(nptr->sc!=0 && (neqname(nptr->nm,name) || + !(sc?(nptr->sc==sc):(nptr->sc!=TAG)))) { if (++nptr== &ntable[GSYMS]) nptr=ntable; if (nptr==iptr) error(GSERR); @@ -3505,12 +3456,13 @@ } static NMTBL * -lsearch(char *name) +lsearch(char *name,int sc) { NMTBL *nptr,*iptr; iptr=nptr= &ntable[hash%LSYMS+GSYMS]; - while(nptr->sc!=0 && neqname(nptr->nm,name)) { + while(nptr->sc!=0 && (neqname(nptr->nm,name) || + !(sc?(nptr->sc==sc):(nptr->sc!=TAG)))) { if (++nptr== &ntable[LSYMS+GSYMS]) nptr= &ntable[GSYMS]; if (nptr==iptr) error(LSERR); @@ -3762,9 +3714,11 @@ int c,i; for(c=0;chptr[c];c++); chptr[c] = ';'; /* this can't happen in macro expression */ + chptr[c+1] = '\n'; + chptr[c+2] = 0; ch= *chptr; in_macro_if = 1; - getsym(); + getsym(0); /* i=cexpr(expr(1)); #if allow undefined symbols.. */ i=expr(1); in_macro_if = 0; @@ -3796,7 +3750,7 @@ if (!macro_if_skip) { mode_save = mode; mode = IFDEF; ch= *chptr; - i = getsym(); + i = getsym(0); mode = mode_save; macro_if_depth = macro_if_current; macro_if_skip = (!i)^c; @@ -3854,7 +3808,7 @@ i=mode; mode=IFDEF; ch= *chptr; - if (getsym()) { + if (getsym(0)) { if (nptr->sc == MACRO) { nptr->sc = EMPTY; } else if (nptr->sc == FMACRO) { @@ -3908,7 +3862,7 @@ } if (cheapp+i >= cheap+CHEAPSIZE) error(STRERR); name[i++] = 0; - (nptr0 = gsearch())->sc = EXTRN; + (nptr0 = gsearch(0))->sc = EXTRN; nptr0->dsp = d; nptr0->ty=type; if (use) extrn_use(nptr0); } @@ -3936,7 +3890,7 @@ mode=MDECL; // ch= *chptr; ?? // fprintf(stderr,"macro def: ch %c *chptr %c\n",ch,*chptr); - getsym(); + getsym(0); // fprintf(stderr,"macro def: %s =>",name); if (nptr->sc != EMPTY) { /* override exisiting macro */ } @@ -3951,7 +3905,17 @@ nptr->dsp = list2((int)cheapp,args); /* macro body */ while ((*cheapp++ = c = *chptr++) && c != '\n') { - if (c=='\\' && (*chptr=='\n'||*chptr==0)) { + if (c=='/'&&chptr[0]=='/') { + cheapp--; while(*chptr++); break; + } else if (c=='/'&&chptr[0]=='*') { + cheapp--; chptr++; + while((c = *chptr++)) { + if (c=='*'&&chptr[0]=='/') { + c = *chptr++; break; + } + } + if (!c) break; + } else if (c=='\\' && (*chptr=='\n'||*chptr==0)) { chptr++; cheapp--; getline(); @@ -4147,6 +4111,8 @@ if (c) { *macropp++=c; macrop = list2((int)macropp-1,macrop); + } else { + macrop = list2((int)macropp,macrop); } } else if (nptrm->sc==FMACRO) { if(c!='(') error(MCERR); @@ -4155,8 +4121,9 @@ list2((int)macro,history)); if (ch) { /*?*/ *macropp++=ch; - *macropp++=0; - macrop = list2((int)macropp-2,macrop); + macrop = list2((int)macropp-1,macrop); + } else { + macrop = list2((int)macropp,macrop); } } else { macro = namebuf;
--- a/test/macro.c Sun Nov 30 06:41:38 2003 +0900 +++ b/test/macro.c Sun Nov 30 18:26:36 2003 +0900 @@ -13,11 +13,13 @@ #define c(a,b) g(a+1,b+1) #define g(a,b) printf("%d %d\n",a+1,b+1); +#define d(a,b) cadr(b)+3 main() { int a,b,e; b = heap[TEST111]; + d(a,b); a =1; b = 3; #ifndef a c(a,
--- a/test/tmp4.c Sun Nov 30 06:41:38 2003 +0900 +++ b/test/tmp4.c Sun Nov 30 18:26:36 2003 +0900 @@ -7,7 +7,7 @@ { int i; i=main0(ac,av); - fprintf(stdout,"1: %s %d\n",av[0],i); + fprintf(stdout,"1: %s %d\n",av[0]+2,i); return 0; } @@ -20,7 +20,7 @@ i=123; j=456; k=789; - fprintf(stdout,"2: av=%x av[0]=%x %s\n",av,av[0],av[0]); + fprintf(stdout,"2: %s\n",av[0]+2); goto code0(i,j,k,av,return,environment); } @@ -31,8 +31,8 @@ void *retenv; { char *p; - p = av[0]; - fprintf(stdout,"3: i=%d j=%d k=%d av=%x p=%s\n",i,j,k,av,p); + p = av[0]+2; + fprintf(stdout,"3: i=%d j=%d k=%d av[0]=%s p=%s\n",i,j,k,av[0]+2,p); goto code1(i,j,k,av,ret,retenv); } @@ -42,7 +42,7 @@ code (*ret)(); void *retenv; { - fprintf(stdout,"4: %s\n",av[0]); + fprintf(stdout,"4: %s\n",av[0]+2); goto code2(i,j,k,av,ret,retenv); } @@ -52,7 +52,7 @@ code (*ret)(); void *retenv; { - fprintf(stdout,"5: %s\n",av[0]); - fprintf(stdout,"5: i=%d j=%d k=%d av=%x\n",i,j,k,av); + fprintf(stdout,"5: %s\n",av[0]+2); + fprintf(stdout,"5: i=%d j=%d k=%d\n",i,j,k); goto (*ret)(1234),retenv; }