added print object method

This commit is contained in:
cozis
2021-11-01 16:12:08 +00:00
parent 5bf6a9f085
commit d93f578eb2
7 changed files with 85 additions and 2 deletions
+13
View File
@@ -13,6 +13,7 @@ typedef struct {
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 _Bool op_eql(Object *self, Object *other);
static const Type t_string = {
@@ -22,6 +23,7 @@ static const Type t_string = {
.hash = hash,
.count = count,
.copy = copy,
.print = print,
.op_eql = op_eql,
};
@@ -93,4 +95,15 @@ static _Bool op_eql(Object *self, Object *other)
StringObject *s2 = (StringObject*) other;
return s1->size == s2->size && !strncmp(s1->body, s2->body, s1->size);
}
static void print(Object *obj, FILE *fp)
{
assert(fp != NULL);
assert(obj != NULL);
assert(obj->type == &t_string);
StringObject *str = (StringObject*) obj;
fprintf(fp, "%.*s", str->size, str->body);
}