Moved 'debugStackPrint' to 'stack_inspector.c'

This commit is contained in:
2026-05-30 20:32:22 +10:00
parent d194bff5fe
commit a00739f580
3 changed files with 48 additions and 29 deletions
+4 -29
View File
@@ -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);
}
+39
View File
@@ -0,0 +1,39 @@
#include "stack_inspector.h"
#include "toy_console_colors.h"
#include "toy_string.h"
#include <stdio.h>
#include <stdlib.h>
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);
}
+5
View File
@@ -0,0 +1,5 @@
#pragma once
#include "toy_stack.h"
void inspect_stack(Toy_Stack* stack);