Add initial tokenizer and tests
This commit is contained in:
73
src/memory.c
Normal file
73
src/memory.c
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "nihilispm.h"
|
||||
#include "nihilispm_internal.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
static struct nl_object *nl_object_alloc();
|
||||
static void nl_cell_delete(struct nl_cell *cell);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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(obj->symbol);
|
||||
obj->symbol = NULL;
|
||||
break;
|
||||
case NL_TYPE_STRING:
|
||||
free(obj->string);
|
||||
obj->string = NULL;
|
||||
case NL_TYPE_INT:
|
||||
case NL_TYPE_COUNT:
|
||||
break;
|
||||
}
|
||||
free(obj);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user