2023-05-03 22:28:41 +00:00
|
|
|
#include "enums.h"
|
2023-05-03 22:21:04 +00:00
|
|
|
#include "object.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2023-05-03 22:28:41 +00:00
|
|
|
#include <stdio.h>
|
2023-05-03 22:21:04 +00:00
|
|
|
#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);
|
2023-05-03 22:49:55 +00:00
|
|
|
object->pair.a = a;
|
|
|
|
object->pair.b = b;
|
2023-05-03 22:21:04 +00:00
|
|
|
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;
|
|
|
|
}
|
2023-05-03 22:28:41 +00:00
|
|
|
|
|
|
|
void Object_print(struct Object *const self, const unsigned indent)
|
|
|
|
{
|
|
|
|
for (unsigned index = 0; index < indent; ++index) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!self) {
|
|
|
|
printf("NULL\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s:", Type_to_str(self->type));
|
|
|
|
|
|
|
|
switch (self->type) {
|
|
|
|
case TYPE_PAIR:
|
|
|
|
printf("\n");
|
2023-05-03 22:49:55 +00:00
|
|
|
Object_print(self->pair.a, indent + 1);
|
|
|
|
Object_print(self->pair.b, indent + 1);
|
2023-05-03 22:28:41 +00:00
|
|
|
break;
|
|
|
|
case TYPE_ATOM:
|
|
|
|
case TYPE_STRING:
|
|
|
|
printf("%s\n", self->s);
|
|
|
|
break;
|
|
|
|
case TYPE_NUMBER:
|
|
|
|
printf("%li\n", self->i);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|