Fixed a bug in multiple dimension array access (issue #80)

Added a test for this


git-svn-id: http://picoc.googlecode.com/svn/trunk@419 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2010-06-05 12:58:37 +00:00
parent 54ad31d6d3
commit fa3e9a1187
4 changed files with 32 additions and 2 deletions

View file

@ -603,7 +603,7 @@ void ExpressionInfixOperator(struct ParseState *Parser, struct ExpressionStack *
/* make the array element result */
switch (BottomValue->Typ->Base)
{
case TypeArray: Result = VariableAllocValueFromExistingData(Parser, BottomValue->Typ->FromType, (union AnyValue *)(&BottomValue->Val->ArrayMem[0] + TypeSize(BottomValue->Typ->FromType, 0, TRUE) * ArrayIndex), BottomValue->IsLValue, BottomValue->LValueFrom); break;
case TypeArray: Result = VariableAllocValueFromExistingData(Parser, BottomValue->Typ->FromType, (union AnyValue *)(&BottomValue->Val->ArrayMem[0] + TypeSize(BottomValue->Typ, ArrayIndex, TRUE)), BottomValue->IsLValue, BottomValue->LValueFrom); break;
case TypePointer: Result = VariableAllocValueFromExistingData(Parser, BottomValue->Typ->FromType, (union AnyValue *)((char *)BottomValue->Val->NativePointer + TypeSize(BottomValue->Typ->FromType, 0, TRUE) * ArrayIndex), BottomValue->IsLValue, BottomValue->LValueFrom); break;
default: ProgramFail(Parser, "this %t is not an array", BottomValue->Typ);
}

View file

@ -0,0 +1,25 @@
int a[4][4];
int b = 0;
int x;
int y;
for (x = 0; x < 4; x++)
{
for (y = 0; y < 4; y++)
{
b++;
a[x][y] = b;
}
}
for (x = 0; x < 4; x++)
{
printf("x=%d: ", x);
for (y = 0; y < 4; y++)
{
printf("%d ", a[x][y]);
}
printf("\n");
}

View file

@ -0,0 +1,4 @@
x=0: 1 2 3 4
x=1: 5 6 7 8
x=2: 9 10 11 12
x=3: 13 14 15 16

View file

@ -30,7 +30,8 @@ TESTS= 00_assignment.test \
34_array_assignment.test \
35_sizeof.test \
36_array_initialisers.test \
37_sprintf.test
37_sprintf.test \
38_multiple_array_index.test
%.test: %.expect %.c
@echo Test: $*...