adding compilation of if-else statements

This commit is contained in:
cozis
2021-10-31 07:01:15 +00:00
parent 5e8fb7ef6b
commit 9eaa82297f
16 changed files with 269 additions and 25 deletions
+38
View File
@@ -0,0 +1,38 @@
#include <assert.h>
#include <string.h>
#include "objects.h"
static _Bool to_bool(Object *obj, Error *err);
static const Type t_int = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "int",
.size = sizeof (Object),
.atomic = ATMTP_BOOL,
.to_bool = to_bool,
};
static Object the_true_object = {
.type = t_bool,
.flags = Object_STATIC,
}
static Object the_false_object = {
.type = t_bool,
.flags = Object_STATIC,
}
static _Bool to_bool(Object *obj, Error *err)
{
assert(obj);
assert(err);
assert(Object_GetType(obj) == &t_bool);
return obj == &the_true_object;
}
Object *Object_FromBool(_Bool val, Heap *heap, Error *error)
{
(void) heap;
(void) error;
return val ? &the_true_object : &the_false_object;
}
+30
View File
@@ -218,6 +218,13 @@ _Bool Object_IsInt(Object *obj)
return obj->type->atomic == ATMTP_INT;
}
_Bool Object_IsBool(Object *obj)
{
assert(obj != NULL);
assert(obj->type != NULL);
return obj->type->atomic == ATMTP_BOOL;
}
_Bool Object_IsFloat(Object *obj)
{
assert(obj != NULL);
@@ -248,6 +255,29 @@ long long int Object_ToInt(Object *obj, Error *err)
return type->to_int(obj, err);
}
_Bool Object_ToBool(Object *obj, Error *err)
{
assert(err);
assert(obj);
if(!Object_IsBool(obj))
{
Error_Report(err, 0, "Object is not a boolean");
return 0;
}
const Type *type = Object_GetType(obj);
assert(type);
if(type->to_bool == NULL)
{
Error_Report(err, "Object %s doesn't implement %s", Object_GetName(obj), __func__);
return 0;
}
return type->to_bool(obj, err);
}
double Object_ToFloat(Object *obj, Error *err)
{
assert(err);
+5
View File
@@ -15,6 +15,7 @@ struct Object {
typedef enum {
ATMTP_NOTATOMIC = 0,
ATMTP_INT,
ATMTP_BOOL,
ATMTP_FLOAT,
} AtomicType;
@@ -47,6 +48,7 @@ struct Type {
// Some.
union {
long long int (*to_int)(Object *self, Error *err);
_Bool (*to_bool)(Object *self, Error *err);
double (*to_float)(Object *self, Error *err);
char *(*to_string)(Object *self, int *size, Heap *heap, Error *err);
};
@@ -81,13 +83,16 @@ Object* Object_NewMap(int num, Heap *heap, Error *error);
Object* Object_NewNone(Heap *heap, Error *error);
Object* Object_FromInt (long long int val, Heap *heap, Error *error);
Object* Object_FromBool (_Bool val, Heap *heap, Error *error);
Object* Object_FromFloat (double val, Heap *heap, Error *error);
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);
long long int Object_ToInt (Object *obj, Error *err);
_Bool Object_ToBool (Object *obj, Error *err);
double Object_ToFloat(Object *obj, Error *err);
_Bool Object_Compare(Object *obj1, Object *obj2, Error *error);