lua-users home
lua-l archive

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


This is looking weird in deed:

Lua 5.1.3  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> =((1 or false) and 2) or false
2
> =((1 or false) and true) or false
1
> =((1 or false or 3) and 2 and true) or false
1

I always thought that the last evaluated value would be returned that
would make the whole condition becoming true, instead... I have no
idea which value is going to be returned in a more complex logical
operation.

>From the manual:

"The negation operator not always returns false or true. The
conjunction operator and returns its first argument if this value is
false or nil; otherwise, and returns its second argument. The
disjunction operator or returns its first argument if this value is
different from nil and false; otherwise, or returns its second
argument. Both and and or use short-cut evaluation; that is, the
second operand is evaluated only if necessary. Here are some examples:
"

The examples in the manual are much simpler and don't cover the given situation.

I am not sure, but I can't find there a clue about the value to be
returned that would match the examples above. Maybe it needs to be
extended there?

Eike


2009/4/16 Kelley, Brian <bkelley@qualcomm.com>:
> I was surprised to notice the following:
>
>   Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
>   > =((1 or false) and true) or false
>   1
>
> Same results on 5.1.2 and 5.1.3, MacOS and Windows.
>
> For what it's worth, I observed in lcode.c that the OPR_AND expression inherited the 'k' (kind) of VTRUE from its rhs subexpression, which seems wrong and seems to throw off the subsequent call to luaK_goiffalse().
>
> -bhk
>
>