From b51aaa3d650235ec88af5e65f05b96472c6dd47e Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Thu, 12 Feb 2026 05:48:48 +0000 Subject: [PATCH] lisp: replace sys_register with sys_alloc Allows us to abstract allocation away, creating Lisps automatically. --- include/alisp/lisp.h | 2 +- src/lisp.c | 44 +++++++++++++++++++++++--------------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/include/alisp/lisp.h b/include/alisp/lisp.h index f383283..21b8ddf 100644 --- a/include/alisp/lisp.h +++ b/include/alisp/lisp.h @@ -75,7 +75,7 @@ typedef struct } sys_t; void sys_init(sys_t *); -void sys_register(sys_t *, lisp_t *); +lisp_t *sys_alloc(sys_t *, tag_t type); void sys_free(sys_t *); // Debugging function: provides total memory usage from system. diff --git a/src/lisp.c b/src/lisp.c index 1a15526..3d1b475 100644 --- a/src/lisp.c +++ b/src/lisp.c @@ -15,26 +15,33 @@ void sys_init(sys_t *sys) memset(sys, 0, sizeof(*sys)); } -void sys_register(sys_t *sys, lisp_t *ptr) +lisp_t *sys_alloc(sys_t *sys, tag_t type) { - // Simply append it to the list of currently active conses - switch (get_tag(ptr)) + switch (type) { case TAG_CONS: - vec_append(&sys->conses, &ptr, sizeof(&ptr)); - break; + { + cons_t *cons = calloc(1, sizeof(*cons)); + lisp_t *lisp = tag_cons(cons); + vec_append(&sys->conses, &lisp, sizeof(&lisp)); + return lisp; + } case TAG_VEC: - vec_append(&sys->vectors, &ptr, sizeof(&ptr)); - break; - // Shouldn't be registered + { + vec_t *vec = calloc(1, sizeof(*vec)); + lisp_t *lisp = tag_vec(vec); + vec_append(&sys->vectors, &lisp, sizeof(&lisp)); + return lisp; + } + // Shouldn't be registered case TAG_NIL: case TAG_INT: case TAG_SYM: - break; case NUM_TAGS: default: FAIL("Unreachable"); } + return NIL; } u64 sys_cost(sys_t *sys) @@ -81,22 +88,17 @@ lisp_t *make_int(i64 i) lisp_t *cons(sys_t *sys, lisp_t *car, lisp_t *cdr) { - cons_t *cons = calloc(1, sizeof(*cons)); - cons->car = car; - cons->cdr = cdr; - - lisp_t *lcons = tag_cons(cons); - sys_register(sys, lcons); - return lcons; + lisp_t *cons = sys_alloc(sys, TAG_CONS); + CAR(cons) = car; + CDR(cons) = cdr; + return cons; } lisp_t *make_vec(sys_t *sys, u64 capacity) { - vec_t *vec = calloc(1, sizeof(*vec)); - vec_init(vec, capacity); - lisp_t *ptr = tag_vec(vec); - sys_register(sys, ptr); - return ptr; + lisp_t *vec = sys_alloc(sys, TAG_VEC); + vec_init(as_vec(vec), capacity); + return vec; } lisp_t *intern(sys_t *sys, sv_t sv)