Skip to content

Commit

Permalink
Fixed nested assignment bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Dec 24, 2024
1 parent 223db84 commit 4faa0c0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 127 deletions.
7 changes: 4 additions & 3 deletions source/toy_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ Toy_Value Toy_copyValue(Toy_Value value) {
}

void Toy_freeValue(Toy_Value value) {
//NOTE: do not unwrap this value, as references shouldn't be freed

switch(value.type) {
case TOY_VALUE_NULL:
case TOY_VALUE_BOOLEAN:
Expand All @@ -143,12 +141,15 @@ void Toy_freeValue(Toy_Value value) {
break;
}

case TOY_VALUE_REFERENCE:
//don't free references
return;

case TOY_VALUE_TABLE:
case TOY_VALUE_FUNCTION:
case TOY_VALUE_OPAQUE:
case TOY_VALUE_TYPE:
case TOY_VALUE_ANY:
case TOY_VALUE_REFERENCE:
case TOY_VALUE_UNKNOWN:
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't free an unknown value type, exiting\n" TOY_CC_RESET);
exit(-1);
Expand Down
163 changes: 39 additions & 124 deletions source/toy_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,8 @@ static void processArithmetic(Toy_VM* vm, Toy_OpcodeType opcode) {
snprintf(buffer, 256, "Invalid types '%s' and '%s' passed in arithmetic", Toy_private_getValueTypeAsCString(left.type), Toy_private_getValueTypeAsCString(right.type));
Toy_error(buffer);

if (TOY_VALUE_IS_REFERENCE(left) != true) {
Toy_freeValue(left);
}

if (TOY_VALUE_IS_REFERENCE(right) != true) {
Toy_freeValue(right);
}

Toy_freeValue(left);
Toy_freeValue(right);
return;
}

Expand Down Expand Up @@ -390,14 +384,8 @@ static void processComparison(Toy_VM* vm, Toy_OpcodeType opcode) {
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_BOOLEAN(!equal) );
}

if (TOY_VALUE_IS_REFERENCE(left) != true) {
Toy_freeValue(left);
}

if (TOY_VALUE_IS_REFERENCE(right) != true) {
Toy_freeValue(right);
}

Toy_freeValue(left);
Toy_freeValue(right);
return;
}

Expand All @@ -406,14 +394,8 @@ static void processComparison(Toy_VM* vm, Toy_OpcodeType opcode) {
snprintf(buffer, 256, "Can't compare value types '%s' and '%s'", Toy_private_getValueTypeAsCString(left.type), Toy_private_getValueTypeAsCString(right.type));
Toy_error(buffer);

if (TOY_VALUE_IS_REFERENCE(left) != true) {
Toy_freeValue(left);
}

if (TOY_VALUE_IS_REFERENCE(right) != true) {
Toy_freeValue(right);
}

Toy_freeValue(left);
Toy_freeValue(right);
return;
}

Expand All @@ -439,13 +421,8 @@ static void processComparison(Toy_VM* vm, Toy_OpcodeType opcode) {
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_BOOLEAN(false));
}

if (TOY_VALUE_IS_REFERENCE(left) != true) {
Toy_freeValue(left);
}

if (TOY_VALUE_IS_REFERENCE(right) != true) {
Toy_freeValue(right);
}
Toy_freeValue(left);
Toy_freeValue(right);
}

static void processLogical(Toy_VM* vm, Toy_OpcodeType opcode) {
Expand Down Expand Up @@ -493,29 +470,22 @@ static void processJump(Toy_VM* vm) {
case TOY_OP_PARAM_JUMP_IF_TRUE: {
Toy_Value value = Toy_popStack(&vm->stack);
if (Toy_checkValueIsTruthy(value) == true) {
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
Toy_freeValue(value);
break;
}

if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
Toy_freeValue(value);
return;
}

case TOY_OP_PARAM_JUMP_IF_FALSE: {
Toy_Value value = Toy_popStack(&vm->stack);
if (Toy_checkValueIsTruthy(value) != true) {
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
break;
}
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
break;
}

Toy_freeValue(value);
return;
}
}
Expand Down Expand Up @@ -566,13 +536,8 @@ static void processAssert(Toy_VM* vm) {
}

//cleanup
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}

if (TOY_VALUE_IS_REFERENCE(message) != true) {
Toy_freeValue(message);
}
Toy_freeValue(value);
Toy_freeValue(message);
}

