[bug?] = doesn't work properly

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

[bug?] = doesn't work properly

Soni "They/Them" L.
$ lua
Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
 > =1
1
 >  =1
stdin:1: unexpected symbol near '='

The fix is to allow an = to be used as `return` in the parser, if it's
the first statement in a function. This'll be a huge improvement for Lua
data files:

= {
   nick = "Soni|Bot",
   network = "irc.freenode.net",
   port = 6667,
}

while also fixing the bug in the interpreter.

Side effects include `local function swap(a,b) = b,a end`,
`s:gsub("[A-Z]", function(k) = tostring(k:byte()) end)`, etc. But I
think those are nice.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: [bug?] = doesn't work properly

Egor Skriptunoff-2
On Sun, Apr 30, 2017 at 7:04 PM, Soni L. <[hidden email]> wrote:
$ lua
Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
> =1
1
>  =1
stdin:1: unexpected symbol near '='


Yes, it looks like a bug.



Reply | Threaded
Open this post in threaded view
|

Re: [bug?] = doesn't work properly

Martin
In reply to this post by Soni "They/Them" L.
On 04/30/2017 09:04 AM, Soni L. wrote:

> The fix is to allow an = to be used as `return` in the parser, if it's
> the first statement in a function. This'll be a huge improvement for Lua
> data files:
>
> = {
>   nick = "Soni|Bot",
>   network = "irc.freenode.net",
>   port = 6667,
> }
>
> while also fixing the bug in the interpreter.
>
> Side effects include `local function swap(a,b) = b,a end`,
> `s:gsub("[A-Z]", function(k) = tostring(k:byte()) end)`, etc. But I
> think those are nice.

Code will become more obscure from this change. I prefer stable habits
like "use `return` anytime you wish to leave current scope and return
values". But this may be done, yes.

--

I'd wish another feature: treat statements as expressions.

(Because sometimes I'm tired from building logic bottom-up when I see a
way to describe it top-down.)

Then some code like this will become valid:

  local result =
    do
      local s = 'some text'
      return s
    end .. '!'

  print(result)

Today you can implement this behavior as anonymous function call:

  local result =
    (
      function()
        local s = 'some text'
        return s
      end
    )() .. '!'

  print(result)

which looks less nice.

--

> $ lua
> Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
>> =1
> 1
>>  =1
> stdin:1: unexpected symbol near '='

It's strange for me why "=1" is valid in interpreter. I consider this as
a glitch and tend to ignore this feature.

-- Martin

Reply | Threaded
Open this post in threaded view
|

Re: [bug?] = doesn't work properly

Dirk Laurie-2
In reply to this post by Egor Skriptunoff-2
2017-04-30 20:33 GMT+02:00 Egor Skriptunoff <[hidden email]>:

> On Sun, Apr 30, 2017 at 7:04 PM, Soni L. <[hidden email]> wrote:
>>
>> $ lua
>> Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
>> > =1
>> 1
>> >  =1
>> stdin:1: unexpected symbol near '='
>>
>
> Yes, it looks like a bug.

"Please stop calling "bug" something that does not behave as you wanted
or imagined."  — Roberto Ierusalimschy [1]

"After reading a line, Lua first try to interpret the line as an expression.
If it succeeds, it prints its value. Otherwise, it interprets the line as
a statement. If you write an incomplete statement, the interpreter waits
for its completion by issuing a different prompt." — Lua User's Manual

[1] http://lua-users.org/lists/lua-l/2013-04/msg00825.html

Reply | Threaded
Open this post in threaded view
|

Re: [bug?] = doesn't work properly

Tony Papadimitriou
In reply to this post by Egor Skriptunoff-2
I think this is because in lua.c, pushline() checks just the first character for = disregarding possible whitespace.
This simple patch works for me (but there could be a simpler patch).
 
--- lua.c
+++ lua.c
@@ -314,12 +314,14 @@
     return 0;  /* no input (prompt will be popped by caller) */
   lua_pop(L, 1);  /* remove prompt */
   l = strlen(b);
   if (l > 0 && b[l-1] == '\n')  /* line ends with newline? */
     b[--l] = '\0';  /* remove it */
-  if (firstline && b[0] == '=')  /* for compatibility with 5.2, ... */
-    lua_pushfstring(L, "return %s", b + 1);  /* change '=' to 'return' */
+  int i = 0;
+  while (b[i] == ' ' || b[i] == '\t') i++;
+  if (firstline && b[i] == '=')  /* for compatibility with 5.2, ... */
+    lua_pushfstring(L, "return %s", b + i + 1);  /* change '=' to 'return' */
   else
     lua_pushlstring(L, b, l);
   lua_freeline(L, b);
   return 1;
}
 
 
Sent: Sunday, April 30, 2017 9:33 PM
Subject: Re: [bug?] = doesn't work properly
 
On Sun, Apr 30, 2017 at 7:04 PM, Soni L. <[hidden email]> wrote:
$ lua
Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
> =1
1
>  =1
stdin:1: unexpected symbol near '='

 
Yes, it looks like a bug.

 
 
Reply | Threaded
Open this post in threaded view
|

Re: [bug?] = doesn't work properly

Daurnimator
On 1 May 2017 at 06:35, Tony Papadimitriou <[hidden email]> wrote:
> -  if (firstline && b[0] == '=')  /* for compatibility with 5.2, ... */
> -    lua_pushfstring(L, "return %s", b + 1);  /* change '=' to 'return' */

As you might have noticed: using '=' at the start of a line is
deprecated in the 5.3 REPL.