pebblisp/src/object.h

252 lines
4.9 KiB
C
Raw Normal View History

2020-05-05 13:42:28 -04:00
#ifndef OBJECT_H
#define OBJECT_H
#include <stdlib.h>
#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(...) ;
#endif
#endif
#define MAX_TOK_CNT 1024
#define MAX_ENV_ELM 100
#define RESULT_LENGTH 128
2020-05-05 13:42:28 -04:00
#define FOR_POINTER_IN_LIST(_list) \
for(Object *_element = (_list)->list; \
_element != NULL;\
_element = _element->forward)
#define POINTER _element
#define FOR_POINTERS_IN_LISTS(_list, _list2) \
for(Object *_element = (_list)->list, *_element2 = (_list2)->list; \
_element != NULL && _element2 != NULL; \
_element = _element->forward, _element2 = _element2->forward)
#define P1 POINTER
#define P2 _element2
#ifdef PBL_PLATFORM_APLITE
#define LOW_MEM
#endif
#ifdef LOW_MEM
#define SIMPLE_ERRORS
#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,
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,
LAMBDA_ARGS_NOT_LIST,
DID_NOT_FIND_SYMBOL,
2020-05-07 20:32:01 -04:00
BAD_TYPE,
UNEXPECTED_FORM,
LISTS_NOT_SAME_SIZE,
BAD_NUMBER,
UNSUPPORTED_NUMBER_TYPE,
NOT_A_SYMBOL,
ONLY_ONE_ARGUMENT,
NOT_A_LIST,
SCRIPT_NOT_FOUND,
NO_CLONE_SPECIFIED,
2021-07-05 01:13:45 -04:00
CAN_ONLY_EVAL_STRINGS,
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,
TYPE_SLIST,
TYPE_STRUCT,
2020-05-05 13:42:28 -04:00
TYPE_FUNC,
TYPE_SYMBOL,
TYPE_LAMBDA,
TYPE_STRING,
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;
2020-05-05 13:42:28 -04:00
struct Lambda;
struct Environment;
struct Slice;
struct Other;
struct Error {
enum errorCode code;
2022-03-19 22:25:20 -04:00
char* context;
};
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-03-19 22:25:20 -04:00
Object (* func)(Object, Object, struct Environment*);
2022-03-19 22:25:20 -04:00
struct StructObject* structObject;
struct Lambda* lambda;
struct Other* other;
#ifdef SIMPLE_ERRORS
enum errorCode error;
#else
2022-03-19 22:25:20 -04:00
struct Error* error;
#endif
2020-05-05 13:42:28 -04:00
};
2022-03-19 22:25:20 -04:00
Object* forward;
2020-05-05 13:42:28 -04:00
};
struct StructDef {
int fieldCount;
char* name;
char** names;
};
struct StructObject {
int definition;
//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;
};
struct Other {
2022-03-19 22:25:20 -04:00
void (* cleanup)(Object*);
2022-03-19 22:25:20 -04:00
Object (* clone)(struct Other*);
2022-03-19 22:25:20 -04:00
void* data;
};
2022-03-19 22:25:20 -04:00
char* stringNObj(char* dest, const Object* obj, size_t len);
2022-03-19 22:25:20 -04:00
char* stringObj(char* dest, const Object* obj);
2022-03-19 22:25:20 -04:00
void printList(const Object* list);
2022-03-19 22:25:20 -04:00
void printType(const Object* obj);
2022-03-19 22:25:20 -04:00
void printObj(const Object* obj);
2022-03-19 22:25:20 -04:00
void _printObj(const Object* obj, int newline);
2022-03-19 22:25:20 -04:00
void debugObj(const Object* obj);
2022-03-19 22:25:20 -04:00
void printErr(const Object* obj);
2020-05-05 13:42:28 -04:00
2022-03-19 22:25:20 -04:00
int isEmpty(const Object* obj);
2022-03-19 22:25:20 -04:00
Object* tail(const Object* listObj);
2022-03-19 22:25:20 -04:00
void insertIntoList(Object* dest, int ind, Object src);
2022-03-19 22:25:20 -04:00
void replaceListing(Object* list, int i, Object src);
2022-03-19 22:25:20 -04:00
void nf_addToList(Object* dest, Object src);
2022-03-19 22:25:20 -04:00
void deleteList(Object* dest);
2022-03-19 22:25:20 -04:00
int listLength(const Object* listObj);
2022-03-19 22:25:20 -04:00
Object* itemAt(const Object* listObj, int n);
2022-03-19 22:25:20 -04:00
void copyList(Object* dest, const Object* src);
2020-05-05 13:42:28 -04:00
2022-03-19 22:25:20 -04:00
void cleanObject(Object* target);
2022-03-19 22:25:20 -04:00
void printAndClean(Object* target);
2022-03-19 22:25:20 -04:00
void allocObject(Object** spot, Object src);
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-19 22:25:20 -04:00
int isListy(Object test);
2022-03-19 22:25:20 -04:00
int isStringy(Object test);
2022-03-19 22:25:20 -04:00
int isValidType(Object test);
2022-03-19 22:25:20 -04:00
int isError(Object obj, enum errorCode err);
2022-03-19 22:25:20 -04:00
int bothAre(enum Type type, const Object* obj1, const Object* obj2);
2022-03-19 22:25:20 -04:00
int eitherIs(enum Type type, const Object* obj1, const Object* obj2);
2022-03-19 22:25:20 -04:00
int areSameType(const Object* obj1, const Object* obj2);
2022-03-19 22:25:20 -04:00
Object cloneList(Object src);
Object cloneString(Object obj);
2022-03-19 22:25:20 -04:00
Object cloneLambda(Object old);
2022-03-19 22:25:20 -04:00
Object cloneObject(Object src);
2020-05-05 19:21:54 -04:00
Object newObject(Type type);
2020-05-05 13:42:28 -04:00
Object listObject();
2022-03-19 22:25:20 -04:00
Object startList(Object start);
2022-03-19 22:25:20 -04:00
Object objFromSlice(const char* string, int len);
2022-03-19 22:25:20 -04:00
Object stringFromSlice(const char* string, int len);
2022-03-19 22:25:20 -04:00
Object symFromSlice(const char* string, int len);
Object boolObject(int b);
Object numberObject(int num);
Object structObject(int definition);
Object otherObject();
2020-05-05 19:21:54 -04:00
Object errorObject(enum errorCode err);
2022-03-19 22:25:20 -04:00
enum errorCode getErrorCode(Object obj);
Object errorWithContextLineNo(enum errorCode code, const char* context, int lineNo, const char* fileName);
#ifdef SIMPLE_ERRORS
#define errorWithContext(code, context) errorObject(code)
#define errorAddContext(x, y, z, a) ;
#else
#define errorWithContext(_code, _context) errorWithContextLineNo(_code, _context, __LINE__, __FILE__)
2022-03-19 22:25:20 -04:00
void errorAddContext(Object* o, const char* context, int lineNo, const char* fileName);
2022-03-19 22:25:20 -04:00
#endif
struct Error noError();
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
int getAllocations();
2022-03-19 22:25:20 -04:00
size_t getBytes();
2020-05-05 13:42:28 -04:00
#endif