From: Mike D. Lowis Date: Fri, 25 Sep 2015 19:52:35 +0000 (-0400) Subject: Started building up the initial object environment X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=e7bb2ffb675fe9215252a7f62ceec8940d5a6e8b;p=proto%2Fgir.git Started building up the initial object environment --- diff --git a/source/gir.c b/source/gir.c index 6961037..7aeeaa5 100644 --- a/source/gir.c +++ b/source/gir.c @@ -1,5 +1,5 @@ -#include "gir.h" #include "gir_internals.h" +#include "gir.h" /* String Interning *****************************************************************************/ @@ -50,12 +50,12 @@ static int slot_cmp(void* a, void* b) { /* Base Object Definition *****************************************************************************/ -Obj* obj_new(uint32_t type, size_t datasz) { +Obj* obj_new(Obj* parent, uint32_t type, size_t datasz) { Obj* obj = (Obj*)malloc(sizeof(struct Obj) + datasz); - obj->type = type; - obj->size = datasz; - obj->parent = NULL; - hamt_init(&(obj->slots), &slot_key, &slot_del, &slot_cmp); + obj->hdr.type = type; + obj->hdr.size = datasz; + obj->hdr.parent = parent; + hamt_init(&(obj->hdr.slots), &slot_key, &slot_del, &slot_cmp); return obj; } @@ -63,33 +63,98 @@ void obj_set(Obj* obj, uintptr_t sel, Obj* val) { Slot* slot = (Slot*)malloc(sizeof(Slot)); slot->sel = sel; slot->val = val; - hamt_insert(&(obj->slots), slot); + hamt_insert(&(obj->hdr.slots), slot); } Obj* obj_get(Obj* obj, uintptr_t sel) { Slot slot = { sel, NULL }; - Slot* entry = hamt_lookup(&(obj->slots), &slot); + Slot* entry = hamt_lookup(&(obj->hdr.slots), &slot); return (entry != NULL) ? entry->val : NULL; } +/* Base Environment Initialization + *****************************************************************************/ +static void init_nil(GirCtx* ctx) { + ctx->Nil = obj_new(NULL, TYPE_BARE, 0); + obj_set(ctx->Lobby, intern(ctx, "Nil"), ctx->Nil); +} + +static void init_bool(GirCtx* ctx) { + ctx->Bool = obj_new(NULL, TYPE_BARE, 0); + obj_set(ctx->Lobby, intern(ctx, "Bool"), ctx->Bool); + obj_set(ctx->Lobby, intern(ctx, "True"), obj_new(ctx->Bool, TYPE_BARE, 0)); + obj_set(ctx->Lobby, intern(ctx, "False"), obj_new(ctx->Bool, TYPE_BARE, 0)); +} + +//static void init_num(GirCtx* ctx) { +// ctx->Num = obj_new(TYPE_BARE, 0); +// obj_set(ctx->Lobby, intern(ctx, "Num"), ctx->Num); +//} +// +//static void init_string(GirCtx* ctx) { +// ctx->String = obj_new(TYPE_BARE, 0); +// obj_set(ctx->Lobby, intern(ctx, "String"), ctx->String); +//} +// +//static void init_symbol(GirCtx* ctx) { +// ctx->Symbol = obj_new(TYPE_BARE, 0); +// obj_set(ctx->Lobby, intern(ctx, "Symbol"), ctx->Symbol); +//} +// +//static void init_list(GirCtx* ctx) { +// ctx->List = obj_new(TYPE_BARE, 0); +// obj_set(ctx->Lobby, intern(ctx, "List"), ctx->List); +//} +// +//static void init_array(GirCtx* ctx) { +// ctx->Array = obj_new(TYPE_BARE, 0); +// obj_set(ctx->Lobby, intern(ctx, "Array"), ctx->Array); +//} +// +//static void init_map(GirCtx* ctx) { +// ctx->Map = obj_new(TYPE_BARE, 0); +// obj_set(ctx->Lobby, intern(ctx, "Map"), ctx->Map); +//} +// +//static void init_set(GirCtx* ctx) { +// ctx->Set = obj_new(TYPE_BARE, 0); +// obj_set(ctx->Lobby, intern(ctx, "Set"), ctx->Set); +//} +// +//static void init_block(GirCtx* ctx) { +// ctx->Block = obj_new(TYPE_BARE, 0); +// obj_set(ctx->Lobby, intern(ctx, "Block"), ctx->Block); +//} + +static void init_lobby(GirCtx* ctx) { + ctx->Lobby = obj_new(NULL, TYPE_BARE, 0); + init_nil(ctx); + init_bool(ctx); + //init_num(ctx); + //init_string(ctx); + //init_symbol(ctx); + //init_list(ctx); + //init_array(ctx); + //init_map(ctx); + //init_set(ctx); + //init_block(ctx); +} + /* Main API *****************************************************************************/ -GirCtx* gir_init(void) -{ +GirCtx* gir_init(void) { GirCtx* ctx = malloc(sizeof(struct GirCtx)); hamt_init(&(ctx->intern_pool), &intern_key, &intern_del, &intern_cmp); - ctx->lobby = NULL; + init_lobby(ctx); return ctx; } -void gir_evalfile(FILE* input, const char* prompt) -{ +void gir_evalfile(FILE* input, const char* prompt) { (void)input; (void)prompt; } -void gir_deinit(GirCtx* ctx) -{ +void gir_deinit(GirCtx* ctx) { free(ctx); } diff --git a/source/gir.h b/source/gir.h index 01c7266..78fc794 100644 --- a/source/gir.h +++ b/source/gir.h @@ -11,6 +11,29 @@ typedef struct Obj Obj; typedef struct GirCtx GirCtx; typedef struct Slot Slot; +/****************************************************************************** + * + *****************************************************************************/ + + +//*/ +//extern Obj* Lobby; +//extern Obj* Nil; +//extern Obj* Bool; +//extern Obj* True; +//extern Obj* False; +//extern Obj* Num; +//extern Obj* String; +//extern Obj* Symbol; +//extern Obj* List; +//extern Obj* Array; +//extern Obj* ByteArray; +//extern Obj* Map; +//extern Obj* Set; +//extern Obj* Block; +/****************************************************************************** + * Top Level API + *****************************************************************************/ GirCtx* gir_init(void); void gir_evalfile(FILE* input, const char* prompt); void gir_deinit(GirCtx* ctx); @@ -25,48 +48,48 @@ void gir_deinit(GirCtx* ctx); -Obj* gir_evalexpr(FILE* input); -Obj* gir_send(Obj* rcvr, const char* sel, ...); - -/* -Lobby - Nil - Bool - True - False - Num - String - Symbol - List - Array - ByteArray - Map - Set - Block -*/ -extern Obj* Lobby; -extern Obj* Nil; -extern Obj* Bool; -extern Obj* True; -extern Obj* False; -extern Obj* Num; -extern Obj* String; -extern Obj* Symbol; -extern Obj* List; -extern Obj* Array; -extern Obj* ByteArray; -extern Obj* Map; -extern Obj* Set; -extern Obj* Block; - -Obj* gir_mknum(double val); -Obj* gir_mkstring(const char* val); -Obj* gir_mksymbol(const char* val); -Obj* gir_mklist(size_t num, ...); -Obj* gir_mkarray(size_t num, ...); -Obj* gir_mkbytearray(size_t num, uint8_t* val); -Obj* gir_mkmap(void); -Obj* gir_mkset(void); -Obj* gir_mkblock(void); +//Obj* gir_evalexpr(FILE* input); +//Obj* gir_send(Obj* rcvr, const char* sel, ...); +// +///* +//Lobby +// Nil +// Bool +// True +// False +// Num +// String +// Symbol +// List +// Array +// ByteArray +// Map +// Set +// Block +//*/ +//extern Obj* Lobby; +//extern Obj* Nil; +//extern Obj* Bool; +//extern Obj* True; +//extern Obj* False; +//extern Obj* Num; +//extern Obj* String; +//extern Obj* Symbol; +//extern Obj* List; +//extern Obj* Array; +//extern Obj* ByteArray; +//extern Obj* Map; +//extern Obj* Set; +//extern Obj* Block; +// +//Obj* gir_mknum(double val); +//Obj* gir_mkstring(const char* val); +//Obj* gir_mksymbol(const char* val); +//Obj* gir_mklist(size_t num, ...); +//Obj* gir_mkarray(size_t num, ...); +//Obj* gir_mkbytearray(size_t num, uint8_t* val); +//Obj* gir_mkmap(void); +//Obj* gir_mkset(void); +//Obj* gir_mkblock(void); #endif /* GIR_H */ diff --git a/source/gir_internals.h b/source/gir_internals.h index b68c2f0..3fd6b31 100644 --- a/source/gir_internals.h +++ b/source/gir_internals.h @@ -98,17 +98,34 @@ void hamt_insert(hamt_t* trie, void* key); /****************************************************************************** * User-facing Opaque Types *****************************************************************************/ -struct Obj { +struct ObjHdr { uint32_t type; uint32_t size; struct Obj* parent; hamt_t slots; +}; + +struct Obj { + struct ObjHdr hdr; uint8_t data[]; }; struct GirCtx { hamt_t intern_pool; - struct Obj* lobby; + struct Obj* Lobby; + struct Obj* Nil; + struct Obj* Bool; + struct Obj* True; + struct Obj* False; + struct Obj* Num; + struct Obj* String; + struct Obj* Symbol; + struct Obj* List; + struct Obj* Array; + struct Obj* ByteArray; + struct Obj* Map; + struct Obj* Set; + struct Obj* Block; }; struct Slot { @@ -116,4 +133,70 @@ struct Slot { struct Obj* val; }; +/****************************************************************************** + * Builtin Object Types + *****************************************************************************/ +#define TYPE_BARE 0 +#define TYPE_BOOL 1 +#define TYPE_NUM 2 +#define TYPE_STRING 3 +#define TYPE_SYMBOl 4 +#define TYPE_LIST 5 +#define TYPE_ARRAY 6 +#define TYPE_MAP 7 +#define TYPE_SET 8 +#define TYPE_BLOCK 9 + +typedef struct ObjBool { + struct ObjHdr hdr; + bool val; +} ObjBool; + +typedef struct ObjNum { + struct ObjHdr hdr; + double val; +} ObjNum; + +typedef struct ObjString { + struct ObjHdr hdr; + char* val; +} ObjString; + +typedef struct ObjSymbol { + struct ObjHdr hdr; + uintptr_t val; +} ObjSymbol; + +typedef struct ObjList { + struct ObjHdr hdr; + slist_t val; +} ObjList; + +//typedef struct ObjArray { +// struct ObjHdr hdr; +// vec_t val; +//} ObjArray; + +typedef struct ObjMap { + struct ObjHdr hdr; + hamt_t val; +} ObjMap; + +typedef struct ObjSet { + struct ObjHdr hdr; + hamt_t val; +} ObjSet; + +//typedef struct ObjBlock { +// struct ObjHdr hdr; +// bytecode_t val; +//} ObjBlock; + +/****************************************************************************** + * Raw Object Operations + *****************************************************************************/ +struct Obj* obj_new(struct Obj* parent, uint32_t type, size_t datasz); +void obj_set(struct Obj* obj, uintptr_t sel, struct Obj* val); +struct Obj* obj_get(struct Obj* obj, uintptr_t sel); + #endif /* GIR_INTERNALS_H */ diff --git a/tests/test_gir.c b/tests/test_gir.c index fc16a24..93514e4 100644 --- a/tests/test_gir.c +++ b/tests/test_gir.c @@ -1,10 +1,39 @@ #include "atf.h" #include "gir.h" +#include "gir_internals.h" TEST_SUITE(GirTests) { /* Verify core object behavior *************************************************************************/ - TEST(Verify_mkobject_constructs_an_object_with_the_lobby_as_a_parent) { + TEST(Constructing a raw object) { + Obj* obj = obj_new(NULL, 0, 0); + CHECK(obj != NULL); + CHECK(obj->hdr.type == 0); + CHECK(obj->hdr.size == 0); + CHECK(obj->hdr.parent == NULL); + } + + TEST(Constructing a raw object with extra space for data) { + Obj* obj = obj_new(NULL, 1, sizeof(int)); + CHECK(obj != NULL); + CHECK(obj->hdr.type == 1); + CHECK(obj->hdr.size == sizeof(int)); + CHECK(obj->hdr.parent == NULL); + } + + TEST(Adding a slot to an object) { + Obj* obj = obj_new(NULL, 1, sizeof(int)); + obj_set(obj, 42, obj); + CHECK(obj != NULL); + CHECK(obj == obj_get(obj, 42)); + } + TEST(Set the value of a slot for an object) { + Obj* obj = obj_new(NULL, 1, sizeof(int)); + obj_set(obj, 42, obj); + CHECK(obj != NULL); + CHECK(obj == obj_get(obj, 42)); + obj_set(obj, 42, NULL); + CHECK(NULL == obj_get(obj, 42)); } }