~brenns10/funlisp

4a71c61ad88d92877ac53d5992f53ef1e806e7e7 — Stephen Brennan 1 year, 5 months ago cbd0dc5
Implement global module registry for builtins
5 files changed, 23 insertions(+), 4 deletions(-)

M Makefile
M src/builtins.c
M src/funlisp_internal.h
M src/gc.c
M src/module.c
M Makefile => Makefile +1 -1
@@ 40,7 40,7 @@ bin/example_list_append: tools/example_list_append.o bin/libfunlisp.a
	$(CC) $(CFLAGS) $^ -o $@

clean: FORCE
	rm -rf bin/* src/*.o tools/*.o src/*.gcda src/*.gcno
	rm -rf bin/* {src,tools}/*.{o,gcda,gcno}

# Meant to be run after downloading the source tarball. This has an un-expressed
# dependency on `man/funlisp.3`, since the source tarball includes generated

M src/builtins.c => src/builtins.c +3 -3
@@ 758,9 758,8 @@ static lisp_value *lisp_builtin_import(
	if (!lisp_get_args(rt, arglist, "s", &sym))
		return NULL;

	if (strcmp(sym->s, "example") == 0)
		mod = create_example_module(rt);
	else
	mod = lisp_lookup_module(rt, sym);
	if (!mod)
		return lisp_error(rt, LE_NOTFOUND, "module not found");

	lisp_scope_bind(scope, sym, (lisp_value*)mod);


@@ 774,6 773,7 @@ static lisp_value *lisp_builtin_getattr(
	lisp_symbol *sym;

	(void) user;
	(void) scope; /* unused */

	if (!lisp_get_args(rt, arglist, "*s", &mod, &sym))
		return NULL;

M src/funlisp_internal.h => src/funlisp_internal.h +4 -0
@@ 84,6 84,8 @@ struct lisp_runtime {
	struct hashtable *symcache;
	/* Maintain cache of lisp_string */
	struct hashtable *strcache;
	/* Maintain builtin module list */
	struct hashtable *modules;
};

/* The below ARE lisp_values! */


@@ 196,4 198,6 @@ int lisp_truthy(lisp_value *v);

/* Module stuff */
lisp_module *create_example_module(lisp_runtime *rt);
void lisp_register_module(lisp_runtime *rt, lisp_module *m);
lisp_module *lisp_lookup_module(lisp_runtime *rt, lisp_symbol *name);
#endif

M src/gc.c => src/gc.c +5 -0
@@ 24,6 24,10 @@ void lisp_init(lisp_runtime *rt)
	rt->stack_depth = 0;
	rt->symcache = NULL;
	rt->strcache = NULL;
	rt->modules = ht_create(lisp_text_hash, lisp_text_compare,
		sizeof(struct lisp_text *), sizeof(struct lisp_module*));

	lisp_register_module(rt, create_example_module(rt));
}

void lisp_destroy(lisp_runtime *rt)


@@ 36,6 40,7 @@ void lisp_destroy(lisp_runtime *rt)
		ht_delete(rt->symcache);
	if (rt->strcache)
		ht_delete(rt->strcache);
	ht_delete(rt->modules);
}

void lisp_mark(lisp_runtime *rt, lisp_value *v)

M src/module.c => src/module.c +10 -0
@@ 19,3 19,13 @@ lisp_module *create_example_module(lisp_runtime *rt)
	build_example_module(rt, m);
	return m;
}

void lisp_register_module(lisp_runtime *rt, lisp_module *m)
{
	ht_insert_ptr(rt->modules, m->name, m);
}

lisp_module *lisp_lookup_module(lisp_runtime *rt, lisp_symbol *name)
{
	return ht_get_ptr(rt->modules, name);
}