From a00739f58074500fd683a7341ee9ae646c3f5a2a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 30 May 2026 20:32:22 +1000 Subject: [PATCH] Moved 'debugStackPrint' to 'stack_inspector.c' --- repl/main.c | 33 ++++----------------------------- repl/stack_inspector.c | 39 +++++++++++++++++++++++++++++++++++++++ repl/stack_inspector.h | 5 +++++ 3 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 repl/stack_inspector.c create mode 100644 repl/stack_inspector.h diff --git a/repl/main.c b/repl/main.c index c60a31b..62a2e24 100644 --- a/repl/main.c +++ b/repl/main.c @@ -1,6 +1,7 @@ +#include "bucket_inspector.h" #include "ast_inspector.h" #include "bytecode_inspector.h" -#include "bucket_inspector.h" +#include "stack_inspector.h" #include "toy_console_colors.h" @@ -246,32 +247,6 @@ CmdLine parseCmdLine(int argc, const char* argv[]) { } //debugging -static void debugStackPrint(Toy_Stack* stack) { - //DEBUG: if there's anything on the stack, print it - if (stack->count > 0) { - Toy_Bucket* stringBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); - - printf("\n" TOY_CC_NOTICE "Stack Dump" TOY_CC_RESET "\n" TOY_CC_NOTICE "%-20s%-20s" TOY_CC_RESET "\n", "type", "value"); - for (unsigned int i = 0; i < stack->count; i++) { - Toy_Value v = ((Toy_Value*)(stack + 1))[i]; //'stack + 1' is a naughty trick - - //print type - printf("%-20s", Toy_getValueTypeAsCString(v.type)); - - //print value - Toy_String* string = Toy_stringifyValue(&stringBucket, Toy_unwrapValue(v)); - char* buffer = Toy_getStringRaw(string); - printf("%-20s", buffer); - free(buffer); - Toy_freeString(string); - - printf("\n"); - } - - Toy_freeBucket(&stringBucket); - } -} - static void debugScopePrint(Toy_Scope* scope, int depth) { //DEBUG: if there's anything in the scope, print it if (scope->count > 0) { @@ -391,7 +366,7 @@ int repl(const char* filepath, bool verbose) { //print the debug info if (verbose) { - debugStackPrint(vm.stack); + inspect_stack(vm.stack); debugScopePrint(vm.scope, 0); depthBeforeGC = inspect_bucket(&vm.memoryBucket); @@ -515,7 +490,7 @@ int main(int argc, const char* argv[]) { //print the debug info if (cmd.verbose) { - debugStackPrint(vm.stack); + inspect_stack(vm.stack); debugScopePrint(vm.scope, 0); } diff --git a/repl/stack_inspector.c b/repl/stack_inspector.c new file mode 100644 index 0000000..7a01ca9 --- /dev/null +++ b/repl/stack_inspector.c @@ -0,0 +1,39 @@ +#include "stack_inspector.h" +#include "toy_console_colors.h" +#include "toy_string.h" + +#include +#include + +void inspect_stack(Toy_Stack* stack) { + printf(TOY_CC_NOTICE "Stack State: %u / %u\n" TOY_CC_RESET, stack->count, stack->capacity); + + if (stack->count == 0) { + printf(TOY_CC_NOTICE "\n-- Empty Stack --\n\n" TOY_CC_RESET); + return; + } + + Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); + + printf(TOY_CC_NOTICE "-- Beginning Stack Dump --\n\n%-10s%-20s%-20s\n\n" TOY_CC_RESET, "Index", "Type", "Value"); + + for (unsigned int i = 0; i < stack->count; i++) { + Toy_Value v = ((Toy_Value*)(stack + 1))[i]; //'stack + 1' is a naughty trick + + //print type + printf("%-10u%-20s", i, Toy_getValueTypeAsCString(v.type)); + + //print value + Toy_String* string = Toy_stringifyValue(&bucket, Toy_unwrapValue(v)); + char* buffer = Toy_getStringRaw(string); //lazy, but it works + printf("%-20s", buffer); + free(buffer); + Toy_freeString(string); + + printf("\n"); + } + + printf(TOY_CC_NOTICE "\n-- End Stack Dump --\n\n" TOY_CC_RESET); + + Toy_freeBucket(&bucket); +} \ No newline at end of file diff --git a/repl/stack_inspector.h b/repl/stack_inspector.h new file mode 100644 index 0000000..b4e1b07 --- /dev/null +++ b/repl/stack_inspector.h @@ -0,0 +1,5 @@ +#pragma once + +#include "toy_stack.h" + +void inspect_stack(Toy_Stack* stack);