Simplify some functions.

Remove some unused stuff.
Add macros for Lambda docString and reference access.
This commit is contained in:
Sage Vaillancourt 2022-04-21 12:01:02 -04:00 committed by Sage Vaillancourt
parent af65349d27
commit 1c52cb0ff1
3 changed files with 31 additions and 59 deletions

View File

@ -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;

View File

@ -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;

View File

@ -3,25 +3,23 @@
#include <stdlib.h>
#ifndef STANDALONE
#ifdef STANDALONE
#define ERR_LEN 256
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else // not STANDALONE
#include <pebble.h>
#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);