From 20f2d02f77cc7027a6d4dced7e91ed2931faa31b Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Fri, 22 Apr 2022 07:37:25 -0400 Subject: [PATCH] A bit of cleanup/simplification. --- src/object.c | 44 ++++++++++++++------------------------------ src/object.h | 2 -- src/pebblisp.c | 40 ++++++++++++++++++++++++---------------- src/plfunc.c | 27 ++++++++++++++++----------- 4 files changed, 54 insertions(+), 59 deletions(-) diff --git a/src/object.c b/src/object.c index 8321a7d..973e4c8 100644 --- a/src/object.c +++ b/src/object.c @@ -49,30 +49,14 @@ int listLength(const Object* listObj) return len; } -/** - * Returns a pointer to the Object at the given index in a given list - * @param listObj The list to fetch an Object from - * @param n The index to to fetch from the list - * @return A pointer to the Object, if it is found, or NULL on an error - */ -Object* itemAt(const Object* listObj, int n) -{ - FOR_POINTER_IN_LIST(listObj) { - if (n-- == 0) { - return POINTER; - } - } - return NULL; -} - /** * Returns a pointer to the last Object in the given list. - * @param listObj The list to find the tail of + * @param listObj The list to find the tail of. * @return A pointer to the last Object if it is found, or NULL on an error */ Object* tail(const Object* listObj) { - if (!listObj || !isListy(*listObj)) { + if (!isListy(*listObj)) { return NULL; } @@ -284,7 +268,7 @@ int stringNObj(struct string* s, const Object* obj) inflate(s, 128); if (obj->error->context && obj->error->context[0] != '\0') { appendf(s, "%s: %s", errorText[code], - obj->error->context); + obj->error->context); } else if (code >= 0 && code <= INDEX_PAST_END) { appendf(s, "%s", errorText[code]); } else { @@ -374,7 +358,7 @@ const char* getTypeName(const Object* obj) void _printObj(const Object* obj, int newline) { if (!obj) { - printf(newline ? "\n" : ""); + printf(newline ? "\n" : ""); return; } size_t length; @@ -421,18 +405,18 @@ void cleanObject(Object* target) if ((target->string[-1] -= 1) == 0) { free(target->string - 1); } - break; + return; case TYPE_LIST: case TYPE_SLIST: deleteList(target); - break; + return; case TYPE_HASH_TABLE: target->table->refs -= 1; if (!target->table->refs) { deleteTable(&target->table->table); free(target->table); } - break; + return; case TYPE_LAMBDA: lambdaRefs(target) -= 1; if (!lambdaRefs(target)) { @@ -441,29 +425,29 @@ void cleanObject(Object* target) free(lambdaDocs(target)); free(target->lambda); } - break; + return; case TYPE_STRUCT: for (int i = 0; i < getStructAt(target->structObject->definition)->fieldCount; i++) { cleanObject(&target->structObject->fields[i]); } free(target->structObject->fields); free(target->structObject); - break; + return; case TYPE_PROMISE: cleanPromise(target->promise); - break; + return; case TYPE_ERROR: #ifndef SIMPLE_ERRORS free(target->error->plContext); free(target->error->context); free(target->error); #endif - break; + return; case TYPE_OTHER: if (target->other->cleanup) { target->other->cleanup(target); } - break; + return; case TYPE_STATIC_FUNC: if ((target->staticF->refs -= 1) == 0) { for (int i = 0; i < target->staticF->argCount; i++) { @@ -471,11 +455,11 @@ void cleanObject(Object* target) } free(target->staticF); } - break; + return; case TYPE_BOOL: case TYPE_NUMBER: case TYPE_FUNC: - break; + return; } } diff --git a/src/object.h b/src/object.h index ac4949a..0ff4292 100644 --- a/src/object.h +++ b/src/object.h @@ -232,8 +232,6 @@ void deleteList(Object* dest); int listLength(const Object* listObj); -Object* itemAt(const Object* listObj, int n); - void cleanObject(Object* target); void printAndClean(Object* target); diff --git a/src/pebblisp.c b/src/pebblisp.c index ffe0964..d0d816f 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -230,7 +230,9 @@ Object evalList(const Object* obj, struct Environment* env) if (first_form->type == TYPE_SYMBOL) { if (strcmp(first_form->string, "if") == 0) { return evalIfArgs(first_form->forward, env); - } else if (strcmp(first_form->string, "fn") == 0) { + } + + if (strcmp(first_form->string, "fn") == 0) { Object* params = first_form->forward; Object* doc = NULL; Object* body = NULL; @@ -240,7 +242,9 @@ Object evalList(const Object* obj, struct Environment* env) body = twoParams ? params->forward->forward : params->forward; } return constructLambda(params, doc, body, env); - } else if (strcmp(first_form->string, "struct") == 0) { + } + + if (strcmp(first_form->string, "struct") == 0) { return evalStructArgs(first_form->forward, first_form->forward->forward, env); } @@ -370,19 +374,21 @@ Result parse(struct Slice* slices) r.obj.type = TYPE_SLIST; } return r; - } else if (token->text[0] == '(') { - return readSeq(rest); - } else { - Result r = parseAtom(token); - r.slices = &r.slices[1]; - return r; } + + if (token->text[0] == '(') { + return readSeq(rest); + } + + Result r = parseAtom(token); + r.slices = &r.slices[1]; + return r; } #ifndef NO_SUGAR -#define sugar(_desc, _code) _code +#define sugar(_code) _code #else -#define sugar(_desc, _code) ; +#define sugar(_code) do { } while (0); #endif Result readSeq(struct Slice* tokens) @@ -396,15 +402,17 @@ Result readSeq(struct Slice* tokens) return (Result) { res, rest }; } Result r = parse(tokens); - sugar("(? fil) => (? 'fil')" // or, - "(def yee 10) => (def 'yee' 10)", - if (forceString && r.obj.type == TYPE_SYMBOL) { - r.obj.type = TYPE_STRING; - } - ) if (r.obj.type == TYPE_ERROR) { return r; } + sugar( + // (? fil) => (? 'fil') // or, + // (def yee 10) => (def 'yee' 10) + // (set yee 10) => (set 'yee' 10) + if (forceString && r.obj.type == TYPE_SYMBOL) { + r.obj.type = TYPE_STRING; + } + ) nf_addToList(&res, cloneObject(r.obj)); tokens = r.slices; cleanObject(&r.obj); diff --git a/src/plfunc.c b/src/plfunc.c index f9e48d5..a9118d3 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -101,7 +101,14 @@ Object at(Object* params, unused int length, unused struct Environment* env) Object index = params[0]; Object list = params[1]; - const Object* found = itemAt(&list, index.number); + const Object* found = NULL; + int n = index.number; + FOR_POINTER_IN_LIST(&list) { + if (n-- == 0) { + found = POINTER; + break; + } + } if (found) { return cloneObject(*found); @@ -116,22 +123,20 @@ Object rest(Object* params, unused int length, unused struct Environment* env) Object list = params[0]; BuildListNamed(ret); - Object* l = &list; - FOR_POINTER_IN_LIST(l) { - if (POINTER == l->list) { - continue; + if (list.list) { + for (Object* _element = list.list->forward; _element != NULL; _element = _element->forward) { + addToList(ret, cloneObject(*POINTER)); } - addToList(ret, cloneObject(*POINTER)); } + return ret; } Object reverse(Object* params, unused int length, unused struct Environment* ignore2) { checkTypes(reverse) - Object _list = params[0]; + const Object* list = ¶ms[0]; - const Object* list = &_list; Object rev = listObject(); Object* tail = NULL; @@ -301,7 +306,8 @@ Object parseEvalO(Object* params, unused int length, struct Environment* env) case TYPE_STRING: return parseEval(text.string, env); default: - throw(CAN_ONLY_EVAL_STRINGS, "Tried to (eval) a %s, instead of a string or symbol list", getTypeName(&text)); + throw(CAN_ONLY_EVAL_STRINGS, "Tried to (eval) a %s, instead of a string or symbol list", + getTypeName(&text)); } } @@ -330,7 +336,6 @@ Object catObjects(Object* params, int length, unused struct Environment* env) return string; } - Object len(Object* params, unused int length, unused struct Environment* env) { checkTypes(len) @@ -382,7 +387,7 @@ int areEqual(const Object* obj1, const Object* obj2) const long n2 = obj2->number; if (bothAre(TYPE_STRING, obj1, obj2)) { - return !strcmp(obj1->string, obj2->string); + return obj1->string == obj2->string || strcmp(obj1->string, obj2->string) == 0; } if (bothAre(TYPE_LIST, obj1, obj2)) { return listEquality(obj1, obj2);