From 69cf9212d7da2028459e2e7c43194e64d475f393 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Sun, 27 Mar 2022 00:47:20 -0400 Subject: [PATCH] Simplify several functions. Namely, printObj() and _copyList(). Remove separate cloneList() function. --- src/object.c | 42 ++++++++++++++---------------------------- src/object.h | 2 +- src/plfunc.c | 16 ++++++++-------- 3 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/object.c b/src/object.c index b6553a8..67d9ba8 100644 --- a/src/object.c +++ b/src/object.c @@ -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; diff --git a/src/object.h b/src/object.h index 824d71e..0ba8f92 100644 --- a/src/object.h +++ b/src/object.h @@ -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); diff --git a/src/plfunc.c b/src/plfunc.c index 3762c6a..49f8153 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -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); }