Added short-circuiting support to && and ||

This commit is contained in:
2023-03-11 17:59:09 +11:00
parent 68ed52b347
commit f2f8aed23a
4 changed files with 21 additions and 6 deletions

View File

@@ -1082,11 +1082,12 @@ static bool execAnd(Toy_Interpreter* interpreter) {
return false;
}
if (TOY_IS_TRUTHY(lhs) && TOY_IS_TRUTHY(rhs)) {
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(true));
//short-circuit support
if (!TOY_IS_TRUTHY(lhs)) {
Toy_pushLiteralArray(&interpreter->stack, lhs);
}
else {
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(false));
Toy_pushLiteralArray(&interpreter->stack, rhs);
}
Toy_freeLiteral(lhs);
@@ -1115,11 +1116,12 @@ static bool execOr(Toy_Interpreter* interpreter) {
return false;
}
if (TOY_IS_TRUTHY(lhs) || TOY_IS_TRUTHY(rhs)) {
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(true));
//short-circuit support
if (TOY_IS_TRUTHY(lhs)) {
Toy_pushLiteralArray(&interpreter->stack, lhs);
}
else {
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_BOOLEAN_LITERAL(false));
Toy_pushLiteralArray(&interpreter->stack, rhs);
}
Toy_freeLiteral(lhs);