~lbnz/xr0

7dd83925dc727666e2fa22987d096d7bc350d90c — Claude Betz a month ago 7c4ed29
feat: debugger cosmetics

issue: https://github.com/xr0-org/xr0/issues/45
7 files changed, 105 insertions(+), 40 deletions(-)

M include/state.h
M src/ast/block.c
M src/ext/ext.c
M src/path/path.c
M src/state/stack.c
M src/state/state.c
M src/value/value.c
M include/state.h => include/state.h +3 -0
@@ 128,6 128,9 @@ state_range_dealloc(struct state *, struct object *,
bool
state_islinear(struct state *);

char *
state_execmode_str(enum execution_mode);

enum execution_mode
state_execmode(struct state *);


M src/ast/block.c => src/ast/block.c +1 -1
@@ 143,7 143,7 @@ ast_block_render(struct ast_block *b, int index, bool indecls)
		free(s);
	}
	for (int i = 0; i < b->nstmt; i++) {
		char *s = ast_stmt_str(b->stmt[i], 1);
		char *s = ast_stmt_str(b->stmt[i], 2);
		if (i == index && !indecls) {
			strbuilder_printf(sb, "-->\t%s\n", s);
		} else {

M src/ext/ext.c => src/ext/ext.c +0 -1
@@ 48,7 48,6 @@ externals_types_str(struct externals *ext, char *indent)
		free(type);
	}


	return strbuilder_build(b);
}


