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

Another Bug with nested ternaries #158

Open
JohnWulff opened this issue Apr 12, 2022 · 2 comments
Open

Another Bug with nested ternaries #158

JohnWulff opened this issue Apr 12, 2022 · 2 comments

Comments

@JohnWulff
Copy link

  1. The following nested ternary expression fails to evaluate:
    (44 != 0 ? 44 : 22 != 0 ? 22 : 11) != 0
    the reported error was: eval error in boolean expression

  2. A very similar ternary expression evaluates OK:
    (44 < 33 ? 44 : 22 < 11 ? 22 : 11) != 0
    I presume the difference is in the use of reflect.DeepEqual for the != operator and not for the < operator.

  3. Another similar nested ternary expression to case 1. is also OK, despite the fact that it uses the != operator:
    (44 != 0 ? 33 != 0 ? 33 : 22 : 11) != 0

These expressions are generated by users of the language translator I am developing. Expression 1. works Ok in C.

I am very happy I found the govaluate library - it does a great job for the language translator - Thank you.

@JohnWulff
Copy link
Author

Addendum:
The error message reported by govaluate for case 1.
(44 != 0 ? 44 : 22 != 0 ? 22 : 11) != 0
was "Value '44' cannot be used with the ternary operator '?', it is not a bool"
The error message I reported first was my own error message, to which I have since added the message passed down by govaluate.

@JohnWulff
Copy link
Author

JohnWulff commented Apr 13, 2022

Solution and apology:
I have found what the problem is with the reported expression. If modified slightly the error becomes obvious:
(44 != 0 ? 11 : 22 != 0 ? 22 : 11) != 0
This produces the error message "Value '11' cannot be used with the ternary operator '?', it is not a bool"
This means the integer result of the first ternary expression was used as a bool for the second ternary expression, which is incorrect by the govaluate rule for the ternary operator ?.

A workaround is to put nested ternary expression in parentheses, which then works:
((44 != 0 ? 11 : 22) != 0 ? 22 : 11) != 0

Comment on the implementation of ternary expressions in govaluate
Since ternary expressions are not part of the GO language, it seems inconsistent to insist on a bool variable as the first variable in a ternary expression. Most other languages, in particular C and Perl, automatically convert an int to bool when an int is used in a boolean context. The C pre-processor, whose conditional translation capabilities I am emulating also does this.

The question is:
Is govaluate meant to only evaluate GO language constant expressions or is it meant to evaluate more general expressions common to all popular programming languages? If the latter is the case, which seems to be the case since ternary expressions are included, I would advocate automatic conversion of int to bool in all boolean contexts - in front of ? as well as around && and ||.

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