#3 Unary operators are not working

1.0
open
nobody
None
4 days ago
2020-06-30
Ingo
No

CODESYS doesn't like the brackets around unary operators when they are not used within another expression.
So, while this works:

i = ++i;
i := (i:=i+1);

This doesn't:

++i;
(i:=i+1);

It's even worse for i++, as I was using a trick there to decrement the resulting value after the expression, which is sadly not possible.

Full example:

int i;
int a()
{
    i = ++i;
    i++;
    ++i;
}

Discussion

  • i-campbell

    i-campbell - 4 days ago

    if i is a DINT you can use __XADD(ADR(i) , 1) for i++

     

    Last edit: i-campbell 4 days ago
    • Ingo

      Ingo - 4 days ago

      Nice idea! Actually I already thought of inventing a runtime library
      (emscripting is doing it similar). Then I could pass the variable by
      reference, to increment it, and return the old value as a result.

      But would this work with an Any-Type? Because in the place where I get the
      operator I don't know the datatype. Not sure, yet.

      BR,
      Ingo

       

Log in to post a comment.