diff --git a/Makefile b/Makefile index f4d9869..d94c7a9 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ TARGET = picoc SRCS = picoc.c table.c lex.c parse.c expression.c heap.c type.c \ variable.c clibrary.c platform.c include.c \ platform/platform_unix.c platform/library_unix.c \ - cstdlib/stdio.c cstdlib/math.c + cstdlib/stdio.c cstdlib/math.c cstdlib/string.c OBJS := $(SRCS:%.c=%.o) all: depend $(TARGET) diff --git a/cstdlib/stdio.c b/cstdlib/stdio.c index 756883a..5026030 100644 --- a/cstdlib/stdio.c +++ b/cstdlib/stdio.c @@ -685,9 +685,14 @@ void StdioSetupFunc(void) VariableDefinePlatformVar(NULL, "stderr", FilePtrType, (union AnyValue *)&stderrValue, FALSE); /* define NULL, TRUE and FALSE */ - VariableDefinePlatformVar(NULL, "NULL", &IntType, (union AnyValue *)&ZeroValue, FALSE); - VariableDefinePlatformVar(NULL, "TRUE", &IntType, (union AnyValue *)&TRUEValue, FALSE); - VariableDefinePlatformVar(NULL, "FALSE", &IntType, (union AnyValue *)&ZeroValue, FALSE); + if (!VariableDefined(TableStrRegister("NULL"))) + VariableDefinePlatformVar(NULL, "NULL", &IntType, (union AnyValue *)&ZeroValue, FALSE); + + if (!VariableDefined(TableStrRegister("TRUE"))) + { + VariableDefinePlatformVar(NULL, "TRUE", &IntType, (union AnyValue *)&TRUEValue, FALSE); + VariableDefinePlatformVar(NULL, "FALSE", &IntType, (union AnyValue *)&ZeroValue, FALSE); + } } /* portability-related I/O calls */ diff --git a/include.c b/include.c index e42f157..6318de5 100644 --- a/include.c +++ b/include.c @@ -21,6 +21,10 @@ struct IncludeLibrary IncludeLibInfo[] = &MathSetupFunc, &MathFunctions, NULL }, + { "string.h", + &StringSetupFunc, + &StringFunctions, + NULL }, { NULL, NULL, NULL, NULL } }; diff --git a/picoc.h b/picoc.h index c07b228..94a99d7 100644 --- a/picoc.h +++ b/picoc.h @@ -421,4 +421,8 @@ void StdioSetupFunc(void); extern struct LibraryFunction MathFunctions[]; void MathSetupFunc(void); +/* string.c */ +extern struct LibraryFunction StringFunctions[]; +void StringSetupFunc(void); + #endif /* PICOC_H */ diff --git a/tests/28_strings.c b/tests/28_strings.c index 8ec7fc3..a4007af 100644 --- a/tests/28_strings.c +++ b/tests/28_strings.c @@ -1,15 +1,11 @@ #include +#include 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); @@ -29,7 +25,7 @@ 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')); +printf("%d\n", rindex(a, 'x') == NULL); memset(&a[1], 'r', 4); printf("%s\n", a); diff --git a/tests/28_strings.expect b/tests/28_strings.expect index 5e19c73..422c6d6 100644 --- a/tests/28_strings.expect +++ b/tests/28_strings.expect @@ -11,7 +11,7 @@ gollo! -1 ollo! lo! -NULL +1 grrrr! grgrr! 1 diff --git a/tests/29_array_address.c b/tests/29_array_address.c index b81ce89..d70e116 100644 --- a/tests/29_array_address.c +++ b/tests/29_array_address.c @@ -1,4 +1,5 @@ #include +#include char a[10]; strcpy(a, "abcdef");