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

Wrong answer for the challenge #3 in chapter 17 #1189

Open
spevnev opened this issue Feb 20, 2025 · 0 comments
Open

Wrong answer for the challenge #3 in chapter 17 #1189

spevnev opened this issue Feb 20, 2025 · 0 comments

Comments

@spevnev
Copy link

spevnev commented Feb 20, 2025

Your solution for conditional (ternary) operator is:

static void conditional()
{
  // Compile the then branch.
  parsePrecedence(compiler, PREC_CONDITIONAL);

  consume(compiler, TOKEN_COLON,
          "Expect ':' after then branch of conditional operator.");

  // Compile the else branch.
  parsePrecedence(compiler, PREC_ASSIGNMENT);
}

I believe it is wrong because (in C) assignment in conditional operator is allowed in the then branch but not the else branch, so the precedence of the statements should be swapped:

static void conditional()
{
  // Compile the then branch.
  parsePrecedence(compiler, PREC_ASSIGNMENT);

  consume(compiler, TOKEN_COLON,
          "Expect ':' after then branch of conditional operator.");

  // Compile the else branch.
  parsePrecedence(compiler, PREC_CONDITIONAL);
}

And since the else branch allows any expression, it may be more clear to use expression (as in grouping)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant