From f0a8f6ca439431fe27b49b821e14a14018b899ec Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 23 May 2020 18:01:15 +0100 Subject: [PATCH] Start using switches to warn on unhandled types --- src/Makefile | 8 +-- src/object.c | 189 ++++++++++++++++++++++++++++++++++++--------------- src/object.h | 25 +------ 3 files changed, 142 insertions(+), 80 deletions(-) diff --git a/src/Makefile b/src/Makefile index 277ac35..61c616f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,16 +2,16 @@ files = pebblisp.c tokens.c object.c env.c exe = pebblisp all: - gcc -g -O0 -o $(exe) -D STANDALONE $(files) && ./tests.sh + gcc -g -O0 -Wall -o $(exe) -D STANDALONE $(files) && ./tests.sh notest: - gcc -g -O0 -o $(exe) -D STANDALONE $(files) + gcc -g -O0 -Wall -o $(exe) -D STANDALONE $(files) val: - gcc -g -O0 -o $(exe) -D STANDALONE $(files) && ./tests.sh -val + gcc -g -O0 -Wall -o $(exe) -D STANDALONE $(files) && ./tests.sh -val debug: - gcc -g -O0 -o $(exe) -D STANDALONE -D DEBUG $(files) + gcc -g -O0 -Wall -o $(exe) -D STANDALONE -D DEBUG $(files) run: ./$(exe) diff --git a/src/object.c b/src/object.c index 6be4b04..bd02a2f 100644 --- a/src/object.c +++ b/src/object.c @@ -95,14 +95,15 @@ inline int isEmpty(const Object *obj) case TYPE_LAMBDA: return obj->lambda == NULL; case TYPE_SYMBOL: - return obj->string == NULL; + case TYPE_STRING: + return obj->string == NULL || obj->string[0] == '\0'; case TYPE_FUNC: return obj->func == NULL; case TYPE_ERROR: return obj->err == 0; - default: - return 0; } + + return 0; } /** @@ -189,6 +190,26 @@ void nf_addToList(Object *dest, const Object src) allocObject(&tail(dest)->forward, src); } +static const char *errorText[] = { + "MISMATCHED_PARENS", + "BAD_LIST_OF_SYMBOL_STRINGS", + "TYPE_LIST_NOT_CAUGHT", + "NULL_ENV", + "EMPTY_ENV", + "BUILT_IN_NOT_FOUND", + "NULL_PARSE", + "NULL_LAMBDA_LIST", + "NULL_MAP_ARGS", + "LAMBDA_ARGS_NOT_LIST", + "DID_NOT_FIND_SYMBOL", + "BAD_TYPE", + "UNEXPECTED_FORM", + "LISTS_NOT_SAME_SIZE", + "SYMBOLS_CANT_START_WITH_DIGITS", + "UNSUPPORTED_NUMBER_TYPE", + "NOT_A_SYMBOL" +}; + /** * Prints out the error of a given error Object * Doesn't print anything if `obj` is NULL or not an error type @@ -247,22 +268,33 @@ char* stringObj(char *dest, const Object *obj) if(!dest || !obj) return NULL; - const Type t = obj->type; - - if(t == TYPE_NUMBER) { - snprintf(dest, RESULT_LENGTH, "%d", obj->number); - } else if(t == TYPE_BOOL) { - snprintf(dest, RESULT_LENGTH, "%s", obj->number ? "T" : "F"); - } else if(t == TYPE_STRING || t == TYPE_SYMBOL) { - snprintf(dest, RESULT_LENGTH, "%s", obj->string); - } else if(t == TYPE_LIST) { - stringList(dest, obj); - } else if(t == TYPE_ERROR) { - snprintf(dest, RESULT_LENGTH, "E%d", obj->err); - } else { - snprintf(dest, RESULT_LENGTH, "X%d", obj->number); + switch(obj->type) { + case TYPE_NUMBER: + snprintf(dest, RESULT_LENGTH, "%d", obj->number); + break; + case TYPE_BOOL: + snprintf(dest, RESULT_LENGTH, "%s", obj->number ? "T" : "F"); + break; + case TYPE_STRING: + snprintf(dest, RESULT_LENGTH, "%s", obj->string); + break; + case TYPE_SYMBOL: + snprintf(dest, RESULT_LENGTH, "`%s`", obj->string); + break; + case TYPE_LIST: + stringList(dest, obj); + break; + case TYPE_ERROR: + snprintf(dest, RESULT_LENGTH, "E%d", obj->err); + break; + case TYPE_FUNC: + case TYPE_LAMBDA: + snprintf(dest, RESULT_LENGTH, "X%d", obj->number); } + if(!isValidType(*obj)) + snprintf(dest, RESULT_LENGTH, "BAD_TYPE(%d) X%d", obj->type, obj->number); + return dest; } @@ -281,27 +313,39 @@ void debugObj(const Object *obj) void _printList(const Object *list, int newline); void _printObj(const Object *obj, int newline) { - if(obj->type == TYPE_NUMBER) { - printd("TYPE_NUMBER"); - } else if(obj->type == TYPE_BOOL) { - printd("TYPE_BOOL"); - } else if(obj->type == TYPE_LIST) { - printd("TYPE_LIST\n"); - } else if(obj->type == TYPE_FUNC) { - printd("TYPE_FUNC"); - } else if(obj->type == TYPE_SYMBOL) { - printd("TYPE_SYMBOL"); - } else if(obj->type == TYPE_LAMBDA) { - printd("TYPE_LAMBDA Params:\n"); - printObj(&obj->lambda->params); - printd("Lambda Body: \n"); - printObj(&obj->lambda->body); - return; - } else if(obj->type == TYPE_ERROR) { - printd("TYPE_ERROR: "); - } else { - printd("TYPE_OTHER (as int)"); + switch(obj->type) { + case TYPE_NUMBER: + printd("TYPE_NUMBER"); + break; + case TYPE_BOOL: + printd("TYPE_BOOL"); + break; + case TYPE_LIST: + printd("TYPE_LIST\n"); + break; + case TYPE_FUNC: + printd("TYPE_FUNC"); + break; + case TYPE_SYMBOL: + printd("TYPE_SYMBOL"); + break; + case TYPE_STRING: + printd("TYPE_STRING"); + break; + case TYPE_LAMBDA: + printd("TYPE_LAMBDA Params:\n"); + printObj(&obj->lambda->params); + printd("Lambda Body: \n"); + printObj(&obj->lambda->body); + return; + case TYPE_ERROR: + printd("TYPE_ERROR: "); + break; } + + if(!isValidType(*obj)) + printd("TYPE_OTHER (as int)"); + char temp[100] = ""; stringObj(temp, obj); if(newline) @@ -360,17 +404,27 @@ void cleanObject(Object *target) if(target == NULL) return; - const Type t = target->type; - if(t == TYPE_STRING || t == TYPE_SYMBOL) { - free(target->string); - target->string = NULL; - } else if(t == TYPE_LIST) { - deleteList(target); - } else if(t == TYPE_LAMBDA) { - cleanObject(&target->lambda->params); - cleanObject(&target->lambda->body); - free(target->lambda); + switch(target->type) { + case TYPE_STRING: + case TYPE_SYMBOL: + free(target->string); + target->string = NULL; + break; + case TYPE_LIST: + deleteList(target); + break; + case TYPE_LAMBDA: + cleanObject(&target->lambda->params); + cleanObject(&target->lambda->body); + free(target->lambda); + break; + case TYPE_BOOL: + case TYPE_NUMBER: + case TYPE_FUNC: + case TYPE_ERROR: + break; } + target->forward = NULL; } @@ -493,9 +547,26 @@ inline Object startList(const Object start) return list; } -inline int isStringy(const Object src) +inline int isStringy(const Object test) { - return src.type == TYPE_STRING || src.type == TYPE_SYMBOL; + return test.type == TYPE_STRING || test.type == TYPE_SYMBOL; +} + +inline int isValidType(const Object test) +{ + switch(test.type) { + case TYPE_NUMBER: + case TYPE_BOOL: + case TYPE_LIST: + case TYPE_FUNC: + case TYPE_SYMBOL: + case TYPE_LAMBDA: + case TYPE_STRING: + case TYPE_ERROR: + return 1; + } + + return 0; } /** @@ -525,10 +596,22 @@ inline Object cloneList(const Object src) inline Object cloneObject(const Object src) { - return src.type == TYPE_LIST? cloneList(src) : - src.type == TYPE_LAMBDA? cloneLambda(src) : - isStringy(src)? cloneString(src) : - src; + switch(src.type) { + case TYPE_LIST: + return cloneList(src); + case TYPE_LAMBDA: + return cloneLambda(src); + case TYPE_STRING: + case TYPE_SYMBOL: + return cloneString(src); + case TYPE_BOOL: + case TYPE_NUMBER: + case TYPE_FUNC: + case TYPE_ERROR: + ; // Fall through to plain return + } + + return src; } inline Object numberObject(int num) diff --git a/src/object.h b/src/object.h index 90740da..b442ab8 100644 --- a/src/object.h +++ b/src/object.h @@ -39,28 +39,6 @@ enum errorCode { NOT_A_SYMBOL }; -//#ifdef STANDALONE -static const char *errorText[] = { - "MISMATCHED_PARENS", - "BAD_LIST_OF_SYMBOL_STRINGS", - "TYPE_LIST_NOT_CAUGHT", - "NULL_ENV", - "EMPTY_ENV", - "BUILT_IN_NOT_FOUND", - "NULL_PARSE", - "NULL_LAMBDA_LIST", - "NULL_MAP_ARGS", - "LAMBDA_ARGS_NOT_LIST", - "DID_NOT_FIND_SYMBOL", - "BAD_TYPE", - "UNEXPECTED_FORM", - "LISTS_NOT_SAME_SIZE", - "SYMBOLS_CANT_START_WITH_DIGITS", - "UNSUPPORTED_NUMBER_TYPE", - "NOT_A_SYMBOL" -}; -//#endif - #define MALLOC_FLAG 64 typedef enum Type { @@ -121,7 +99,8 @@ void printAndClean(Object *target); void allocObject(Object **spot, const Object src); void appendList(Object *dest, const Object *src); -int isStringy(const Object src); +int isStringy(const Object test); +int isValidType(const Object test); Object cloneList(const Object src); Object cloneString(Object obj);