From 46018370c9d3b4f36ac6099742c64f22309d6507 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Thu, 24 Sep 2015 16:41:47 -0400 Subject: [PATCH] Added basic types and routines for defining an object --- build.rb | 2 +- source/gir.c | 249 ++++++++++++++++++++++++++++------------- source/gir.h | 19 +++- source/gir_internals.h | 21 ++++ source/main.c | 4 +- tests/main.c | 4 + tests/test_gir.c | 10 ++ 7 files changed, 223 insertions(+), 86 deletions(-) create mode 100644 tests/test_gir.c diff --git a/build.rb b/build.rb index f82f8fe..ef02517 100755 --- a/build.rb +++ b/build.rb @@ -6,7 +6,7 @@ require './modules/build-system/setup' #------------------------------------------------------------------------------ # Define the default compiler environment main_env = BuildEnv.new do |env| - env["CFLAGS"] += ['-O3', '-Wall', '-Wextra', '--std=c99', '--pedantic'] + env["CFLAGS"] += ['-g', '-O3', '-Wall', '-Wextra', '--std=c99', '--pedantic'] env["CPPPATH"] += ['source/'] + Dir['modules/atf/source/**/'] end diff --git a/source/gir.c b/source/gir.c index 52f97fe..1c224c8 100644 --- a/source/gir.c +++ b/source/gir.c @@ -1,116 +1,205 @@ #include "gir.h" #include "gir_internals.h" -struct Obj { - uint32_t type; - uint8_t data[]; -}; - -Obj* Lobby; -Obj* Nil; -Obj* Bool; -Obj* True; -Obj* False; -Obj* Num; -Obj* String; -Obj* Symbol; -Obj* List; -Obj* Array; -Obj* ByteArray; -Obj* Map; -Obj* Set; -Obj* Block; - -hamt_t InternPool; - -/* Private API Declarations +/* String Interning *****************************************************************************/ - -/* Top-Level API - *****************************************************************************/ -void gir_init(void* stack_btm) -{ - (void)stack_btm; -// hamt_init(&InternPool); +static uintptr_t intern(GirCtx* ctx, char* str) { + uintptr_t sym = (uintptr_t)hamt_lookup(&(ctx->intern_pool), str); + if (sym == 0) { + hamt_insert(&(ctx->intern_pool), str); + sym = (uintptr_t)str; + } + return sym; } -void gir_deinit(void) -{ +static void intern_key(void* entry, uint8_t** addr, size_t* len) { + *addr = (uint8_t*)entry; + *len = strlen((char*)entry); } -Obj* gir_evalexpr(FILE* input) -{ - (void)input; - return Nil; +static void intern_del(void* entry) { + (void)entry; // noop } -void gir_evalfile(FILE* input, const char* prompt) -{ - extern void exec_file(FILE* file, const char* prompt); - exec_file(input, prompt); +static int intern_cmp(void* a, void* b) { + return strcmp((char*)a, (char*)b); } -Obj* gir_send(Obj* rcvr, const char* sel, ...) -{ - (void)rcvr; - (void)sel; - return Nil; -} - - -/* Constructors +/* Slot Management *****************************************************************************/ -Obj* gir_mknum(double val) -{ +static Slot* slot_new(uintptr_t sel, Obj* val) { + (void)sel; (void)val; - return Nil; + return NULL; } -Obj* gir_mkstring(const char* val) -{ - (void)val; - return Nil; +static void slot_key(void* entry, uint8_t** addr, size_t* len) { + *addr = (uint8_t*)&(((Slot*)entry)->sel); + *len = sizeof(uintptr_t); } -Obj* gir_mksymbol(const char* val) -{ - (void)val; - return Nil; +static void slot_del(void* entry) { + free(entry); } -Obj* gir_mklist(size_t num, ...) -{ - (void)num; - return Nil; +static int slot_cmp(void* a, void* b) { + Slot* sa = (Slot*)a; + Slot* sb = (Slot*)b; + return sa->sel - sb->sel; } -Obj* gir_mkarray(size_t num, ...) -{ - (void)num; - return Nil; +/* Base Object Definition + *****************************************************************************/ +Obj* mkobject(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); + return obj; } -Obj* gir_mkbytearray(size_t num, uint8_t* val) +/* Main API + *****************************************************************************/ +GirCtx* gir_init(void) { - (void)num; - (void)val; - return Nil; + GirCtx* ctx = malloc(sizeof(struct GirCtx)); + hamt_init(&(ctx->intern_pool), &intern_key, &intern_del, &intern_cmp); + ctx->lobby = NULL; + return ctx; } -Obj* gir_mkmap(void) +void gir_evalfile(FILE* input, const char* prompt) { - return Nil; + (void)input; + (void)prompt; } -Obj* gir_mkset(void) +void gir_deinit(GirCtx* ctx) { - return Nil; + free(ctx); } -Obj* gir_mkblock(void) -{ - return Nil; -} + + + + + + + + + + +//Obj* Lobby; +//Obj* Nil; +//Obj* Bool; +//Obj* True; +//Obj* False; +//Obj* Num; +//Obj* String; +//Obj* Symbol; +//Obj* List; +//Obj* Array; +//Obj* ByteArray; +//Obj* Map; +//Obj* Set; +//Obj* Block; +// +//hamt_t InternPool; +// +///* Private API Declarations +// *****************************************************************************/ +// +///* Top-Level API +// *****************************************************************************/ +//void gir_init(void* stack_btm) +//{ +// (void)stack_btm; +//// hamt_init(&InternPool); +//} +// +//void gir_deinit(void) +//{ +//} +// +//Obj* gir_evalexpr(FILE* input) +//{ +// (void)input; +// return Nil; +//} +// +//void gir_evalfile(FILE* input, const char* prompt) +//{ +// extern void exec_file(FILE* file, const char* prompt); +// exec_file(input, prompt); +//} +// +//Obj* gir_send(Obj* rcvr, const char* sel, ...) +//{ +// (void)rcvr; +// (void)sel; +// return Nil; +//} +// +///* Constructors +// *****************************************************************************/ +//Obj* gir_mkobject(double val) +//{ +// (void)val; +// return Nil; +//} +// +//Obj* gir_mknum(double val) +//{ +// (void)val; +// return Nil; +//} +// +//Obj* gir_mkstring(const char* val) +//{ +// (void)val; +// return Nil; +//} +// +//Obj* gir_mksymbol(const char* val) +//{ +// (void)val; +// return Nil; +//} +// +//Obj* gir_mklist(size_t num, ...) +//{ +// (void)num; +// return Nil; +//} +// +//Obj* gir_mkarray(size_t num, ...) +//{ +// (void)num; +// return Nil; +//} +// +//Obj* gir_mkbytearray(size_t num, uint8_t* val) +//{ +// (void)num; +// (void)val; +// return Nil; +//} +// +//Obj* gir_mkmap(void) +//{ +// return Nil; +//} +// +//Obj* gir_mkset(void) +//{ +// return Nil; +//} +// +//Obj* gir_mkblock(void) +//{ +// return Nil; +//} /* Private API Definitions *****************************************************************************/ diff --git a/source/gir.h b/source/gir.h index ad8a57c..01c7266 100644 --- a/source/gir.h +++ b/source/gir.h @@ -8,11 +8,24 @@ #include typedef struct Obj Obj; +typedef struct GirCtx GirCtx; +typedef struct Slot Slot; -void gir_init(void* stack_btm); -void gir_deinit(void); -Obj* gir_evalexpr(FILE* input); +GirCtx* gir_init(void); void gir_evalfile(FILE* input, const char* prompt); +void gir_deinit(GirCtx* ctx); + + + + + + + + + + + +Obj* gir_evalexpr(FILE* input); Obj* gir_send(Obj* rcvr, const char* sel, ...); /* diff --git a/source/gir_internals.h b/source/gir_internals.h index 6c4b08b..21de7b4 100644 --- a/source/gir_internals.h +++ b/source/gir_internals.h @@ -84,4 +84,25 @@ void* hamt_lookup(hamt_t* trie, void* key); void hamt_insert(hamt_t* trie, void* key); +/****************************************************************************** + * User-facing Opaque Types + *****************************************************************************/ +struct Obj { + uint32_t type; + uint32_t size; + struct Obj* parent; + hamt_t slots; + uint8_t data[]; +}; + +struct GirCtx { + hamt_t intern_pool; + struct Obj* lobby; +}; + +struct Slot { + uintptr_t sel; + struct Obj* val; +}; + #endif /* GIR_INTERNALS_H */ diff --git a/source/main.c b/source/main.c index 71ad52e..0911e45 100644 --- a/source/main.c +++ b/source/main.c @@ -5,9 +5,9 @@ int main(int argc, char** argv) { (void)argc; (void)argv; - gir_init((void*)&(int){0}); + GirCtx* ctx = gir_init(); gir_evalfile(stdin, ":> "); - gir_deinit(); + gir_deinit(ctx); return 0; } diff --git a/tests/main.c b/tests/main.c index 5cc4a50..f8ac126 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,9 +1,13 @@ #include "atf.h" +#include "gir.h" int main(int argc, char** argv) { (void)argc; (void)argv; + GirCtx* ctx = gir_init(); + RUN_EXTERN_TEST_SUITE(GirTests); + gir_deinit(ctx); RUN_EXTERN_TEST_SUITE(LobbyTests); RUN_EXTERN_TEST_SUITE(NilTests); diff --git a/tests/test_gir.c b/tests/test_gir.c new file mode 100644 index 0000000..fc16a24 --- /dev/null +++ b/tests/test_gir.c @@ -0,0 +1,10 @@ +#include "atf.h" +#include "gir.h" + +TEST_SUITE(GirTests) { + /* Verify core object behavior + *************************************************************************/ + TEST(Verify_mkobject_constructs_an_object_with_the_lobby_as_a_parent) { + + } +} -- 2.55.0