2020-05-05 13:42:28 -04:00
|
|
|
#ifndef OBJECT_H
|
|
|
|
#define OBJECT_H
|
|
|
|
|
2021-07-21 11:26:04 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
#ifndef STANDALONE
|
|
|
|
#include <pebble.h>
|
|
|
|
#undef printf
|
|
|
|
#define printf(...) APP_LOG(APP_LOG_LEVEL_DEBUG, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define printd(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define printd(...) (0)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2021-07-21 11:26:04 -04:00
|
|
|
#define MAX_TOK_CNT 1024
|
2020-11-06 15:07:12 -05:00
|
|
|
#define MAX_ENV_ELM 100
|
2021-07-21 11:26:04 -04:00
|
|
|
#define RESULT_LENGTH 128
|
2020-05-05 13:42:28 -04:00
|
|
|
|
2020-05-09 23:51:55 -04:00
|
|
|
#define FOR_POINTER_IN_LIST(_list) \
|
2022-01-07 16:55:03 -05:00
|
|
|
for(Object *_element = (_list)->list; \
|
2020-05-09 23:51:55 -04:00
|
|
|
_element != NULL;\
|
|
|
|
_element = _element->forward)
|
|
|
|
#define POINTER _element
|
|
|
|
|
2020-05-19 21:26:56 -04:00
|
|
|
#define FOR_POINTERS_IN_LISTS(_list, _list2) \
|
2022-01-07 16:55:03 -05:00
|
|
|
for(Object *_element = (_list)->list, *_element2 = (_list2)->list; \
|
2020-05-09 23:51:55 -04:00
|
|
|
_element != NULL && _element2 != NULL; \
|
|
|
|
_element = _element->forward, _element2 = _element2->forward)
|
|
|
|
#define P1 POINTER
|
|
|
|
#define P2 _element2
|
|
|
|
|
2021-07-21 14:31:40 -04:00
|
|
|
#ifdef PBL_PLATFORM_APLITE
|
2022-01-07 16:55:03 -05:00
|
|
|
#define LOW_MEM
|
2021-07-21 14:31:40 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LOW_MEM
|
2022-01-07 16:55:03 -05:00
|
|
|
#define SIMPLE_ERRORS
|
2021-07-21 14:31:40 -04:00
|
|
|
#endif
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
enum errorCode {
|
2020-05-08 02:29:06 -04:00
|
|
|
MISMATCHED_PARENS,
|
2020-05-05 13:42:28 -04:00
|
|
|
BAD_LIST_OF_SYMBOL_STRINGS,
|
|
|
|
TYPE_LIST_NOT_CAUGHT,
|
|
|
|
NULL_ENV,
|
2020-05-09 14:57:21 -04:00
|
|
|
EMPTY_ENV,
|
2020-05-05 13:42:28 -04:00
|
|
|
BUILT_IN_NOT_FOUND,
|
2020-05-05 19:21:54 -04:00
|
|
|
NULL_PARSE,
|
|
|
|
NULL_LAMBDA_LIST,
|
2020-05-07 20:32:01 -04:00
|
|
|
NULL_MAP_ARGS,
|
2020-05-06 00:47:14 -04:00
|
|
|
LAMBDA_ARGS_NOT_LIST,
|
2020-05-06 02:12:58 -04:00
|
|
|
DID_NOT_FIND_SYMBOL,
|
2020-05-07 20:32:01 -04:00
|
|
|
BAD_TYPE,
|
2020-05-09 23:51:55 -04:00
|
|
|
UNEXPECTED_FORM,
|
2020-05-16 14:31:14 -04:00
|
|
|
LISTS_NOT_SAME_SIZE,
|
2020-08-02 16:16:26 -04:00
|
|
|
BAD_NUMBER,
|
2020-05-22 01:16:45 -04:00
|
|
|
UNSUPPORTED_NUMBER_TYPE,
|
2020-05-28 10:28:28 -04:00
|
|
|
NOT_A_SYMBOL,
|
|
|
|
ONLY_ONE_ARGUMENT,
|
2020-08-04 13:30:40 -04:00
|
|
|
NOT_A_LIST,
|
2020-08-09 15:03:02 -04:00
|
|
|
SCRIPT_NOT_FOUND,
|
2020-11-02 15:46:17 -05:00
|
|
|
NO_CLONE_SPECIFIED,
|
2021-07-05 01:13:45 -04:00
|
|
|
CAN_ONLY_EVAL_STRINGS,
|
2021-12-13 10:47:35 -05:00
|
|
|
UNEXPECTED_EOF,
|
2021-07-05 01:13:45 -04:00
|
|
|
INDEX_PAST_END,
|
2020-05-05 13:42:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum Type {
|
|
|
|
TYPE_NUMBER,
|
|
|
|
TYPE_BOOL,
|
|
|
|
TYPE_LIST,
|
2021-12-15 14:30:16 -05:00
|
|
|
TYPE_SLIST,
|
2022-03-14 16:51:45 -04:00
|
|
|
TYPE_STRUCT,
|
2020-05-05 13:42:28 -04:00
|
|
|
TYPE_FUNC,
|
|
|
|
TYPE_SYMBOL,
|
|
|
|
TYPE_LAMBDA,
|
2020-05-10 02:27:59 -04:00
|
|
|
TYPE_STRING,
|
2020-08-03 11:21:04 -04:00
|
|
|
TYPE_OTHER,
|
2020-05-07 20:32:01 -04:00
|
|
|
TYPE_ERROR
|
2020-05-05 13:42:28 -04:00
|
|
|
} Type;
|
|
|
|
|
|
|
|
typedef struct Object Object;
|
2022-03-14 16:51:45 -04:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
struct Lambda;
|
2020-05-09 23:51:55 -04:00
|
|
|
struct Environment;
|
2020-05-10 02:27:59 -04:00
|
|
|
struct Slice;
|
2020-08-03 11:21:04 -04:00
|
|
|
struct Other;
|
2021-07-21 14:31:40 -04:00
|
|
|
struct Error {
|
|
|
|
enum errorCode code;
|
2022-01-07 16:55:03 -05:00
|
|
|
char *context;
|
2021-07-21 14:31:40 -04:00
|
|
|
};
|
2020-05-09 23:51:55 -04:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
struct Object {
|
|
|
|
Type type;
|
|
|
|
Object *forward;
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
union {
|
|
|
|
int number;
|
|
|
|
Object *list;
|
2020-05-10 02:27:59 -04:00
|
|
|
char *string;
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-14 23:51:59 -04:00
|
|
|
Object (*func)(Object, Object, struct Environment *);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-14 16:51:45 -04:00
|
|
|
struct StructObject *structObject;
|
2020-08-02 16:16:26 -04:00
|
|
|
struct Lambda *lambda;
|
2020-08-03 11:21:04 -04:00
|
|
|
struct Other *other;
|
2022-01-07 16:55:03 -05:00
|
|
|
#ifdef SIMPLE_ERRORS
|
2021-07-21 14:31:40 -04:00
|
|
|
enum errorCode error;
|
2022-01-07 16:55:03 -05:00
|
|
|
#else
|
2021-07-21 11:26:04 -04:00
|
|
|
struct Error *error;
|
2022-01-07 16:55:03 -05:00
|
|
|
#endif
|
2020-05-05 13:42:28 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-03-14 16:51:45 -04:00
|
|
|
struct StructDef {
|
|
|
|
int fieldCount;
|
|
|
|
char* name;
|
|
|
|
const char** names;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct StructObject {
|
|
|
|
struct StructDef *definition;
|
|
|
|
struct Object* fields; // Order should match that in the definition.
|
|
|
|
};
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
struct Lambda {
|
|
|
|
Object params;
|
|
|
|
Object body;
|
|
|
|
};
|
|
|
|
|
2020-08-03 11:21:04 -04:00
|
|
|
struct Other {
|
2022-01-07 16:55:03 -05:00
|
|
|
void (*cleanup)(Object *);
|
|
|
|
|
|
|
|
Object (*clone)(struct Other *);
|
|
|
|
|
2020-08-03 11:21:04 -04:00
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
char *stringNObj(char *dest, const Object *obj, size_t len);
|
|
|
|
|
|
|
|
char *stringObj(char *dest, const Object *obj);
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
void printList(const Object *list);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-11-06 15:07:12 -05:00
|
|
|
void printType(const Object *obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
void printObj(const Object *obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-11-02 15:23:04 -05:00
|
|
|
void _printObj(const Object *obj, int newline);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-06 00:47:14 -04:00
|
|
|
void debugObj(const Object *obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-07 20:32:01 -04:00
|
|
|
void printErr(const Object *obj);
|
2020-05-05 13:42:28 -04:00
|
|
|
|
2020-05-06 02:12:58 -04:00
|
|
|
int isEmpty(const Object *obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
Object *tail(const Object *listObj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-14 23:57:51 -04:00
|
|
|
void insertIntoList(Object *dest, int ind, const Object src);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-07 20:32:01 -04:00
|
|
|
void replaceListing(Object *list, int i, const Object src);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-07 20:32:01 -04:00
|
|
|
void nf_addToList(Object *dest, Object src);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-09 14:57:21 -04:00
|
|
|
void deleteList(Object *dest);
|
2020-05-08 00:32:08 -04:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
int listLength(const Object *listObj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
Object *itemAt(const Object *listObj, int n);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
void copyList(Object *dest, const Object *src);
|
|
|
|
|
2020-05-05 19:21:54 -04:00
|
|
|
void cleanObject(Object *target);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-07 20:32:01 -04:00
|
|
|
void printAndClean(Object *target);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-06 02:12:58 -04:00
|
|
|
void allocObject(Object **spot, const Object src);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-14 23:51:59 -04:00
|
|
|
void appendList(Object *dest, const Object *src);
|
2020-05-05 19:21:54 -04:00
|
|
|
|
2021-12-15 14:30:16 -05:00
|
|
|
int isListy(const Object test);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-23 13:01:15 -04:00
|
|
|
int isStringy(const Object test);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-23 13:01:15 -04:00
|
|
|
int isValidType(const Object test);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-28 10:28:28 -04:00
|
|
|
int isError(const Object obj, const enum errorCode err);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2021-07-04 23:48:26 -04:00
|
|
|
int bothAre(const enum Type type, const Object *obj1, const Object *obj2);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2021-07-04 23:48:26 -04:00
|
|
|
int eitherIs(const enum Type type, const Object *obj1, const Object *obj2);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2021-07-04 23:48:26 -04:00
|
|
|
int areSameType(const Object *obj1, const Object *obj2);
|
2020-05-22 01:16:45 -04:00
|
|
|
|
|
|
|
Object cloneList(const Object src);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-22 01:16:45 -04:00
|
|
|
Object cloneString(Object obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-22 01:16:45 -04:00
|
|
|
Object cloneLambda(const Object old);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-22 01:16:45 -04:00
|
|
|
Object cloneObject(const Object src);
|
|
|
|
|
2020-05-05 19:21:54 -04:00
|
|
|
Object newObject(Type type);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
Object listObject();
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-08 12:00:14 -04:00
|
|
|
Object startList(const Object start);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-10 02:27:59 -04:00
|
|
|
Object objFromSlice(const char *string, int len);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-11-02 15:55:55 -05:00
|
|
|
Object stringFromSlice(const char *string, int len);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-22 01:16:45 -04:00
|
|
|
Object symFromSlice(const char *string, int len);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-06 02:12:58 -04:00
|
|
|
Object boolObject(int b);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-06 02:12:58 -04:00
|
|
|
Object numberObject(int num);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-14 16:51:45 -04:00
|
|
|
Object structObject(struct StructDef *definition);
|
|
|
|
|
2020-08-03 11:21:04 -04:00
|
|
|
Object otherObject();
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-05 19:21:54 -04:00
|
|
|
Object errorObject(enum errorCode err);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2021-07-21 14:31:40 -04:00
|
|
|
enum errorCode getErrorCode(const Object obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2021-07-21 14:31:40 -04:00
|
|
|
#ifdef SIMPLE_ERRORS
|
2022-01-07 16:55:03 -05:00
|
|
|
#define errorWithContext(code, context) errorObject(code)
|
|
|
|
#define errorAddContext(x, y) ;
|
2021-07-21 14:31:40 -04:00
|
|
|
#else
|
2022-01-07 16:55:03 -05:00
|
|
|
|
|
|
|
Object errorWithContext(enum errorCode err, const char *context);
|
|
|
|
|
|
|
|
void errorAddContext(Object *o, const char *context);
|
|
|
|
|
2021-07-21 14:31:40 -04:00
|
|
|
#endif
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2021-12-13 10:47:35 -05:00
|
|
|
struct Error noError();
|
2021-07-21 14:31:40 -04:00
|
|
|
|
2020-05-06 02:12:58 -04:00
|
|
|
Object constructLambda(const Object *params, const Object *body);
|
2020-05-05 13:42:28 -04:00
|
|
|
|
2020-05-09 14:57:21 -04:00
|
|
|
// Object version of listLength()
|
2020-05-09 23:51:55 -04:00
|
|
|
Object len(Object obj1, Object, struct Environment *);
|
2020-05-09 14:57:21 -04:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
#endif
|