Syntax/implementation question

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Syntax/implementation question

Martin
Hi all,

I'm experimenting with lua syntax and cannot understand why in
interpreter I'm getting such output

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> print(1), print(2), print(3)
1
2
3
nil nil

When running this code from standalone file output is

$ lua test_something.lua
lua: test_something.lua:1: syntax error near ','

which is what I awaiting.

Any hints?

Reply | Threaded
Open this post in threaded view
|

Re: Syntax/implementation question

Daurnimator
On 27 July 2016 at 19:20, Martin <[hidden email]> wrote:

> Hi all,
>
> I'm experimenting with lua syntax and cannot understand why in
> interpreter I'm getting such output
>
> Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
>> print(1), print(2), print(3)
> 1
> 2
> 3
> nil     nil
>
> When running this code from standalone file output is
>
> $ lua test_something.lua
> lua: test_something.lua:1: syntax error near ','
>
> which is what I awaiting.
>
> Any hints?
>

The 5.3 REPL attempts to prefix code with `return `
i.e. you are running:
`return print(1), print(2), print(3)`
The argument evaluation order is undefined (so you're lucky you see
1,2,3 in order).
The 'nil, nil' output is the return value of that expression: print
doesn't return anything, but when you have a subsequent return value
the 'nothing' is coerced to `nil`.

Reply | Threaded
Open this post in threaded view
|

Re: Syntax/implementation question

Nagaev Boris
On Wed, Jul 27, 2016 at 7:18 PM, Daurnimator <[hidden email]> wrote:

> On 27 July 2016 at 19:20, Martin <[hidden email]> wrote:
>> Hi all,
>>
>> I'm experimenting with lua syntax and cannot understand why in
>> interpreter I'm getting such output
>>
>> Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
>>> print(1), print(2), print(3)
>> 1
>> 2
>> 3
>> nil     nil
>>
>> When running this code from standalone file output is
>>
>> $ lua test_something.lua
>> lua: test_something.lua:1: syntax error near ','
>>
>> which is what I awaiting.
>>
>> Any hints?
>>
>
> The 5.3 REPL attempts to prefix code with `return `
> i.e. you are running:
> `return print(1), print(2), print(3)`
> The argument evaluation order is undefined (so you're lucky you see
> 1,2,3 in order).
> The 'nil, nil' output is the return value of that expression: print
> doesn't return anything, but when you have a subsequent return value
> the 'nothing' is coerced to `nil`.
>

Why does it print "nil, nil" and not "nil, nil, nil"?


--


Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|

Re: Syntax/implementation question

Philipp Janda
Am 27.07.2016 um 19:16 schröbte Nagaev Boris:

> On Wed, Jul 27, 2016 at 7:18 PM, Daurnimator <[hidden email]> wrote:
>> On 27 July 2016 at 19:20, Martin <[hidden email]> wrote:
>>>
>>> Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
>>>> print(1), print(2), print(3)
>>> 1
>>> 2
>>> 3
>>> nil     nil
>>>
>> The 'nil, nil' output is the return value of that expression: print
>> doesn't return anything, but when you have a subsequent return value
>> the 'nothing' is coerced to `nil`.
>>
>
> Why does it print "nil, nil" and not "nil, nil, nil"?

As Daurnimator already indicated, no return value is a special case of
multiple return values: In certain situations multiple (or no) values
are coerced into one value, in other situations, like at the end of an
expression list, they are taken as is. All `print()` calls return no
value, but only the last call is the last expression of an expression
list, so the other two calls are coerced to one `nil` value each.

Philipp




Reply | Threaded
Open this post in threaded view
|

Re: Syntax/implementation question

Peter Aronoff
Philipp Janda <[hidden email]> wrote:
> Am 27.07.2016 um 19:16 schröbte Nagaev Boris:
> > Why does it print "nil, nil" and not "nil, nil, nil"?
>
> As Daurnimator already indicated, no return value is a special case of
> multiple return values: In certain situations multiple (or no) values are
> coerced into one value, in other situations, like at the end of an
> expression list, they are taken as is. All `print()` calls return no
> value, but only the last call is the last expression of an expression
> list, so the other two calls are coerced to one `nil` value each.

