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

order of operations "-" vs "[..]" (was: "Cannot negate ListValue") #547

Closed
akrasuski1 opened this issue Feb 5, 2015 · 3 comments
Closed
Labels
kerboscript For changes to the script language itself.

Comments

@akrasuski1
Copy link

The following code:

SET test TO list().
test:add(1).
PRINT -test[0].

Should probably print -1. Instead, I get "Cannot negate ListValue".
In the beginning I thought it is because kOS doesn't know what is the type of the object in the list (string couldn't be negated surely), but the very same code works if I switch from negating to multiplying by -1.

@Dunbaratu
Copy link
Member

This could merely be a simple matter of the order operations between the '[ ]' operator and the '-' operator.

Does this work?
PRINT - ( test[0] ).

It sounds like it's trying to do this: (-test) first and then do the [ operator.

The unary negation operator is a pain, when it comes to order of operations because it has different levels of precedence depending on context.

You want it to be very early when doing this:
-x + y
So it does that as (-x) + y instead of as -(x+y).

But you don't want it to be that early when doing other operations like [].

@akrasuski1
Copy link
Author

Yes, you were right. Taking test[0] into brackets solves the issue. Perhaps the [] should have even bigger precedence? I don't think there's a situation when we want it the other way around: (-x)[15]. Either way, thanks for making it clear.

@erendrake erendrake added the kerboscript For changes to the script language itself. label Feb 21, 2015
@Dunbaratu Dunbaratu changed the title "Cannot negate ListValue" order of operations "-" vs "[..]"Cannot negate ListValue" Feb 21, 2015
@Dunbaratu Dunbaratu changed the title order of operations "-" vs "[..]"Cannot negate ListValue" order of operations "-" vs "[..]" (was: "Cannot negate ListValue") Feb 21, 2015
@Dunbaratu
Copy link
Member

Changed the title so it is clearer in the list what the actual problem is that needs working on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kerboscript For changes to the script language itself.
Projects
None yet
Development

No branches or pull requests

3 participants