wl/src/type.c

45 lines
873 B
C

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <wolflisp.h>
// clang-format off
static const char *typenames[] = {
[TYPE_NIL] = "nil",
[TYPE_NUM] = "number",
[TYPE_SYM] = "symbol",
[TYPE_PRIM] = "primitive",
[TYPE_PAIR] = "pair",
[TYPE_STR] = "string",
[TYPE_CLOS] = "closure",
[TYPE_MAC] = "macro",
};
// clang-format on
I type(O obj) {
if (obj == NIL) {
return TYPE_NIL;
} else if (IMM(obj)) {
return TYPE_NUM;
} else {
switch (TAG_OF(obj)) {
case TAG_SYM:
return TYPE_SYM;
case TAG_PRIM:
return TYPE_PRIM;
case TAG_MAN: {
Gh *hdr = UNBOX(obj);
return hdr->type;
}
default:
fprintf(stderr, "unknown pointer tag %" PRIdPTR "\n", TAG_OF(obj));
abort();
}
}
}
const char *typename(I t) {
if (t >= TYPE__MAX)
return "??";
return typenames[t];
}