Simplify some functions.
Remove some unused stuff. Add macros for Lambda docString and reference access.
This commit is contained in:
parent
af65349d27
commit
1c52cb0ff1
|
@ -7,6 +7,8 @@
|
||||||
#include "pebbleobject.h"
|
#include "pebbleobject.h"
|
||||||
#include "calc.h"
|
#include "calc.h"
|
||||||
|
|
||||||
|
#define RESULT_LENGTH 128
|
||||||
|
|
||||||
// Custom layers that users can build
|
// Custom layers that users can build
|
||||||
Window* s_custom_window;
|
Window* s_custom_window;
|
||||||
TextLayer* s_heading_text_layer;
|
TextLayer* s_heading_text_layer;
|
||||||
|
|
53
src/object.c
53
src/object.c
|
@ -301,7 +301,7 @@ int stringNObj(struct string* s, const Object* obj)
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
appendf(s, "\\x%d", obj->number);
|
appendf(s, "\\x%d", obj->number);
|
||||||
#endif
|
#endif
|
||||||
char* docString = obj->lambda->params.docString;
|
char* docString = lambdaDocs(obj);
|
||||||
if (docString) {
|
if (docString) {
|
||||||
inflate(s, strlen(docString));
|
inflate(s, strlen(docString));
|
||||||
appendf(s, "%s\n", docString);
|
appendf(s, "%s\n", docString);
|
||||||
|
@ -434,11 +434,11 @@ void cleanObject(Object* target)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TYPE_LAMBDA:
|
case TYPE_LAMBDA:
|
||||||
target->lambda->refs -= 1;
|
lambdaRefs(target) -= 1;
|
||||||
if (!target->lambda->refs) {
|
if (!lambdaRefs(target)) {
|
||||||
cleanObject(&target->lambda->params);
|
cleanObject(&target->lambda->params);
|
||||||
cleanObject(&target->lambda->body);
|
cleanObject(&target->lambda->body);
|
||||||
free(target->lambda->params.docString);
|
free(lambdaDocs(target));
|
||||||
free(target->lambda);
|
free(target->lambda);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -519,7 +519,7 @@ void deleteList(Object* dest)
|
||||||
* @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
|
||||||
*/
|
*/
|
||||||
Object copyList(const Object* src)
|
Object cloneList(const Object* src)
|
||||||
{
|
{
|
||||||
BuildListNamed(list);
|
BuildListNamed(list);
|
||||||
FOR_POINTER_IN_LIST(src) {
|
FOR_POINTER_IN_LIST(src) {
|
||||||
|
@ -600,31 +600,7 @@ inline int isFuncy(const Object test)
|
||||||
|
|
||||||
inline int isValidType(const Object test)
|
inline int isValidType(const Object test)
|
||||||
{
|
{
|
||||||
switch (test.type) {
|
return test.type >= 0 && test.type <= TYPE_ERROR;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object cloneString(Object obj)
|
Object cloneString(Object obj)
|
||||||
|
@ -645,9 +621,10 @@ 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 copyList(&src);
|
return cloneList(&src);
|
||||||
case TYPE_LAMBDA:
|
case TYPE_LAMBDA:
|
||||||
return cloneLambda(src);
|
lambdaRefs(&src) += 1;
|
||||||
|
return src;
|
||||||
case TYPE_STRUCT:
|
case TYPE_STRUCT:
|
||||||
return cloneStruct(src);
|
return cloneStruct(src);
|
||||||
case TYPE_STRING:
|
case TYPE_STRING:
|
||||||
|
@ -774,15 +751,15 @@ inline Object constructLambda(const Object* params, const Object* docs, const Ob
|
||||||
|
|
||||||
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 = copyList(params);
|
o.lambda->params = cloneList(params);
|
||||||
o.lambda->body = copyList(body);
|
o.lambda->body = cloneList(body);
|
||||||
o.lambda->refs = 1;
|
lambdaRefs(&o) = 1;
|
||||||
|
|
||||||
if (docs) {
|
if (docs) {
|
||||||
o.lambda->params.docString = malloc(sizeof(char) * (strlen(docs->string) + 1));
|
lambdaDocs(&o) = malloc(sizeof(char) * (strlen(docs->string) + 1));
|
||||||
strcpy(o.lambda->params.docString, docs->string);
|
strcpy(lambdaDocs(&o), docs->string);
|
||||||
} else {
|
} else {
|
||||||
o.lambda->params.docString = NULL;
|
lambdaDocs(&o) = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object* dest = &o.lambda->body;
|
Object* dest = &o.lambda->body;
|
||||||
|
|
35
src/object.h
35
src/object.h
|
@ -3,25 +3,23 @@
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#ifndef STANDALONE
|
#ifdef STANDALONE
|
||||||
|
#define ERR_LEN 256
|
||||||
|
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
|
||||||
|
|
||||||
|
#else // not STANDALONE
|
||||||
#include <pebble.h>
|
#include <pebble.h>
|
||||||
#undef sprintf
|
#undef sprintf
|
||||||
#define sprintf(_dest, args...) snprintf(_dest, 999, args)
|
#define sprintf(_dest, args...) snprintf(_dest, 999, args)
|
||||||
|
#endif // STANDALONE
|
||||||
#else
|
|
||||||
|
|
||||||
#define ERR_LEN 256
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#define printd(...) printf(__VA_ARGS__)
|
#define printd(...) printf(__VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
#define printd(...) do { } while (0)
|
#define printd(...) do { } while (0)
|
||||||
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MAX_TOK_CNT 1024
|
#define MAX_TOK_CNT 1024
|
||||||
#define RESULT_LENGTH 128
|
|
||||||
|
|
||||||
#define FOR_POINTER_IN_LIST(_list) \
|
#define FOR_POINTER_IN_LIST(_list) \
|
||||||
for(Object *_element = (_list)->list; \
|
for(Object *_element = (_list)->list; \
|
||||||
|
@ -98,7 +96,7 @@ enum errorCode {
|
||||||
NO_CLONE_SPECIFIED,
|
NO_CLONE_SPECIFIED,
|
||||||
CAN_ONLY_EVAL_STRINGS,
|
CAN_ONLY_EVAL_STRINGS,
|
||||||
UNEXPECTED_EOF,
|
UNEXPECTED_EOF,
|
||||||
INDEX_PAST_END,
|
INDEX_PAST_END // INDEX_PAST_END should always be the highest value
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef enum Type {
|
typedef enum Type {
|
||||||
|
@ -115,7 +113,7 @@ typedef enum Type {
|
||||||
TYPE_HASH_TABLE,
|
TYPE_HASH_TABLE,
|
||||||
TYPE_PROMISE,
|
TYPE_PROMISE,
|
||||||
TYPE_OTHER,
|
TYPE_OTHER,
|
||||||
TYPE_ERROR
|
TYPE_ERROR // TYPE_ERROR should always be the highest value
|
||||||
} Type;
|
} Type;
|
||||||
|
|
||||||
typedef struct Object Object;
|
typedef struct Object Object;
|
||||||
|
@ -159,6 +157,7 @@ struct Object {
|
||||||
union {
|
union {
|
||||||
Object* forward;
|
Object* forward;
|
||||||
char* docString;
|
char* docString;
|
||||||
|
int refs;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -179,10 +178,12 @@ struct StructObject {
|
||||||
struct Object* fields; // Order should match that in the definition.
|
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 {
|
struct Lambda {
|
||||||
int refs;
|
/// params.docstring can contain details about the constructed lambda.
|
||||||
/// Note: params.docstring can contain details about the constructed lambda.
|
|
||||||
Object params;
|
Object params;
|
||||||
|
/// body.refs contains the reference count
|
||||||
Object body;
|
Object body;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -223,10 +224,6 @@ void printObj(const Object* obj);
|
||||||
|
|
||||||
void _printObj(const Object* obj, int newline);
|
void _printObj(const Object* obj, int newline);
|
||||||
|
|
||||||
void debugObj(const Object* obj);
|
|
||||||
|
|
||||||
int isEmpty(const Object* obj);
|
|
||||||
|
|
||||||
Object* tail(const Object* listObj);
|
Object* tail(const Object* listObj);
|
||||||
|
|
||||||
Object* nf_addToList(Object* dest, Object src);
|
Object* nf_addToList(Object* dest, Object src);
|
||||||
|
@ -237,8 +234,6 @@ int listLength(const Object* listObj);
|
||||||
|
|
||||||
Object* itemAt(const Object* listObj, int n);
|
Object* itemAt(const Object* listObj, int n);
|
||||||
|
|
||||||
Object copyList(const Object* src);
|
|
||||||
|
|
||||||
void cleanObject(Object* target);
|
void cleanObject(Object* target);
|
||||||
|
|
||||||
void printAndClean(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);
|
int areSameType(const Object* obj1, const Object* obj2);
|
||||||
|
|
||||||
Object cloneList(Object src);
|
Object cloneList(const Object* src);
|
||||||
|
|
||||||
Object cloneString(Object obj);
|
Object cloneString(Object obj);
|
||||||
|
|
||||||
Object cloneLambda(Object old);
|
|
||||||
|
|
||||||
Object cloneObject(Object src);
|
Object cloneObject(Object src);
|
||||||
|
|
||||||
Object newObject(Type type);
|
Object newObject(Type type);
|
||||||
|
|
Loading…
Reference in New Issue