From 9607c779394e2d799f835d901ab73393a4d9b050 Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Wed, 13 May 2026 08:35:55 -0500 Subject: [PATCH] . --- Makefile | 2 +- lib/array.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/priv.h | 14 +++++++++++++ test/tests.c | 22 ++++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 lib/array.c diff --git a/Makefile b/Makefile index 9a0d5bb..1aa715f 100644 --- a/Makefile +++ b/Makefile @@ -77,7 +77,7 @@ uninstall: # executable files # -LIB_SRC=lib/value.o lib/stack.o lib/heap.o lib/vm.o lib/utils.o +LIB_SRC=lib/value.o lib/stack.o lib/array.o lib/heap.o lib/vm.o lib/utils.o tyche: CFLAGS += ${RELEASE_CFLAGS} tyche: LDFLAGS += ${RELEASE_LDFLAGS} diff --git a/lib/array.c b/lib/array.c new file mode 100644 index 0000000..326fe10 --- /dev/null +++ b/lib/array.c @@ -0,0 +1,58 @@ +#include "priv.h" + +#include + +struct Array { + size_t n; + size_t cap; + VALUE* items; +}; + +Array* array_new(void) +{ + Array* a = xcalloc(1, sizeof(Array)); + a->n = 0; + a->cap = 1; + a->items = xmalloc(1 * sizeof(Array)); + a->items[0] = create_value_nil(); + return a; +} + +void array_destroy(Array* a) +{ + free(a->items); + free(a); +} + +size_t array_len(Array* a) +{ + return a->n; +} + +VALUE array_get(Array* a, size_t pos) +{ + if (pos >= a->n) + return create_value_nil(); + return a->items[pos]; +} + +void array_set(Array* a, size_t pos, VALUE v) +{ + // extend array + if (pos > a->cap - 1) { + a->cap *= 2; + a->items = xrealloc(a->items, a->cap * sizeof(VALUE)); + for (size_t i = a->n; i < a->cap; ++i) + a->items[i] = create_value_nil(); + } + + // set item + a->items[pos] = v; + if (pos + 1 > a->n) + a->n = pos + 1; +} + +void array_append(Array* a, VALUE v) +{ + array_set(a, a->n, v); +} diff --git a/lib/priv.h b/lib/priv.h index 422e1ef..014e6a4 100644 --- a/lib/priv.h +++ b/lib/priv.h @@ -67,6 +67,20 @@ size_t stack_fp_level(Stack* s); size_t stack_collectable_array(Stack* s, VALUE** values); +// +// HEAP ARRAY +// + +typedef struct Array Array; + +Array* array_new(void); +void array_destroy(Array* a); + +size_t array_len(Array* a); +VALUE array_get(Array* a, size_t pos); +void array_set(Array* a, size_t pos, VALUE v); +void array_append(Array* a, VALUE v); + // // HEAP // diff --git a/test/tests.c b/test/tests.c index 91c5a41..fb6f0f3 100644 --- a/test/tests.c +++ b/test/tests.c @@ -91,6 +91,28 @@ int main() stack_destroy(s); } + { + printf("### Arrays\n"); + + Array* a = array_new(); + assert(array_len(a) == 0); + + array_set(a, 1, create_value_integer(40)); + assert(array_len(a) == 2); + assert(value_type(array_get(a, 0)) == TT_NIL); + assert(value_type(array_get(a, 1)) == TT_INTEGER); + + array_append(a, create_value_integer(50)); + assert(array_len(a) == 3); + assert(value_integer(array_get(a, 2)) == 50); + + array_set(a, 2, create_value_integer(60)); + assert(array_len(a) == 3); + assert(value_integer(array_get(a, 2)) == 60); + + array_destroy(a); + } + { printf("### Heap - strings\n");