Simplify several functions.
Namely, printObj() and _copyList(). Remove separate cloneList() function.
This commit is contained in:
parent
b715b2116a
commit
69cf9212d7
42
src/object.c
42
src/object.c
|
@ -313,7 +313,9 @@ int stringNObj(char* dest, const Object* obj, const size_t len)
|
||||||
break;
|
break;
|
||||||
case TYPE_LAMBDA: {
|
case TYPE_LAMBDA: {
|
||||||
#ifdef STANDALONE
|
#ifdef STANDALONE
|
||||||
|
#ifdef DEBUG
|
||||||
dest += stringf(dest, len, "\\x%d", obj->number);
|
dest += stringf(dest, len, "\\x%d", obj->number);
|
||||||
|
#endif
|
||||||
dest += stringNObj(dest, &obj->lambda->params, len);
|
dest += stringNObj(dest, &obj->lambda->params, len);
|
||||||
dest += sprintf(dest, " -> ");
|
dest += sprintf(dest, " -> ");
|
||||||
dest += stringNObj(dest, &obj->lambda->body, len);
|
dest += stringNObj(dest, &obj->lambda->body, len);
|
||||||
|
@ -394,12 +396,6 @@ void _printObj(const Object* obj, int newline)
|
||||||
printf(newline ? "\n" : "");
|
printf(newline ? "\n" : "");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (obj->type == TYPE_LAMBDA) {
|
|
||||||
printObj(&obj->lambda->params);
|
|
||||||
printf("->");
|
|
||||||
printObj(&obj->lambda->body);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
char temp[1024] = "";
|
char temp[1024] = "";
|
||||||
stringNObj(temp, obj, 1024);
|
stringNObj(temp, obj, 1024);
|
||||||
if (newline) {
|
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) {
|
FOR_POINTER_IN_LIST(src) {
|
||||||
if (isListy(*POINTER)) {
|
if (isListy(*POINTER)) {
|
||||||
nf_addToList(dest, *POINTER);
|
nf_addToList(dest, *POINTER);
|
||||||
tail(dest)->list = NULL;
|
tail(dest)->list = NULL;
|
||||||
_copyList(tail(dest), POINTER, 0);
|
_copyList(tail(dest), POINTER);
|
||||||
} else if (isStringy(*POINTER)) {
|
} else if (isStringy(*POINTER)) {
|
||||||
nf_addToList(dest, cloneString(*POINTER));
|
nf_addToList(dest, cloneString(*POINTER));
|
||||||
} else {
|
} else {
|
||||||
|
@ -546,9 +538,13 @@ void _copyList(Object* dest, const Object* src, int delete)
|
||||||
* @param dest The list to copy to
|
* @param dest The list to copy to
|
||||||
* @param src The list to copy from
|
* @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)
|
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
|
// 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)
|
inline Object cloneOther(const Object src)
|
||||||
{
|
{
|
||||||
return src.other->clone ? src.other->clone(src.other) : src;
|
return src.other->clone ? src.other->clone(src.other) : src;
|
||||||
|
@ -672,7 +660,7 @@ inline Object cloneObject(const Object src)
|
||||||
switch (src.type) {
|
switch (src.type) {
|
||||||
case TYPE_SLIST:
|
case TYPE_SLIST:
|
||||||
case TYPE_LIST:
|
case TYPE_LIST:
|
||||||
return cloneList(src);
|
return copyList(&src);
|
||||||
case TYPE_LAMBDA:
|
case TYPE_LAMBDA:
|
||||||
return cloneLambda(src);
|
return cloneLambda(src);
|
||||||
case TYPE_STRUCT:
|
case TYPE_STRUCT:
|
||||||
|
@ -750,11 +738,9 @@ inline Object constructLambda(const Object* params, const Object* body, struct E
|
||||||
|
|
||||||
Object o = newObject(TYPE_LAMBDA);
|
Object o = newObject(TYPE_LAMBDA);
|
||||||
o.lambda = malloc(sizeof(struct Lambda));
|
o.lambda = malloc(sizeof(struct Lambda));
|
||||||
o.lambda->params = listObject();
|
o.lambda->params = copyList(params);
|
||||||
o.lambda->body = listObject();
|
o.lambda->body = copyList(body);
|
||||||
o.lambda->refs = 1;
|
o.lambda->refs = 1;
|
||||||
copyList(&o.lambda->params, params);
|
|
||||||
copyList(&o.lambda->body, body);
|
|
||||||
|
|
||||||
if (env) {
|
if (env) {
|
||||||
Object* dest = &o.lambda->body;
|
Object* dest = &o.lambda->body;
|
||||||
|
|
|
@ -159,7 +159,7 @@ int listLength(const Object* listObj);
|
||||||
|
|
||||||
Object* itemAt(const Object* listObj, int n);
|
Object* itemAt(const Object* listObj, int n);
|
||||||
|
|
||||||
void copyList(Object* dest, const Object* src);
|
Object copyList(const Object* src);
|
||||||
|
|
||||||
void cleanObject(Object* target);
|
void cleanObject(Object* target);
|
||||||
|
|
||||||
|
|
|
@ -315,12 +315,12 @@ Object len(Object* params, int length, struct Environment* env)
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BASIC_OP(_name, _char) \
|
#define BASIC_OP(_name, _char) \
|
||||||
Object _name(Object* params, int length, struct Environment* env) \
|
Object _name(Object* params, int length, struct Environment* env) \
|
||||||
{ \
|
{ \
|
||||||
Object obj1 = params[0]; \
|
Object obj1 = params[0]; \
|
||||||
Object obj2 = params[1]; \
|
Object obj2 = params[1]; \
|
||||||
return basicOp(&obj1, &obj2, _char, env); \
|
return basicOp(&obj1, &obj2, _char, env); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BASIC_MATH(_name, _op) \
|
#define BASIC_MATH(_name, _op) \
|
||||||
Object _name(Object* params, int length, struct Environment* env) \
|
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) {
|
if (c.type != TYPE_NUMBER) {
|
||||||
return errorObject(BAD_NUMBER);
|
return errorObject(BAD_NUMBER);
|
||||||
}
|
}
|
||||||
char ch[1] = {c.number % 256};
|
char ch[1] = {c.number};
|
||||||
return stringFromSlice(ch, 1);
|
return stringFromSlice(ch, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue