From e44aca659f73683efc0597593b61ebec6e0c57b7 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Sun, 1 Nov 2009 20:49:23 +0000 Subject: [PATCH] Fixed problem with using array index operator on pointers. Issue #61. git-svn-id: http://picoc.googlecode.com/svn/trunk@367 21eae674-98b7-11dd-bd71-f92a316d2d60 --- expression.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/expression.c b/expression.c index 65092c4..b600f65 100644 --- a/expression.c +++ b/expression.c @@ -657,20 +657,19 @@ void ExpressionInfixOperator(struct ParseState *Parser, struct ExpressionStack * int ArrayIndex; struct Value *Result; - if (BottomValue->Typ->Base != TypeArray) - ProgramFail(Parser, "this %t is not an array", BottomValue->Typ); - if (!IS_NUMERIC_COERCIBLE(TopValue)) ProgramFail(Parser, "array index must be an integer"); ArrayIndex = ExpressionCoerceInteger(TopValue); -#ifndef NATIVE_POINTERS - if (ArrayIndex < 0 || ArrayIndex >= BottomValue->Val->Array.Size) - ProgramFail(Parser, "illegal array index %d [0..%d]", ArrayIndex, BottomValue->Val->Array.Size-1); -#endif /* make the array element result */ - Result = VariableAllocValueFromExistingData(Parser, BottomValue->Typ->FromType, (union AnyValue *)((char *)BottomValue->Val->Array.Data + TypeSize(BottomValue->Typ->FromType, 0, TRUE) * ArrayIndex), BottomValue->IsLValue, BottomValue->LValueFrom); + switch (BottomValue->Typ->Base) + { + case TypeArray: Result = VariableAllocValueFromExistingData(Parser, BottomValue->Typ->FromType, (union AnyValue *)((char *)BottomValue->Val->Array.Data + TypeSize(BottomValue->Typ->FromType, 0, TRUE) * ArrayIndex), 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); + } + ExpressionStackPushValueNode(Parser, StackTop, Result); } #ifndef NO_FP