From 5226640fb62ec11f8eaa15b8c79feae5e0a6cb11 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Sat, 26 Mar 2022 12:54:38 -0400 Subject: [PATCH] Some stringing cleanup and unused code removal. --- src/env.c | 26 ------- src/env.h | 1 - src/object.c | 195 ++++++++++--------------------------------------- src/object.h | 15 ---- src/pebblisp.c | 49 +------------ src/pebblisp.h | 2 - src/tokens.c | 5 -- src/web.c | 3 - 8 files changed, 44 insertions(+), 252 deletions(-) diff --git a/src/env.c b/src/env.c index 76db6a1..d96c325 100644 --- a/src/env.c +++ b/src/env.c @@ -52,7 +52,6 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms, } struct Environment env = { - .name = "lambdaEnv", .outer = outer, .strings = NULL, .objects = NULL, @@ -82,30 +81,6 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms, return env; } -// Determine if a given symbol exists in the given environment chain -// -// Modifies `env` to the environment that the symbol was found in, only if the -// symbol was found -// -// Returns the index of the symbol in the environment, if found. Otherwise, -1 -int findInEnv(struct Environment** env, const char* name) -{ - struct Environment* temp_env = *env; - while (temp_env) { - for (int i = 0; i < temp_env->size; i++) { - if (temp_env->strings[i] == NULL) { - break; - } - if (strcmp(temp_env->strings[i], name) == 0) { - *env = temp_env; - return i; - } - } - temp_env = temp_env->outer; - } - return -1; -} - // TODO Maybe extend environment using a new outer, instead of realloc void addToEnv(struct Environment* env, const char* name, const Object obj) { @@ -401,7 +376,6 @@ struct Environment defaultEnv() char size = MAX_ENV_ELM; struct Environment e = { - .name = "defaultEnv", .outer = NULL, .strings = strings, .objects = objects, diff --git a/src/env.h b/src/env.h index fe7eec2..b1bb2c2 100644 --- a/src/env.h +++ b/src/env.h @@ -15,7 +15,6 @@ struct Environment { struct StructDef* structDefs; int refs; - const char* name; }; struct Environment* global(); diff --git a/src/object.c b/src/object.c index 0b61491..d48e05a 100644 --- a/src/object.c +++ b/src/object.c @@ -155,59 +155,6 @@ void allocObject(Object** spot, const Object src) (*spot)->forward = NULL; } -/** - * Inserts a given Object into a list at the given index. - * Immediately returns if `dest` is NULL or not a list type - * Adds to the end of the list if the index is higher than the list length - - * @param dest The list to insert into - * @param ind The index to insert at - * @param src The Object to add to the list - - * @warning UNTESTED - */ -void insertIntoList(Object* dest, int ind, const Object src) -{ - if (!dest || !isListy(*dest)) { - return; - } - - // Merely append, when possible - if (ind >= listLength(dest)) { - nf_addToList(dest, src); - return; - } - - // The Objects to preceed and follow the new one - Object* beforeNew = itemAt(dest, ind - 1); - Object* afterNew = beforeNew->forward; - - // Replace the `before` Object's pointer - allocObject(&beforeNew->forward, src); - beforeNew->forward->forward = afterNew; -} - -/** - * Replace an Object in a list at a given index with a given Object - * Attempts to clean the replaced Object before adding the new one - * Immediately returns if `list` is NULL or not a list type - * - * @param list The list Object to replace an element of - * @param i The index of the element to replace - * @param src The Object to copy into the list - */ -void replaceListing(Object* list, int i, const Object src) -{ - if (!list || isListy(*list)) { - return; - } - Object* replace = itemAt(list, i); - Object* oldForward = replace->forward; - cleanObject(replace); - *replace = src; - replace->forward = oldForward; -} - /** * Adds an Object to the end of a list * Does nothing if `dest` is NULL or not a list type @@ -230,8 +177,6 @@ void nf_addToList(Object* dest, const Object src) #ifndef SIMPLE_ERRORS static const char* errorText[] = {"MISMATCHED_PARENS", - "BAD_LIST_OF_SYMBOL_STRINGS", - "TYPE_LIST_NOT_CAUGHT", "NULL_ENV", "EMPTY_ENV", "BUILT_IN_NOT_FOUND", @@ -241,7 +186,6 @@ static const char* errorText[] = {"MISMATCHED_PARENS", "LAMBDA_ARGS_NOT_LIST", "DID_NOT_FIND_SYMBOL", "BAD_TYPE", - "UNEXPECTED_FORM", "LISTS_NOT_SAME_SIZE", "BAD_NUMBER", "UNSUPPORTED_NUMBER_TYPE", @@ -267,21 +211,17 @@ int stringList(char* dest, const Object* obj) if (!dest || !isListy(*obj)) { return 0; } + const char* initial = dest; - dest[0] = '('; - dest[1] = '\0'; - int length = 1; + dest += sprintf(dest, "("); FOR_POINTER_IN_LIST(obj) { - strcat(dest, " "); - length += 1; char tok[90] = ""; - length += stringObj(tok, POINTER); - strcat(dest, tok); + stringObj(tok, POINTER); + dest += sprintf(dest, " %s", tok); } - strcat(dest, " )"); - length += 2; - return length; + dest += sprintf(dest, " )"); + return dest - initial; } /** @@ -291,40 +231,28 @@ int stringList(char* dest, const Object* obj) int stringStruct(char* dest, const Object* obj) { struct StructObject* so = obj->structObject; - dest[0] = '{'; - dest[1] = '\0'; - int length = 1; + const char* initial = dest; + dest += sprintf(dest, "{"); for (int i = 0; i < global()->structDefs[so->definition].fieldCount; i++) { - strcat(dest, " "); - strcat(dest, global()->structDefs[so->definition].names[i]); - strcat(dest, ": "); - length += 3; + dest += sprintf(dest, " %s: ", global()->structDefs[so->definition].names[i]); int isString = so->fields[i].type == TYPE_STRING; if (isString) { - strcat(dest, "\""); + dest += sprintf(dest, "\""); } char tok[90] = ""; - length += stringObj(tok, &so->fields[i]); - strcat(dest, tok); + stringObj(tok, &so->fields[i]); + dest += sprintf(dest, "%s", tok); if (isString) { - strcat(dest, "\""); - length += 1; + dest += sprintf(dest, "\""); } - strcat(dest, ","); - length += 1; + dest += sprintf(dest, ","); } - int i = 0; - while (dest[i]) { - i++; - } - dest[i - 1] = ' '; - dest[i + 0] = '}'; - length += 2; - dest[i + 1] = '\0'; - return length; + dest--; + dest += sprintf(dest, " }"); + return dest - initial; } /** * Creates a string from a given Object @@ -350,83 +278,73 @@ int stringStruct(char* dest, const Object* obj) int stringNObj(char* dest, const Object* obj, const size_t len) { - int size = 0; + const char* initial = dest; if (!dest || !obj) { return 0; } switch (obj->type) { case TYPE_NUMBER: - size += stringf(dest, len, "%d", obj->number); + dest += stringf(dest, len, "%d", obj->number); break; case TYPE_BOOL: - size += stringf(dest, len, "%s", obj->number ? "T" : "F"); + dest += stringf(dest, len, "%s", obj->number ? "T" : "F"); break; case TYPE_STRING: - size += stringf(dest, len, "%s", obj->string); + dest += stringf(dest, len, "%s", obj->string); break; case TYPE_SYMBOL: - size += stringf(dest, len, "`%s`", obj->string); + dest += stringf(dest, len, "`%s`", obj->string); break; case TYPE_STRUCT: //snprintf(dest, len, "{%s}", obj->structObject->definition->names[0]); - size += stringStruct(dest, obj); + dest += stringStruct(dest, obj); break; case TYPE_LIST: case TYPE_SLIST: - size += stringList(dest, obj); + dest += stringList(dest, obj); break; case TYPE_ERROR: { int code = getErrorCode(*obj); #ifdef SIMPLE_ERRORS - size += stringf(dest, len, "E[%d]", (int)code); + dest += stringf(dest, len, "E[%d]", (int)code); #else if (obj->error->context && obj->error->context[0] != '\0') { - size += stringf(dest, len, "'%s': %s", errorText[code], + dest += stringf(dest, len, "'%s': %s", errorText[code], obj->error->context); } else if (code >= 0 && code <= INDEX_PAST_END) { - size += stringf(dest, len, "%s", errorText[code]); + dest += stringf(dest, len, "%s", errorText[code]); } else { - size += stringf(dest, len, "BROKEN ERROR CODE: %d", code); + dest += stringf(dest, len, "BROKEN ERROR CODE: %d", code); } #endif break; } case TYPE_FUNC: - size += stringf(dest, len, "F%d", obj->number); + dest += stringf(dest, len, "F%d", obj->number); break; case TYPE_LAMBDA: { #ifdef STANDALONE - int n = stringf(dest, len, "\\x%d", obj->number); - dest += n; - size += n; - n = stringNObj(dest, &obj->lambda->params, len); - dest += n; - size += n; - strcat(dest, " -> "); - dest += 4; - size += 4; - n = stringNObj(dest, &obj->lambda->body, len); - size += n; - dest += n; - dest += strlen(dest); - strcat(dest, ">"); - size += 1; + dest += stringf(dest, len, "\\x%d", obj->number); + dest += stringNObj(dest, &obj->lambda->params, len); + dest += sprintf(dest, " -> "); + dest += stringNObj(dest, &obj->lambda->body, len); + dest += sprintf(dest, ">"); #else - size += stringf(dest, len, "\\x%d", obj->number); + dest += stringf(dest, len, "\\x%d", obj->number); #endif break; } case TYPE_OTHER: - size += stringf(dest, len, "%p", obj->other->data); + dest += stringf(dest, len, "%p", obj->other->data); break; } if (!isValidType(*obj)) { - size += stringf(dest, len, "BAD_TYPE(%d) X%d", obj->type, obj->number); + dest += stringf(dest, len, "BAD_TYPE(%d) X%d", obj->type, obj->number); } - return size; + return dest - initial; } int stringObj(char* dest, const Object* obj) @@ -538,15 +456,6 @@ void nestedPrintList(const Object* list, int newline) } } -/** - * Prints each Object in a given list, surrounded by parentheses - * @param list The list Object to print - */ -void printList(const Object* list) -{ - nestedPrintList(list, 1); -} - #endif /** @@ -632,10 +541,6 @@ void printAndClean(Object* target) */ void deleteList(Object* dest) { - if (!dest) { - return; - } - if (!isListy(*dest)) { printf("Tried to delete something other than a list\n"); return; @@ -821,10 +726,11 @@ inline Object cloneObject(const Object src) return cloneString(src); case TYPE_ERROR: return errorWithContext(getErrorCode(src), src.error->context); + case TYPE_OTHER: + return cloneObject(src); case TYPE_BOOL: case TYPE_NUMBER: case TYPE_FUNC: - case TYPE_OTHER: return src; } @@ -922,16 +828,6 @@ inline int bothAre(const enum Type type, const Object* obj1, const Object* obj2) return (obj1->type == type) && (obj2->type == type); } -inline int eitherIs(const enum Type type, const Object* o1, const Object* o2) -{ - return (o1->type == type) || (o2->type == type); -} - -inline int eitherIsNot(const enum Type type, const Object* o1, const Object* o2) -{ - return (o1->type != type) || (o2->type != type); -} - inline int areSameType(const Object* obj1, const Object* obj2) { return obj1->type == obj2->type; @@ -972,10 +868,7 @@ inline enum errorCode getErrorCode(const Object obj) inline void errorAddContext(Object* o, const char* context, int lineNo, const char* fileName) { - // printf("o: %p\n", o); - // printf("o->error: %s\n", o->error); o->error->context = calloc(sizeof(char), RESULT_LENGTH); - // printf("context: %p\n", context); sprintf(o->error->context, "%s %s:%d", context, fileName, lineNo); } @@ -996,11 +889,3 @@ struct Error noError() err.context = NULL; return err; } - -inline Object toBool(const Object test) -{ - if (test.number == 0) { - return boolObject(0); - } - return boolObject(1); -} diff --git a/src/object.h b/src/object.h index c19a04d..c51e9e7 100644 --- a/src/object.h +++ b/src/object.h @@ -42,8 +42,6 @@ enum errorCode { MISMATCHED_PARENS, - BAD_LIST_OF_SYMBOL_STRINGS, - TYPE_LIST_NOT_CAUGHT, NULL_ENV, EMPTY_ENV, BUILT_IN_NOT_FOUND, @@ -53,7 +51,6 @@ enum errorCode { LAMBDA_ARGS_NOT_LIST, DID_NOT_FIND_SYMBOL, BAD_TYPE, - UNEXPECTED_FORM, LISTS_NOT_SAME_SIZE, BAD_NUMBER, UNSUPPORTED_NUMBER_TYPE, @@ -144,8 +141,6 @@ int stringNObj(char* dest, const Object* obj, size_t len); int stringObj(char* dest, const Object* obj); -void printList(const Object* list); - void printType(const Object* obj); void printObj(const Object* obj); @@ -154,16 +149,10 @@ void _printObj(const Object* obj, int newline); void debugObj(const Object* obj); -void printErr(const Object* obj); - int isEmpty(const Object* obj); Object* tail(const Object* listObj); -void insertIntoList(Object* dest, int ind, Object src); - -void replaceListing(Object* list, int i, Object src); - void nf_addToList(Object* dest, Object src); void deleteList(Object* dest); @@ -192,10 +181,6 @@ int isError(Object obj, enum errorCode err); int bothAre(enum Type type, const Object* obj1, const Object* obj2); -int eitherIsNot(const enum Type type, const Object* o1, const Object* o2); - -int eitherIs(enum Type type, const Object* obj1, const Object* obj2); - int areSameType(const Object* obj1, const Object* obj2); Object cloneList(Object src); diff --git a/src/pebblisp.c b/src/pebblisp.c index 4edb681..0061833 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -58,15 +58,6 @@ Object evalDefArgs(const Object* symbol, const Object* value, return cloneObject(*symbol); } -void printStructDef(const struct StructDef* def) -{ - printf("%s: {\n", def->name); - for (int i = 0; i < def->fieldCount; i++) { - printf(" %s,\n", def->names[i]); - } - printf("}\n"); -} - /** * Add a struct to the environment with a given name and fields. * @@ -107,7 +98,6 @@ Object evalStructArgs(const Object* symbol, const Object* fields, struct Environ int prevCapacity = env->structCapacity; env->structCapacity *= 2; env->structDefs = malloc(sizeof(struct StructDef) * env->structCapacity); - printf("Can malloc\n"); for (int i = 0; i < prevCapacity; i++) { env->structDefs[i] = prev[i]; } @@ -245,20 +235,8 @@ Object simpleFuncEval(const Object func, Object arg1, Object arg2, struct Enviro Object listEvalFunc(const Object* list, const Object* function, const int length, struct Environment* env) { - //printf("Using new funcEval.\n"); - - //printf("list: "); - //printObj(list); - //printf("\nfunction:"); - //printObj(function); - //printf("\n"); Object rest[length]; evalForms(rest, list->list->forward, env); - // for (int i = 0; i < length; i++) { - // printf("rest[%d]: ", i); - // printObj(&rest[i]); - // printf("\n"); - // } Object result = function->func(rest, length, env); for (int i = 0; i < length; i++) { @@ -408,25 +386,6 @@ void copySlice(char* dest, struct Slice* src) dest[(int) src->length] = '\0'; } -void debugSlice(struct Slice* s) -{ -#ifdef DEBUG - if (!s) { - printf("NULL SLICE\n"); - return; - } - printf("Debug Slice\n text:'"); - for (int i = 0; i < s->length; i++) { - printf("%c", s->text[i]); - if (s->text[i] == '\0') { - printf("NULLCHAR\n"); - } - } - printf("'\n"); - printf(" length: %d\n", s->length); -#endif -} - Object possessive(Object* params, int length, struct Environment* env) { Object structo = params[0]; @@ -497,9 +456,9 @@ Result readSeq(struct Slice* tokens) } Result r = parse(tokens); sugar("(? fil) => (? 'fil')", - if (isHelp && r.obj.type == TYPE_SYMBOL) { - r.obj.type = TYPE_STRING; - } + if (isHelp && r.obj.type == TYPE_SYMBOL) { + r.obj.type = TYPE_STRING; + } ) if (r.obj.type == TYPE_ERROR) { return r; @@ -693,7 +652,7 @@ int _readFile(FILE* input, struct Environment* env) if (line[i] == ';') { break; } else { - int j = 0; + int j; for (j = i; j < LINE_MAX; j++) { if (line[j] == '"') { isQuote = !isQuote; diff --git a/src/pebblisp.h b/src/pebblisp.h index 51e00c5..e77191b 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -58,8 +58,6 @@ Object evalLambdaArgs(const Object* arg_forms, struct Environment* env); Object listEvalLambda(Object* lambda, const Object* remaining, struct Environment* env); -void debugSlice(struct Slice* s); - Object simpleFuncEval(Object func, Object arg1, Object arg2, struct Environment* env); #ifdef STANDALONE diff --git a/src/tokens.c b/src/tokens.c index 5dd0951..9257634 100644 --- a/src/tokens.c +++ b/src/tokens.c @@ -43,11 +43,6 @@ int isWhitespace(const char c) return c == ' ' || c == '\t' || c == '\n'; } -int notWhitespace(const char c) -{ - return !isWhitespace(c); -} - struct Slice* nf_tokenize(const char* input, struct Error* err) { if (!input) { diff --git a/src/web.c b/src/web.c index 627ecd2..634c869 100644 --- a/src/web.c +++ b/src/web.c @@ -135,9 +135,6 @@ int start(int port) return 1; } - // (void) getchar(); - - // MHD_stop_daemon(daemon); return 0; }