~brenns10/funlisp

e520cb8ad7cce01ba2f2c370136fd59086014191 — Stephen Brennan 2 years ago fcc46c4
Implement builtin for dumping stack
4 files changed, 53 insertions(+), 0 deletions(-)

M doc/api.rst
M inc/funlisp.h
M src/builtins.c
M src/util.c
M doc/api.rst => doc/api.rst +6 -0
@@ 44,3 44,9 @@ Embedding Tools

.. doxygengroup:: embed
   :content-only:

Miscellaneous Functions
-----------------------

.. doxygengroup:: misc
   :content-only:

M inc/funlisp.h => inc/funlisp.h +17 -0
@@ 690,6 690,23 @@ lisp_value *lisp_quote(lisp_runtime *rt, lisp_value *value);

/**
 * @}
 * @defgroup misc Miscellaneous Functions
 * @{
 */

/**
 * Dump the execution stack to a file. This is useful if you want to print a
 * stack trace at your current location. This functionality can also be accessed
 * via the ``dump-stack`` builtin function.
 * @param rt runtime
 * @param stack When NULL, the runtime's execution stack is used. When non-NULL,
 * the @a stack argument is used to specify what stack to dump.
 * @param file where to dump stack trace to
 */
void lisp_dump_stack(lisp_runtime *rt, lisp_list *stack, FILE *file);

/**
 * @}
 */

#endif

M src/builtins.c => src/builtins.c +14 -0
@@ 4,6 4,8 @@
 * Stephen Brennan <stephen@brennan.io>
 */

#include <stdio.h>

#include "funlisp_internal.h"

static lisp_value *lisp_builtin_eval(lisp_runtime *rt, lisp_scope *scope,


@@ 471,6 473,17 @@ static lisp_value *lisp_builtin_print(lisp_runtime *rt, lisp_scope *scope, lisp_
	return lisp_nil_new(rt);
}

static lisp_value *lisp_builtin_dump_stack(lisp_runtime *rt, lisp_scope *scope,
                                           lisp_value *a, void *user)
{
	/* NB: This is very debugging oriented. We don't even eval arguments! */
	(void) scope; /* unused args */
	(void) a;
	(void) user;
	lisp_dump_stack(rt, NULL, stderr);
	return lisp_nil_new(rt);
}

void lisp_scope_populate_builtins(lisp_runtime *rt, lisp_scope *scope)
{
	lisp_scope_add_builtin(rt, scope, "eval", lisp_builtin_eval, NULL);


@@ 495,4 508,5 @@ void lisp_scope_populate_builtins(lisp_runtime *rt, lisp_scope *scope)
	lisp_scope_add_builtin(rt, scope, "map", lisp_builtin_map, NULL);
	lisp_scope_add_builtin(rt, scope, "reduce", lisp_builtin_reduce, NULL);
	lisp_scope_add_builtin(rt, scope, "print", lisp_builtin_print, NULL);
	lisp_scope_add_builtin(rt, scope, "dump-stack", lisp_builtin_dump_stack, NULL);
}

M src/util.c => src/util.c +16 -0
@@ 11,6 11,7 @@
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "funlisp_internal.h"
#include "hashtable.h"


@@ 321,3 322,18 @@ int lisp_integer_get(lisp_integer *integer)
{
	return integer->x;
}

void lisp_dump_stack(lisp_runtime *rt, lisp_list *stack, FILE *file)
{
	if (!stack)
		stack = rt->stack;

	fprintf(file, "Stack trace (most recent call first):\n");
	while (!lisp_nil_p((lisp_value *) stack)) {
		fprintf(file, "  ");
		lisp_print(file, stack->left);
		fprintf(file, "\n");

		stack = (lisp_list *) stack->right;
	}
}