2020-05-05 13:42:28 -04:00
|
|
|
#ifndef OBJECT_H
|
|
|
|
#define OBJECT_H
|
|
|
|
|
2020-05-08 01:18:20 -04:00
|
|
|
#define MAX_TOK_LEN 10 // 11
|
2020-05-05 13:42:28 -04:00
|
|
|
#define MAX_TOK_CNT 128 // 128
|
2020-05-15 23:22:08 -04:00
|
|
|
#define MAX_ENV_ELM 25 // 50
|
2020-05-05 13:42:28 -04:00
|
|
|
|
2020-05-09 23:51:55 -04:00
|
|
|
#define FOR_POINTER_IN_LIST(_list) \
|
|
|
|
for(Object *_element = _list->list; \
|
|
|
|
_element != NULL;\
|
|
|
|
_element = _element->forward)
|
|
|
|
#define POINTER _element
|
|
|
|
|
2020-05-19 21:26:56 -04:00
|
|
|
#define FOR_POINTERS_IN_LISTS(_list, _list2) \
|
|
|
|
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
|
|
|
|
|
2020-05-19 21:26:56 -04:00
|
|
|
#define SKIP_FIRST() if(_element == _list->list) {continue;}
|
|
|
|
|
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,
|
|
|
|
SYMBOLS_CANT_START_WITH_DIGITS,
|
2020-05-17 21:22:39 -04:00
|
|
|
UNSUPPORTED_NUMBER_TYPE
|
2020-05-05 13:42:28 -04:00
|
|
|
};
|
|
|
|
|
2020-05-07 20:32:01 -04:00
|
|
|
//#ifdef STANDALONE
|
|
|
|
static const char *errorText[] = {
|
2020-05-08 02:29:06 -04:00
|
|
|
"MISMATCHED_PARENS",
|
2020-05-07 20:32:01 -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-07 20:32:01 -04:00
|
|
|
"BUILT_IN_NOT_FOUND",
|
|
|
|
"NULL_PARSE",
|
|
|
|
"NULL_LAMBDA_LIST",
|
|
|
|
"NULL_MAP_ARGS",
|
|
|
|
"LAMBDA_ARGS_NOT_LIST",
|
|
|
|
"DID_NOT_FIND_SYMBOL",
|
|
|
|
"BAD_TYPE",
|
2020-05-09 23:51:55 -04:00
|
|
|
"UNEXPECTED_FORM",
|
2020-05-16 14:31:14 -04:00
|
|
|
"LISTS_NOT_SAME_SIZE",
|
|
|
|
"SYMBOLS_CANT_START_WITH_DIGITS",
|
2020-05-17 21:22:39 -04:00
|
|
|
"UNSUPPORTED_NUMBER_TYPE"
|
2020-05-07 20:32:01 -04:00
|
|
|
};
|
|
|
|
//#endif
|
|
|
|
|
2020-05-17 21:22:39 -04:00
|
|
|
#define MALLOC_FLAG 64
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
typedef enum Type {
|
|
|
|
TYPE_NUMBER,
|
|
|
|
TYPE_BOOL,
|
|
|
|
TYPE_LIST,
|
|
|
|
TYPE_FUNC,
|
|
|
|
TYPE_SYMBOL,
|
|
|
|
TYPE_LAMBDA,
|
2020-05-10 02:27:59 -04:00
|
|
|
TYPE_STRING,
|
2020-05-07 20:32:01 -04:00
|
|
|
TYPE_ERROR
|
2020-05-05 13:42:28 -04:00
|
|
|
} Type;
|
|
|
|
|
|
|
|
typedef struct Object Object;
|
|
|
|
struct Lambda;
|
2020-05-09 23:51:55 -04:00
|
|
|
struct Environment;
|
2020-05-10 02:27:59 -04:00
|
|
|
struct Slice;
|
2020-05-09 23:51:55 -04:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
struct Object {
|
|
|
|
Type type;
|
|
|
|
Object *forward;
|
|
|
|
union {
|
|
|
|
int number;
|
|
|
|
Object *list;
|
|
|
|
char name[MAX_TOK_LEN];
|
2020-05-10 02:27:59 -04:00
|
|
|
char *string;
|
2020-05-14 23:51:59 -04:00
|
|
|
Object (*func)(Object, Object, struct Environment *);
|
2020-05-06 02:12:58 -04:00
|
|
|
struct Lambda *lambda; // Maybe better as not a pointer?
|
2020-05-05 13:42:28 -04:00
|
|
|
enum errorCode err;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-06 02:12:58 -04:00
|
|
|
// Maybe better as pointers?
|
2020-05-05 13:42:28 -04:00
|
|
|
struct Lambda {
|
|
|
|
Object params;
|
|
|
|
Object body;
|
|
|
|
};
|
|
|
|
|
|
|
|
char* stringObj(char *dest, const Object *obj);
|
|
|
|
void printList(const Object *list);
|
|
|
|
void printObj(const Object *obj);
|
2020-05-06 00:47:14 -04:00
|
|
|
void debugObj(const Object *obj);
|
2020-05-07 20:32:01 -04:00
|
|
|
void printErr(const Object *obj);
|
|
|
|
int getType(const Object *obj);
|
2020-05-05 13:42:28 -04:00
|
|
|
|
2020-05-06 02:12:58 -04:00
|
|
|
int isEmpty(const Object *obj);
|
2020-05-05 13:42:28 -04:00
|
|
|
Object *tail(const Object *listObj);
|
2020-05-14 23:57:51 -04:00
|
|
|
void insertIntoList(Object *dest, int ind, const Object src);
|
2020-05-07 20:32:01 -04:00
|
|
|
void replaceListing(Object *list, int i, const Object src);
|
|
|
|
void nf_addToList(Object *dest, Object src);
|
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);
|
|
|
|
Object *itemAt(const Object *listObj, int n);
|
|
|
|
void copyList(Object *dest, const Object *src);
|
|
|
|
|
2020-05-05 19:21:54 -04:00
|
|
|
void cleanObject(Object *target);
|
2020-05-07 20:32:01 -04:00
|
|
|
void printAndClean(Object *target);
|
2020-05-06 02:12:58 -04:00
|
|
|
void allocObject(Object **spot, const Object src);
|
2020-05-14 23:51:59 -04:00
|
|
|
void appendList(Object *dest, const Object *src);
|
2020-05-05 19:21:54 -04:00
|
|
|
|
|
|
|
Object newObject(Type type);
|
2020-05-05 13:42:28 -04:00
|
|
|
Object listObject();
|
2020-05-08 12:00:14 -04:00
|
|
|
Object startList(const Object start);
|
2020-05-16 10:46:19 -04:00
|
|
|
Object cloneList(const Object src);
|
|
|
|
Object cloneObject(const Object src);
|
2020-05-05 13:42:28 -04:00
|
|
|
Object lambdaObject();
|
2020-05-06 02:12:58 -04:00
|
|
|
Object symbolObject();
|
2020-05-10 02:27:59 -04:00
|
|
|
Object objFromSlice(const char *string, int len);
|
2020-05-06 02:12:58 -04:00
|
|
|
Object boolObject(int b);
|
|
|
|
Object numberObject(int num);
|
2020-05-05 19:21:54 -04:00
|
|
|
Object errorObject(enum errorCode err);
|
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-15 16:28:16 -04:00
|
|
|
Object copyString(Object obj);
|
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
|