diff --git a/clibrary.c b/clibrary.c index 1f51da5..38161e9 100644 --- a/clibrary.c +++ b/clibrary.c @@ -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; diff --git a/expression.c b/expression.c index fc62003..60e60ed 100644 --- a/expression.c +++ b/expression.c @@ -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; diff --git a/tests/28_strings.c b/tests/28_strings.c new file mode 100644 index 0000000..46f886c --- /dev/null +++ b/tests/28_strings.c @@ -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)); + diff --git a/tests/28_strings.expect b/tests/28_strings.expect new file mode 100644 index 0000000..41ee1b2 --- /dev/null +++ b/tests/28_strings.expect @@ -0,0 +1,19 @@ +hello +gollo +1 +1 +-1 +5 +gollo! +6 +0 +0 +-19 +ollo! +lo! +NULL +grrrr! +grgrr! +1 +0 +-1 diff --git a/tests/Makefile b/tests/Makefile index 0a10352..1c5cd08 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -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