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 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

View File

@ -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);

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)
{
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)