~brenns10/funlisp

8a818de42a7237383efdc1148a9d484922ef61cc — Stephen Brennan 2 years ago dadcf08
Some minor surgery to remove the lisp_error type
5 files changed, 13 insertions(+), 88 deletions(-)

M doc/embedding.rst
M inc/funlisp.h
M src/types.c
M src/util.c
M tools/call_lisp.c
M doc/embedding.rst => doc/embedding.rst +0 -1
@@ 139,7 139,6 @@ characters are:
- ``s``: for symbol
- ``S``: for string
- ``o``: for scope
- ``e``: for error
- ``b``: for builtin
- ``t``: for type
- ``*``: for anything

M inc/funlisp.h => inc/funlisp.h +5 -25
@@ 117,14 117,6 @@ typedef struct lisp_scope lisp_scope;
typedef struct lisp_symbol lisp_symbol;

/**
 * Error is a lisp type returned whenever (shockingly) an error occurs. This is
 * a bit of a hack to enable a base support for error handling. Errors may have
 * a string message.
 * @ingroup types
 */
typedef struct lisp_error lisp_error;

/**
 * ::lisp_integer contains an int object of whatever size the C implementation
 * supports.
 * @ingroup types


@@ 184,7 176,8 @@ lisp_value *lisp_eval(lisp_runtime *rt, lisp_scope *scope, lisp_value *value);

/**
 * Call a callable object with a list of arguments. Many data types are not
 * callable, in which case a ::lisp_error is returned.
 * callable, in which case a NULL is returned and an error is set within the
 * runtime.
 * @param rt runtime
 * @param scope scope in which we are being evaluated
 * @param callable value to call


@@ 259,11 252,11 @@ void lisp_scope_bind(lisp_scope *scope, lisp_symbol *symbol, lisp_value *value);
/**
 * Look up a symbol within a scope. If it is not found in this scope, look
 * within the parent scope etc, until it is found. If it is not found at all,
 * return a ::lisp_error object.
 * return NULL and set an error within the interpreter.
 * @param rt runtime
 * @param scope scope to look in
 * @param symbol symbol to look up
 * @return value found, or a ::lisp_error when not found
 * @return value found, or a NULL when not found
 */
lisp_value *lisp_scope_lookup(lisp_runtime *rt, lisp_scope *scope,
                              lisp_symbol *symbol);


@@ 273,7 266,7 @@ lisp_value *lisp_scope_lookup(lisp_runtime *rt, lisp_scope *scope,
 * @param rt runtime
 * @param scope scope to look in
 * @param name string name to look up
 * @return value found, or a ::lisp_error when not found
 * @return value found, or NULL when not found
 */
lisp_value *lisp_scope_lookup_string(lisp_runtime *rt, lisp_scope *scope, char *name);



@@ 392,12 385,6 @@ int lisp_nil_p(lisp_value *l);
extern lisp_type *type_symbol;

/**
 * Type object of ::lisp_error, for type checking.
 * @sa lisp_is()
 */
extern lisp_type *type_error;

/**
 * Type object of ::lisp_integer, for type checking.
 * @sa lisp_is()
 */


@@ 489,13 476,6 @@ char *lisp_symbol_get(lisp_symbol *s);
lisp_value  *lisp_error_new(lisp_runtime *rt, char *message);

/**
 * Return the message from an error.
 * @param e the error to retrieve the message from
 * @return the message contained in the error
 */
char *lisp_error_get(lisp_error *e);

/**
 * Create a new integer.
 * @param rt runtime
 * @param n the integer value

M src/types.c => src/types.c +5 -53
@@ 40,15 40,6 @@ static lisp_value *call_error(lisp_runtime *rt, lisp_scope *s, lisp_value *c,
	return lisp_error_new(rt, "not callable!");
}

static lisp_value *call_same(lisp_runtime *rt, lisp_scope *s, lisp_value *c,
                             lisp_value *v)
{
	(void)rt;
	(void)s;
	(void)v;
	return c;
}

static bool has_next_index_lt_state(struct iterator *iter)
{
	return iter->index < iter->state_int;


@@ 331,47 322,6 @@ static void symbol_free(void *v)
}

/*
 * error
 */

static void error_print(FILE *f, lisp_value *v);
static lisp_value *error_new(void);
static void error_free(void *v);

static lisp_type type_error_obj = {
	.type=&type_type_obj,
	.name="error",
	.print=error_print,
	.new=error_new,
	.eval=eval_same,
	.free=error_free,
	.call=call_same,
	.expand=iterator_empty,
};
lisp_type *type_error = &type_error_obj;

static void error_print(FILE *f, lisp_value *v)
{
	lisp_error *error = (lisp_error*) v;
	fprintf(f, "error: %s", error->message);
}

static lisp_value *error_new(void)
{
	lisp_error *error = malloc(sizeof(lisp_error));
	error->type = type_error;
	error->message = NULL;
	return (lisp_value*)error;
}

static void error_free(void *v)
{
	lisp_error *error = (lisp_error*) v;
	free(error->message);
	free(error);
}

/*
 * integer
 */



@@ 599,12 549,14 @@ lisp_value *lisp_call(lisp_runtime *rt, lisp_scope *scope,
                      lisp_value *callable, lisp_value *args)
{
	lisp_value *rv;
	if (callable->type == type_error) {
		return callable;
	}
	/* create new stack frame */
	rt->stack = lisp_list_new(rt, callable, (lisp_value *) rt->stack);
	rt->stack_depth++;

	/* make function call */
	rv = callable->type->call(rt, scope, callable, args);

	/* get rid of stack frame */
	rt->stack = (lisp_list*) rt->stack->right;
	rt->stack_depth--;
	return rv;

M src/util.c => src/util.c +2 -8
@@ 104,8 104,6 @@ static lisp_type *lisp_get_type(char c)
		return type_string;
	case 'o':
		return type_scope;
	case 'e':
		return type_error;
	case 'b':
		return type_builtin;
	case 't':


@@ 217,7 215,8 @@ lisp_value *lisp_run_main_if_exists(lisp_runtime *rt, lisp_scope *scope,
	lisp_value *main_func = lisp_scope_lookup(
		rt, scope, lisp_symbol_new(rt, "main"));

	if (main_func->type == type_error) {
	if (main_func->type == NULL) {
		lisp_clear_error(rt);
		return lisp_nil_new(rt);
	}



@@ 286,11 285,6 @@ char *lisp_symbol_get(lisp_symbol *sym)
	return sym->sym;
}

char *lisp_error_get(lisp_error *err)
{
	return err->message;
}

lisp_list *lisp_list_new(lisp_runtime *rt, lisp_value *left, lisp_value *right)
{
	lisp_list *l = (lisp_list *) lisp_new(rt, type_list);

M tools/call_lisp.c => tools/call_lisp.c +1 -1
@@ 16,7 16,7 @@ int call_double_or_square(lisp_runtime *rt, lisp_scope *scope, int x)
	lisp_value *args, *res;
	lisp_value *function = lisp_scope_lookup_string(rt, scope,
		"double_or_square");
	assert(function->type != type_error);
	assert(function->type != NULL);

	args = (lisp_value*) lisp_list_new(rt,
		(lisp_value *) lisp_integer_new(rt, x),