Simplify several functions.

Namely, printObj() and _copyList().
Remove separate cloneList() function.
This commit is contained in:
Sage Vaillancourt 2022-03-27 00:47:20 -04:00
parent b715b2116a
commit 69cf9212d7
3 changed files with 23 additions and 37 deletions

View File

@ -313,7 +313,9 @@ int stringNObj(char* dest, const Object* obj, const size_t len)
break;
case TYPE_LAMBDA: {
#ifdef STANDALONE
#ifdef DEBUG
dest += stringf(dest, len, "\\x%d", obj->number);
#endif
dest += stringNObj(dest, &obj->lambda->params, len);
dest += sprintf(dest, " -> ");
dest += stringNObj(dest, &obj->lambda->body, len);
@ -394,12 +396,6 @@ void _printObj(const Object* obj, int newline)
printf(newline ? "\n" : "");
return;
}
if (obj->type == TYPE_LAMBDA) {
printObj(&obj->lambda->params);
printf("->");
printObj(&obj->lambda->body);
return;
}
char temp[1024] = "";
stringNObj(temp, obj, 1024);
if (newline) {
@ -518,17 +514,13 @@ void deleteList(Object* dest)
}
}
void _copyList(Object* dest, const Object* src, int delete)
void _copyList(Object* dest, const Object* src)
{
if (delete) {
deleteList(dest);
}
FOR_POINTER_IN_LIST(src) {
if (isListy(*POINTER)) {
nf_addToList(dest, *POINTER);
tail(dest)->list = NULL;
_copyList(tail(dest), POINTER, 0);
_copyList(tail(dest), POINTER);
} else if (isStringy(*POINTER)) {
nf_addToList(dest, cloneString(*POINTER));
} else {
@ -546,9 +538,13 @@ void _copyList(Object* dest, const Object* src, int delete)
* @param dest The list to copy to
* @param src The list to copy from
*/
void copyList(Object* dest, const Object* src)
Object copyList(const Object* src)
{
_copyList(dest, src, 1);
Object list = *src;
list.forward = NULL;
list.list = NULL;
_copyList(&list, src);
return list;
}
/**
@ -561,7 +557,7 @@ void copyList(Object* dest, const Object* src)
*/
void appendList(Object* dest, const Object* src)
{
_copyList(dest, src, 0);
_copyList(dest, src);
}
/**
@ -652,14 +648,6 @@ Object cloneString(Object obj)
}
// Returns an Object with a deep copy of the given Object
inline Object cloneList(const Object src)
{
Object list = listObject();
copyList(&list, &src);
list.type = src.type;
return list;
}
inline Object cloneOther(const Object src)
{
return src.other->clone ? src.other->clone(src.other) : src;
@ -672,7 +660,7 @@ inline Object cloneObject(const Object src)
switch (src.type) {
case TYPE_SLIST:
case TYPE_LIST:
return cloneList(src);
return copyList(&src);
case TYPE_LAMBDA:
return cloneLambda(src);
case TYPE_STRUCT:
@ -750,11 +738,9 @@ inline Object constructLambda(const Object* params, const Object* body, struct E
Object o = newObject(TYPE_LAMBDA);
o.lambda = malloc(sizeof(struct Lambda));
o.lambda->params = listObject();
o.lambda->body = listObject();
o.lambda->params = copyList(params);
o.lambda->body = copyList(body);
o.lambda->refs = 1;
copyList(&o.lambda->params, params);
copyList(&o.lambda->body, body);
if (env) {
Object* dest = &o.lambda->body;

View File

@ -159,7 +159,7 @@ int listLength(const Object* listObj);
Object* itemAt(const Object* listObj, int n);
void copyList(Object* dest, const Object* src);
Object copyList(const Object* src);
void cleanObject(Object* target);

View File

@ -314,13 +314,13 @@ Object len(Object* params, int length, struct Environment* env)
return o;
}
#define BASIC_OP(_name, _char) \
Object _name(Object* params, int length, struct Environment* env) \
{ \
Object obj1 = params[0]; \
Object obj2 = params[1]; \
return basicOp(&obj1, &obj2, _char, env); \
}
#define BASIC_OP(_name, _char) \
Object _name(Object* params, int length, struct Environment* env) \
{ \
Object obj1 = params[0]; \
Object obj2 = params[1]; \
return basicOp(&obj1, &obj2, _char, env); \
}
#define BASIC_MATH(_name, _op) \
Object _name(Object* params, int length, struct Environment* env) \
@ -374,7 +374,7 @@ Object numToChar(Object* params, int length, struct Environment* env)
if (c.type != TYPE_NUMBER) {
return errorObject(BAD_NUMBER);
}
char ch[1] = {c.number % 256};
char ch[1] = {c.number};
return stringFromSlice(ch, 1);
}