From e1d05f02ab7c865d752d38d0611b95583876bd8b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 8 Mar 2025 15:22:47 +0000 Subject: Partial implementation of array and lists --- runtime/meson.build | 1 + runtime/src/shed/namespaces/Std/Lists.c | 84 +++++++++++++++++++++++++++++++++ runtime/src/shed/namespaces/Std/Lists.h | 15 ++++++ 3 files changed, 100 insertions(+) create mode 100644 runtime/src/shed/namespaces/Std/Lists.c create mode 100644 runtime/src/shed/namespaces/Std/Lists.h (limited to 'runtime') diff --git a/runtime/meson.build b/runtime/meson.build index 70bf51fd..90e17f4d 100644 --- a/runtime/meson.build +++ b/runtime/meson.build @@ -21,6 +21,7 @@ static_library( 'src/shed/primitives.c', 'src/shed/strings.c', 'src/shed/namespaces/Shed/Builtins.c', + 'src/shed/namespaces/Std/Lists.c', 'src/shed/namespaces/Std/Process.c', gc_source, c_args: ['-DSHED_GC_COLLECT_INTERVAL=' + get_option('gc_collect_interval').to_string()], diff --git a/runtime/src/shed/namespaces/Std/Lists.c b/runtime/src/shed/namespaces/Std/Lists.c new file mode 100644 index 00000000..41760d83 --- /dev/null +++ b/runtime/src/shed/namespaces/Std/Lists.c @@ -0,0 +1,84 @@ +#include "shed/gc.h" +#include "shed/ints.h" +#include "./Lists.h" + +struct ShedArrayBuilder { + struct ShedGcObjectHeader object_header; + PatioInt capacity; + PatioInt size; + struct ShedArray* array; +}; + +struct ShedArray { + struct ShedGcObjectHeader object_header; + PatioInt size; + ShedObject elements[]; +}; + +ShedObject shed__runtime__namespace__Std__Lists__arrayBuilder(ShedClosurePointer closurePointer, ShedObject capacityObject) { + // TODO: runtime type tag / GC support (don't set mark to 0b110, set appropriate runtime type) + + PatioInt capacity = shed__runtime__int_value(capacityObject); + + struct ShedArray* array = shed__runtime__gc_alloc( + sizeof(struct ShedArray) + sizeof(ShedObject) * capacity, + 0 + ); + array->object_header.mark = 0b110; + array->size = 0; + + struct ShedArrayBuilder* array_builder = shed__runtime__gc_alloc(sizeof(struct ShedArrayBuilder), 0); + array_builder->object_header.mark = 0b110; + array_builder->capacity = capacity; + array_builder->size = 0; + array_builder->array = array; + + return (ShedObject) array_builder; +} + +ShedObject shed__runtime__namespace__Std__Lists__method__Array__unsafeGet(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject arg1) { + struct ShedArray* array = (struct ShedArray*) arg0; + PatioInt index = shed__runtime__int_value(arg1); + return array->elements[index]; +} + +ShedObject shed__runtime__namespace__Std__Lists__method__Array__size(ShedClosurePointer closurePointer, ShedObject arg0) { + struct ShedArray* array = (struct ShedArray*) arg0; + return shed__runtime__int_create(array->size); +} + +ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__capacity(ShedClosurePointer closurePointer, ShedObject arg0) { + struct ShedArrayBuilder* array_builder = (struct ShedArrayBuilder*) arg0; + return shed__runtime__int_create(array_builder->capacity); +} + +ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__size(ShedClosurePointer closurePointer, ShedObject arg0) { + struct ShedArrayBuilder* array_builder = (struct ShedArrayBuilder*) arg0; + return shed__runtime__int_create(array_builder->size); +} + +ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__unsafeAdd(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject arg1) { + // TODO: track whether array has been built or added to + struct ShedArrayBuilder* array_builder = (struct ShedArrayBuilder*) arg0; + array_builder->array->elements[array_builder->array->size] = arg1; + array_builder->array->size += 1; + + struct ShedArrayBuilder* new_array_builder = shed__runtime__gc_alloc(sizeof(struct ShedArrayBuilder), 0); + new_array_builder->object_header.mark = 0b110; + new_array_builder->capacity = array_builder->capacity; + new_array_builder->size = array_builder->size + 1; + new_array_builder->array = array_builder->array; + + return (ShedObject) new_array_builder; +} + +ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__unsafeEnlarge(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject capacity) { + // TODO: implement + return arg0; +} + +ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__build(ShedClosurePointer closurePointer, ShedObject arg0) { + // TODO: track whether array has been built or added to + struct ShedArrayBuilder* array_builder = (struct ShedArrayBuilder*) arg0; + return (ShedObject) array_builder->array; +} diff --git a/runtime/src/shed/namespaces/Std/Lists.h b/runtime/src/shed/namespaces/Std/Lists.h new file mode 100644 index 00000000..373d58d8 --- /dev/null +++ b/runtime/src/shed/namespaces/Std/Lists.h @@ -0,0 +1,15 @@ +#ifndef SHED_NAMESPACES__Std__Lists__H +#define SHED_NAMESPACES__Std__Lists__H + +#include "shed/shed.h" + +ShedObject shed__runtime__namespace__Std__Lists__arrayBuilder(ShedClosurePointer closurePointer, ShedObject capacity); +ShedObject shed__runtime__namespace__Std__Lists__method__Array__unsafeGet(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject arg1); +ShedObject shed__runtime__namespace__Std__Lists__method__Array__size(ShedClosurePointer closurePointer, ShedObject arg0); +ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__capacity(ShedClosurePointer closurePointer, ShedObject arg0); +ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__size(ShedClosurePointer closurePointer, ShedObject arg0); +ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__unsafeAdd(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject arg1); +ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__unsafeEnlarge(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject capacity); +ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__build(ShedClosurePointer closurePointer, ShedObject arg0); + +#endif -- cgit v1.2.3