Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operators '&&' and '||' may not be acting correctly. #154

Open
Ratstail91 opened this issue Nov 17, 2024 · 1 comment
Open

Operators '&&' and '||' may not be acting correctly. #154

Ratstail91 opened this issue Nov 17, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Ratstail91
Copy link
Owner

Toy/source/toy_vm.c

Lines 336 to 347 in 7398898

if (opcode == TOY_OPCODE_AND) {
Toy_Value right = Toy_popStack(&vm->stack);
Toy_Value left = Toy_popStack(&vm->stack);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_BOOLEAN( Toy_checkValueIsTruthy(left) && Toy_checkValueIsTruthy(right) ));
}
else if (opcode == TOY_OPCODE_OR) {
Toy_Value right = Toy_popStack(&vm->stack);
Toy_Value left = Toy_popStack(&vm->stack);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_BOOLEAN( Toy_checkValueIsTruthy(left) || Toy_checkValueIsTruthy(right) ));
}

In most languages, the && and || operators have short-circuiting behavior. As it is, the current implementation doesn't do that.

I may need the JUMP keyword to skip over the rhs if the top value is true.

@Ratstail91 Ratstail91 added the bug Something isn't working label Nov 17, 2024
@Ratstail91
Copy link
Owner Author

Formal definition for &&:

Return the first falsy value, or the last value, skipping the evaluation of other operands.

Formal definition for ||:

Return the first truthy value, or the last value, skipping the evaluation of other operands.

This definition matches JavaScript's implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant