From d2a0abeff3d6a7b1c72f9c5906a110075f24295e Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Thu, 31 Mar 2022 11:41:02 -0400 Subject: [PATCH] Cleaner and more optimized (cat) implementation. stringObj() will now _add_ to its length parameter. More global scalloc and smalloc. Bit of Makefile re-org. --- src/Makefile | 18 +++++++------- src/object.c | 7 +++++- src/object.h | 12 ++++++++++ src/plfunc.c | 68 +++++++++++++++++++--------------------------------- 4 files changed, 51 insertions(+), 54 deletions(-) diff --git a/src/Makefile b/src/Makefile index 1fd09ee..598a053 100644 --- a/src/Makefile +++ b/src/Makefile @@ -12,21 +12,21 @@ mkfile_dir := $(dir $(mkfile_path)) GCC_ARGS ?= -g -Wall -o $(exe) -D WEBSERVER -D STANDALONE -DSCRIPTDIR=\"$(SCRIPTDIR)\" GCC_COM ?= gcc -O0 $(GCC_ARGS) -release: - gcc -O3 $(GCC_ARGS) $(file_libs) && strip ./$(exe) && echo && ./tests.sh +all: notest + echo && ./tests.sh notest: $(GCC_COM) $(file_libs) -all: notest +val: notest + ./tests.sh -val + +release_notest: + gcc -O3 $(GCC_ARGS) $(file_libs) && strip ./$(exe) + +release: release_notest echo && ./tests.sh -local: - gcc -g -O0 -Wall -o $(exe) -D STANDALONE -DSCRIPTDIR=\"$(mkfile_dir)/examples\" $(file_libs) - -val: - $(GCC_COM) $(file_libs) && ./tests.sh -val - debug: $(GCC_COM) -D DEBUG $(file_libs) diff --git a/src/object.c b/src/object.c index 10f7997..ef4dceb 100644 --- a/src/object.c +++ b/src/object.c @@ -5,6 +5,7 @@ #include #include +#ifdef STANDALONE size_t bytes = 0; size_t getBytes() @@ -12,6 +13,9 @@ size_t getBytes() return bytes; } +#undef malloc +#undef calloc + void* smalloc(size_t size) { bytes += size; @@ -27,6 +31,7 @@ void* scalloc(size_t size, size_t count) #define malloc(x) smalloc(x) #define calloc(x, y) scalloc(x, y) +#endif /** * Returns the length of a given list Object @@ -313,7 +318,7 @@ char* stringObj(const Object* obj, size_t* length) }; s.allocPoint[0] = '\0'; stringNObj(&s, obj); - *length = s.cursor - s.allocPoint; + *length += s.cursor - s.allocPoint; return s.allocPoint; } diff --git a/src/object.h b/src/object.h index 55aa05c..3e0c955 100644 --- a/src/object.h +++ b/src/object.h @@ -134,6 +134,11 @@ struct string { size_t capacity; }; +/** + * Returns a dynamically-sized string representation of the given object. + * + * _Adds_ the length of the new string to the `length` parameter. + */ char* stringObj(const Object* obj, size_t* length); void printType(const Object* obj); @@ -240,4 +245,11 @@ int getAllocations(); size_t getBytes(); +void* scalloc(size_t size, size_t count); + +void* smalloc(size_t size); + +#define malloc(x) smalloc(x) +#define calloc(x, y) scalloc(x, y) + #endif diff --git a/src/plfunc.c b/src/plfunc.c index 5b43d9a..865e952 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -224,46 +224,28 @@ Object parseEvalO(Object* params, int length, struct Environment* env) } } -#ifdef STANDALONE -#define CAT_MAX 1024 -#else -#define CAT_MAX 64 -#endif - -Object _catObjects(Object obj1, Object obj2, struct Environment* env) -{ - Object evalObj1 = eval(&obj1, env); - Object evalObj2 = eval(&obj2, env); - - size_t length1; - size_t length2; - char* str1 = stringObj(&evalObj1, &length1); - char* str2 = stringObj(&evalObj2, &length2); - - cleanObject(&evalObj1); - cleanObject(&evalObj2); - - Object o = withLen(length1 + length2, TYPE_STRING); - sprintf(o.string, "%s%s", str1, str2); - free(str1); - free(str2); - - return o; -} - Object catObjects(Object* params, int length, struct Environment* env) { checkTypes(catObjects) - Object string = stringFromSlice("", 0); if (length == 0) { - return string; + return stringFromSlice("", 0); } + + char* strings[length]; + size_t totalLength = 0; for (int i = 0; i < length; i++) { - Object newString = _catObjects(string, params[i], env); - cleanObject(&string); - string = newString; + strings[i] = stringObj(¶ms[i], &totalLength); } + + Object string = withLen(totalLength, TYPE_STRING); + string.string[0] = '\0'; + + for (int i = 0; i < length; i++) { + strcat(string.string, strings[i]); + free(strings[i]); + } + return string; } @@ -332,14 +314,14 @@ Object equ(Object* params, int length, struct Environment* env) if (length < 2) { return errorObject(NOT_ENOUGH_ARGUMENTS); } - int bool = 1; + for (int i = 0; i < length - 1; i++) { if (!areEqual(¶ms[i], ¶ms[i + 1])) { - bool = 0; - break; + return falseObject(); } } - return boolObject(bool); + + return trueObject(); } Object or(Object* params, int length, struct Environment* env) @@ -347,14 +329,13 @@ Object or(Object* params, int length, struct Environment* env) if (length < 2) { return errorObject(NOT_ENOUGH_ARGUMENTS); } - int bool = 0; + for (int i = 0; i < length - 1; i++) { if (params[i].number || params[i + 1].number) { - bool = 1; - break; + return trueObject(); } } - return boolObject(bool); + return falseObject(); } #define BASIC_COMPARISON(NAME, OP)\ @@ -363,14 +344,13 @@ Object NAME(Object* params, int length, struct Environment* env) \ if (length < 2) { \ return errorObject(NOT_ENOUGH_ARGUMENTS); \ } \ - int bool = 1; \ + \ for (int i = 0; i < length - 1; i++) { \ if (!(params[i].number OP params[i + 1].number)) { \ - bool = 0; \ - break; \ + return falseObject(); \ } \ } \ - return boolObject(bool); \ + return trueObject(); \ } BASIC_COMPARISON(greaterThan, >)