Makes funcname in Lua same as var? (lua syntax)

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

Makes funcname in Lua same as var? (lua syntax)

Xavier Wang
this is the complete syntax of lua:


chunk ::= {stat [`;´]} [laststat [`;´]]

block ::= chunk

stat ::=  varlist `=´ explist | 
functioncall | 
do block end | 
while exp do block end | 
repeat block until exp | 
if exp then block {elseif exp then block} [else block] end | 
for Name `=´ exp `,´ exp [`,´ exp] do block end | 
for namelist in explist do block end | 
function funcname funcbody | 
local function Name funcbody | 
local namelist [`=´ explist] 

laststat ::= return [explist] | break

funcname ::= Name {`.´ Name} [`:´ Name]

varlist ::= var {`,´ var}

var ::=  Name | prefixexp `[´ exp `]´ | prefixexp `.´ Name 

namelist ::= Name {`,´ Name}

some Personal view:

function foo() ... end is the semantics sugar of foo = function() ... end, but one can write
local t = {}
t[1] = function() end

but can not write:
local t = {}
function t[1]() end

so maybe:
funcname = var [`:' Name]

Reply | Threaded
Open this post in threaded view
|

Re: Makes funcname in Lua same as var? (lua syntax)

David Manura
On Thu, Feb 10, 2011 at 10:31 PM, Xavier Wang <[hidden email]> wrote:
> but can not write:
> local t = {}
> function t[1]() end

The closest may be

  local t = {}
  setmetatable(t, {__newindex = function(t,k,v)
    local i = k:match'^_(%d+)$'
    rawset(t, i and tonumber(i) or k, v)
  end})

  function t._1() print '1' end
  t[1]()

> so maybe:
> funcname = var [`:' Name]

var there can lead to some confusion:

  function f(x)[1](y) end

because parenthesis are used in multiple ways in Lua (as well as in
math): expression grouping, function calls, and function argument
lists.

Reply | Threaded
Open this post in threaded view
|

Re: Makes funcname in Lua same as var? (lua syntax)

Xavier Wang


2011/2/11 David Manura <[hidden email]>
On Thu, Feb 10, 2011 at 10:31 PM, Xavier Wang <[hidden email]> wrote:
> but can not write:
> local t = {}
> function t[1]() end

The closest may be

 local t = {}
 setmetatable(t, {__newindex = function(t,k,v)
   local i = k:match'^_(%d+)$'
   rawset(t, i and tonumber(i) or k, v)
 end})

 function t._1() print '1' end
 t[1]()

> so maybe:
> funcname = var [`:' Name]

var there can lead to some confusion:

 function f(x)[1](y) end

because parenthesis are used in multiple ways in Lua (as well as in
math): expression grouping, function calls, and function argument
lists.

is this confusion?
function foo() end is the semantics sugar of foo = function() end
so function f(x)[1](y) end is the semantics sugar of f(x)[1] = function(y) end

if everyone knows this semantics sugar, I don't think this confusion. e.g. Lua can write f{} for function call, so do you think this confusion?

Form() {
    x = 10,
    y = 10,

    Button() {
        text = "OK"
    }
}

but this is valid Lua code. if you know the truth "lua can call function with table directly", this will not confusion you.
Reply | Threaded
Open this post in threaded view
|

Re: Makes funcname in Lua same as var? (lua syntax)

Roberto Ierusalimschy
> > var there can lead to some confusion:
> >
> >  function f(x)[1](y) end
> >
> > because parenthesis are used in multiple ways in Lua (as well as in
> > math): expression grouping, function calls, and function argument
> > lists.
> >
> > is this confusion?
> function foo() end is the semantics sugar of foo = function() end
> so function f(x)[1](y) end is the semantics sugar of f(x)[1] = function(y)
> end

It is confusing for the parser, which will need unlimited look-ahead.
(I think it is confusing for people, too, but I will not argue that.)
Consider:

  function f(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s).x(a) return a end
  function f(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s) return a end

The parser needs to read some 40 tokens before knowning whether the
first 'f' is the name of the new function or a call.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Makes funcname in Lua same as var? (lua syntax)

Xavier Wang


2011/2/11 Roberto Ierusalimschy <[hidden email]>
> > var there can lead to some confusion:
> >
> >  function f(x)[1](y) end
> >
> > because parenthesis are used in multiple ways in Lua (as well as in
> > math): expression grouping, function calls, and function argument
> > lists.
> >
> > is this confusion?
> function foo() end is the semantics sugar of foo = function() end
> so function f(x)[1](y) end is the semantics sugar of f(x)[1] = function(y)
> end

It is confusing for the parser, which will need unlimited look-ahead.
(I think it is confusing for people, too, but I will not argue that.)
Consider:

 function f(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s).x(a) return a end
 function f(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s) return a end

The parser needs to read some 40 tokens before knowning whether the
first 'f' is the name of the new function or a call.

-- Roberto

this is really a problem. Yes you right....