lisp: replace sys_register with sys_alloc

Allows us to abstract allocation away, creating Lisps automatically.
This commit is contained in:
2026-02-12 05:48:48 +00:00
committed by oreodave
parent 6499a9dd6d
commit b51aaa3d65
2 changed files with 24 additions and 22 deletions

View File

@@ -75,7 +75,7 @@ typedef struct
} sys_t; } sys_t;
void sys_init(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 *); void sys_free(sys_t *);
// Debugging function: provides total memory usage from system. // Debugging function: provides total memory usage from system.

View File

@@ -15,26 +15,33 @@ void sys_init(sys_t *sys)
memset(sys, 0, sizeof(*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 (type)
switch (get_tag(ptr))
{ {
case TAG_CONS: 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: case TAG_VEC:
vec_append(&sys->vectors, &ptr, sizeof(&ptr)); {
break; vec_t *vec = calloc(1, sizeof(*vec));
// Shouldn't be registered lisp_t *lisp = tag_vec(vec);
vec_append(&sys->vectors, &lisp, sizeof(&lisp));
return lisp;
}
// Shouldn't be registered
case TAG_NIL: case TAG_NIL:
case TAG_INT: case TAG_INT:
case TAG_SYM: case TAG_SYM:
break;
case NUM_TAGS: case NUM_TAGS:
default: default:
FAIL("Unreachable"); FAIL("Unreachable");
} }
return NIL;
} }
u64 sys_cost(sys_t *sys) 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) lisp_t *cons(sys_t *sys, lisp_t *car, lisp_t *cdr)
{ {
cons_t *cons = calloc(1, sizeof(*cons)); lisp_t *cons = sys_alloc(sys, TAG_CONS);
cons->car = car; CAR(cons) = car;
cons->cdr = cdr; CDR(cons) = cdr;
return cons;
lisp_t *lcons = tag_cons(cons);
sys_register(sys, lcons);
return lcons;
} }
lisp_t *make_vec(sys_t *sys, u64 capacity) lisp_t *make_vec(sys_t *sys, u64 capacity)
{ {
vec_t *vec = calloc(1, sizeof(*vec)); lisp_t *vec = sys_alloc(sys, TAG_VEC);
vec_init(vec, capacity); vec_init(as_vec(vec), capacity);
lisp_t *ptr = tag_vec(vec); return vec;
sys_register(sys, ptr);
return ptr;
} }
lisp_t *intern(sys_t *sys, sv_t sv) lisp_t *intern(sys_t *sys, sv_t sv)