From 1c52cb0ff1df6b1023cb3498d44308f7950bb30d Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Thu, 21 Apr 2022 12:01:02 -0400 Subject: [PATCH] Simplify some functions. Remove some unused stuff. Add macros for Lambda docString and reference access. --- src/calc.c | 2 ++ src/object.c | 53 +++++++++++++++------------------------------------- src/object.h | 35 ++++++++++++++-------------------- 3 files changed, 31 insertions(+), 59 deletions(-) diff --git a/src/calc.c b/src/calc.c index 0e77d3d..fbdcb55 100644 --- a/src/calc.c +++ b/src/calc.c @@ -7,6 +7,8 @@ #include "pebbleobject.h" #include "calc.h" +#define RESULT_LENGTH 128 + // Custom layers that users can build Window* s_custom_window; TextLayer* s_heading_text_layer; diff --git a/src/object.c b/src/object.c index dab217a..8321a7d 100644 --- a/src/object.c +++ b/src/object.c @@ -301,7 +301,7 @@ int stringNObj(struct string* s, const Object* obj) #ifdef DEBUG appendf(s, "\\x%d", obj->number); #endif - char* docString = obj->lambda->params.docString; + char* docString = lambdaDocs(obj); if (docString) { inflate(s, strlen(docString)); appendf(s, "%s\n", docString); @@ -434,11 +434,11 @@ void cleanObject(Object* target) } break; case TYPE_LAMBDA: - target->lambda->refs -= 1; - if (!target->lambda->refs) { + lambdaRefs(target) -= 1; + if (!lambdaRefs(target)) { cleanObject(&target->lambda->params); cleanObject(&target->lambda->body); - free(target->lambda->params.docString); + free(lambdaDocs(target)); free(target->lambda); } break; @@ -519,7 +519,7 @@ void deleteList(Object* dest) * @param dest The list to copy to * @param src The list to copy from */ -Object copyList(const Object* src) +Object cloneList(const Object* src) { BuildListNamed(list); FOR_POINTER_IN_LIST(src) { @@ -600,31 +600,7 @@ inline int isFuncy(const Object test) inline int isValidType(const Object test) { - switch (test.type) { - case TYPE_NUMBER: - case TYPE_BOOL: - case TYPE_LIST: - case TYPE_STRUCT: - case TYPE_SLIST: - case TYPE_FUNC: - case TYPE_STATIC_FUNC: - case TYPE_SYMBOL: - case TYPE_LAMBDA: - case TYPE_STRING: - case TYPE_HASH_TABLE: - case TYPE_PROMISE: - case TYPE_OTHER: - case TYPE_ERROR: - return 1; - } - - return 0; -} - -inline Object cloneLambda(const Object old) -{ - old.lambda->refs += 1; - return old; + return test.type >= 0 && test.type <= TYPE_ERROR; } Object cloneString(Object obj) @@ -645,9 +621,10 @@ inline Object cloneObject(const Object src) switch (src.type) { case TYPE_SLIST: case TYPE_LIST: - return copyList(&src); + return cloneList(&src); case TYPE_LAMBDA: - return cloneLambda(src); + lambdaRefs(&src) += 1; + return src; case TYPE_STRUCT: return cloneStruct(src); case TYPE_STRING: @@ -774,15 +751,15 @@ inline Object constructLambda(const Object* params, const Object* docs, const Ob Object o = newObject(TYPE_LAMBDA); o.lambda = malloc(sizeof(struct Lambda)); - o.lambda->params = copyList(params); - o.lambda->body = copyList(body); - o.lambda->refs = 1; + o.lambda->params = cloneList(params); + o.lambda->body = cloneList(body); + lambdaRefs(&o) = 1; if (docs) { - o.lambda->params.docString = malloc(sizeof(char) * (strlen(docs->string) + 1)); - strcpy(o.lambda->params.docString, docs->string); + lambdaDocs(&o) = malloc(sizeof(char) * (strlen(docs->string) + 1)); + strcpy(lambdaDocs(&o), docs->string); } else { - o.lambda->params.docString = NULL; + lambdaDocs(&o) = NULL; } Object* dest = &o.lambda->body; diff --git a/src/object.h b/src/object.h index b848af7..ac4949a 100644 --- a/src/object.h +++ b/src/object.h @@ -3,25 +3,23 @@ #include -#ifndef STANDALONE +#ifdef STANDALONE +#define ERR_LEN 256 +#define eprintf(...) fprintf(stderr, __VA_ARGS__) + +#else // not STANDALONE #include #undef sprintf #define sprintf(_dest, args...) snprintf(_dest, 999, args) - -#else - -#define ERR_LEN 256 +#endif // STANDALONE #ifdef DEBUG #define printd(...) printf(__VA_ARGS__) #else #define printd(...) do { } while (0) -#define eprintf(...) fprintf(stderr, __VA_ARGS__) -#endif #endif #define MAX_TOK_CNT 1024 -#define RESULT_LENGTH 128 #define FOR_POINTER_IN_LIST(_list) \ for(Object *_element = (_list)->list; \ @@ -98,7 +96,7 @@ enum errorCode { NO_CLONE_SPECIFIED, CAN_ONLY_EVAL_STRINGS, UNEXPECTED_EOF, - INDEX_PAST_END, + INDEX_PAST_END // INDEX_PAST_END should always be the highest value }; typedef enum Type { @@ -115,7 +113,7 @@ typedef enum Type { TYPE_HASH_TABLE, TYPE_PROMISE, TYPE_OTHER, - TYPE_ERROR + TYPE_ERROR // TYPE_ERROR should always be the highest value } Type; typedef struct Object Object; @@ -159,6 +157,7 @@ struct Object { union { Object* forward; char* docString; + int refs; }; }; @@ -179,10 +178,12 @@ struct StructObject { struct Object* fields; // Order should match that in the definition. }; +#define lambdaDocs(LAMBDA_OBJECT) (LAMBDA_OBJECT)->lambda->params.docString +#define lambdaRefs(LAMBDA_OBJECT) (LAMBDA_OBJECT)->lambda->body.refs struct Lambda { - int refs; - /// Note: params.docstring can contain details about the constructed lambda. + /// params.docstring can contain details about the constructed lambda. Object params; + /// body.refs contains the reference count Object body; }; @@ -223,10 +224,6 @@ void printObj(const Object* obj); void _printObj(const Object* obj, int newline); -void debugObj(const Object* obj); - -int isEmpty(const Object* obj); - Object* tail(const Object* listObj); Object* nf_addToList(Object* dest, Object src); @@ -237,8 +234,6 @@ int listLength(const Object* listObj); Object* itemAt(const Object* listObj, int n); -Object copyList(const Object* src); - void cleanObject(Object* target); void printAndClean(Object* target); @@ -265,12 +260,10 @@ int bothAre(enum Type type, const Object* obj1, const Object* obj2); int areSameType(const Object* obj1, const Object* obj2); -Object cloneList(Object src); +Object cloneList(const Object* src); Object cloneString(Object obj); -Object cloneLambda(Object old); - Object cloneObject(Object src); Object newObject(Type type);