Tweaked truthiness, fixed int to float coersion

This commit is contained in:
2025-02-02 16:38:46 +11:00
parent 63cc530899
commit 336616a1bf
3 changed files with 98 additions and 0 deletions

View File

@@ -194,6 +194,14 @@ bool Toy_checkValueIsTruthy(Toy_Value value) {
return value.as.boolean;
}
if (value.type == TOY_VALUE_INTEGER) {
return value.as.integer != 0;
}
if (value.type == TOY_VALUE_FLOAT) {
return value.as.number != 0.0f;
}
//anything else is truthy
return true;
}

View File

@@ -203,6 +203,11 @@ static void processDeclare(Toy_VM* vm) {
//get the value
Toy_Value value = Toy_popStack(&vm->stack);
//BUGFIX: only allowable type coersion
if (type == TOY_VALUE_FLOAT && value.type == TOY_VALUE_INTEGER) {
value = TOY_VALUE_FROM_FLOAT( (float)TOY_VALUE_AS_INTEGER(value) );
}
//declare it
Toy_declareScope(vm->scope, name, value);
@@ -223,6 +228,11 @@ static void processAssign(Toy_VM* vm) {
return;
}
//BUGFIX: only allowable type coersion
if (TOY_VALUE_AS_STRING(name)->name.varType == TOY_VALUE_FLOAT && value.type == TOY_VALUE_INTEGER) {
value = TOY_VALUE_FROM_FLOAT( (float)TOY_VALUE_AS_INTEGER(value) );
}
//assign it
Toy_assignScope(vm->scope, TOY_VALUE_AS_STRING(name), value); //scope now owns the value, doesn't need to be freed