From d4e9eb23d3cde7a21938f7be387c2fbb4e7cc95d Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Wed, 3 Nov 2021 13:44:41 +0100 Subject: [PATCH] added list object --- build.sh | 1 + samples/if-else.noja | 18 +---- src/objects/o_list.c | 154 ++++++++++++++++++++++++++++++++++++++++++ src/objects/objects.h | 1 + 4 files changed, 159 insertions(+), 15 deletions(-) create mode 100644 src/objects/o_list.c diff --git a/build.sh b/build.sh index 89cfb2a..a02769e 100755 --- a/build.sh +++ b/build.sh @@ -17,6 +17,7 @@ mkdir temp/objects gcc -c src/objects/heap.c -o temp/objects/heap.o $FLAGS gcc -c src/objects/o_int.c -o temp/objects/o_int.o $FLAGS gcc -c src/objects/o_map.c -o temp/objects/o_map.o $FLAGS +gcc -c src/objects/o_list.c -o temp/objects/o_list.o $FLAGS gcc -c src/objects/o_none.c -o temp/objects/o_none.o $FLAGS gcc -c src/objects/o_bool.c -o temp/objects/o_bool.o $FLAGS gcc -c src/objects/o_float.c -o temp/objects/o_float.o $FLAGS diff --git a/samples/if-else.noja b/samples/if-else.noja index 976aeb5..6033fae 100644 --- a/samples/if-else.noja +++ b/samples/if-else.noja @@ -1,17 +1,5 @@ -p = (false); -if p: - { - a = 1; - } +if 'hello' == 'hello': + print('Yup!\n'); else - a = 2; - -if 'hello2' == 'hello': print('Yup!'); else print('Nop!'); -print('\n'); -print(a); - -print(1 < 1 < 1); - - -return a; \ No newline at end of file + print('Nop!\n'); \ No newline at end of file diff --git a/src/objects/o_list.c b/src/objects/o_list.c new file mode 100644 index 0000000..cb55cb7 --- /dev/null +++ b/src/objects/o_list.c @@ -0,0 +1,154 @@ +#include +#include "../utils/defs.h" +#include "objects.h" + +typedef struct { + Object base; + int capacity, count; + Object **vals; +} ListObject; + +static Object *select(Object *self, Object *key, Heap *heap, Error *err); +static _Bool insert(Object *self, Object *key, Object *val, Heap *heap, Error *err); +static int count(Object *self); + +static const Type t_list = { + .base = (Object) { .type = &t_type, .flags = Object_STATIC }, + .name = "list", + .size = sizeof (ListObject), + .select = select, + .insert = insert, + .count = count, +}; + +static inline int calc_capacity(int mapper_size) +{ + return mapper_size * 2.0 / 3.0; +} + +Object *Object_NewList(int capacity, Heap *heap, Error *error) +{ + // Handle default args. + if(capacity < 8) + capacity = 8; + + // Make the thing. + ListObject *obj; + { + obj = (ListObject*) Heap_Malloc(heap, &t_list, error); + + if(obj == NULL) + return NULL; + + obj->count = 0; + obj->capacity = capacity; + obj->vals = Heap_RawMalloc(heap, sizeof(Object*) * capacity, error); + + if(obj->vals == NULL) + return NULL; + } + + return (Object*) obj; +} + +static Object *select(Object *self, Object *key, Heap *heap, Error *error) +{ + assert(self != NULL); + assert(self->type == &t_list); + assert(key != NULL); + assert(heap != NULL); + assert(error != NULL); + + if(!Object_IsInt(key)) + { + Error_Report(error, 0, "Non integer key"); + return NULL; + } + + int idx = Object_ToInt(key, error); + assert(error->occurred == 0); + + ListObject *list = (ListObject*) self; + + if(idx < 0 || idx >= list->count) + { + Error_Report(error, 0, "Out of range index"); + return NULL; + } + + return list->vals[idx]; +} + +static unsigned int calc_new_capacity(unsigned int old_capacity) +{ + return old_capacity * 2; +} + +static _Bool grow(ListObject *list, Heap *heap, Error *error) +{ + assert(list != NULL); + + int new_capacity = calc_new_capacity(list->capacity); + + Object **vals = Heap_RawMalloc(heap, sizeof(Object*) * new_capacity, error); + + if(vals == NULL) + return 0; + + for(int i = 0; i < list->count; i += 1) + vals[i] = list->vals[i]; + + list->vals = vals; + list->capacity = new_capacity; + return 1; +} + +static _Bool insert(Object *self, Object *key, Object *val, Heap *heap, Error *error) +{ + assert(error != NULL); + assert(key != NULL); + assert(val != NULL); + assert(heap != NULL); + assert(self != NULL); + assert(self->type == &t_list); + + ListObject *list = (ListObject*) self; + + if(!Object_IsInt(key)) + { + Error_Report(error, 0, "Non integer key"); + return NULL; + } + + int idx = Object_ToInt(key, error); + assert(error->occurred == 0); + + if(idx < 0 || idx > list->count) + { + Error_Report(error, 0, "Out of range index"); + return NULL; + } + + if(idx == list->count) + { + if(list->count == list->capacity) + if(!grow(list, heap, error)) + return 0; + + list->vals[list->count] = val; + list->count += 1; + } + else + { + list->vals[idx] = val; + } + + return 1; +} + +static int count(Object *self) +{ + ListObject *list = (ListObject*) self; + + return list->count; +} \ No newline at end of file diff --git a/src/objects/objects.h b/src/objects/objects.h index 020cca0..b4beceb 100644 --- a/src/objects/objects.h +++ b/src/objects/objects.h @@ -83,6 +83,7 @@ Object* Object_Next (Object *iter, Heap *heap, Error *err); Object* Object_Prev (Object *iter, Heap *heap, Error *err); Object* Object_NewMap(int num, Heap *heap, Error *error); +Object* Object_NewList(int capacity, Heap *heap, Error *error); Object* Object_NewNone(Heap *heap, Error *error); Object* Object_FromInt (long long int val, Heap *heap, Error *error);