added print object method
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
|
|
||||||
static _Bool to_bool(Object *obj, Error *err);
|
static _Bool to_bool(Object *obj, Error *err);
|
||||||
|
static void print(Object *obj, FILE *fp);
|
||||||
|
|
||||||
static const Type t_bool = {
|
static const Type t_bool = {
|
||||||
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
|
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
|
||||||
@@ -10,6 +11,7 @@ static const Type t_bool = {
|
|||||||
.size = sizeof (Object),
|
.size = sizeof (Object),
|
||||||
.atomic = ATMTP_BOOL,
|
.atomic = ATMTP_BOOL,
|
||||||
.to_bool = to_bool,
|
.to_bool = to_bool,
|
||||||
|
.print = print,
|
||||||
};
|
};
|
||||||
|
|
||||||
static Object the_true_object = {
|
static Object the_true_object = {
|
||||||
@@ -36,3 +38,12 @@ Object *Object_FromBool(_Bool val, Heap *heap, Error *error)
|
|||||||
(void) error;
|
(void) error;
|
||||||
return val ? &the_true_object : &the_false_object;
|
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");
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
|
|
||||||
static double to_float(Object *obj, Error *err);
|
static double to_float(Object *obj, Error *err);
|
||||||
|
static void print(Object *obj, FILE *fp);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Object base;
|
Object base;
|
||||||
@@ -14,6 +15,7 @@ static const Type t_float = {
|
|||||||
.size = sizeof (FloatObject),
|
.size = sizeof (FloatObject),
|
||||||
.atomic = ATMTP_FLOAT,
|
.atomic = ATMTP_FLOAT,
|
||||||
.to_float = to_float,
|
.to_float = to_float,
|
||||||
|
.print = print,
|
||||||
};
|
};
|
||||||
|
|
||||||
static double to_float(Object *obj, Error *err)
|
static double to_float(Object *obj, Error *err)
|
||||||
@@ -38,3 +40,12 @@ Object *Object_FromFloat(double val, Heap *heap, Error *error)
|
|||||||
|
|
||||||
return (Object*) obj;
|
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);
|
||||||
|
}
|
||||||
+16
-2
@@ -3,6 +3,7 @@
|
|||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
|
|
||||||
static long long int to_int(Object *obj, Error *err);
|
static long long int to_int(Object *obj, Error *err);
|
||||||
|
static void print(Object *obj, FILE *fp);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Object base;
|
Object base;
|
||||||
@@ -15,12 +16,13 @@ static const Type t_int = {
|
|||||||
.size = sizeof (IntObject),
|
.size = sizeof (IntObject),
|
||||||
.atomic = ATMTP_INT,
|
.atomic = ATMTP_INT,
|
||||||
.to_int = to_int,
|
.to_int = to_int,
|
||||||
|
.print = print,
|
||||||
};
|
};
|
||||||
|
|
||||||
static long long int to_int(Object *obj, Error *err)
|
static long long int to_int(Object *obj, Error *err)
|
||||||
{
|
{
|
||||||
assert(obj);
|
assert(obj != NULL);
|
||||||
assert(err);
|
assert(err != NULL);
|
||||||
assert(Object_GetType(obj) == &t_int);
|
assert(Object_GetType(obj) == &t_int);
|
||||||
|
|
||||||
(void) err;
|
(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)
|
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);
|
IntObject *obj = (IntObject*) Heap_Malloc(heap, &t_int, error);
|
||||||
|
|
||||||
if(obj == 0)
|
if(obj == 0)
|
||||||
@@ -39,3 +44,12 @@ Object *Object_FromInt(long long int val, Heap *heap, Error *error)
|
|||||||
|
|
||||||
return (Object*) obj;
|
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);
|
||||||
|
}
|
||||||
@@ -2,10 +2,13 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
|
|
||||||
|
static void print(Object *obj, FILE *fp);
|
||||||
|
|
||||||
static const Type t_none = {
|
static const Type t_none = {
|
||||||
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
|
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
|
||||||
.name = "none",
|
.name = "none",
|
||||||
.size = sizeof (Object),
|
.size = sizeof (Object),
|
||||||
|
.print = print,
|
||||||
};
|
};
|
||||||
|
|
||||||
static Object the_none_object = {
|
static Object the_none_object = {
|
||||||
@@ -19,3 +22,13 @@ Object *Object_NewNone(Heap *heap, Error *error)
|
|||||||
(void) error;
|
(void) error;
|
||||||
return &the_none_object;
|
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");
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ typedef struct {
|
|||||||
static int hash(Object *self);
|
static int hash(Object *self);
|
||||||
static int count(Object *self);
|
static int count(Object *self);
|
||||||
static Object *copy(Object *self, Heap *heap, Error *err);
|
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 _Bool op_eql(Object *self, Object *other);
|
||||||
|
|
||||||
static const Type t_string = {
|
static const Type t_string = {
|
||||||
@@ -22,6 +23,7 @@ static const Type t_string = {
|
|||||||
.hash = hash,
|
.hash = hash,
|
||||||
.count = count,
|
.count = count,
|
||||||
.copy = copy,
|
.copy = copy,
|
||||||
|
.print = print,
|
||||||
.op_eql = op_eql,
|
.op_eql = op_eql,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -94,3 +96,14 @@ static _Bool op_eql(Object *self, Object *other)
|
|||||||
|
|
||||||
return s1->size == s2->size && !strncmp(s1->body, s2->body, s1->size);
|
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);
|
||||||
|
}
|
||||||
@@ -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);
|
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)
|
Object *Object_Select(Object *coll, Object *key, Heap *heap, Error *err)
|
||||||
{
|
{
|
||||||
assert(err);
|
assert(err);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef OBJECT_H
|
#ifndef OBJECT_H
|
||||||
#define OBJECT_H
|
#define OBJECT_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include "../utils/error.h"
|
#include "../utils/error.h"
|
||||||
|
|
||||||
typedef struct Type Type;
|
typedef struct Type Type;
|
||||||
@@ -33,6 +34,7 @@ struct Type {
|
|||||||
int (*hash)(Object *self);
|
int (*hash)(Object *self);
|
||||||
Object* (*copy)(Object *self, Heap *heap, Error *err);
|
Object* (*copy)(Object *self, Heap *heap, Error *err);
|
||||||
Object* (*call)(Object *self, Object **argv, unsigned int argc, 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);
|
unsigned int (*deepsize)(const Object *self);
|
||||||
|
|
||||||
// Collections.
|
// Collections.
|
||||||
@@ -72,6 +74,7 @@ unsigned int Object_GetDeepSize(const Object *obj, Error *err);
|
|||||||
int Object_Hash (Object *obj, Error *err);
|
int Object_Hash (Object *obj, Error *err);
|
||||||
Object* Object_Copy (Object *obj, Heap *heap, 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);
|
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_Select(Object *coll, Object *key, Heap *heap, Error *err);
|
||||||
Object* Object_Delete(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);
|
_Bool Object_Insert(Object *coll, Object *key, Object *val, Heap *heap, Error *err);
|
||||||
|
|||||||
Reference in New Issue
Block a user