PLC-lang / rusty

Structured Text Parser and LLVM Frontend
GNU Lesser General Public License v3.0
183 stars 48 forks source link

Improved Method/Function value return syntax #1094

Open TheColonel2688 opened 3 months ago

TheColonel2688 commented 3 months ago

The IEC 61131-3 Syntax for the returned Value from a Method or Function isn't very readable.

The syntax is *functionName* := *Value to Return*

I was actually trying to find the section of IEC 61131-3 that requires this syntax, but I can't seem to find it. I found code examples in the standard that use it, though. Could someone point me to it? I have a copy of the 3rd edition.

My proposal is an alternate syntax that is in line with almost all other modern languages. The system uses "return" as an alias over the function name. This ideal would be an optional syntax to maintain compatibility. return := *value to return*

FUNCTION mySumFunc : DINT
VAR_INPUT
    Blah_Blah1 : DINT;
    Blah_Blah2 : DINT;
VAR_END
return := Blah_Blah1 + Blah_Blah2;
END FUNCTION

This, I think, is much more readable and will also help with implementing Properties. (I will create another feature request for Properties)

Edit:

The new proposed syntax is the following with no := operator


VAR_INPUT
    Blah_Blah1 : DINT;
    Blah_Blah2 : DINT;
VAR_END
  RETURN Blah_Blah1 + Blah_Blah2; 
END FUNCTION```
ghaith commented 3 months ago

I was actually trying to find the section of IEC 61131-3 that requires this syntax, but I can't seem to find it. I found code examples in the standard that use it, though. Could someone point me to it? I have a copy of the 3rd edition.

7.3.3.2.3 ~I only have the German version at hand at the moment, they call it Result there (Ergebnis)~ I checked a draft of the English version and it's the same wording (Result)

I feel something like

FUNCTION mySumFunc : DINT
VAR_INPUT
    Blah_Blah1 : DINT;
    Blah_Blah2 : DINT;
VAR_END
return Blah_Blah1 + Blah_Blah2; (* or RETURN since the standard loves upper case :) *)
END FUNCTION

might even be better because

The problem is the standard treats this as a variable, so you can assign it before actually exiting. Which means no hope for #310 if we use the return := ... syntax

TheColonel2688 commented 3 months ago

I am fine with not treating return as a variable that would require := operator.

I just figured it would be easier to implement that way, but your explanation make sense.

riederm commented 3 months ago

is it fine to mix the two?

FUNCTION xxx : DINT
  VAR_INPUT
      a : DINT;
      b : DINT;
  VAR_END
  xxx := 0;
  IF a < 0 THEN
    RETURN a + b;
  END_IF
END FUNCTION
TheColonel2688 commented 3 months ago

Good question.

I would say it should be discouraged to mix them in the same function? As it might be confusing. I don't have a strong opinion it it though.

tisis2 commented 3 months ago

in my understanding i would just treat a RETURN 0; as a shorthand for xxx := 0; RETURN; that said... why shouldn't you mix it...

riederm commented 3 months ago

Good question.

I would say it should be discouraged to mix them in the same function? As it might be confusing. I don't have a strong opinion it it though.

i would also discourage it. :/

mhasel commented 3 months ago

I think the RETURN keyword would be a neat addition. If we implement it not as a strict alias to the FOO := ... variable assignment but rather as a keyword to exit from a function early, like it behaves in other languages, I'd argue that even mixing the FOO := ... and RETURN ... syntax in the same function would be fine.

riederm commented 3 months ago

I think mixing it, adds some potentials for confusion, but it is no show-stopper for me. I'd vote for a low-prio issue that adds a warning to return-statements if users mix the two approaches (the same way lots of compilers warn you, if you re-assign a parameter-variable).