lisp: tag_generic, tag_sizeof, and lisp_sizeof
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
45
src/lisp.c
45
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
|
||||
|
||||
Reference in New Issue
Block a user