made an immutable map object specifically for builtins

This commit is contained in:
Francesco Cozzuto
2021-11-29 16:35:15 +01:00
parent ac4dfbc8d3
commit 0d805b84b0
9 changed files with 211 additions and 109 deletions
+20 -1
View File
@@ -14,19 +14,38 @@ static int hash(Object *self);
static int count(Object *self);
static Object *copy(Object *self, Heap *heap, Error *err);
static void print(Object *obj, FILE *fp);
static char *to_string(Object *self, int *size, Heap *heap, Error *err);
static _Bool op_eql(Object *self, Object *other);
static const Type t_string = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "string",
.size = sizeof (StringObject),
.atomic = ATMTP_STRING,
.size = sizeof(StringObject),
.hash = hash,
.count = count,
.copy = copy,
.print = print,
.to_string = to_string,
.op_eql = op_eql,
};
static char *to_string(Object *self, int *size, Heap *heap, Error *err)
{
assert(self != NULL);
assert(self->type == &t_string);
(void) heap;
(void) err;
StringObject *s = (StringObject*) self;
if(size)
*size = s->size;
return s->body;
}
Object *Object_FromString(const char *str, int len, Heap *heap, Error *error)
{
assert(str != NULL);
+42 -12
View File
@@ -10,7 +10,7 @@ const Type t_type = {
const char *Object_GetName(const Object *obj)
{
assert(obj);
assert(obj != NULL);
const Type *type = Object_GetType(obj);
assert(type);
@@ -23,15 +23,15 @@ const char *Object_GetName(const Object *obj)
const Type *Object_GetType(const Object *obj)
{
assert(obj);
assert(obj->type);
assert(obj != NULL);
assert(obj->type != NULL);
return obj->type;
}
unsigned int Object_GetSize(const Object *obj, Error *err)
{
assert(err);
assert(obj);
assert(err != NULL);
assert(obj != NULL);
const Type *type = Object_GetType(obj);
assert(type);
@@ -41,8 +41,8 @@ unsigned int Object_GetSize(const Object *obj, Error *err)
unsigned int Object_GetDeepSize(const Object *obj, Error *err)
{
assert(err);
assert(obj);
assert(err != NULL);
assert(obj != NULL);
const Type *type = Object_GetType(obj);
assert(type);
@@ -58,10 +58,10 @@ unsigned int Object_GetDeepSize(const Object *obj, Error *err)
int Object_Hash(Object *obj, Error *err)
{
assert(obj);
assert(obj != NULL);
const Type *type = Object_GetType(obj);
assert(type);
assert(type != NULL);
if(type->hash == NULL)
{
@@ -74,11 +74,11 @@ int Object_Hash(Object *obj, Error *err)
Object *Object_Copy(Object *obj, Heap *heap, Error *err)
{
assert(err);
assert(obj);
assert(err != NULL);
assert(obj != NULL);
const Type *type = Object_GetType(obj);
assert(type);
assert(type != NULL);
if(type->copy == NULL)
{
@@ -245,6 +245,13 @@ _Bool Object_IsFloat(Object *obj)
return obj->type->atomic == ATMTP_FLOAT;
}
_Bool Object_IsString(Object *obj)
{
assert(obj != NULL);
assert(obj->type != NULL);
return obj->type->atomic == ATMTP_STRING;
}
long long int Object_ToInt(Object *obj, Error *err)
{
assert(err);
@@ -314,6 +321,29 @@ double Object_ToFloat(Object *obj, Error *err)
return type->to_float(obj, err);
}
const char *Object_ToString(Object *obj, int *size, Heap *heap, Error *err)
{
assert(err != NULL);
assert(obj != NULL);
if(!Object_IsString(obj))
{
Error_Report(err, 0, "Object is not a string");
return NULL;
}
const Type *type = Object_GetType(obj);
assert(type);
if(type->to_string == NULL)
{
Error_Report(err, 0, "Object %s doesn't implement %s", Object_GetName(obj), __func__);
return NULL;
}
return type->to_string(obj, size, heap, err);
}
_Bool Object_Compare(Object *obj1, Object *obj2, Error *error)
{
assert(obj1 != NULL);
+3
View File
@@ -18,6 +18,7 @@ typedef enum {
ATMTP_INT,
ATMTP_BOOL,
ATMTP_FLOAT,
ATMTP_STRING,
} AtomicType;
struct Type {
@@ -94,10 +95,12 @@ Object* Object_FromString(const char *str, int len, Heap *heap, Error *error);
_Bool Object_IsInt (Object *obj);
_Bool Object_IsBool (Object *obj);
_Bool Object_IsFloat(Object *obj);
_Bool Object_IsString(Object *obj);
long long int Object_ToInt (Object *obj, Error *err);
_Bool Object_ToBool (Object *obj, Error *err);
double Object_ToFloat(Object *obj, Error *err);
const char *Object_ToString(Object *obj, int *size, Heap *heap, Error *err);
_Bool Object_Compare(Object *obj1, Object *obj2, Error *error);