added the ability to inspect the stack from the debugger

This commit is contained in:
cozis
2021-11-02 14:17:24 +00:00
parent acc8cbd75f
commit f351be78bb
6 changed files with 115 additions and 6 deletions
+42 -1
View File
@@ -267,7 +267,8 @@ _Bool Debug_Callback(Runtime *runtime, void *userp)
"step .............. Run an instruction\n"
"quit .............. Stop execution\n"
"continue .......... Run until a breakpoint or the end of the code is reached\n"
"breakpoint ........ Add a breakpoint\n");
"breakpoint ........ Add a breakpoint\n"
"stack ............. Show the contents of the stack\n");
}
else if(!strcmp(argv[1], "help"))
{
@@ -339,6 +340,17 @@ _Bool Debug_Callback(Runtime *runtime, void *userp)
" | a line number, a character offset or an instruction index.\n"
"\n");
}
else if(!strcmp(argv[1], "stack"))
{
fprintf(stderr,
"\n"
" Command | stack\n"
" | \n"
" Usage | > stack\n"
" | \n"
" Description | Show the contents of the stack.\n"
"\n");
}
else
{
fprintf(stdout, "Unknown command \"%s\".\n", argv[1]);
@@ -436,6 +448,35 @@ _Bool Debug_Callback(Runtime *runtime, void *userp)
{
return 1;
}
else if(!strcmp(argv[0], "stack"))
{
Stack *stack = Runtime_GetStack(runtime);
assert(stack != NULL);
Error error;
Error_Init(&error);
if(Stack_Size(stack) == 0)
fprintf(stderr, "The stack is empty.\n");
for(int i = 0; i < (int) Stack_Size(stack); i += 1)
{
Object *obj = Stack_Top(stack, -i);
assert(obj != NULL);
fprintf(stderr, " %d | ", i);
Object_Print(obj, stderr, &error);
if(error.occurred)
{
Error_Free(&error);
Error_Init(&error);
fprintf(stderr, "(unprintable)");
}
fprintf(stderr, "\n");
}
}
else if(!strcmp(argv[0], "quit"))
{
return 0;