From b32f420cb91e58b863d0544f90a8fbe25c942548 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Thu, 5 Feb 2026 05:39:35 +0000 Subject: [PATCH] lisp: split off lisp_free as it's own function lisp_free will do a shallow clean of any object, freeing its associated memory. It won't recur through any containers, nor will it freakout if you give it something that is constant (symbols, small integers, NIL, etc). --- include/alisp/lisp.h | 2 ++ src/lisp.c | 45 ++++++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/include/alisp/lisp.h b/include/alisp/lisp.h index ce52176..94f2caf 100644 --- a/include/alisp/lisp.h +++ b/include/alisp/lisp.h @@ -49,6 +49,8 @@ vec_t *as_vec(lisp_t *); lisp_t *car(lisp_t *); lisp_t *cdr(lisp_t *); +void lisp_free(lisp_t *); + #endif /* Copyright (C) 2026 Aryadev Chavali diff --git a/src/lisp.c b/src/lisp.c index 6a6f388..a180928 100644 --- a/src/lisp.c +++ b/src/lisp.c @@ -34,26 +34,7 @@ void sys_free(sys_t *sys) for (size_t i = 0; i < VEC_SIZE(&sys->memory, lisp_t **); ++i) { lisp_t *allocated = VEC_GET(&sys->memory, i, lisp_t *); - switch (get_tag(allocated)) - { - case TAG_CONS: - // Delete the cons - free(as_cons(allocated)); - break; - case TAG_VEC: - { - vec_t *vec = as_vec(allocated); - vec_free(vec); - free(vec); - break; - } - case TAG_NIL: - case TAG_INT: - case TAG_SYM: - case NUM_TAGS: - // shouldn't be dealt with (either constant or dealt with elsewhere) - break; - } + lisp_free(allocated); } // Free the container @@ -110,6 +91,30 @@ lisp_t *cdr(lisp_t *lsp) return CDR(lsp); } +void lisp_free(lisp_t *item) +{ + switch (get_tag(item)) + { + case TAG_CONS: + // Delete the cons + free(as_cons(item)); + break; + case TAG_VEC: + { + vec_t *vec = as_vec(item); + vec_free(vec); + free(vec); + break; + } + case TAG_NIL: + case TAG_INT: + case TAG_SYM: + case NUM_TAGS: + // shouldn't be dealt with (either constant or dealt with elsewhere) + break; + } +} + /* Copyright (C) 2025, 2026 Aryadev Chavali * This program is distributed in the hope that it will be useful, but WITHOUT