lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


if this is the logical or, the combined operator should be "or=" or "or ="; if this is the numerical operation it should be "|="....
so
  var = var or function1()
could become:
  var or = function1()

This should apply consistently to all binary operators.

And does not require any change to the lexical analyzer, inserting a whitespace or newline in "or =" would still be valid, parsed as two tokens.

* With the logical operation, the right-handside _expression_ (including function calls) may not be evaluated if the left-side operand (the assignable variable) is boolean-evaluated to be **true** (like for the binary operator), and so no reassignment would occur to the variable, otherwise it would evaluate the left-hand side _expression_ and assign its result to the variable (without coalescing it to a boolean).
* As well with "and=", the right-hand-side _expression_ may not be evaluated if the left-side operand is boolean- evaluated to be **false**, and so no reassignment would occur to the variable ; otherwise it would evaluate the left-hand side _expression_ and assign its result to the variable (without coalescing it to a boolean).
* In both cases, the boolean-evaluation of the left-hand side varaible would consist in getting its value: if it's false or nil (or NaN ?), it evaluates as false, otherwise any other value (including integer 0 or float 0.0) would evaluate to true.

The behavior in case of NaN is debatable but it should be the same as in the binary _expression_ using "and", "or", "not" operators. If
* "<NaN> or <anyvalue>" should have non-boolean value "<NaN>", and 
* "<NaN> and <anyvalue>" should both have non-boolean value "<NaN>",
then this means that the "or=" and "and=" bindop-assignments must implement the shortcut to not evaluate the right-handside if the variable is NaN, in order to preserve the NaN value in the lef-handside assignable variable.

So the rules should become precisely:
* With "<var> or= <_expression_>", the right-handside _expression_ may not be evaluated if the left-side operand (the assignable variable) is boolean-evaluated to be **true** or **NaN**, and so no reassignment would occur to the variable, otherwise it would evaluate the left-hand side _expression_ and assign its result to the variable (without coalescing it to a boolean).
* With "<var> and= <_expression_>", the right-hand-side _expression_ may not be evaluated if the left-side operand is boolean- evaluated to be **false** or **NaN**, and so no reassignment would occur to the variable ; otherwise it would evaluate the left-hand side _expression_ and assign its result to the variable (without coalescing it to a boolean).


Le dim. 2 févr. 2020 à 20:00, v <v19930312@gmail.com> a écrit :
On Sun, 2020-02-02 at 19:55 +0100, Peter Mueller wrote:
> Hello,
>
> I try to port some code from C to Lua.
>
> There is some code similar to this (simplified).
>
>   ...
>   unsigned char var=0;
>   var |= function1();
>   var |= function2();
>   return var
> }
>
> How would I implement the |= in Lua?
>
> Thanks,
> Peter
>

Is that a logical `or` (on bools) or numerical one (on numbers)?

First one would be `var = var or function1()`
Second one is `var = var | function1()` since Lua 5.3
or `var = bit32.bor(var, function1())` in Lua 5.2/LuaJIT.
--
v <v19930312@gmail.com>