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

No Exception when left associative single operand operator is in wrong position #64

Open
fasseg opened this issue Apr 22, 2016 · 2 comments

Comments

@fasseg
Copy link
Owner

fasseg commented Apr 22, 2016

The following expression gets evaluated although the factorial is in a nonsensical position:

new ExpressionBuilder("3-!2")
    .operator(factorial)
    .build()
    .evaluate()

This should throw an exception

@dktcoding
Copy link

Given that operator association doesn't mean operator position, that expression is actually valid, maybe it's possible to add an "operator position" flag but it will actually create a fairly complex code for some really weird corner cases...

Question: Is there other "just unary" operator that uses postfix notation in an infix environment? (aside from factorial)

Currently unary operators like - and + don't fail because the tokenizer attempts to retrieve the binary operator, but if you comment lines 96, 97, 98 and 100 of Operators.class, an expression like 3- becomes -3.

Since I honestly can't think/find another operator that behaves like factorial, IMHO the simplest solution is to add ! as a builtin operator and special case it (for instance treat it like a binary operator, and insert an unused value as the second argument).

The only issue I can see (besides having an unused token) is that implicit multiplication operator won't be added (but actually fails now too).

This was referenced Dec 6, 2016
@fasseg
Copy link
Owner Author

fasseg commented Jan 30, 2017

Hmmm, I will have a think about that. I'm not in favour of adding it as a builtin operator and have some special handling for that, since it could happen with any user-defined unary operator.

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

2 participants