Initial commit
This commit is contained in:
73
include/lisp/object.h
Normal file
73
include/lisp/object.h
Normal file
@ -0,0 +1,73 @@
|
||||
#ifndef INCLUDED_OBJECT_H
|
||||
#define INCLUDED_OBJECT_H
|
||||
|
||||
#include "lisp/method_macro.h"
|
||||
#include "lisp/util.h"
|
||||
|
||||
#include <ht.h>
|
||||
#include <refcount/refcount.h>
|
||||
#include <stddef.h>
|
||||
|
||||
LISP_BEGIN_DECLS
|
||||
|
||||
#define DECLARE_CLASS(Name) \
|
||||
typedef struct Name Name; \
|
||||
typedef struct Name##Class Name##Class
|
||||
|
||||
// clang-format off
|
||||
#define ELEVENTH(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, ...) a11
|
||||
#define COUNT_ARGS(...) ELLEVENTH(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
|
||||
// clang-format on
|
||||
|
||||
#undef COUNT_ARGS
|
||||
#undef TENTH
|
||||
|
||||
typedef struct Object Object;
|
||||
typedef struct Class Class;
|
||||
|
||||
struct Object {
|
||||
RefcountEntry refcount;
|
||||
Class *class;
|
||||
|
||||
Object **slots;
|
||||
};
|
||||
|
||||
struct Class {
|
||||
Object base;
|
||||
Class *superclass;
|
||||
HTTable *subclasses;
|
||||
size_t object_size;
|
||||
|
||||
HTTable *slots_indicies;
|
||||
uint64_t nslots;
|
||||
HTTable *methods;
|
||||
|
||||
Object *(*construct)(Object *self, Object *args);
|
||||
};
|
||||
|
||||
Object *make_object(Object *class, Object *args);
|
||||
|
||||
DECLARE_CLASS(Pair);
|
||||
|
||||
struct Pair {
|
||||
Object base;
|
||||
|
||||
Object *head;
|
||||
Object *tail;
|
||||
};
|
||||
struct PairClass {
|
||||
Class base;
|
||||
|
||||
Object *(*head)(Object *self);
|
||||
Object *(*sethead)(Object *self, Object *new_head);
|
||||
Object *(*tail)(Object *self);
|
||||
Object *(*settail)(Object *self, Object *new_tail);
|
||||
};
|
||||
|
||||
METHOD(Object *, head, ((Pair *) self));
|
||||
VOID_METHOD(sethead, ((Pair *) self));
|
||||
METHOD(Object *, tail, ((Pair *) self));
|
||||
VOID_METHOD(settail, ((Pair *) self));
|
||||
|
||||
LISP_END_DECLS
|
||||
#endif
|
Reference in New Issue
Block a user