pebblisp/src/object.h

154 lines
3.9 KiB
C
Raw Normal View History

2020-05-05 13:42:28 -04:00
#ifndef OBJECT_H
#define OBJECT_H
#include <stdlib.h>
#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) \
if(_list && _list->type == TYPE_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
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,
INDEX_PAST_END,
2020-05-05 13:42:28 -04:00
};
typedef enum Type {
TYPE_NUMBER,
TYPE_BOOL,
TYPE_LIST,
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;
struct Lambda;
struct Environment;
struct Slice;
struct Other;
struct Error;
2020-05-05 13:42:28 -04:00
struct Object {
Type type;
Object *forward;
union {
int number;
Object *list;
char *string;
Object (*func)(Object, Object, struct Environment *);
struct Lambda *lambda;
struct Other *other;
struct Error *error;
2020-05-05 13:42:28 -04:00
};
};
struct Error {
enum errorCode code;
char* context;
};
2020-05-05 13:42:28 -04:00
struct Lambda {
Object params;
Object body;
};
struct Other {
void (*cleanup)(Object*);
Object (*clone)(struct Other*);
void *data;
};
char* stringNObj(char *dest, const Object *obj, size_t len);
2020-05-05 13:42:28 -04:00
char* stringObj(char *dest, const Object *obj);
void printList(const Object *list);
void printType(const Object *obj);
2020-05-05 13:42:28 -04:00
void printObj(const Object *obj);
void _printObj(const Object *obj, int newline);
void debugObj(const Object *obj);
2020-05-07 20:32:01 -04:00
void printErr(const Object *obj);
2020-05-05 13:42:28 -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);
void deleteList(Object *dest);
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);
void allocObject(Object **spot, const Object src);
void appendList(Object *dest, const Object *src);
2020-05-05 19:21:54 -04:00
int isStringy(const Object test);
int isValidType(const Object test);
int isError(const Object obj, const enum errorCode err);
int bothAre(const enum Type type, const Object *obj1, const Object *obj2);
int eitherIs(const enum Type type, const Object *obj1, const Object *obj2);
int areSameType(const Object *obj1, const Object *obj2);
Object cloneList(const Object src);
Object cloneString(Object obj);
Object cloneLambda(const Object old);
Object cloneObject(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();
Object startList(const Object start);
Object objFromSlice(const char *string, int len);
Object stringFromSlice(const char *string, int len);
Object symFromSlice(const char *string, int len);
Object boolObject(int b);
Object numberObject(int num);
Object otherObject();
2020-05-05 19:21:54 -04:00
Object errorObject(enum errorCode err);
Object errorWithContext(enum errorCode err, const char* context);
void errorAddContext(Object *o, const char* context);
Object constructLambda(const Object *params, const Object *body);
2020-05-05 13:42:28 -04:00
// Object version of listLength()
Object len(Object obj1, Object, struct Environment *);
2020-05-05 13:42:28 -04:00
#endif