diff --git a/source/toy_value.c b/source/toy_value.c index ae6121b..f884106 100644 --- a/source/toy_value.c +++ b/source/toy_value.c @@ -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; } diff --git a/source/toy_vm.c b/source/toy_vm.c index 3825bc7..c372563 100644 --- a/source/toy_vm.c +++ b/source/toy_vm.c @@ -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 diff --git a/tests/integrations/test_truthiness.toy b/tests/integrations/test_truthiness.toy new file mode 100644 index 0000000..9703aed --- /dev/null +++ b/tests/integrations/test_truthiness.toy @@ -0,0 +1,80 @@ +//booleans +{ + var value = true; + + if (value) { + print "correct"; + } + else { + assert false; + } +} + +{ + var value = false; + + if (value) { + assert false; + } + else { + print "correct"; + } +} + +//integers +{ + var value: int = 42; + + if (value) { + print "correct"; + } + else { + assert false; + } +} + +{ + var value: int = 0; + + if (value) { + assert false; + } + else { + print "correct"; + } +} + +//floats +{ + var value: float = 42.8891; + + if (value) { + print "correct"; + } + else { + assert false; + } +} + +{ + var value: float = 0; + + if (value) { + assert false; + } + else { + print "correct"; + } +} + +//everything else +{ + var value: string = "foobar"; + + if (value) { + print "correct"; + } + else { + assert false; + } +} \ No newline at end of file