Implemented logical && and ||

This commit is contained in:
2022-08-23 05:07:17 +01:00
parent 6939b216a9
commit 4f70bea808
5 changed files with 70 additions and 5 deletions

View File

@@ -624,6 +624,40 @@ static bool execCompareLessEqual(Interpreter* interpreter, bool invert) {
return true;
}
static bool execAnd(Interpreter* interpreter) {
Literal rhs = popLiteralArray(&interpreter->stack);
Literal lhs = popLiteralArray(&interpreter->stack);
parseIdentifierToValue(interpreter, &rhs);
parseIdentifierToValue(interpreter, &lhs);
if (IS_TRUTHY(lhs) && IS_TRUTHY(rhs)) {
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(true));
}
else {
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(false));
}
return true;
}
static bool execOr(Interpreter* interpreter) {
Literal rhs = popLiteralArray(&interpreter->stack);
Literal lhs = popLiteralArray(&interpreter->stack);
parseIdentifierToValue(interpreter, &rhs);
parseIdentifierToValue(interpreter, &lhs);
if (IS_TRUTHY(lhs) || IS_TRUTHY(rhs)) {
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(true));
}
else {
pushLiteralArray(&interpreter->stack, TO_BOOLEAN_LITERAL(false));
}
return true;
}
static bool execJump(Interpreter* interpreter) {
int target = (int)readShort(interpreter->bytecode, &interpreter->count);
@@ -806,6 +840,18 @@ static void execInterpreter(Interpreter* interpreter) {
}
break;
case OP_AND:
if (!execAnd(interpreter)) {
return;
}
break;
case OP_OR:
if (!execOr(interpreter)) {
return;
}
break;
case OP_JUMP:
if (!execJump(interpreter)) {
return;