-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Ternary operator #20
Comments
Here are some forms of ternary operators to think about
|
Throwing out some rough code for handling more complex expressions - maybe that is the answer as opposed to a ternary operator? <?php
use function Verraes\Parsica\Expression\compound;
// ...
compound(function ($previousPrecedenceLevel) {
return collect([
$token('if'),
$previousPrecedenceLevel,
$token('do:'),
$previousPrecedenceLevel,
$token('else:'),
$previousPrecedenceLevel,
])
->map(fn($v) => [$v[1], $v[3], $v[6]]);
})
->map(fn($test, $whenTrue, $whenFalse) => ... ); |
Thanks for this input. Something else to consider: So far the expression handler can deal with |
In math, a ternary operation has the type |
#21 is an experiment to gain some insights into how ternary expressions could work. |
In my case I didn't want to make a distinction between class Ternary implements ExpressionType {
public function buildPrecedenceLevel(Parser $previousPrecedenceLevel): Parser
{
$question = keepFirst(char("?"), skipHSpace());
$colon = keepFirst(char(":"), skipHSpace());
return choice(
map(
collect(
$previousPrecedenceLevel->thenIgnore($question),
$previousPrecedenceLevel->thenIgnore($colon),
$previousPrecedenceLevel,
),
function(array $v) {
$tern = new _Ternary;
$tern->test = $v[0];
$tern->whenTrue = $v[1];
$tern->whenFalse = $v[2];
return $tern;
}
),
$previousPrecedenceLevel
);
}
} |
Wanted to move the discussion from Twitter to here. I looked at the source to evaluate making a PR, and processed through the docs and was struggling with a clear path for multiple reasons.
Firstly, ternary is almost always
a ? b : c
, so do you implement with the expectation of those specific tokens? This is a minor question, and the answer is probably, no, don't expect specific tokens, the user provides those.Secondly, all the current operators work with one symbol, not two and you need two or more symbols so all the
Verraes\Parsica\Expression\*Assoc
classes would probably need to change? OR instead there would it be a newExpressionType
?The text was updated successfully, but these errors were encountered: