Fixed some error states and error messages, read more

By leaving 'null' on the stack, it won't cause stack underflows in a
bunch of erroneous situations. This will allow the repl (and other
situations) to continue if they want to.

I've also fixed some error messages in toy_table.c, which were formatted
badly.

Closes #162
This commit is contained in:
2024-12-26 14:29:25 +11:00
parent 3ca816439e
commit 24cfe7f539
4 changed files with 26 additions and 9 deletions

View File

@@ -45,7 +45,6 @@ typedef enum Toy_OpcodeType {
TOY_OPCODE_PRINT, TOY_OPCODE_PRINT,
TOY_OPCODE_CONCAT, TOY_OPCODE_CONCAT,
TOY_OPCODE_INDEX, TOY_OPCODE_INDEX,
//URGENT: clear the program stack - much needed
//meta instructions //meta instructions
TOY_OPCODE_PASS, TOY_OPCODE_PASS,

View File

@@ -58,7 +58,8 @@ Toy_Table* Toy_private_adjustTableCapacity(Toy_Table* oldTable, unsigned int new
Toy_Table* newTable = malloc(newCapacity * sizeof(Toy_TableEntry) + sizeof(Toy_Table)); Toy_Table* newTable = malloc(newCapacity * sizeof(Toy_TableEntry) + sizeof(Toy_Table));
if (newTable == NULL) { if (newTable == NULL) {
Toy_error(TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Table'\n" TOY_CC_RESET); fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Table'\n" TOY_CC_RESET);
exit(1);
} }
newTable->capacity = newCapacity; newTable->capacity = newCapacity;
@@ -103,7 +104,8 @@ void Toy_freeTable(Toy_Table* table) {
void Toy_insertTable(Toy_Table** tableHandle, Toy_Value key, Toy_Value value) { void Toy_insertTable(Toy_Table** tableHandle, Toy_Value key, Toy_Value value) {
if (TOY_VALUE_IS_NULL(key) || TOY_VALUE_IS_BOOLEAN(key)) { //TODO: disallow functions and opaques if (TOY_VALUE_IS_NULL(key) || TOY_VALUE_IS_BOOLEAN(key)) { //TODO: disallow functions and opaques
Toy_error(TOY_CC_ERROR "ERROR: Bad table key\n" TOY_CC_RESET); fprintf(stderr, TOY_CC_ERROR "ERROR: Bad table key\n" TOY_CC_RESET);
exit(1);
} }
//expand the capacity //expand the capacity
@@ -116,7 +118,8 @@ void Toy_insertTable(Toy_Table** tableHandle, Toy_Value key, Toy_Value value) {
Toy_TableEntry* Toy_private_lookupTableEntryPtr(Toy_Table** tableHandle, Toy_Value key) { Toy_TableEntry* Toy_private_lookupTableEntryPtr(Toy_Table** tableHandle, Toy_Value key) {
if (TOY_VALUE_IS_NULL(key) || TOY_VALUE_IS_BOOLEAN(key)) { //TODO: disallow functions and opaques if (TOY_VALUE_IS_NULL(key) || TOY_VALUE_IS_BOOLEAN(key)) { //TODO: disallow functions and opaques
Toy_error(TOY_CC_ERROR "ERROR: Bad table key\n" TOY_CC_RESET); fprintf(stderr, TOY_CC_ERROR "ERROR: Bad table key\n" TOY_CC_RESET);
exit(1);
} }
//lookup //lookup
@@ -153,7 +156,8 @@ Toy_Value Toy_lookupTable(Toy_Table** tableHandle, Toy_Value key) {
void Toy_removeTable(Toy_Table** tableHandle, Toy_Value key) { void Toy_removeTable(Toy_Table** tableHandle, Toy_Value key) {
if (TOY_VALUE_IS_NULL(key) || TOY_VALUE_IS_BOOLEAN(key)) { //TODO: disallow functions and opaques if (TOY_VALUE_IS_NULL(key) || TOY_VALUE_IS_BOOLEAN(key)) { //TODO: disallow functions and opaques
Toy_error(TOY_CC_ERROR "ERROR: Bad table key\n" TOY_CC_RESET); fprintf(stderr, TOY_CC_ERROR "ERROR: Bad table key\n" TOY_CC_RESET);
exit(1);
} }
//lookup //lookup

View File

@@ -81,7 +81,7 @@ static void processRead(Toy_VM* vm) {
value = TOY_VALUE_FROM_STRING(Toy_createNameStringLength(&vm->stringBucket, cstring, len, valueType, false)); value = TOY_VALUE_FROM_STRING(Toy_createNameStringLength(&vm->stringBucket, cstring, len, valueType, false));
} }
else { else {
Toy_error("Invalid string type found"); Toy_error("Invalid string type found in opcode read");
} }
break; break;
@@ -270,6 +270,7 @@ static void processAssignCompound(Toy_VM* vm) {
Toy_freeValue(target); Toy_freeValue(target);
Toy_freeValue(key); Toy_freeValue(key);
Toy_freeValue(value); Toy_freeValue(value);
return;
} }
//set the value //set the value
@@ -303,6 +304,7 @@ static void processAccess(Toy_VM* vm) {
//check name string type //check name string type
if (!TOY_VALUE_IS_STRING(name) && TOY_VALUE_AS_STRING(name)->info.type != TOY_STRING_NAME) { if (!TOY_VALUE_IS_STRING(name) && TOY_VALUE_AS_STRING(name)->info.type != TOY_STRING_NAME) {
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
Toy_error("Invalid access target"); Toy_error("Invalid access target");
return; return;
} }
@@ -312,6 +314,7 @@ static void processAccess(Toy_VM* vm) {
if (valuePtr == NULL) { if (valuePtr == NULL) {
Toy_freeValue(name); Toy_freeValue(name);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -352,6 +355,7 @@ static void processArithmetic(Toy_VM* vm, Toy_OpcodeType opcode) {
Toy_freeValue(left); Toy_freeValue(left);
Toy_freeValue(right); Toy_freeValue(right);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -361,6 +365,7 @@ static void processArithmetic(Toy_VM* vm, Toy_OpcodeType opcode) {
Toy_error("Can't divide or modulo by zero"); Toy_error("Can't divide or modulo by zero");
Toy_freeValue(left); Toy_freeValue(left);
Toy_freeValue(right); Toy_freeValue(right);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
} }
@@ -370,6 +375,7 @@ static void processArithmetic(Toy_VM* vm, Toy_OpcodeType opcode) {
Toy_error("Can't modulo by a float"); Toy_error("Can't modulo by a float");
Toy_freeValue(left); Toy_freeValue(left);
Toy_freeValue(right); Toy_freeValue(right);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -443,6 +449,7 @@ static void processComparison(Toy_VM* vm, Toy_OpcodeType opcode) {
Toy_freeValue(left); Toy_freeValue(left);
Toy_freeValue(right); Toy_freeValue(right);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -608,6 +615,7 @@ static void processConcat(Toy_VM* vm) {
Toy_error("Failed to concatenate a value that is not a string"); Toy_error("Failed to concatenate a value that is not a string");
Toy_freeValue(left); Toy_freeValue(left);
Toy_freeValue(right); Toy_freeValue(right);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -634,7 +642,7 @@ static void processIndex(Toy_VM* vm) {
} }
else { else {
Toy_error("Incorrect number of elements found in index"); Toy_error("Incorrect number of elements found in index");
//TODO: clear stack, then leave null Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -646,6 +654,7 @@ static void processIndex(Toy_VM* vm) {
Toy_freeValue(value); Toy_freeValue(value);
Toy_freeValue(index); Toy_freeValue(index);
Toy_freeValue(length); Toy_freeValue(length);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -654,6 +663,7 @@ static void processIndex(Toy_VM* vm) {
Toy_freeValue(value); Toy_freeValue(value);
Toy_freeValue(index); Toy_freeValue(index);
Toy_freeValue(length); Toy_freeValue(length);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -668,6 +678,7 @@ static void processIndex(Toy_VM* vm) {
Toy_freeValue(value); Toy_freeValue(value);
Toy_freeValue(index); Toy_freeValue(index);
Toy_freeValue(length); Toy_freeValue(length);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -700,6 +711,7 @@ static void processIndex(Toy_VM* vm) {
Toy_freeValue(value); Toy_freeValue(value);
Toy_freeValue(index); Toy_freeValue(index);
Toy_freeValue(length); Toy_freeValue(length);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -708,6 +720,7 @@ static void processIndex(Toy_VM* vm) {
Toy_freeValue(value); Toy_freeValue(value);
Toy_freeValue(index); Toy_freeValue(index);
Toy_freeValue(length); Toy_freeValue(length);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -722,6 +735,7 @@ static void processIndex(Toy_VM* vm) {
Toy_freeValue(value); Toy_freeValue(value);
Toy_freeValue(index); Toy_freeValue(index);
Toy_freeValue(length); Toy_freeValue(length);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -742,6 +756,7 @@ static void processIndex(Toy_VM* vm) {
Toy_freeValue(value); Toy_freeValue(value);
Toy_freeValue(index); Toy_freeValue(index);
Toy_freeValue(length); Toy_freeValue(length);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }
@@ -754,6 +769,7 @@ static void processIndex(Toy_VM* vm) {
Toy_freeValue(value); Toy_freeValue(value);
Toy_freeValue(index); Toy_freeValue(index);
Toy_freeValue(length); Toy_freeValue(length);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
return; return;
} }

View File

@@ -39,8 +39,6 @@ typedef struct Toy_VM {
//easy access to memory //easy access to memory
Toy_Bucket* stringBucket; //stores the string literals Toy_Bucket* stringBucket; //stores the string literals
Toy_Bucket* scopeBucket; //stores the scopes Toy_Bucket* scopeBucket; //stores the scopes
//URGENT: panic/failed state flag
} Toy_VM; } Toy_VM;
TOY_API void Toy_initVM(Toy_VM* vm); TOY_API void Toy_initVM(Toy_VM* vm);