I apologize if I’m being dense, but I still don’t follow. I’m going to try
to say what I understand. If anyone can correct or expand, I appreciate it.

1. print() calls return no value
2. In a list of expressions, multiple or no value returns are taken “as
   is”. (I don’t really see what “taken as is” means, to be frank.)
3. print(1), print(2), print(3) is an expression list.
4. So, (per (2)), the first two (only?) print() calls are taken “as is”.
   The two `nil`s come from that. I.e., they are coerced to display their
   not having a return value by displaying `nil`?

If all that is right, my remaining question: why isn’t the last call in
an expression list also taken “as is”. I.e., once again, why not `nil, nil,
nil`?

Thanks, Peter
--
We have not been faced with the need to satisfy someone else's
requirements, and for this freedom we are grateful.
    Dennis Ritchie and Ken Thompson, The UNIX Time-Sharing System

Reply | Threaded
Open this post in threaded view
|

Re: Syntax/implementation question

Coda Highland
On Wed, Jul 27, 2016 at 5:36 PM, Peter Aronoff <[hidden email]> wrote:

> Philipp Janda <[hidden email]> wrote:
>> Am 27.07.2016 um 19:16 schröbte Nagaev Boris:
>> > Why does it print "nil, nil" and not "nil, nil, nil"?
>>
>> As Daurnimator already indicated, no return value is a special case of
>> multiple return values: In certain situations multiple (or no) values are
>> coerced into one value, in other situations, like at the end of an
>> expression list, they are taken as is. All `print()` calls return no
>> value, but only the last call is the last expression of an expression
>> list, so the other two calls are coerced to one `nil` value each.
>
> I apologize if I’m being dense, but I still don’t follow. I’m going to try
> to say what I understand. If anyone can correct or expand, I appreciate it.
>
> 1. print() calls return no value
> 2. In a list of expressions, multiple or no value returns are taken “as
>    is”. (I don’t really see what “taken as is” means, to be frank.)
> 3. print(1), print(2), print(3) is an expression list.
> 4. So, (per (2)), the first two (only?) print() calls are taken “as is”.
>    The two `nil`s come from that. I.e., they are coerced to display their
>    not having a return value by displaying `nil`?
>
> If all that is right, my remaining question: why isn’t the last call in
> an expression list also taken “as is”. I.e., once again, why not `nil, nil,
> nil`?
>
> Thanks, Peter
> --
> We have not been faced with the need to satisfy someone else's
> requirements, and for this freedom we are grateful.
>     Dennis Ritchie and Ken Thompson, The UNIX Time-Sharing System

It's the other way around.

The first two AREN'T being taken as-is. They're being used in an
expression context instead of an argument list context, and in an
expression context an empty arg list is coerced to nil.

The last one is an argument list context, and therefore the rest of
the value list is replaced with the last print()'s return value list.

If it helps, consider this:

function test()
  return 1, 2, 3
end
print(test(), test(), test())

This outputs "1 1 1 2 3" for the same reason -- the first two calls
are coerced to a single value, but the last one is expanded as
multiple arguments.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Syntax/implementation question

Paul K-2
In reply to this post by Peter Aronoff
Hi Peter,

> 3. print(1), print(2), print(3) is an expression list.
> If all that is right, my remaining question: why isn’t the last call in
> an expression list also taken “as is”. I.e., once again, why not `nil, nil,
> nil`?

According to the docs [1] "return f1(), f2(), f3()" has f1 adjusted to
1 result (which returns `nil` instead of nothing), f2 also adjusted to
1 result, and f3 is adjusted to return all results (and there are
none), which produces `nil, nil` result you get.

Paul.

[1] https://www.lua.org/manual/5.3/manual.html#3.4