mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Added short-circuiting support to && and ||
This commit is contained in:
@@ -1082,11 +1082,12 @@ static bool execAnd(Toy_Interpreter* interpreter) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_TRUTHY(lhs) && TOY_IS_TRUTHY(rhs)) {
|
//short-circuit support
|
||||||
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(true));
|
if (!TOY_IS_TRUTHY(lhs)) {
|
||||||
|
Toy_pushLiteralArray(&interpreter->stack, lhs);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(false));
|
Toy_pushLiteralArray(&interpreter->stack, rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_freeLiteral(lhs);
|
Toy_freeLiteral(lhs);
|
||||||
@@ -1115,11 +1116,12 @@ static bool execOr(Toy_Interpreter* interpreter) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_TRUTHY(lhs) || TOY_IS_TRUTHY(rhs)) {
|
//short-circuit support
|
||||||
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(true));
|
if (TOY_IS_TRUTHY(lhs)) {
|
||||||
|
Toy_pushLiteralArray(&interpreter->stack, lhs);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(false));
|
Toy_pushLiteralArray(&interpreter->stack, rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_freeLiteral(lhs);
|
Toy_freeLiteral(lhs);
|
||||||
|
|||||||
@@ -23,5 +23,8 @@ assert !false, "!false";
|
|||||||
var c = false;
|
var c = false;
|
||||||
assert !c, "!c";
|
assert !c, "!c";
|
||||||
|
|
||||||
|
//test multiple comparisons
|
||||||
|
assert 1 == 2 == false, "Left-accociative equality failed";
|
||||||
|
|
||||||
|
|
||||||
print "All good";
|
print "All good";
|
||||||
|
|||||||
9
test/scripts/short-circuiting-support.toy
Normal file
9
test/scripts/short-circuiting-support.toy
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
//explicitly support && and || short circuits
|
||||||
|
|
||||||
|
assert 1 && 2 == 2, "&& short-circuit failed";
|
||||||
|
|
||||||
|
assert 1 || 2 == 1, "|| short-circuit failed";
|
||||||
|
|
||||||
|
|
||||||
|
print "All good";
|
||||||
|
|
||||||
@@ -132,6 +132,7 @@ int main() {
|
|||||||
"panic-within-functions.toy",
|
"panic-within-functions.toy",
|
||||||
"polyfill-insert.toy",
|
"polyfill-insert.toy",
|
||||||
"polyfill-remove.toy",
|
"polyfill-remove.toy",
|
||||||
|
"short-circuiting-support.toy",
|
||||||
"ternary-expressions.toy",
|
"ternary-expressions.toy",
|
||||||
"types.toy",
|
"types.toy",
|
||||||
NULL
|
NULL
|
||||||
|
|||||||
Reference in New Issue
Block a user