mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Tweaked truthiness, fixed int to float coersion
This commit is contained in:
@@ -194,6 +194,14 @@ bool Toy_checkValueIsTruthy(Toy_Value value) {
|
|||||||
return value.as.boolean;
|
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
|
//anything else is truthy
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -203,6 +203,11 @@ static void processDeclare(Toy_VM* vm) {
|
|||||||
//get the value
|
//get the value
|
||||||
Toy_Value value = Toy_popStack(&vm->stack);
|
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
|
//declare it
|
||||||
Toy_declareScope(vm->scope, name, value);
|
Toy_declareScope(vm->scope, name, value);
|
||||||
|
|
||||||
@@ -223,6 +228,11 @@ static void processAssign(Toy_VM* vm) {
|
|||||||
return;
|
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
|
//assign it
|
||||||
Toy_assignScope(vm->scope, TOY_VALUE_AS_STRING(name), value); //scope now owns the value, doesn't need to be freed
|
Toy_assignScope(vm->scope, TOY_VALUE_AS_STRING(name), value); //scope now owns the value, doesn't need to be freed
|
||||||
|
|
||||||
|
|||||||
80
tests/integrations/test_truthiness.toy
Normal file
80
tests/integrations/test_truthiness.toy
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user