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

Stepper: parenthesis handling #1744

Open
CATISNOTSODIUM opened this issue Mar 3, 2025 · 2 comments
Open

Stepper: parenthesis handling #1744

CATISNOTSODIUM opened this issue Mar 3, 2025 · 2 comments
Labels
Bug Something isn't working

Comments

@CATISNOTSODIUM
Copy link
Member

Considering the expression (1 + 2) * (3 + 4).

Image

While it is correct that 1 + 2 has to be reduced first, the way the stepper visualizes this contraction by removing parentheses can create confusion to some students as shown in the picture (like why wouldn't 2 * 3 be reduced first due to PEMDAS rule).

On the other hand, the stepper tries to add unnecessary parentheses (as shown in the video).

Source.Academy.Mozilla.Firefox.2025-03-02.21-11-08.mp4

There might be an issue with parentheses insertion. Hopefully, this will be resolved in a future version of the stepper.

Because the sourceRunner uses the Acorn parser without Parenthesized Expressions to prevent unnecessary parentheses, the current stepper must convert the parsed AST to a string and add parentheses where needed. Readding parenthesized AST might help, but there should be a better solution.

@martin-henz
Copy link
Member

Yes, there definitely should be a better solution. Basically when unparsing an AST, you need to check if you have a binary operator expression b2 as an operand of another binary operator expression b1. In that case you need to check the position of b2 in b1 and the operators. You need to place parentheses around b2, if the operator of b2 has lower precedence than the operator of b1. If the operators have the same precedence, you may or may not need parentheses, depending on the position of b2 in b1 and the operator associativity.

@martin-henz
Copy link
Member

Similar if you have a binary operator expression b2 as an operand of a unary operator expression b1. (In that case, I think you always need to place parentheses, because unary operators have higher precedence than binary operators.)

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
Development

No branches or pull requests

2 participants