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

[bug] Or and And words don't make their jobs #23

Closed
Olyno opened this issue Jul 29, 2020 · 10 comments · Fixed by #83
Closed

[bug] Or and And words don't make their jobs #23

Olyno opened this issue Jul 29, 2020 · 10 comments · Fixed by #83
Labels
bug Reports a bug in the code. help wanted Other input is desired.

Comments

@Olyno
Copy link
Contributor

Olyno commented Jul 29, 2020

Description

Recently I saw when working on #22 that the and and or operators didn't work correctly sometimes.

Expected

Should be false: true
Should be true: true
Should be false: false
Should be false: false
Should be true: true
Should be true: true
Should be true: false
Should be false: false

Code:

on script load:
    set {_x} to true and false
    print "Should be false: %{_x}%"
    set {_x} to true and true
    print "Should be true: %{_x}%"
    set {_x} to false and false
    print "Should be false: %{_x}%"
    set {_x} to false and true
    print "Should be false: %{_x}%"
    set {_x} to true or false
    print "Should be true: %{_x}%"
    set {_x} to true or true
    print "Should be true: %{_x}%"
    set {_x} to false or true
    print "Should be true: %{_x}%"
    set {_x} to false or false
    print "Should be false: %{_x}%"

It's good to note using operators as && instead of and it works perfectly. No errors, simply wrong results.

@APickledWalrus
Copy link
Contributor

Trying to set a single variable to multiple values shouldnt error, not just select one of them, no?

@Olyno
Copy link
Contributor Author

Olyno commented Jul 30, 2020

It should select one of them in fact (see the getValues method of the expression)

@Syst3ms
Copy link
Contributor

Syst3ms commented Jul 30, 2020

This issue is due to the %boolean% or %boolean% syntax conflicting with literal lists of the form object 1 or object 2. I have some ideas about how to fix this, such as adjusting the syntax for the literal list case, but it might prove to be unwieldy and unnecessarily complicated.
I'm also reluctant to adding beginner-unfriendly shorthands, as I mentioned in #20. However if absolutely necessary, I might resort to that option.

@Syst3ms Syst3ms added bug Reports a bug in the code. help wanted Other input is desired. labels Jul 30, 2020
@Mwexim
Copy link
Owner

Mwexim commented Aug 15, 2020

Why not give priority to single value expressions if one is asked?

For example:

{a} = true and false # Results in false because it prioritises the condition operator expression because it is single
{b::*} = true and false # Results in the list 'true and false' because it's not single

Then if the user wants to set a list to a single value using the condition operators, it can just use the workaround of setting it to a single temporary variable first and then using that variable to set the list variable.

Does that make sense?

@Syst3ms
Copy link
Contributor

Syst3ms commented Aug 15, 2020

That could work fine for variables, but for other changeable expressions less so. Imagine a singular expression that allows being changed using multiple values : which one should be picked in such a case?
Regardless, this solution isn't the most elegant in my eyes.

@Mwexim
Copy link
Owner

Mwexim commented Aug 16, 2020

That could work fine for variables, but for other changeable expressions less so. Imagine a singular expression that allows being changed using multiple values : which one should be picked in such a case?
Regardless, this solution isn't the most elegant in my eyes.

Just looked at the change(...) code. What if we make the method signature change(Duration[] toChange, Expression<Object> changeWith, ChangeMode mode) (if that is even possible)?
If we do that, it should be possible for the author to check if the Expression is single or plural, shouldn't it?

Either way, I just looked at some of the code and found some redundancy that may have to do with this issue:

    @Override
    public Class<?>[] acceptsChange(ChangeMode mode) {
        if (!list && mode == ChangeMode.SET)
            return new Class[]{Object[].class};
        return new Class[]{Object[].class};
    }

(Variable.java)
This is pretty redundant if you ask me :)

@Syst3ms
Copy link
Contributor

Syst3ms commented Aug 16, 2020

If we do that, it should be possible for the author to check if the Expression is single or plural, shouldn't it?

Well, no, since that singular/plural distinction has to be made much earlier at parse time (in fact it has to be made at matching time), and acceptsChange isn't called until much later in the process.

@Olyno
Copy link
Contributor Author

Olyno commented Sep 3, 2020

I guess what I found is related to this issue:

When creating the pull request #60, the getValues method gave me only the first string (expression) it find. For example:

Expected:

set {_upper::*} to all upper case chars in "Hello World" and "Hello Parser"
print "Upper: %{_upper::*}% # Upper: "H", "W", "H" and "P"

Got:

set {_upper::*} to all upper case chars in "Hello World" and "Hello Parser"
print "Upper: %{_upper::*}% # Upper: "H", "W" and "Hello Parser"

@Syst3ms
Copy link
Contributor

Syst3ms commented Sep 4, 2020

That's because list literals come before expressions in the parsing order. Use parentheses to specify the correct interpretation.

@Mwexim
Copy link
Owner

Mwexim commented Oct 14, 2020

That's because list literals come before expressions in the parsing order. Use parentheses to specify the correct interpretation.

Maybe force-parsing (as I will call it for now) for ExprBooleanOperators could fix the issue?
What I mean by force-parsing is something that was present earlier for inline conditions: there is a separate section that parses boolean operators first, and then the other expressions follow (in the SyntaxParser class).

Mwexim pushed a commit that referenced this issue Oct 18, 2020
Mwexim added a commit that referenced this issue Dec 29, 2020
* fix: boolean operator fix (#23)

* suggestion

Co-authored-by: Giovanni <42092549+Matocolotoe@users.noreply.github.com>

* improve: small code improvement

* request: add requests

* request: add request

* improve: small parsing optimization

Co-authored-by: CHarcoalToast <CharcoalToast@users.noreply.github.com>
Co-authored-by: Giovanni <42092549+Matocolotoe@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Reports a bug in the code. help wanted Other input is desired.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants