A bit of cleanup/simplification.

This commit is contained in:
Sage Vaillancourt 2022-04-22 07:37:25 -04:00
parent 1c52cb0ff1
commit 20f2d02f77
4 changed files with 54 additions and 59 deletions

View File

@ -49,30 +49,14 @@ int listLength(const Object* listObj)
return len;
}
/**
* Returns a pointer to the Object at the given index in a given list
* @param listObj The list to fetch an Object from
* @param n The index to to fetch from the list
* @return A pointer to the Object, if it is found, or NULL on an error
*/
Object* itemAt(const Object* listObj, int n)
{
FOR_POINTER_IN_LIST(listObj) {
if (n-- == 0) {
return POINTER;
}
}
return NULL;
}
/**
* Returns a pointer to the last Object in the given list.
* @param listObj The list to find the tail of
* @param listObj The list to find the tail of.
* @return A pointer to the last Object if it is found, or NULL on an error
*/
Object* tail(const Object* listObj)
{
if (!listObj || !isListy(*listObj)) {
if (!isListy(*listObj)) {
return NULL;
}
@ -374,7 +358,7 @@ const char* getTypeName(const Object* obj)
void _printObj(const Object* obj, int newline)
{
if (!obj) {
printf(newline ? "\n" : "");
printf(newline ? "<null>\n" : "<null>");
return;
}
size_t length;
@ -421,18 +405,18 @@ void cleanObject(Object* target)
if ((target->string[-1] -= 1) == 0) {
free(target->string - 1);
}
break;
return;
case TYPE_LIST:
case TYPE_SLIST:
deleteList(target);
break;
return;
case TYPE_HASH_TABLE:
target->table->refs -= 1;
if (!target->table->refs) {
deleteTable(&target->table->table);
free(target->table);
}
break;
return;
case TYPE_LAMBDA:
lambdaRefs(target) -= 1;
if (!lambdaRefs(target)) {
@ -441,29 +425,29 @@ void cleanObject(Object* target)
free(lambdaDocs(target));
free(target->lambda);
}
break;
return;
case TYPE_STRUCT:
for (int i = 0; i < getStructAt(target->structObject->definition)->fieldCount; i++) {
cleanObject(&target->structObject->fields[i]);
}
free(target->structObject->fields);
free(target->structObject);
break;
return;
case TYPE_PROMISE:
cleanPromise(target->promise);
break;
return;
case TYPE_ERROR:
#ifndef SIMPLE_ERRORS
free(target->error->plContext);
free(target->error->context);
free(target->error);
#endif
break;
return;
case TYPE_OTHER:
if (target->other->cleanup) {
target->other->cleanup(target);
}
break;
return;
case TYPE_STATIC_FUNC:
if ((target->staticF->refs -= 1) == 0) {
for (int i = 0; i < target->staticF->argCount; i++) {
@ -471,11 +455,11 @@ void cleanObject(Object* target)
}
free(target->staticF);
}
break;
return;
case TYPE_BOOL:
case TYPE_NUMBER:
case TYPE_FUNC:
break;
return;
}
}

View File

@ -232,8 +232,6 @@ void deleteList(Object* dest);
int listLength(const Object* listObj);
Object* itemAt(const Object* listObj, int n);
void cleanObject(Object* target);
void printAndClean(Object* target);

View File

@ -230,7 +230,9 @@ Object evalList(const Object* obj, struct Environment* env)
if (first_form->type == TYPE_SYMBOL) {
if (strcmp(first_form->string, "if") == 0) {
return evalIfArgs(first_form->forward, env);
} else if (strcmp(first_form->string, "fn") == 0) {
}
if (strcmp(first_form->string, "fn") == 0) {
Object* params = first_form->forward;
Object* doc = NULL;
Object* body = NULL;
@ -240,7 +242,9 @@ Object evalList(const Object* obj, struct Environment* env)
body = twoParams ? params->forward->forward : params->forward;
}
return constructLambda(params, doc, body, env);
} else if (strcmp(first_form->string, "struct") == 0) {
}
if (strcmp(first_form->string, "struct") == 0) {
return evalStructArgs(first_form->forward, first_form->forward->forward, env);
}
@ -370,19 +374,21 @@ Result parse(struct Slice* slices)
r.obj.type = TYPE_SLIST;
}
return r;
} else if (token->text[0] == '(') {
}
if (token->text[0] == '(') {
return readSeq(rest);
} else {
}
Result r = parseAtom(token);
r.slices = &r.slices[1];
return r;
}
}
#ifndef NO_SUGAR
#define sugar(_desc, _code) _code
#define sugar(_code) _code
#else
#define sugar(_desc, _code) ;
#define sugar(_code) do { } while (0);
#endif
Result readSeq(struct Slice* tokens)
@ -396,15 +402,17 @@ Result readSeq(struct Slice* tokens)
return (Result) { res, rest };
}
Result r = parse(tokens);
sugar("(? fil) => (? 'fil')" // or,
"(def yee 10) => (def 'yee' 10)",
if (r.obj.type == TYPE_ERROR) {
return r;
}
sugar(
// (? fil) => (? 'fil') // or,
// (def yee 10) => (def 'yee' 10)
// (set yee 10) => (set 'yee' 10)
if (forceString && r.obj.type == TYPE_SYMBOL) {
r.obj.type = TYPE_STRING;
}
)
if (r.obj.type == TYPE_ERROR) {
return r;
}
nf_addToList(&res, cloneObject(r.obj));
tokens = r.slices;
cleanObject(&r.obj);

View File

@ -101,7 +101,14 @@ Object at(Object* params, unused int length, unused struct Environment* env)
Object index = params[0];
Object list = params[1];
const Object* found = itemAt(&list, index.number);
const Object* found = NULL;
int n = index.number;
FOR_POINTER_IN_LIST(&list) {
if (n-- == 0) {
found = POINTER;
break;
}
}
if (found) {
return cloneObject(*found);
@ -116,22 +123,20 @@ Object rest(Object* params, unused int length, unused struct Environment* env)
Object list = params[0];
BuildListNamed(ret);
Object* l = &list;
FOR_POINTER_IN_LIST(l) {
if (POINTER == l->list) {
continue;
}
if (list.list) {
for (Object* _element = list.list->forward; _element != NULL; _element = _element->forward) {
addToList(ret, cloneObject(*POINTER));
}
}
return ret;
}
Object reverse(Object* params, unused int length, unused struct Environment* ignore2)
{
checkTypes(reverse)
Object _list = params[0];
const Object* list = &params[0];
const Object* list = &_list;
Object rev = listObject();
Object* tail = NULL;
@ -301,7 +306,8 @@ Object parseEvalO(Object* params, unused int length, struct Environment* env)
case TYPE_STRING:
return parseEval(text.string, env);
default:
throw(CAN_ONLY_EVAL_STRINGS, "Tried to (eval) a %s, instead of a string or symbol list", getTypeName(&text));
throw(CAN_ONLY_EVAL_STRINGS, "Tried to (eval) a %s, instead of a string or symbol list",
getTypeName(&text));
}
}
@ -330,7 +336,6 @@ Object catObjects(Object* params, int length, unused struct Environment* env)
return string;
}
Object len(Object* params, unused int length, unused struct Environment* env)
{
checkTypes(len)
@ -382,7 +387,7 @@ int areEqual(const Object* obj1, const Object* obj2)
const long n2 = obj2->number;
if (bothAre(TYPE_STRING, obj1, obj2)) {
return !strcmp(obj1->string, obj2->string);
return obj1->string == obj2->string || strcmp(obj1->string, obj2->string) == 0;
}
if (bothAre(TYPE_LIST, obj1, obj2)) {
return listEquality(obj1, obj2);