new built-in keysof and rewrote the json parser

This commit is contained in:
cozis
2022-12-06 22:43:13 +01:00
parent 7e3eb6364a
commit 380b8a8917
11 changed files with 521 additions and 219 deletions
+15
View File
@@ -317,6 +317,20 @@ static int bin_istypeof(Runtime *runtime, Object **argv, unsigned int argc, Obje
return 1;
}
static int bin_keysof(Runtime *runtime, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Error *error)
{
UNUSED(argc);
UNUSED(rets);
UNUSED(runtime);
ASSERT(argc == 1);
Heap *heap = Runtime_GetHeap(runtime);
Object *keys = Object_KeysOf(argv[0], heap, error);
if (keys == NULL)
return -1;
rets[0] = keys;
return 1;
}
void bins_basic_init(StaticMapSlot slots[])
{
slots[0].as_type = Object_GetTypeType();
@@ -361,5 +375,6 @@ StaticMapSlot bins_basic[] = {
{ "error", SM_FUNCT, .as_funct = bin_error, .argc = 1 },
{ "assert", SM_FUNCT, .as_funct = bin_assert, .argc = -1 },
{ "istypeof", SM_FUNCT, .as_funct = bin_istypeof, .argc = 2, },
{ "keysof", SM_FUNCT, .as_funct = bin_keysof, .argc = 1, },
{ NULL, SM_END, {}, {} },
};
+10
View File
@@ -274,4 +274,14 @@ static void buffer_print(Object *self, FILE *fp)
{
BufferObject *buffer = (BufferObject*) self;
print_bytes(fp, buffer->payload->body + buffer->offset, buffer->length);
}
static Object*
keysof(Object *self,
Heap *heap,
Error *error)
{
BufferObject *buffer = (BufferObject*) self;
int count = buffer->length;
return Object_NewListOfConsecutiveIntegers(0, count-1, heap, error);
}
+41
View File
@@ -48,6 +48,7 @@ static void walkexts(Object *self, void (*callback)(void **referer, unsigne
static Object *copy(Object *self, Heap *heap, Error *err);
static int hash(Object *self);
static bool istypeof(Object *self, Object *other, Heap *heap, Error *error);
static Object* keysof(Object *self, Heap *heap, Error *error);
static TypeObject t_list = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
@@ -59,6 +60,7 @@ static TypeObject t_list = {
.insert = insert,
.count = count,
.print = print,
.keysof = keysof,
.istypeof = istypeof,
.walk = walk,
.walkexts = walkexts,
@@ -92,6 +94,16 @@ istypeof(Object *self,
return true;
}
static Object*
keysof(Object *self,
Heap *heap,
Error *error)
{
ListObject *list = (ListObject*) self;
int count = list->count;
return Object_NewListOfConsecutiveIntegers(0, count-1, heap, error);
}
static int hash(Object *self)
{
ASSERT(self != NULL);
@@ -303,4 +315,33 @@ static void print(Object *self, FILE *fp)
fprintf(fp, ", ");
}
fprintf(fp, "]");
}
Object *Object_NewListOfConsecutiveIntegers(int first, int last, Heap *heap, Error *error)
{
int count = last - first + 1;
Object *list = Object_NewList(count, heap, error);
if (list == NULL)
return NULL;
for (int i = 0; i < count; i++) {
Object *key = Object_FromInt(i, heap, error);
if (key == NULL)
return NULL;
Object *val;
if (first == 0)
val = key;
else {
val = Object_FromInt(first+i, heap, error);
if (val == NULL)
return NULL;
}
if (!Object_Insert(list, key, val, heap, error))
return NULL;
}
return list;
}
+12 -1
View File
@@ -48,7 +48,8 @@ static void walk(Object *self, void (*callback)(Object **referer, void *userp),
static void walkexts(Object *self, void (*callback)(void **referer, unsigned int size, void *userp), void *userp);
static Object *copy(Object *self, Heap *heap, Error *err);
static int hash(Object *self);
static bool istypeof(Object *self, Object *other, Heap *heap, Error *error);
static bool istypeof(Object *self, Object *other, Heap *heap, Error *error);
static Object *keysof(Object *self, Heap *heap, Error *error);
static TypeObject t_map = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
@@ -60,11 +61,21 @@ static TypeObject t_map = {
.insert = insert,
.count = count,
.print = print,
.keysof = keysof,
.istypeof = istypeof,
.walk = walk,
.walkexts = walkexts,
};
static Object*
keysof(Object *self,
Heap *heap,
Error *error)
{
MapObject *map = (MapObject*) self;
return Object_NewList2(map->count, map->keys, heap, error);
}
static bool
istypeof(Object *self,
Object *other,
+13 -1
View File
@@ -49,6 +49,7 @@ static void print(Object *obj, FILE *fp);
static _Bool op_eql(Object *self, Object *other);
static void walkexts(Object *self, void (*callback)(void **referer, unsigned int size, void *userp), void *userp);
static Object *select_(Object *self, Object *key, Heap *heap, Error *error);
static Object *keysof(Object *self, Heap *heap, Error *error);
static TypeObject t_string = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
@@ -60,6 +61,7 @@ static TypeObject t_string = {
.print = print,
.select = select_,
.op_eql = op_eql,
.keysof = keysof,
.walkexts = walkexts,
};
@@ -231,4 +233,14 @@ static void walkexts(Object *self, void (*callback)(void **referer, unsigned int
StringObject *str = (StringObject*) self;
callback((void**) &str->body, str->bytes+1, userp);
}
}
static Object*
keysof(Object *self,
Heap *heap,
Error *error)
{
StringObject *str = (StringObject*) self;
int count = str->count;
return Object_NewListOfConsecutiveIntegers(0, count-1, heap, error);
}
+10 -1
View File
@@ -2,6 +2,7 @@
#include "objects.h"
static void walk(Object *self, void (*callback)(Object **referer, void *userp), void *userp);
static void print(Object *self, FILE *fp);
static bool istypeof(Object *self, Object *other, Heap *heap, Error *error);
typedef struct {
@@ -18,7 +19,7 @@ static TypeObject t_sum = {
.hash = NULL,
.copy = NULL,
.call = NULL,
.print = NULL,
.print = print,
.select = NULL,
.delete = NULL,
@@ -64,3 +65,11 @@ static void walk(Object *self, void (*callback)(Object **referer, void *userp),
callback(sum->items + 0, userp);
callback(sum->items + 1, userp);
}
static void print(Object *self, FILE *fp)
{
SumObject *sum = (SumObject*) self;
Object_Print(sum->items[0], fp);
fprintf(fp, " | ");
Object_Print(sum->items[1], fp);
}
+10
View File
@@ -88,6 +88,16 @@ const TypeObject *Object_GetType(const Object *obj)
return obj->type;
}
Object*
Object_KeysOf(Object *self,
Heap *heap,
Error *error)
{
if (self->type->keysof == NULL)
return Object_NewNone(heap, error);
return self->type->keysof(self, heap, error);
}
bool Object_IsTypeOf(Object *typ, Object *obj, Heap *heap, Error *error)
{
if (typ->type->istypeof == NULL)
+3
View File
@@ -71,6 +71,7 @@ struct TypeObject {
Object *(*delete)(Object *self, Object *key, Heap *heap, Error *err);
bool (*insert)(Object *self, Object *key, Object *val, Heap *heap, Error *err);
int (*count)(Object *self);
Object *(*keysof)(Object *self, Heap *heap, Error *error);
// Types.
bool (*istypeof)(Object *self, Object *other, Heap *heap, Error *error);
@@ -104,6 +105,7 @@ int Object_Hash (Object *obj);
Object* Object_Copy (Object *obj, Heap *heap, Error *err);
int Object_Call (Object *obj, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Heap *heap, Error *err);
void Object_Print(Object *obj, FILE *fp);
Object* Object_KeysOf(Object *self, Heap *heap, Error *error);
bool Object_IsTypeOf(Object *typ, Object *obj, Heap *heap, Error *error);
Object* Object_Select(Object *coll, Object *key, Heap *heap, Error *err);
Object* Object_Delete(Object *coll, Object *key, Heap *heap, Error *err);
@@ -115,6 +117,7 @@ void Object_WalkExtensions(Object *parent, void (*callback)(void **referer,
Object* Object_NewMap(int num, Heap *heap, Error *error);
Object* Object_NewList(int capacity, Heap *heap, Error *error);
Object* Object_NewList2(int num, Object **items, Heap *heap, Error *error);
Object* Object_NewListOfConsecutiveIntegers(int first, int last, Heap *heap, Error *error);
Object* Object_NewNone(Heap *heap, Error *error);
Object* Object_NewBuffer(size_t size, Heap *heap, Error *error);
Object* Object_NewClosure(Object *parent, Object *new_map, Heap *heap, Error *error);