From d93f578eb24fff468c16e6c4bfb0ac1d30d33c61 Mon Sep 17 00:00:00 2001 From: cozis Date: Mon, 1 Nov 2021 16:12:08 +0000 Subject: [PATCH] added print object method --- src/objects/o_bool.c | 11 +++++++++++ src/objects/o_float.c | 11 +++++++++++ src/objects/o_int.c | 18 ++++++++++++++++-- src/objects/o_none.c | 13 +++++++++++++ src/objects/o_string.c | 13 +++++++++++++ src/objects/objects.c | 18 ++++++++++++++++++ src/objects/objects.h | 3 +++ 7 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/objects/o_bool.c b/src/objects/o_bool.c index 2331b6f..53508e7 100644 --- a/src/objects/o_bool.c +++ b/src/objects/o_bool.c @@ -3,6 +3,7 @@ #include "objects.h" static _Bool to_bool(Object *obj, Error *err); +static void print(Object *obj, FILE *fp); static const Type t_bool = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, @@ -10,6 +11,7 @@ static const Type t_bool = { .size = sizeof (Object), .atomic = ATMTP_BOOL, .to_bool = to_bool, + .print = print, }; static Object the_true_object = { @@ -35,4 +37,13 @@ Object *Object_FromBool(_Bool val, Heap *heap, Error *error) (void) heap; (void) error; return val ? &the_true_object : &the_false_object; +} + +static void print(Object *obj, FILE *fp) +{ + assert(fp != NULL); + assert(obj != NULL); + assert(obj->type == &t_bool); + + fprintf(fp, obj == &the_true_object ? "true" : "false"); } \ No newline at end of file diff --git a/src/objects/o_float.c b/src/objects/o_float.c index 81a5e4c..ebc2bf7 100644 --- a/src/objects/o_float.c +++ b/src/objects/o_float.c @@ -2,6 +2,7 @@ #include "objects.h" static double to_float(Object *obj, Error *err); +static void print(Object *obj, FILE *fp); typedef struct { Object base; @@ -14,6 +15,7 @@ static const Type t_float = { .size = sizeof (FloatObject), .atomic = ATMTP_FLOAT, .to_float = to_float, + .print = print, }; static double to_float(Object *obj, Error *err) @@ -38,3 +40,12 @@ Object *Object_FromFloat(double val, Heap *heap, Error *error) return (Object*) obj; } + +static void print(Object *obj, FILE *fp) +{ + assert(fp != NULL); + assert(obj != NULL); + assert(obj->type == &t_float); + + fprintf(fp, "%2.2f", ((FloatObject*) obj)->val); +} \ No newline at end of file diff --git a/src/objects/o_int.c b/src/objects/o_int.c index 2219336..5f81ac4 100644 --- a/src/objects/o_int.c +++ b/src/objects/o_int.c @@ -3,6 +3,7 @@ #include "objects.h" static long long int to_int(Object *obj, Error *err); +static void print(Object *obj, FILE *fp); typedef struct { Object base; @@ -15,12 +16,13 @@ static const Type t_int = { .size = sizeof (IntObject), .atomic = ATMTP_INT, .to_int = to_int, + .print = print, }; static long long int to_int(Object *obj, Error *err) { - assert(obj); - assert(err); + assert(obj != NULL); + assert(err != NULL); assert(Object_GetType(obj) == &t_int); (void) err; @@ -30,6 +32,9 @@ static long long int to_int(Object *obj, Error *err) Object *Object_FromInt(long long int val, Heap *heap, Error *error) { + assert(heap != NULL); + assert(error != NULL); + IntObject *obj = (IntObject*) Heap_Malloc(heap, &t_int, error); if(obj == 0) @@ -38,4 +43,13 @@ Object *Object_FromInt(long long int val, Heap *heap, Error *error) obj->val = val; return (Object*) obj; +} + +static void print(Object *obj, FILE *fp) +{ + assert(fp != NULL); + assert(obj != NULL); + assert(obj->type == &t_int); + + fprintf(fp, "%lld", ((IntObject*) obj)->val); } \ No newline at end of file diff --git a/src/objects/o_none.c b/src/objects/o_none.c index 16fa570..3796a71 100644 --- a/src/objects/o_none.c +++ b/src/objects/o_none.c @@ -2,10 +2,13 @@ #include #include "objects.h" +static void print(Object *obj, FILE *fp); + static const Type t_none = { .base = (Object) { .type = &t_type, .flags = Object_STATIC }, .name = "none", .size = sizeof (Object), + .print = print, }; static Object the_none_object = { @@ -18,4 +21,14 @@ Object *Object_NewNone(Heap *heap, Error *error) (void) heap; (void) error; return &the_none_object; +} + + +static void print(Object *obj, FILE *fp) +{ + assert(fp != NULL); + assert(obj != NULL); + assert(obj->type == &t_none); + + fprintf(fp, "none"); } \ No newline at end of file diff --git a/src/objects/o_string.c b/src/objects/o_string.c index 74036fb..075c41b 100644 --- a/src/objects/o_string.c +++ b/src/objects/o_string.c @@ -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); } \ No newline at end of file diff --git a/src/objects/objects.c b/src/objects/objects.c index a7485e7..e7d9d27 100644 --- a/src/objects/objects.c +++ b/src/objects/objects.c @@ -106,6 +106,24 @@ Object *Object_Call(Object *obj, Object **argv, unsigned int argc, Heap *heap, E return type->call(obj, argv, argc, heap, err); } +_Bool Object_Print(Object *obj, FILE *fp, Error *error) +{ + assert(error != NULL); + assert(obj != NULL); + + const Type *type = Object_GetType(obj); + assert(type); + + if(type->print == NULL) + { + Error_Report(error, 0, "Object %s doesn't implement %s", Object_GetName(obj), __func__); + return 0; + } + + type->print(obj, fp); + return 1; +} + Object *Object_Select(Object *coll, Object *key, Heap *heap, Error *err) { assert(err); diff --git a/src/objects/objects.h b/src/objects/objects.h index 89045c7..020cca0 100644 --- a/src/objects/objects.h +++ b/src/objects/objects.h @@ -1,6 +1,7 @@ #ifndef OBJECT_H #define OBJECT_H +#include #include "../utils/error.h" typedef struct Type Type; @@ -33,6 +34,7 @@ struct Type { int (*hash)(Object *self); Object* (*copy)(Object *self, Heap *heap, Error *err); Object* (*call)(Object *self, Object **argv, unsigned int argc, Heap *heap, Error *err); + void (*print)(Object *self, FILE *fp); unsigned int (*deepsize)(const Object *self); // Collections. @@ -72,6 +74,7 @@ unsigned int Object_GetDeepSize(const Object *obj, Error *err); int Object_Hash (Object *obj, Error *err); Object* Object_Copy (Object *obj, Heap *heap, Error *err); Object* Object_Call (Object *obj, Object **argv, unsigned int argc, Heap *heap, Error *err); +_Bool Object_Print (Object *obj, FILE *fp, Error *error); Object* Object_Select(Object *coll, Object *key, Heap *heap, Error *err); Object* Object_Delete(Object *coll, Object *key, Heap *heap, Error *err); _Bool Object_Insert(Object *coll, Object *key, Object *val, Heap *heap, Error *err);