added list object

This commit is contained in:
Francesco Cozzuto
2021-11-03 13:44:41 +01:00
parent 9d600b66be
commit d4e9eb23d3
4 changed files with 159 additions and 15 deletions
+1
View File
@@ -17,6 +17,7 @@ mkdir temp/objects
gcc -c src/objects/heap.c -o temp/objects/heap.o $FLAGS 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_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_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_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_bool.c -o temp/objects/o_bool.o $FLAGS
gcc -c src/objects/o_float.c -o temp/objects/o_float.o $FLAGS gcc -c src/objects/o_float.c -o temp/objects/o_float.o $FLAGS
+3 -15
View File
@@ -1,17 +1,5 @@
p = (false);
if p: if 'hello' == 'hello':
{ print('Yup!\n');
a = 1;
}
else else
a = 2; print('Nop!\n');
if 'hello2' == 'hello': print('Yup!'); else print('Nop!');
print('\n');
print(a);
print(1 < 1 < 1);
return a;
+154
View File
@@ -0,0 +1,154 @@
#include <assert.h>
#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;
}
+1
View File
@@ -83,6 +83,7 @@ Object* Object_Next (Object *iter, Heap *heap, Error *err);
Object* Object_Prev (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_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_NewNone(Heap *heap, Error *error);
Object* Object_FromInt (long long int val, Heap *heap, Error *error); Object* Object_FromInt (long long int val, Heap *heap, Error *error);