From e6e501c5a3774fb5a1aba5c427e2884ab253ea04 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 13 Feb 2026 00:06:57 +0000 Subject: [PATCH] lisp: tag_generic, tag_sizeof, and lisp_sizeof --- include/alisp/lisp.h | 3 +++ src/lisp.c | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/include/alisp/lisp.h b/include/alisp/lisp.h index ba77720..31d5ea3 100644 --- a/include/alisp/lisp.h +++ b/include/alisp/lisp.h @@ -65,6 +65,7 @@ lisp_t *tag_int(const i64); lisp_t *tag_sym(const char *); lisp_t *tag_cons(const cons_t *); lisp_t *tag_vec(const vec_t *); +lisp_t *tag_generic(void *, tag_t); i64 as_int(lisp_t *); char *as_sym(lisp_t *); @@ -75,6 +76,8 @@ vec_t *as_vec(lisp_t *); #define CDR(L) (as_cons(L)->cdr) void lisp_print(FILE *, lisp_t *); +u64 tag_sizeof(tag_t); +u64 lisp_sizeof(lisp_t *); #endif diff --git a/src/lisp.c b/src/lisp.c index 4caf67c..f9ce6d4 100644 --- a/src/lisp.c +++ b/src/lisp.c @@ -30,6 +30,26 @@ lisp_t *tag_cons(const cons_t *cons) return TAG((u64)cons, CONS); } +lisp_t *tag_generic(void *ptr, tag_t type) +{ + switch (type) + { + case TAG_NIL: + return NIL; + case TAG_INT: + return tag_int((i64)ptr); + case TAG_SYM: + return tag_sym(ptr); + case TAG_CONS: + return tag_cons(ptr); + case TAG_VEC: + return tag_vec(ptr); + default: + FAIL("Unreachable"); + return NIL; + } +} + tag_t get_tag(const lisp_t *lisp) { static_assert(NUM_TAGS == 5); @@ -165,6 +185,31 @@ void lisp_print(FILE *fp, lisp_t *lisp) } } +u64 tag_sizeof(tag_t tag) +{ + switch (tag) + { + case TAG_NIL: + return 0; + case TAG_INT: + case TAG_SYM: + return sizeof(lisp_t *); + case TAG_CONS: + return sizeof(cons_t); + case TAG_VEC: + return sizeof(vec_t); + case NUM_TAGS: + default: + FAIL("Unreachable"); + return 0; + } +} + +u64 lisp_sizeof(lisp_t *lisp) +{ + return tag_sizeof(get_tag(lisp)); +} + /* Copyright (C) 2025, 2026 Aryadev Chavali * This program is distributed in the hope that it will be useful, but WITHOUT