From 3074ba5babaf39e4a177444a8ffb5f81b991440f Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 20 Aug 2025 21:12:46 +0100 Subject: Constructors and context -> sys We can register memory we've allocated onto the heap into a ~sys_t~ instance for examination (read: garbage collection) later. We can also add items to the symbol table it has internally. --- constructor.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 constructor.c (limited to 'constructor.c') diff --git a/constructor.c b/constructor.c new file mode 100644 index 0000000..e500430 --- /dev/null +++ b/constructor.c @@ -0,0 +1,48 @@ +/* Copyright (C) 2025 Aryadev Chavali + + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the Unlicense for details. + + * You may distribute and modify this code under the terms of the Unlicense, + * which you should have received a copy of along with this program. If not, + * please go to . + + * Created: 2025-08-20 + * Description: Lisp constructors/destructors + */ + +#include + +#include "./base.h" + +lisp_t *make_int(i64 i) +{ + return tag_int(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 *make_vec(sys_t *sys, u64 capacity) +{ + lvec_t *lvec = calloc(1, sizeof(*lvec)); + vec_make(&lvec->data, capacity); + lisp_t *ptr = tag_vec(lvec); + sys_register(sys, ptr); + return ptr; +} + +lisp_t *intern(sys_t *sys, sv_t sv) +{ + char *s = sym_table_find(&sys->symtable, sv); + return tag_sym(s); +} -- cgit v1.2.3-13-gbd6f