M src/path/path.c => src/path/path.c +3 -3
@@ 461,9 461,9 @@ static struct error *
path_step_abstract(struct path *p, bool print)
{
	if (print) {
		d_printf("mode:%d\n", state_execmode(p->abstract));
		d_printf("mode:%s\n", state_execmode_str(state_execmode(p->abstract)));
		d_printf("text:\n%s\n", state_programtext(p->abstract));
		d_printf("abstract: %s\n", state_str(p->abstract));
		d_printf("%s\n", state_str(p->abstract));
	}
	if (state_atend(p->abstract) && state_frameid(p->abstract) == 0) {
		p->path_state = PATH_STATE_HALFWAY;


@@ 486,7 486,7 @@ static struct error *
path_step_actual(struct path *p, bool print)
{
	if (print) {
		d_printf("mode:%d\n", state_execmode(p->actual));
		d_printf("mode:%s\n", state_execmode_str(state_execmode(p->actual)));
		d_printf("text:\n%s\n", state_programtext(p->actual));
		d_printf("actual: %s\n", state_str(p->actual));
	}

M src/state/stack.c => src/state/stack.c +59 -9
@@ 215,18 215,38 @@ varmap_copy(struct map *m)
	return m_copy;
}

static struct string_arr *
var_strs(struct stack *, struct state *);

static int
maxlenorzero(struct string_arr *);

char *
stack_str(struct stack *stack, struct state *state)
{
	struct strbuilder *b = strbuilder_create();
	struct map *m = stack->varmap;
	struct string_arr *var_str_arr = var_strs(stack, state);
	int var_str_max = maxlenorzero(var_str_arr);
	assert(string_arr_n(var_str_arr) == m->n);
	char **var_str = string_arr_s(var_str_arr);

	for (int i = 0; i < m->n; i++) {
		struct entry e = m->entry[i];
		char *var = variable_str((struct variable *) e.value, stack, state);
		strbuilder_printf(b, "\t%s: %s", e.key, var);
		free(var);
		strbuilder_putc(b, '\n');
		struct variable *v = (struct variable *) e.value;
		strbuilder_printf(
			b, 
			"\t%d: %-*s (%s",
			i,
			var_str_max, var_str[i],
			e.key
		);
		if (variable_isparam(v)) {
			strbuilder_printf(b, ", π");
		}
		strbuilder_printf(b, ")\n");
	}
	string_arr_destroy(var_str_arr);
	strbuilder_printf(b, "\t");
	/* TODO: fix length of line */
	for (int i = 0, len = 30; i < len-2; i++ ) {


@@ 241,6 261,40 @@ stack_str(struct stack *stack, struct state *state)
	return strbuilder_build(b);
}


static struct string_arr *
var_strs(struct stack *stack, struct state *state)
{
	struct string_arr *arr = string_arr_create();
	struct map *m = stack->varmap;
	for (int i = 0; i < m->n; i++) {
		string_arr_append(
			arr,
			variable_str(
				(struct variable *) m->entry[i].value,
				stack,
				state
			)
		);
	}
	return arr;
}

static int
maxlenorzero(struct string_arr *arr)
{
	int n = string_arr_n(arr);
	char **s = string_arr_s(arr);
	int max = 0;
	for (int i = 0; i < n; i++) {
		int len = strlen(s[i]);
		if (len > max) {
			max = len;
		}
	}
	return max;
}

bool
stack_islinear(struct stack *s)
{


@@ 596,15 650,11 @@ char *
variable_str(struct variable *var, struct stack *stack, struct state *state)
{
	assert(location_type(var->loc) != LOCATION_VCONST);

	struct strbuilder *b = strbuilder_create();
	char *type = ast_type_str(var->type);
	char *loc = location_str(var->loc);
	char *isparam = var->isparam ? "param " : "";
	char *obj_str = object_or_nothing_str(var->loc, stack, state);
	strbuilder_printf(b, "{%s%s := %s} @ %s", isparam, type, obj_str, loc);
	strbuilder_printf(b, "{%s := %s}", type, obj_str);
	free(obj_str);
	free(loc);
	free(type);
	return strbuilder_build(b);
}

M src/state/state.c => src/state/state.c +38 -23
@@ 94,48 94,41 @@ char *
state_str(struct state *state)
{
	struct strbuilder *b = strbuilder_create();
	strbuilder_printf(b, "[[\n");
	char *ext = externals_types_str(state->ext, "\t");
	if (strlen(ext) > 0) {
		strbuilder_printf(b, "%s\n", ext);
	}
	free(ext);
	strbuilder_printf(
		b,
		"\treturn: {<type> := {%s}}\n\n",
		state->reg ? value_str(state->reg) : "empty"
	);
	char *context = externals_types_str(state->ext, "\t");
	if (strlen(context) > 0) {
		strbuilder_printf(b, "context:\n%s\n", context);
	}
	free(context);
	char *static_mem = static_memory_str(state->static_memory, "\t");
	if (strlen(static_mem) > 0) {
		strbuilder_printf(b, "%s\n", static_mem);
		strbuilder_printf(b, "static:\n%s\n", static_mem);
	}
	free(static_mem);
	char *vconst = vconst_str(state->vconst, "\t");
	if (strlen(vconst) > 0) {
		strbuilder_printf(b, "%s\n", vconst);
		strbuilder_printf(b, "rconst:\n%s\n", vconst);
	}
	free(vconst);
	char *props = props_str(state->props, "\t");
	if (strlen(props) > 0) {
		strbuilder_printf(b, "assume:\n%s\n", props);
	}
	free(props);
	char *clump = clump_str(state->clump, "\t");
	if (strlen(clump) > 0) {
		strbuilder_printf(b, "%s\n", clump);
		strbuilder_printf(b, "clump:\n%s\n", clump);
	}
	free(clump);
	char *stack = stack_str(state->stack, state);
	if (strlen(stack) > 0) {
		strbuilder_printf(b, "%s\n", stack);
		strbuilder_printf(b, "stack:\n%s\n", stack);
	}
	free(stack);
	char *props = props_str(state->props, "\t");
	if (strlen(props) > 0) {
		strbuilder_printf(b, "%s", props);
	}
	free(props);
	free(stack);	
	char *heap = heap_str(state->heap, "\t");
	if (strlen(heap) > 0) {
		strbuilder_printf(b, "\n%s\n", heap);
		strbuilder_printf(b, "heap:\n%s\n", heap);
	}
	free(heap);
	strbuilder_printf(b, "]]\n");
	return strbuilder_build(b);
}



@@ 145,6 138,28 @@ state_islinear(struct state *s)
	return stack_islinear(s->stack);
}

char *
state_execmode_str(enum execution_mode m)
{
	char *execmode_type_str[] = {
		[EXEC_ABSTRACT] 		= "ABSTRACT",
		[EXEC_ABSTRACT_NO_SETUP] 	= "INTERNAL",
		[EXEC_SETUP]			= "INTERNAL",
		[EXEC_ACTUAL]			= "ACTUAL",
		[EXEC_VERIFY]			= "VERIFY",
	};
	switch (m) {
	case EXEC_ABSTRACT:
	case EXEC_ABSTRACT_NO_SETUP:
	case EXEC_SETUP:
	case EXEC_ACTUAL:
	case EXEC_VERIFY:
		return dynamic_str(execmode_type_str[m]);
	default:
		assert(false);
	}
}

enum execution_mode
state_execmode(struct state *s)
{

M src/value/value.c => src/value/value.c +1 -3
@@ 436,8 436,6 @@ struct_referencesheap(struct value *v, struct state *s, struct circuitbreaker *c
	return false;
}



void
value_struct_sprint(struct value *v, struct strbuilder *b)
{


@@ 468,7 466,7 @@ value_int_sprint(struct value *v, struct strbuilder *b)
void
value_sync_sprint(struct value *v, struct strbuilder *b)
{
	strbuilder_printf(b, "comp:%s", number_str(v->n));
	strbuilder_printf(b, "rconst:%s", number_str(v->n));
}

struct value *