b001c8c12ee2f915ff7b76bb0c55fb547da897cb — Stephen Brennan 3 months ago cbd0dc5 feature_modules
wip
4 files changed, 20 insertions(+), 3 deletions(-)

M src/builtins.c
M src/funlisp_internal.h
M src/gc.c
M src/module.c
M src/builtins.c => src/builtins.c +2 -3
@@ 758,9 758,8 @@
 	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);

M src/funlisp_internal.h => src/funlisp_internal.h +4 -0
@@ 84,6 84,8 @@
 	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 @@
 
 /* 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 +4 -0
@@ 24,6 24,9 @@
 	rt->stack_depth = 0;
 	rt->symcache = NULL;
 	rt->strcache = NULL;
+	rt->modules = ht_create(lisp_text_hash, lisp_text_compare, sizeof(struct lisp_module *), 0);
+
+	lisp_register_module(rt, create_example_module(rt));
 }
 
 void lisp_destroy(lisp_runtime *rt)


@@ 36,6 39,7 @@
 		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 @@
 	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);
+}