exca / Stationeers-IC10-Automation

Some automation projects for Stationeers, coded using IC10, including a *Basic to IC10 Compiler*.
Other
25 stars 1 forks source link

Logical && operators having issues #4

Open altmank opened 6 months ago

altmank commented 6 months ago

If instead of declaring all of the conditions as variables, I inline them into the IF statements, this results in broken IC code that incorrectly uses 'and' keyword.

ALIAS AirTank Pin0
ALIAS AirVent Pin1
ALIAS GasSensor Pin2

CONST MaxTankPressure = 50MPa
CONST MaxTankTemperature = 135C

yield()

VAR outsideTempCool = GasSensor.Temperature < 128C
VAR tankBelowMaxPressure = AirTank.Pressure < MaxTankPressure
VAR outsideTempHot = GasSensor.Temperature >= 135C
VAR tankAboveMaxTemp = AirTank.Temperature > MaxTankTemperature 
VAR tankAboveMinPressure = AirTank.Pressure > 1MPa

IF outsideTempCool && tankBelowMaxPressure THEN 
    AirVent.Mode = 1
    AirVent.On = 1
ELSEIF outsideTempHot && tankAboveMaxTemp && tankAboveMinPressure THEN 
    AirVent.Mode = 0
    AirVent.On = 1
ELSE
    AirVent.On = 0
ENDIF
exca commented 6 months ago

Hey @altmank! Welcome here!

Could you explain better what you mean by "broken IC code that incorrectly uses 'and' keyword"?

If I consider line 17:

IF outsideTempCool && tankBelowMaxPressure THEN

This line is compiled into:

and r0 r3 r1 # r3 is outsideTempCool, and r1 is tankBelowMaxPressure
beqz r0 22 # if the result is false, it jumps directly to line 22, which is the ELSEIF line

The and is correct here, and you want both conditions to be set to true.

I'm always using this kind of logic in my scripts, and never had any issues with it. Could you please provide more info so we can debug it better?

What are you observing while running the code? Which part of the logic is not running or not executed the way you wanted it to be?

If you want, you can join us on Discord, so I can answer faster there. https://discord.gg/2rnpHTWVyV

Thanks!

maschine34675 commented 6 months ago

I think he accidentally posted the working code, not the one where he tried to use inline statements. I had problems with that too until I realized that you have to put the statements in brackets when using && or ||.

IF GasSensor.Temperature < 128C && AirTank.Pressure < 1MPa THEN would compare 128C with AirTank.Pressure

l r2 d2 Temperature
l r0 d0 Pressure
and r1 401.15 r0
slt r0 r1 1000
bge r2 r0 14

IF (GasSensor.Temperature < 128C) && (AirTank.Pressure < 1MPa) THEN works as intended

l r0 d2 Temperature
slt r2 r0 401.15
l r0 d0 Pressure
slt r1 r0 1000
and r0 r2 r1
beqz r0 15
exca commented 6 months ago

Hey @maschine34675 ! Thanks for the help with this bug report.

You're right, the problem is that, currently, I can't set different operator priorities between:

Only the Multiplicative operators (* and /) have a separate priority level.

For now, the workaround is to use parenthesis, but I will work on an improvement to allow more priority levels in the compiler.

Cheers!