~brenns10/funlisp

2255dd2d193c3803653e2f8dd4e70b8c3a3d290b — Stephen Brennan 1 year, 5 months ago 46da27d
Add module type

Signed-off-by: Stephen Brennan <stephen@brennan.io>
3 files changed, 75 insertions(+), 0 deletions(-)

M inc/funlisp.h
M src/funlisp_internal.h
M src/types.c
M inc/funlisp.h => inc/funlisp.h +11 -0
@@ 197,6 197,12 @@ typedef struct lisp_lambda lisp_lambda;
typedef struct lisp_list lisp_list;

/**
 * Data structure representing a module.
 * @ingroup types
 */
typedef struct lisp_module lisp_module;

/**
 * @defgroup value Lisp Values
 * @{
 */


@@ 508,6 514,11 @@ extern lisp_type *type_builtin;
extern lisp_type *type_lambda;

/**
 * Type object of ::lisp_module
 */
extern lisp_type *type_module;

/**
 * Flag instructing string/symbol creation routines that they should copy the
 * string buffer itself, and use the copy rather than the original argument.
 * This could be useful in case callers would like to free the string after

M src/funlisp_internal.h => src/funlisp_internal.h +7 -0
@@ 143,6 143,13 @@ struct lisp_lambda {
	int lambda_type;
};

struct lisp_module {
	LISP_VALUE_HEAD;
	lisp_scope *contents;
	lisp_string *name;
	lisp_string *file;
};

/**
 * A function which consumes a single ::lisp_value and produces a new one as a
 * result.

M src/types.c => src/types.c +57 -0
@@ 795,3 795,60 @@ int lisp_compare(lisp_value *self, lisp_value *other)
{
	return self->type->compare(self, other);
}

/*
 * module
 */

static void module_print(FILE *f, lisp_value*v);
static lisp_value *module_new(lisp_runtime *rt);
static struct iterator module_expand(lisp_value *);
static int module_compare(lisp_value *self, lisp_value *other);

static lisp_type type_module_obj = {
	TYPE_HEADER,
	/* name */ "module",
	/* print */ module_print,
	/* new */ module_new,
	/* free */ simple_free,
	/* expand */ module_expand,
	/* eval */ eval_error,
	/* call */ call_error,
	/* compare */ module_compare,
};
lisp_type *type_module = &type_module_obj;

static lisp_value *module_new(lisp_runtime *rt)
{
	lisp_module *module;
	(void) rt; /* unused */

	module = malloc(sizeof(lisp_module));
	module->contents = NULL;
	module->name = NULL;
	module->name = NULL;
	return (lisp_value*)module;
}

static void module_print(FILE *f, lisp_value *v)
{
	lisp_module *module = (lisp_module*) v;
	fprintf(f, "<module '%s' from '%s' at 0x%p>",
			module->name->s, module->file->s, (void*)module);
}

static struct iterator module_expand(lisp_value *v)
{
	lisp_module *module = (lisp_module *) v;
	return iterator_from_args(3,
		module->name,
		module->file,
		module->contents
	);
}

static int module_compare(lisp_value *self, lisp_value *other)
{
	/* Compare by value for simplicity. */
	return self == other;
}