From ff1ef1352a12ed09ebb1ffeb8f26edd94c9c992d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 13 May 2026 10:56:40 +1000 Subject: [PATCH] Reviewed and updated tagged comments --- source/toy_array.c | 2 +- source/toy_attributes.c | 2 +- source/toy_compiler.c | 1 + source/toy_scope.c | 2 +- source/toy_stack.c | 4 ++-- source/toy_table.c | 2 +- source/toy_value.c | 8 +++----- source/toy_vm.c | 6 +++--- tests/scripts/test_tables.toy | 2 -- 9 files changed, 13 insertions(+), 16 deletions(-) diff --git a/source/toy_array.c b/source/toy_array.c index fcaa60f..aa778f4 100644 --- a/source/toy_array.c +++ b/source/toy_array.c @@ -20,7 +20,7 @@ Toy_Array* Toy_resizeArray(Toy_Array* paramArray, unsigned int capacity) { unsigned int originalCapacity = paramArray == NULL ? 0 : paramArray->capacity; - Toy_Array* array = realloc(paramArray, capacity * sizeof(Toy_Value) + sizeof(Toy_Array)); //URGENT: Swap to a bucket + Toy_Array* array = realloc(paramArray, capacity * sizeof(Toy_Value) + sizeof(Toy_Array)); if (array == NULL) { fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to resize a 'Toy_Array' from %d to %d capacity\n" TOY_CC_RESET, (int)originalCapacity, (int)capacity); diff --git a/source/toy_attributes.c b/source/toy_attributes.c index e54364d..5c703d8 100644 --- a/source/toy_attributes.c +++ b/source/toy_attributes.c @@ -239,7 +239,7 @@ Toy_Value Toy_private_handleTableAttributes(Toy_VM* vm, Toy_Value compound, Toy_ Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_tableRemove); return TOY_VALUE_FROM_FUNCTION(fn); } - else if (strncmp(TOY_VALUE_AS_STRING(attribute)->leaf.data, "forEach", 7) == 0) { //BUG: compare the contents AND length of these strings + else if (strncmp(TOY_VALUE_AS_STRING(attribute)->leaf.data, "forEach", 7) == 0) { //URGENT: compare the contents AND length of these strings Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_tableForEach); return TOY_VALUE_FROM_FUNCTION(fn); } diff --git a/source/toy_compiler.c b/source/toy_compiler.c index 9a7e7f9..d27cc95 100644 --- a/source/toy_compiler.c +++ b/source/toy_compiler.c @@ -801,6 +801,7 @@ static unsigned int writeInstructionAssign(Toy_Bytecode** mb, Toy_AstVarAssign a unsigned int result = 0; //BUG: flip the order of target & value, to allow chained assignment AND multiple return values + //do I need multiple return values? //target is a variable name if (ast.target->type == TOY_AST_VALUE && TOY_VALUE_IS_STRING(ast.target->value.value)) { diff --git a/source/toy_scope.c b/source/toy_scope.c index baef500..29a32be 100644 --- a/source/toy_scope.c +++ b/source/toy_scope.c @@ -71,7 +71,7 @@ static void probeAndInsert(Toy_Scope* scope, Toy_String* key, Toy_Value value, T static Toy_ScopeEntry* adjustScopeEntries(Toy_Scope* scope, unsigned int newCapacity) { //allocate and zero a new Toy_ScopeEntry array in memory - Toy_ScopeEntry* newEntries = malloc(newCapacity * sizeof(Toy_ScopeEntry)); //URGENT: Swap to a bucket + Toy_ScopeEntry* newEntries = malloc(newCapacity * sizeof(Toy_ScopeEntry)); if (newEntries == NULL) { fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate space for 'Toy_Scope' entries\n" TOY_CC_RESET); diff --git a/source/toy_stack.c b/source/toy_stack.c index 597f1d5..a21773f 100644 --- a/source/toy_stack.c +++ b/source/toy_stack.c @@ -5,8 +5,8 @@ #include #include -Toy_Stack* Toy_allocateStack(void) { //TODO: add initial size as parameter - Toy_Stack* stack = malloc(TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack)); //URGENT: Swap to a bucket (4 instances) +Toy_Stack* Toy_allocateStack(void) { + Toy_Stack* stack = malloc(TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack)); if (stack == NULL) { fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Stack' of %d capacity (%d space in memory)\n" TOY_CC_RESET, TOY_STACK_INITIAL_CAPACITY, (int)(TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack))); diff --git a/source/toy_table.c b/source/toy_table.c index 65420fd..149a1a3 100644 --- a/source/toy_table.c +++ b/source/toy_table.c @@ -46,7 +46,7 @@ static void probeAndInsert(Toy_Table** tableHandle, Toy_Value key, Toy_Value val //exposed functions Toy_Table* Toy_private_adjustTableCapacity(Toy_Table* oldTable, unsigned int newCapacity) { //allocate and zero a new table in memory - Toy_Table* newTable = malloc(newCapacity * sizeof(Toy_TableEntry) + sizeof(Toy_Table)); //URGENT: Swap to a bucket + Toy_Table* newTable = malloc(newCapacity * sizeof(Toy_TableEntry) + sizeof(Toy_Table)); if (newTable == NULL) { fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Table'\n" TOY_CC_RESET); diff --git a/source/toy_value.c b/source/toy_value.c index 82d761c..e7bf06e 100644 --- a/source/toy_value.c +++ b/source/toy_value.c @@ -104,7 +104,7 @@ Toy_Value Toy_copyValue(Toy_Bucket** bucketHandle, Toy_Value value) { return TOY_VALUE_FROM_STRING(Toy_copyString(value.as.string)); } - case TOY_VALUE_ARRAY: { //TODO: switch to buckets + case TOY_VALUE_ARRAY: { //arrays probably won't get copied much Toy_Array* ptr = value.as.array; Toy_Array* result = Toy_resizeArray(NULL, ptr->capacity); @@ -119,7 +119,7 @@ Toy_Value Toy_copyValue(Toy_Bucket** bucketHandle, Toy_Value value) { return TOY_VALUE_FROM_ARRAY(result); } - case TOY_VALUE_TABLE: { //TODO: switch to buckets + case TOY_VALUE_TABLE: { //tables probably won't get copied much Toy_Table* ptr = value.as.table; Toy_Table* result = Toy_allocateTable(ptr->capacity); @@ -550,7 +550,7 @@ Toy_String* Toy_stringifyValue(Toy_Bucket** bucketHandle, Toy_Value value) { //clean up Toy_freeString(open); Toy_freeString(close); - Toy_freeString(comma); //TODO: reusable global, or string type "permanent", needs benchmarking + Toy_freeString(comma); Toy_freeString(quote); return string; @@ -626,8 +626,6 @@ Toy_String* Toy_stringifyValue(Toy_Bucket** bucketHandle, Toy_Value value) { //finally string = final; - //TODO: would a simple buffer be faster here? - //if there's more elements needsComma = true; } diff --git a/source/toy_vm.c b/source/toy_vm.c index d719a32..97d3819 100644 --- a/source/toy_vm.c +++ b/source/toy_vm.c @@ -725,7 +725,7 @@ static void processAssert(Toy_VM* vm) { //determine the args if (count == 1) { - message = TOY_VALUE_FROM_STRING(Toy_toString(&vm->memoryBucket, "assertion failed")); //TODO: needs a better default message + message = TOY_VALUE_FROM_STRING(Toy_toString(&vm->memoryBucket, "assertion failed")); value = Toy_popStack(&vm->stack); } else if (count == 2) { @@ -1104,7 +1104,7 @@ void Toy_resetVM(Toy_VM* vm, bool preserveScope, bool preserveStack) { //not sure how often to call teh GC if (vm->memoryBucket) { - Toy_collectBucketGarbage(&vm->memoryBucket); + Toy_collectBucketGarbage(&vm->memoryBucket); //TODO: call GC after a certain number of bucket links allocated } } @@ -1199,7 +1199,7 @@ Toy_Array* Toy_extractResultsFromVM(Toy_VM* parentVM, Toy_VM* subVM, unsigned in const unsigned int offset = subVM->stack->count - resultCount; //first element to extract - for (/* EMPTY */; results->count < resultCount; results->count++) { //TODO: make sure the parent bucket adopts the child bucket's responsibilities + for (/* EMPTY */; results->count < resultCount; results->count++) { results->data[results->count] = Toy_copyValue(&parentVM->memoryBucket, subVM->stack->data[offset + results->count]); } diff --git a/tests/scripts/test_tables.toy b/tests/scripts/test_tables.toy index e52a877..08ad0e8 100644 --- a/tests/scripts/test_tables.toy +++ b/tests/scripts/test_tables.toy @@ -5,8 +5,6 @@ a["beta"] = 6; print a; assert a == ["alpha": 1, "beta": 6, "gamma": 3], "1-D tables failed"; -//WONTFIX: Nested tables cause an issue under very specific circumstances: run under GDB, in verbose mode, only in github's CI runner. The reason is unclear. - //nested var b = [ "outer": ["inner": true],