Open
Description
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
)?
Metadata
Metadata
Assignees
Labels
No labels