.
This commit is contained in:
2
Makefile
2
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}
|
||||
|
||||
58
lib/array.c
Normal file
58
lib/array.c
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "priv.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
||||
14
lib/priv.h
14
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
|
||||
//
|
||||
|
||||
22
test/tests.c
22
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");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user