diff --git a/parse.c b/parse.c index 981dee1..f59daa2 100644 --- a/parse.c +++ b/parse.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "common.h" #include "parse.h" @@ -3886,12 +3887,13 @@ static void ParseArrayIndices(symbolNode_t *sym, int requiredIndices) } } -static int *ProcessArrayLevel(int level, int *entry, int ndim, +static void ProcessArrayLevel(int level, int *entry, int ndim, int dims[MAX_ARRAY_DIMS], int muls[MAX_ARRAY_DIMS], char *name) { + int warned_too_many = NO; int i; - for(i = 0; i < dims[level-1]; ++i) + for(i = 0; ; ++i) { if(tk_Token == TK_COMMA) { @@ -3901,14 +3903,7 @@ static int *ProcessArrayLevel(int level, int *entry, int ndim, else if(tk_Token == TK_RBRACE) { TK_NextToken(); - if(level > 1) - { - return entry + muls[level-2] - i; - } - else - { - return entry + (muls[0]*dims[0]) - i; - } + return; } else { @@ -3923,15 +3918,28 @@ static int *ProcessArrayLevel(int level, int *entry, int ndim, } else { - *entry++ = EvalConstExpression(); + int val; + + if (i >= dims[level - 1] && !warned_too_many) + { + warned_too_many = YES; + ERR_Error(ERR_TOO_MANY_ARRAY_INIT, YES); + } + val = EvalConstExpression(); ArrayHasStrings |= pa_ConstExprIsString; + if (i < dims[level - 1]) + { + entry[i] = val; + } } } else { TK_TokenMustBe(TK_LBRACE, ERR_MISSING_LBRACE_ARR); TK_NextToken(); - entry = ProcessArrayLevel(level+1, entry, ndim, dims, muls, name); + ProcessArrayLevel(level+1, entry, ndim, dims, muls, name); + assert(level > 0); + entry += muls[level-1]; } if(i < dims[level-1]-1) { @@ -3956,7 +3964,6 @@ static int *ProcessArrayLevel(int level, int *entry, int ndim, } TK_TokenMustBe(TK_RBRACE, ERR_MISSING_RBRACE_ARR); TK_NextToken(); - return entry; } //========================================================================== diff --git a/pcode.c b/pcode.c index 923747c..baa8360 100644 --- a/pcode.c +++ b/pcode.c @@ -830,6 +830,7 @@ static void CloseNew(void) PC_Append("AINI", 4); PC_AppendInt(ArraySizes[i]*4+4); PC_AppendInt((U_INT)i); + MS_Message(MSG_DEBUG, "Writing array initializers for array %d (size %d)\n", i, ArraySizes[i]); for(j = 0; j < ArraySizes[i]; ++j) { PC_AppendInt((U_INT)ArrayInits[i][j]);