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
|
2022-03-31 14:12:52 -04:00
|
|
|
#define printd(...) do { } while (0)
|
2022-01-07 16:55:03 -05:00
|
|
|
#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
|
|
|
|
|
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
|
|
|
NULL_ENV,
|
2020-05-09 14:57:21 -04:00
|
|
|
EMPTY_ENV,
|
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,
|
2022-03-28 13:19:34 -04:00
|
|
|
BAD_PARAMS_ON,
|
2020-08-02 16:16:26 -04:00
|
|
|
BAD_NUMBER,
|
2020-05-22 01:16:45 -04:00
|
|
|
UNSUPPORTED_NUMBER_TYPE,
|
2022-03-27 18:52:56 -04:00
|
|
|
NOT_ENOUGH_ARGUMENTS,
|
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-08-03 11:21:04 -04:00
|
|
|
struct Other;
|
2021-07-21 14:31:40 -04:00
|
|
|
struct Error {
|
|
|
|
enum errorCode code;
|
2022-03-19 22:25:20 -04:00
|
|
|
char* context;
|
2022-03-21 12:33:35 -04:00
|
|
|
struct Slice* plContext;
|
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;
|
2022-03-19 22:25:20 -04:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
union {
|
|
|
|
int number;
|
2022-03-19 22:25:20 -04:00
|
|
|
Object* list;
|
|
|
|
char* string;
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-24 16:56:52 -04:00
|
|
|
Object (* func)(Object*, int, struct Environment*);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
struct StructObject* structObject;
|
|
|
|
struct Lambda* lambda;
|
|
|
|
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
|
2022-03-19 22:25:20 -04:00
|
|
|
struct Error* error;
|
2022-01-07 16:55:03 -05:00
|
|
|
#endif
|
2020-05-05 13:42:28 -04:00
|
|
|
};
|
2022-03-16 16:36:04 -04:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object* forward;
|
2020-05-05 13:42:28 -04:00
|
|
|
};
|
|
|
|
|
2022-03-14 16:51:45 -04:00
|
|
|
struct StructDef {
|
|
|
|
int fieldCount;
|
|
|
|
char* name;
|
2022-03-15 15:26:01 -04:00
|
|
|
char** names;
|
2022-03-14 16:51:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct StructObject {
|
2022-03-15 15:26:01 -04:00
|
|
|
int definition;
|
2022-03-14 16:51:45 -04:00
|
|
|
struct Object* fields; // Order should match that in the definition.
|
|
|
|
};
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
struct Lambda {
|
2022-03-26 19:51:52 -04:00
|
|
|
int refs;
|
2020-05-05 13:42:28 -04:00
|
|
|
Object params;
|
|
|
|
Object body;
|
|
|
|
};
|
|
|
|
|
2020-08-03 11:21:04 -04:00
|
|
|
struct Other {
|
2022-03-19 22:25:20 -04:00
|
|
|
void (* cleanup)(Object*);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object (* clone)(struct Other*);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void* data;
|
2020-08-03 11:21:04 -04:00
|
|
|
};
|
|
|
|
|
2022-03-29 13:55:51 -04:00
|
|
|
struct string {
|
|
|
|
char* allocPoint;
|
|
|
|
char* cursor;
|
|
|
|
size_t capacity;
|
|
|
|
};
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-31 11:41:02 -04:00
|
|
|
/**
|
|
|
|
* Returns a dynamically-sized string representation of the given object.
|
|
|
|
*
|
|
|
|
* _Adds_ the length of the new string to the `length` parameter.
|
|
|
|
*/
|
2022-03-29 13:55:51 -04:00
|
|
|
char* stringObj(const Object* obj, size_t* length);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void printType(const Object* obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void printObj(const Object* obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void _printObj(const Object* obj, int newline);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void debugObj(const Object* obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
int isEmpty(const Object* obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object* tail(const Object* listObj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-28 23:51:05 -04:00
|
|
|
Object* nf_addToList(Object* dest, Object src);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void deleteList(Object* dest);
|
2020-05-08 00:32:08 -04:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
int listLength(const Object* listObj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object* itemAt(const Object* listObj, int n);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-27 00:47:20 -04:00
|
|
|
Object copyList(const Object* src);
|
2020-05-05 13:42:28 -04:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void cleanObject(Object* target);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void printAndClean(Object* target);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void allocObject(Object** spot, Object src);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void appendList(Object* dest, const Object* src);
|
2020-05-05 19:21:54 -04:00
|
|
|
|
2022-03-28 13:19:34 -04:00
|
|
|
int isNumber(Object test);
|
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
int isListy(Object test);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
int isStringy(Object test);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-28 13:19:34 -04:00
|
|
|
int isBool(Object test);
|
|
|
|
|
2022-03-31 12:09:16 -04:00
|
|
|
int isStruct(Object test);
|
|
|
|
|
2022-03-28 13:19:34 -04:00
|
|
|
int isFuncy(Object test);
|
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
int isValidType(Object test);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
int isError(Object obj, enum errorCode err);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
int bothAre(enum Type type, const Object* obj1, const Object* obj2);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
int areSameType(const Object* obj1, const Object* obj2);
|
2020-05-22 01:16:45 -04:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object cloneList(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
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object cloneLambda(Object old);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object cloneObject(Object src);
|
2020-05-22 01:16:45 -04:00
|
|
|
|
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
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object startList(Object start);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object objFromSlice(const char* string, int len);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-29 14:40:18 -04:00
|
|
|
Object nullTerminated(const char* string);
|
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object stringFromSlice(const char* string, int len);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object symFromSlice(const char* string, int len);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-31 10:29:06 -04:00
|
|
|
Object withLen(size_t len, enum Type type);
|
2022-03-28 09:56:24 -04: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-15 15:26:01 -04:00
|
|
|
Object structObject(int definition);
|
2022-03-14 16:51:45 -04:00
|
|
|
|
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
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
enum errorCode getErrorCode(Object obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-18 16:10:23 -04:00
|
|
|
Object errorWithContextLineNo(enum errorCode code, const char* context, int lineNo, const char* fileName);
|
|
|
|
|
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)
|
2022-03-18 16:10:23 -04:00
|
|
|
#define errorAddContext(x, y, z, a) ;
|
2021-07-21 14:31:40 -04:00
|
|
|
#else
|
2022-03-18 16:10:23 -04:00
|
|
|
#define errorWithContext(_code, _context) errorWithContextLineNo(_code, _context, __LINE__, __FILE__)
|
2022-03-19 22:25:20 -04:00
|
|
|
|
2022-03-18 16:10:23 -04:00
|
|
|
void errorAddContext(Object* o, const char* context, int lineNo, const char* fileName);
|
2022-03-19 22:25:20 -04:00
|
|
|
|
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
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
Object constructLambda(const Object* params, const Object* body, struct Environment* env);
|
2020-05-05 13:42:28 -04:00
|
|
|
|
2022-03-18 16:10:23 -04:00
|
|
|
int getAllocations();
|
2022-03-19 22:25:20 -04:00
|
|
|
|
|
|
|
size_t getBytes();
|
2022-03-18 16:10:23 -04:00
|
|
|
|
2022-03-31 11:41:02 -04:00
|
|
|
void* scalloc(size_t size, size_t count);
|
|
|
|
|
|
|
|
void* smalloc(size_t size);
|
|
|
|
|
|
|
|
#define malloc(x) smalloc(x)
|
|
|
|
#define calloc(x, y) scalloc(x, y)
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
#endif
|