Files
uclisp/src/memory.c

85 lines
1.9 KiB
C

#include "nihilispm.h"
#include "nihilispm_internal.h"
#include <stddef.h>
#include <stdlib.h>
static struct nl_object *nl_object_alloc();
struct nl_object *nl_cell_create(struct nl_object *car, struct nl_object *cdr)
{
struct nl_object* obj = nl_object_alloc();
obj->type = NL_TYPE_CELL;
obj->cell.car = car;
obj->cell.cdr = cdr;
return obj;
}
struct nl_object *nl_int_create(int integer)
{
struct nl_object* obj = nl_object_alloc();
obj->type = NL_TYPE_INT;
obj->integer = integer;
return obj;
}
struct nl_object *nl_symbol_create(const char *symbol)
{
struct nl_object* obj = nl_object_alloc();
obj->type = NL_TYPE_SYMBOL;
obj->symbol = symbol;
return obj;
}
struct nl_object *nl_string_create(const char *string)
{
struct nl_object* obj = nl_object_alloc();
obj->type = NL_TYPE_STRING;
obj->string = string;
return obj;
}
struct nl_object *nl_error_create(const char *error)
{
struct nl_object* obj = nl_object_alloc();
obj->type = NL_TYPE_ERROR;
obj->error = error;
return obj;
}
static struct nl_object* nl_object_alloc() {
return malloc(sizeof(struct nl_object));
}
void nl_object_delete(struct nl_object *obj) {
if (obj == NULL) {
return;
}
switch (obj->type) {
case NL_TYPE_CELL:
nl_object_delete(obj->cell.car);
obj->cell.car = NULL;
nl_object_delete(obj->cell.cdr);
obj->cell.cdr = NULL;
break;
case NL_TYPE_SYMBOL:
free((void *) obj->symbol);
obj->symbol = NULL;
break;
case NL_TYPE_STRING:
free((void *) obj->string);
obj->string = NULL;
break;
case NL_TYPE_ERROR:
free((void *) obj->error);
obj->error = NULL;
break;
case NL_TYPE_INT:
case NL_TYPE_COUNT:
break;
}
free(obj);
}