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:
parent
66f40f6310
commit
50b45890bc
|
@ -273,7 +273,10 @@ void GenericPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct
|
||||||
else
|
else
|
||||||
Str = NextArg->Val->Array.Data;
|
Str = NextArg->Val->Array.Data;
|
||||||
|
|
||||||
PrintStr(Str, Stream);
|
if (Str == NULL)
|
||||||
|
PrintStr("NULL", Stream);
|
||||||
|
else
|
||||||
|
PrintStr(Str, Stream);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd': PrintInt(ExpressionCoerceInteger(NextArg), FieldWidth, ZeroPad, LeftJustify, Stream); break;
|
case 'd': PrintInt(ExpressionCoerceInteger(NextArg), FieldWidth, ZeroPad, LeftJustify, Stream); break;
|
||||||
|
|
18
expression.c
18
expression.c
|
@ -411,7 +411,6 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue, struct
|
||||||
/* evaluate a prefix operator */
|
/* evaluate a prefix operator */
|
||||||
void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, enum LexToken Op, struct Value *TopValue)
|
void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, enum LexToken Op, struct Value *TopValue)
|
||||||
{
|
{
|
||||||
struct Value *TempLValue;
|
|
||||||
struct Value *Result;
|
struct Value *Result;
|
||||||
|
|
||||||
if (Parser->Mode != RunModeRun)
|
if (Parser->Mode != RunModeRun)
|
||||||
|
@ -428,18 +427,17 @@ void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack
|
||||||
if (!TopValue->IsLValue)
|
if (!TopValue->IsLValue)
|
||||||
ProgramFail(Parser, "can't get the address of this");
|
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);
|
Result = VariableAllocValueFromType(Parser, TypeGetMatching(Parser, TopValue->Typ, TypePointer, 0, StrEmpty), FALSE, NULL, FALSE);
|
||||||
#ifndef NATIVE_POINTERS
|
#ifndef NATIVE_POINTERS
|
||||||
Result->Val->Pointer.Segment = TempLValue;
|
{
|
||||||
if (Result->LValueFrom != NULL)
|
struct Value *TempLValue = TopValue->LValueFrom;
|
||||||
Result->Val->Pointer.Offset = (char *)Result->Val - (char *)Result->LValueFrom;
|
assert(TempLValue != NULL);
|
||||||
|
Result->Val->Pointer.Segment = TempLValue;
|
||||||
|
if (Result->LValueFrom != NULL)
|
||||||
|
Result->Val->Pointer.Offset = (char *)Result->Val - (char *)Result->LValueFrom;
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
if (TempLValue->Typ->Base == TypeArray)
|
Result->Val->NativePointer = TopValue->Val;
|
||||||
Result->Val->NativePointer = TopValue->Val;
|
|
||||||
else
|
|
||||||
Result->Val->NativePointer = TempLValue->Val;
|
|
||||||
#endif
|
#endif
|
||||||
ExpressionStackPushValueNode(Parser, StackTop, Result);
|
ExpressionStackPushValueNode(Parser, StackTop, Result);
|
||||||
break;
|
break;
|
||||||
|
|
41
tests/28_strings.c
Normal file
41
tests/28_strings.c
Normal 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
19
tests/28_strings.expect
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
hello
|
||||||
|
gollo
|
||||||
|
1
|
||||||
|
1
|
||||||
|
-1
|
||||||
|
5
|
||||||
|
gollo!
|
||||||
|
6
|
||||||
|
0
|
||||||
|
0
|
||||||
|
-19
|
||||||
|
ollo!
|
||||||
|
lo!
|
||||||
|
NULL
|
||||||
|
grrrr!
|
||||||
|
grgrr!
|
||||||
|
1
|
||||||
|
0
|
||||||
|
-1
|
|
@ -24,7 +24,9 @@ TESTS= 00_assignment.test \
|
||||||
23_type_coercion.test \
|
23_type_coercion.test \
|
||||||
24_math_library.test \
|
24_math_library.test \
|
||||||
25_quicksort.test \
|
25_quicksort.test \
|
||||||
26_character_constants.test
|
26_character_constants.test \
|
||||||
|
28_strings.test \
|
||||||
|
29_array_address.test
|
||||||
# 27_sizeof.test
|
# 27_sizeof.test
|
||||||
|
|
||||||
%.test: %.expect %.c
|
%.test: %.expect %.c
|
||||||
|
|
Loading…
Reference in a new issue