merged in faluco's decl changes !1

--HG--
branch : dvander
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/branches/dvander%4021
This commit is contained in:
David Anderson 2006-07-15 03:19:23 +00:00
commit 83331994ed
3 changed files with 79 additions and 57 deletions

View File

@ -283,7 +283,7 @@ typedef struct s_stringpair {
*/ */
#define tFIRST 256 /* value of first multi-character operator */ #define tFIRST 256 /* value of first multi-character operator */
#define tMIDDLE 280 /* value of last multi-character operator */ #define tMIDDLE 280 /* value of last multi-character operator */
#define tLAST 325 /* value of last multi-character match-able token */ #define tLAST 326 /* value of last multi-character match-able token */
/* multi-character operators */ /* multi-character operators */
#define taMULT 256 /* *= */ #define taMULT 256 /* *= */
#define taDIV 257 /* /= */ #define taDIV 257 /* /= */
@ -329,44 +329,45 @@ typedef struct s_stringpair {
#define tIF 296 #define tIF 296
#define tNATIVE 297 #define tNATIVE 297
#define tNEW 298 #define tNEW 298
#define tOPERATOR 299 #define tDECL 299
#define tPUBLIC 300 #define tOPERATOR 300
#define tRETURN 301 #define tPUBLIC 301
#define tSIZEOF 302 #define tRETURN 302
#define tSLEEP 303 #define tSIZEOF 303
#define tSTATE 304 #define tSLEEP 304
#define tSTATIC 305 #define tSTATE 305
#define tSTOCK 306 #define tSTATIC 306
#define tSWITCH 307 #define tSTOCK 307
#define tTAGOF 308 #define tSWITCH 308
#define tWHILE 309 #define tTAGOF 309
#define tWHILE 310
/* compiler directives */ /* compiler directives */
#define tpASSERT 310 /* #assert */ #define tpASSERT 311 /* #assert */
#define tpDEFINE 311 #define tpDEFINE 312
#define tpELSE 312 /* #else */ #define tpELSE 313 /* #else */
#define tpELSEIF 313 /* #elseif */ #define tpELSEIF 314 /* #elseif */
#define tpEMIT 314 #define tpEMIT 315
#define tpENDIF 315 #define tpENDIF 316
#define tpENDINPUT 316 #define tpENDINPUT 317
#define tpENDSCRPT 317 #define tpENDSCRPT 318
#define tpERROR 318 #define tpERROR 319
#define tpFILE 319 #define tpFILE 320
#define tpIF 320 /* #if */ #define tpIF 321 /* #if */
#define tINCLUDE 321 #define tINCLUDE 322
#define tpLINE 322 #define tpLINE 323
#define tpPRAGMA 323 #define tpPRAGMA 324
#define tpTRYINCLUDE 324 #define tpTRYINCLUDE 325
#define tpUNDEF 325 #define tpUNDEF 326
/* semicolon is a special case, because it can be optional */ /* semicolon is a special case, because it can be optional */
#define tTERM 326 /* semicolon or newline */ #define tTERM 327 /* semicolon or newline */
#define tENDEXPR 327 /* forced end of expression */ #define tENDEXPR 328 /* forced end of expression */
/* other recognized tokens */ /* other recognized tokens */
#define tNUMBER 328 /* integer number */ #define tNUMBER 329 /* integer number */
#define tRATIONAL 329 /* rational number */ #define tRATIONAL 330 /* rational number */
#define tSYMBOL 330 #define tSYMBOL 331
#define tLABEL 331 #define tLABEL 332
#define tSTRING 332 #define tSTRING 333
#define tEXPR 333 /* for assigment to "lastst" only */ #define tEXPR 334 /* for assigment to "lastst" only */
/* (reversed) evaluation of staging buffer */ /* (reversed) evaluation of staging buffer */
#define sSTARTREORDER 0x01 #define sSTARTREORDER 0x01

View File

@ -135,6 +135,7 @@ static int *readwhile(void);
static void inst_datetime_defines(void); static void inst_datetime_defines(void);
static int norun = 0; /* the compiler never ran */ static int norun = 0; /* the compiler never ran */
static int autozero = 0; /* if 1 will zero out the variable, if 0 omit the zeroing */
static int lastst = 0; /* last executed statement type */ static int lastst = 0; /* last executed statement type */
static int nestlevel = 0; /* number of active (open) compound statements */ static int nestlevel = 0; /* number of active (open) compound statements */
static int rettype = 0; /* the type that a "return" expression should have */ static int rettype = 0; /* the type that a "return" expression should have */
@ -1977,11 +1978,15 @@ static int declloc(int fstatic)
int ctag = tag; /* set to "tag" by default */ int ctag = tag; /* set to "tag" by default */
int explicit_init=FALSE;/* is the variable explicitly initialized? */ int explicit_init=FALSE;/* is the variable explicitly initialized? */
if (matchtoken('=')) { if (matchtoken('=')) {
if (!autozero)
error(10);
doexpr(FALSE,FALSE,FALSE,FALSE,&ctag,NULL,TRUE); doexpr(FALSE,FALSE,FALSE,FALSE,&ctag,NULL,TRUE);
explicit_init=TRUE; explicit_init=TRUE;
} else { } else {
if (autozero)
ldconst(0,sPRI); /* uninitialized variable, set to zero */ ldconst(0,sPRI); /* uninitialized variable, set to zero */
} /* if */ } /* if */
if (autozero) {
/* now try to save the value (still in PRI) in the variable */ /* now try to save the value (still in PRI) in the variable */
lval.sym=sym; lval.sym=sym;
lval.ident=iVARIABLE; lval.ident=iVARIABLE;
@ -1990,6 +1995,7 @@ static int declloc(int fstatic)
check_userop(NULL,ctag,lval.tag,2,NULL,&ctag); check_userop(NULL,ctag,lval.tag,2,NULL,&ctag);
store(&lval); store(&lval);
markexpr(sEXPR,NULL,0); /* full expression ends after the store */ markexpr(sEXPR,NULL,0); /* full expression ends after the store */
}
assert(staging); /* end staging phase (optimize expression) */ assert(staging); /* end staging phase (optimize expression) */
stgout(staging_start); stgout(staging_start);
stgset(FALSE); stgset(FALSE);
@ -2004,6 +2010,7 @@ static int declloc(int fstatic)
assert(cur_lit>=0 && cur_lit<=litidx && litidx<=litmax); assert(cur_lit>=0 && cur_lit<=litidx && litidx<=litmax);
assert(size>0 && size>=sym->dim.array.length); assert(size>0 && size>=sym->dim.array.length);
assert(numdim>1 || size==sym->dim.array.length); assert(numdim>1 || size==sym->dim.array.length);
if (autozero) {
/* final literal values that are zero make no sense to put in the literal /* final literal values that are zero make no sense to put in the literal
* pool, because values get zero-initialized anyway; we check for this, * pool, because values get zero-initialized anyway; we check for this,
* because users often explicitly initialize strings to "" * because users often explicitly initialize strings to ""
@ -2013,6 +2020,7 @@ static int declloc(int fstatic)
/* if the array is not completely filled, set all values to zero first */ /* if the array is not completely filled, set all values to zero first */
if (litidx-cur_lit<size && (ucell)size<CELL_MAX) if (litidx-cur_lit<size && (ucell)size<CELL_MAX)
fillarray(sym,size*sizeof(cell),0); fillarray(sym,size*sizeof(cell),0);
}
if (cur_lit<litidx) { if (cur_lit<litidx) {
/* check whether the complete array is set to a single value; if /* check whether the complete array is set to a single value; if
* it is, more compact code can be generated */ * it is, more compact code can be generated */
@ -2264,12 +2272,12 @@ static cell initvector(int ident,int tag,cell size,int fillzero,
constvalue *enumroot,int *errorfound) constvalue *enumroot,int *errorfound)
{ {
cell prev1=0,prev2=0; cell prev1=0,prev2=0;
int ellips=FALSE; int ellips=FALSE,hadtoken=0;
int curlit=litidx; int curlit=litidx;
int rtag,ctag; int rtag,ctag;
assert(ident==iARRAY || ident==iREFARRAY); assert(ident==iARRAY || ident==iREFARRAY);
if (matchtoken('{')) { if ((hadtoken=matchtoken('{')) && autozero) {
constvalue *enumfield=(enumroot!=NULL) ? enumroot->next : NULL; constvalue *enumfield=(enumroot!=NULL) ? enumroot->next : NULL;
do { do {
int fieldlit=litidx; int fieldlit=litidx;
@ -2331,6 +2339,8 @@ static cell initvector(int ident,int tag,cell size,int fillzero,
} while (matchtoken(',')); /* do */ } while (matchtoken(',')); /* do */
needtoken('}'); needtoken('}');
} else { } else {
if (hadtoken && !autozero)
error(10);
init(ident,&ctag,errorfound); init(ident,&ctag,errorfound);
if (!matchtag(tag,ctag,TRUE)) if (!matchtag(tag,ctag,TRUE))
error(213); /* tagname mismatch */ error(213); /* tagname mismatch */
@ -4612,12 +4622,22 @@ static void statement(int *lastindent,int allow_decl)
break; break;
case tNEW: case tNEW:
if (allow_decl) { if (allow_decl) {
autozero=1;
declloc(FALSE); declloc(FALSE);
lastst=tNEW; lastst=tNEW;
} else { } else {
error(3); /* declaration only valid in a block */ error(3); /* declaration only valid in a block */
} /* if */ } /* if */
break; break;
case tDECL:
if (allow_decl) {
autozero=0;
declloc(FALSE);
lastst=tDECL;
} else {
error(3); /* declaration only valid in a block */
} /* if */
break;
case tSTATIC: case tSTATIC:
if (allow_decl) { if (allow_decl) {
declloc(TRUE); declloc(TRUE);
@ -4993,6 +5013,7 @@ static void dofor(void)
* 'compound statement' level of it own. * 'compound statement' level of it own.
*/ */
nestlevel++; nestlevel++;
autozero=1;
declloc(FALSE); /* declare local variable */ declloc(FALSE); /* declare local variable */
} else { } else {
doexpr(TRUE,TRUE,TRUE,TRUE,NULL,NULL,FALSE); /* expression 1 */ doexpr(TRUE,TRUE,TRUE,TRUE,NULL,NULL,FALSE); /* expression 1 */

View File

@ -1818,7 +1818,7 @@ char *sc_tokens[] = {
"...", "..", "::", "...", "..", "::",
"assert", "break", "case", "char", "const", "continue", "default", "assert", "break", "case", "char", "const", "continue", "default",
"defined", "do", "else", "enum", "exit", "for", "forward", "goto", "defined", "do", "else", "enum", "exit", "for", "forward", "goto",
"if", "native", "new", "operator", "public", "return", "sizeof", "if", "native", "new", "decl", "operator", "public", "return", "sizeof",
"sleep", "state", "static", "stock", "switch", "tagof", "while", "sleep", "state", "static", "stock", "switch", "tagof", "while",
"#assert", "#define", "#else", "#elseif", "#emit", "#endif", "#endinput", "#assert", "#define", "#else", "#elseif", "#emit", "#endif", "#endinput",
"#endscript", "#error", "#file", "#if", "#include", "#line", "#pragma", "#endscript", "#error", "#file", "#if", "#include", "#line", "#pragma",