From fa3e9a11872d5e2f24bf663dbfee6df0d967ad18 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Sat, 5 Jun 2010 12:58:37 +0000 Subject: [PATCH] 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 --- expression.c | 2 +- tests/38_multiple_array_index.c | 25 +++++++++++++++++++++++++ tests/38_multiple_array_index.expect | 4 ++++ tests/Makefile | 3 ++- 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/38_multiple_array_index.c create mode 100644 tests/38_multiple_array_index.expect diff --git a/expression.c b/expression.c index fb9337e..5583bf6 100644 --- a/expression.c +++ b/expression.c @@ -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); } diff --git a/tests/38_multiple_array_index.c b/tests/38_multiple_array_index.c new file mode 100644 index 0000000..d6f9092 --- /dev/null +++ b/tests/38_multiple_array_index.c @@ -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"); +} diff --git a/tests/38_multiple_array_index.expect b/tests/38_multiple_array_index.expect new file mode 100644 index 0000000..747ad75 --- /dev/null +++ b/tests/38_multiple_array_index.expect @@ -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 diff --git a/tests/Makefile b/tests/Makefile index b61bdd2..2ebcafd 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -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: $*...