static void processPrint(Toy_VM* vm) {
Expand All @@ -585,10 +550,7 @@ static void processPrint(Toy_VM* vm) {

free(buffer);
Toy_freeString(string);

if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
Toy_freeValue(value);
}

static void processConcat(Toy_VM* vm) {
Expand All @@ -597,13 +559,8 @@ static void processConcat(Toy_VM* vm) {

if (!TOY_VALUE_IS_STRING(left) || !TOY_VALUE_IS_STRING(right)) {
Toy_error("Failed to concatenate a value that is not a string");

if (TOY_VALUE_IS_REFERENCE(left) != true) {
Toy_freeValue(left);
}
if (TOY_VALUE_IS_REFERENCE(right) != true) {
Toy_freeValue(right);
}
Toy_freeValue(left);
Toy_freeValue(right);
return;
}

Expand Down Expand Up @@ -639,29 +596,17 @@ static void processIndex(Toy_VM* vm) {
//type checks
if (!TOY_VALUE_IS_INTEGER(index)) {
Toy_error("Failed to index a string");
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
if (TOY_VALUE_IS_REFERENCE(index) != true) {
Toy_freeValue(index);
}
if (TOY_VALUE_IS_REFERENCE(length) != true) {
Toy_freeValue(length);
}
Toy_freeValue(value);
Toy_freeValue(index);
Toy_freeValue(length);
return;
}

if (!(TOY_VALUE_IS_NULL(length) || TOY_VALUE_IS_INTEGER(length))) {
Toy_error("Failed to index-length a string");
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
if (TOY_VALUE_IS_REFERENCE(index) != true) {
Toy_freeValue(index);
}
if (TOY_VALUE_IS_REFERENCE(length) != true) {
Toy_freeValue(length);
}
Toy_freeValue(value);
Toy_freeValue(index);
Toy_freeValue(length);
return;
}

Expand All @@ -673,15 +618,9 @@ static void processIndex(Toy_VM* vm) {
//check indexing is within bounds
if ( (i < 0 || (unsigned int)i >= str->info.length) || (i+l <= 0 || (unsigned int)(i+l) > str->info.length)) {
Toy_error("String index is out of bounds");
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
if (TOY_VALUE_IS_REFERENCE(index) != true) {
Toy_freeValue(index);
}
if (TOY_VALUE_IS_REFERENCE(length) != true) {
Toy_freeValue(length);
}
Toy_freeValue(value);
Toy_freeValue(index);
Toy_freeValue(length);
return;
}

Expand Down Expand Up @@ -711,29 +650,17 @@ static void processIndex(Toy_VM* vm) {
//type checks
if (!TOY_VALUE_IS_INTEGER(index)) {
Toy_error("Failed to index a string");
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
if (TOY_VALUE_IS_REFERENCE(index) != true) {
Toy_freeValue(index);
}
if (TOY_VALUE_IS_REFERENCE(length) != true) {
Toy_freeValue(length);
}
Toy_freeValue(value);
Toy_freeValue(index);
Toy_freeValue(length);
return;
}

if (!(TOY_VALUE_IS_NULL(length) || TOY_VALUE_IS_INTEGER(length))) {
Toy_error("Failed to index-length a string");
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
if (TOY_VALUE_IS_REFERENCE(index) != true) {
Toy_freeValue(index);
}
if (TOY_VALUE_IS_REFERENCE(length) != true) {
Toy_freeValue(length);
}
Toy_freeValue(value);
Toy_freeValue(index);
Toy_freeValue(length);
return;
}

Expand All @@ -745,15 +672,9 @@ static void processIndex(Toy_VM* vm) {
//check indexing is within bounds
if ( (i < 0 || (unsigned int)i >= array->count) || (i+l <= 0 || (unsigned int)(i+l) > array->count)) {
Toy_error("Array index is out of bounds");
if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
if (TOY_VALUE_IS_REFERENCE(index) != true) {
Toy_freeValue(index);
}
if (TOY_VALUE_IS_REFERENCE(length) != true) {
Toy_freeValue(length);
}
Toy_freeValue(value);
Toy_freeValue(index);
Toy_freeValue(length);
return;
}

Expand All @@ -775,15 +696,9 @@ static void processIndex(Toy_VM* vm) {
exit(-1);
}

if (TOY_VALUE_IS_REFERENCE(value) != true) {
Toy_freeValue(value);
}
if (TOY_VALUE_IS_REFERENCE(index) != true) {
Toy_freeValue(index);
}
if (TOY_VALUE_IS_REFERENCE(length) != true) {
Toy_freeValue(length);
}
Toy_freeValue(value);
Toy_freeValue(index);
Toy_freeValue(length);
}

static void process(Toy_VM* vm) {
Expand Down

0 comments on commit 4faa0c0

Please sign in to comment.