1
0
Fork 0
lesson-lisp/object.c
2023-05-04 02:21:04 +04:00

47 lines
1.1 KiB
C

#include "object.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
static struct Object *new(const enum Type type)
{
struct Object *const object = malloc(sizeof(struct Object));
assert(object);
memset(object, 0, sizeof(struct Object));
object->type = type;
return object;
}
struct Object *Object_new_pair(struct Object *const a, struct Object *const b)
{
struct Object *const object = new(TYPE_PAIR);
object->a = a;
object->b = b;
return object;
}
struct Object *Object_new_atom(const char *const s)
{
struct Object *const object = new(TYPE_ATOM);
object->s = malloc(strlen(s) + 1);
assert(object->s);
strcpy(object->s, s);
return object;
}
struct Object *Object_new_string(const char *const s)
{
struct Object *const object = new(TYPE_STRING);
object->s = malloc(strlen(s) + 1);
assert(object->s);
strcpy(object->s, s);
return object;
}
struct Object *Object_new_number(const int64_t i)
{
struct Object *const object = new(TYPE_NUMBER);
object->i = i;
return object;
}