Simplify copyList and inline some of prepend().

This commit is contained in:
Sage Vaillancourt 2022-04-21 11:43:07 -04:00 committed by Sage Vaillancourt
parent 9d2effe206
commit af65349d27
3 changed files with 11 additions and 40 deletions

View File

@ -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 a deep copy of all items from `src` to `dest`
* Does nothing if either param is NULL, or not a list type * 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 copyList(const Object* src)
{ {
Object list = *src; BuildListNamed(list);
list.forward = NULL; FOR_POINTER_IN_LIST(src) {
list.list = NULL; addToList(list, cloneObject(*POINTER));
_copyList(&list, src); }
list.type = src->type;
return list; 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 * Returns a basic Object with NULL forward and the given type
* @param type The type of Object to create * @param type The type of Object to create

View File

@ -245,8 +245,6 @@ void printAndClean(Object* target);
void allocObject(Object** spot, Object src); void allocObject(Object** spot, Object src);
void appendList(Object* dest, const Object* src);
int isNumber(Object test); int isNumber(Object test);
int isListy(Object test); int isListy(Object test);

View File

@ -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) Object prepend(Object* params, unused int length, unused struct Environment* env)
{ {
checkTypes(prepend) checkTypes(prepend)
Object list = params[0]; Object list = cloneObject(params[0]);
Object newElement = params[1]; Object newElement = cloneObject(params[1]);
Object newList = listObject(); Object* previousFirst = list.list;
nf_addToList(&newList, cloneObject(newElement)); allocObject(&list.list, newElement);
appendList(&newList, &list); list.list->forward = previousFirst;
return newList; return list;
} }
Object at(Object* params, unused int length, unused struct Environment* env) Object at(Object* params, unused int length, unused struct Environment* env)