~brenns10/funlisp

8ed8f2e2e4b1f33be7769c15da910bf8abaa195a — Stephen Brennan 1 year, 5 months ago 4a71c61
Make example module into OS module
3 files changed, 27 insertions(+), 6 deletions(-)

M src/funlisp_internal.h
M src/gc.c
M src/module.c
M src/funlisp_internal.h => src/funlisp_internal.h +1 -1
@@ 197,7 197,7 @@ void lisp_textcache_remove(struct hashtable *cache, struct lisp_text *t);
int lisp_truthy(lisp_value *v);

/* Module stuff */
lisp_module *create_example_module(lisp_runtime *rt);
lisp_module *create_os_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 +1 -1
@@ 27,7 27,7 @@ void lisp_init(lisp_runtime *rt)
	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));
	lisp_register_module(rt, create_os_module(rt));
}

void lisp_destroy(lisp_runtime *rt)

M src/module.c => src/module.c +25 -4
@@ 1,18 1,39 @@
/*
 * Module-related functionality. Includes builtin modules.
 */
#include <stdlib.h>

#include "funlisp_internal.h"

static lisp_value *lisp_os_getenv(lisp_runtime *rt, lisp_scope *scope,
                                  lisp_list *arguments, void *user)
{
	lisp_string *str;
	char *res;

	/* args evaluated */
	(void) user;
	(void) scope;

	if (!lisp_get_args(rt, arguments, "S", &str))
		return NULL;

	res = getenv(str->s);
	if (res)
		return (lisp_value*) lisp_string_new(rt, res, LS_CPY | LS_OWN);
	else
		return lisp_nil_new(rt);
}

static void build_example_module(lisp_runtime *rt, lisp_module *m)
{
	lisp_scope_bind(m->contents, lisp_symbol_new(rt, "foo", 0),
			(lisp_value*)lisp_string_new(rt, "bar", 0));
	lisp_scope_add_builtin(rt, m->contents, "getenv", lisp_os_getenv, NULL, 1);
}

lisp_module *create_example_module(lisp_runtime *rt)
lisp_module *create_os_module(lisp_runtime *rt)
{
	lisp_module *m = (lisp_module *) lisp_new(rt, type_module);
	m->name = lisp_string_new(rt, "example", 0);
	m->name = lisp_string_new(rt, "os", 0);
	m->file = lisp_string_new(rt, __FILE__, 0);
	m->contents = lisp_new_empty_scope(rt);