Files
uclisp/src/memory.c
2022-11-02 20:36:07 -04:00

95 lines
2.2 KiB
C

#include "uclisp.h"
#include "internal.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
static struct ucl_object *ucl_object_alloc();
struct ucl_object *ucl_cell_create(struct ucl_object *car, struct ucl_object *cdr)
{
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_CELL;
obj->cell.car = car;
obj->cell.cdr = cdr;
return obj;
}
struct ucl_object *ucl_int_create(int integer)
{
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_INT;
obj->integer = integer;
return obj;
}
struct ucl_object *ucl_symbol_create(const char *symbol)
{
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_SYMBOL;
obj->symbol = strdup(symbol);
return obj;
}
struct ucl_object *ucl_string_create(const char *string)
{
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_STRING;
obj->string = strdup(string);
return obj;
}
struct ucl_object *ucl_error_create(const char *error)
{
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_ERROR;
obj->error = strdup(error);
return obj;
}
struct ucl_object *ucl_builtin_create(ucl_builtin builtin)
{
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_BUILTIN;
obj->builtin = builtin;
return obj;
}
static struct ucl_object* ucl_object_alloc() {
return malloc(sizeof(struct ucl_object));
}
void ucl_object_delete(struct ucl_object *obj) {
if (obj == NULL) {
return;
}
switch (obj->type) {
case UCL_TYPE_CELL:
ucl_object_delete(obj->cell.car);
obj->cell.car = NULL;
ucl_object_delete(obj->cell.cdr);
obj->cell.cdr = NULL;
break;
case UCL_TYPE_SYMBOL:
free((void *) obj->symbol);
obj->symbol = NULL;
break;
case UCL_TYPE_STRING:
free((void *) obj->string);
obj->string = NULL;
break;
case UCL_TYPE_ERROR:
free((void *) obj->error);
obj->error = NULL;
break;
case UCL_TYPE_INT:
case UCL_TYPE_BUILTIN:
case UCL_TYPE_COUNT:
break;
}
free(obj);
}