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

Ternary expression generated wrong #40

Closed
tomforwood opened this issue Sep 30, 2021 · 2 comments
Closed

Ternary expression generated wrong #40

tomforwood opened this issue Sep 30, 2021 · 2 comments

Comments

@tomforwood
Copy link

ore1 = @lead
ore2 = @coal
container = container1
    o1 = container.ore1
    o2 = container.ore2
    ore = o1>o2? ore2 : ore1

generated

sensor o1 container @ore1
sensor o2 container @ore2
jump 16 notEqual o2 true
set __tmp9 ore2
jump 17 always 0 0
set __tmp9 ore1
op greaterThan ore o1 __tmp9

It should be something like

sensor o1 container @ore1
sensor o2 container @ore2
jump 16 lessThanEq o1 o2
set ore ore2
jump 17 always 0 0
set ore ore1
@tomforwood
Copy link
Author

Workaround : use if style syntax
There appears to be a second bug, ore1 and ore2 have been made into constants, @Ore1 and @ore2 when they should be variables

@cardillan
Copy link
Owner

cardillan commented Jan 6, 2023

You can use the dot syntax (container.resource) only if the resource denotes an existing ore, and you omit the leading @: container.copper.

If you want to obtain the level of item specified by a variable, you need to call sensor function: container.sensor(ore)

Also, if you compile just the code snippet you've posted, lots of instructions get lost to dead code elimination. We can mark the final output as 'not dead' by printing it, for example.

The last problem is that the ternary operator takes precedence over the greater-than comparison.

Your example, when modified with respect to the above rules, looks like this:

ore1 = @lead
ore2 = @coal
container = container1
o1 = container.sensor(ore1)
o2 = container.sensor(ore2)
ore = (o1 > o2) ? ore2 : ore1
print(ore)

produces

set ore1 @lead
set ore2 @coal
set container container1
sensor o1 container ore1
sensor o2 container ore2
jump 8 lessThanEq o1 o2
set __tmp3 ore2
jump 9 always 0 0
set __tmp3 ore1
set ore __tmp3
print ore
end

I don't think there's anything wrong about it as of now.

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