Fixed array indexing issue.

Fixed problem in index() and rindex() NULL pointer returns.
Nicer handling of printing NULL pointers.
New test suite for strings.


git-svn-id: http://picoc.googlecode.com/svn/trunk@353 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-10-28 21:34:51 +00:00
parent 66f40f6310
commit 50b45890bc
5 changed files with 75 additions and 12 deletions

View file

@ -273,7 +273,10 @@ void GenericPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct
else
Str = NextArg->Val->Array.Data;
PrintStr(Str, Stream);
if (Str == NULL)
PrintStr("NULL", Stream);
else
PrintStr(Str, Stream);
break;
}
case 'd': PrintInt(ExpressionCoerceInteger(NextArg), FieldWidth, ZeroPad, LeftJustify, Stream); break;

View file

@ -411,7 +411,6 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue, struct
/* evaluate a prefix operator */
void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, enum LexToken Op, struct Value *TopValue)
{
struct Value *TempLValue;
struct Value *Result;
if (Parser->Mode != RunModeRun)
@ -428,18 +427,17 @@ void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack
if (!TopValue->IsLValue)
ProgramFail(Parser, "can't get the address of this");
TempLValue = TopValue->LValueFrom;
assert(TempLValue != NULL);
Result = VariableAllocValueFromType(Parser, TypeGetMatching(Parser, TopValue->Typ, TypePointer, 0, StrEmpty), FALSE, NULL, FALSE);
#ifndef NATIVE_POINTERS
Result->Val->Pointer.Segment = TempLValue;
if (Result->LValueFrom != NULL)
Result->Val->Pointer.Offset = (char *)Result->Val - (char *)Result->LValueFrom;
{
struct Value *TempLValue = TopValue->LValueFrom;
assert(TempLValue != NULL);
Result->Val->Pointer.Segment = TempLValue;
if (Result->LValueFrom != NULL)
Result->Val->Pointer.Offset = (char *)Result->Val - (char *)Result->LValueFrom;
}
#else
if (TempLValue->Typ->Base == TypeArray)
Result->Val->NativePointer = TopValue->Val;
else
Result->Val->NativePointer = TempLValue->Val;
Result->Val->NativePointer = TopValue->Val;
#endif
ExpressionStackPushValueNode(Parser, StackTop, Result);
break;

41
tests/28_strings.c Normal file
View file

@ -0,0 +1,41 @@
char a[10];
strcpy(a, "hello");
printf("%s\n", a);
/*
strcpy(&a, "there");
printf("%s\n", a);
*/
strncpy(a, "gosh", 2);
printf("%s\n", a);
printf("%d\n", strcmp(a, "apple"));
printf("%d\n", strcmp(a, "goere"));
printf("%d\n", strcmp(a, "zebra"));
printf("%d\n", strlen(a));
strcat(a, "!");
printf("%s\n", a);
printf("%d\n", strncmp(a, "apple", 2));
printf("%d\n", strncmp(a, "goere", 2));
printf("%d\n", strncmp(a, "goerg", 2));
printf("%d\n", strncmp(a, "zebra", 2));
printf("%s\n", index(a, 'o'));
printf("%s\n", rindex(a, 'l'));
printf("%s\n", rindex(a, 'x'));
memset(&a[1], 'r', 4);
printf("%s\n", a);
memcpy(&a[2], a, 2);
printf("%s\n", a);
printf("%d\n", memcmp(a, "apple", 4));
printf("%d\n", memcmp(a, "grgr", 4));
printf("%d\n", memcmp(a, "zebra", 4));

19
tests/28_strings.expect Normal file
View file

@ -0,0 +1,19 @@
hello
gollo
1
1
-1
5
gollo!
6
0
0
-19
ollo!
lo!
NULL
grrrr!
grgrr!
1
0
-1

View file

@ -24,7 +24,9 @@ TESTS= 00_assignment.test \
23_type_coercion.test \
24_math_library.test \
25_quicksort.test \
26_character_constants.test
26_character_constants.test \
28_strings.test \
29_array_address.test
# 27_sizeof.test
%.test: %.expect %.c