From af65349d2766bcb5e2899cda9b19df46c95aa361 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Thu, 21 Apr 2022 11:43:07 -0400 Subject: [PATCH] Simplify copyList and inline some of prepend(). --- src/object.c | 37 +++++-------------------------------- src/object.h | 2 -- src/plfunc.c | 12 ++++++------ 3 files changed, 11 insertions(+), 40 deletions(-) diff --git a/src/object.c b/src/object.c index 683713e..dab217a 100644 --- a/src/object.c +++ b/src/object.c @@ -510,21 +510,6 @@ void deleteList(Object* dest) } } -void _copyList(Object* dest, const Object* src) -{ - FOR_POINTER_IN_LIST(src) { - if (isListy(*POINTER)) { - nf_addToList(dest, *POINTER); - tail(dest)->list = NULL; - _copyList(tail(dest), POINTER); - } else if (isStringy(*POINTER)) { - nf_addToList(dest, cloneString(*POINTER)); - } else { - nf_addToList(dest, cloneObject(*POINTER)); - } - } -} - /** * Does a deep copy of all items from `src` to `dest` * Does nothing if either param is NULL, or not a list type @@ -536,26 +521,14 @@ void _copyList(Object* dest, const Object* src) */ Object copyList(const Object* src) { - Object list = *src; - list.forward = NULL; - list.list = NULL; - _copyList(&list, src); + BuildListNamed(list); + FOR_POINTER_IN_LIST(src) { + addToList(list, cloneObject(*POINTER)); + } + list.type = src->type; return list; } -/** - * Does a deep copy of all items from `src` to the end of `dest` - * Does nothing if either param is NULL, or not a list type - * May recurse into lists in the list - * - * @param dest The list to copy to - * @param src The list to copy from - */ -void appendList(Object* dest, const Object* src) -{ - _copyList(dest, src); -} - /** * Returns a basic Object with NULL forward and the given type * @param type The type of Object to create diff --git a/src/object.h b/src/object.h index f21593c..b848af7 100644 --- a/src/object.h +++ b/src/object.h @@ -245,8 +245,6 @@ void printAndClean(Object* target); void allocObject(Object** spot, Object src); -void appendList(Object* dest, const Object* src); - int isNumber(Object test); int isListy(Object test); diff --git a/src/plfunc.c b/src/plfunc.c index c6e4ad9..f9e48d5 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -86,13 +86,13 @@ Object append(Object* params, unused int length, unused struct Environment* env) Object prepend(Object* params, unused int length, unused struct Environment* env) { checkTypes(prepend) - Object list = params[0]; - Object newElement = params[1]; + Object list = cloneObject(params[0]); + Object newElement = cloneObject(params[1]); - Object newList = listObject(); - nf_addToList(&newList, cloneObject(newElement)); - appendList(&newList, &list); - return newList; + Object* previousFirst = list.list; + allocObject(&list.list, newElement); + list.list->forward = previousFirst; + return list; } Object at(Object* params, unused int length, unused struct Environment* env)