lua-users home
lua-l archive

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


2. Expression trees

If one wants to get really compact, then we could actually allow function construction at compile time at least for single argument functions. For example, $ isn't currently used as a token, so consider:

	$ would be defined as function( x ) return x end
	$ * $ would be defined as function( x ) return x * x end
	$ + 1 would be defined as function( x ) return x + 1 end
	etc

Nice idea, but Super hard to specify correctly. What is g(x + f($ * $ + 1)):

g(x + f(function (_) return _ end + function (_) return _ end))
g(x + f(function (_) return _ * _ end + 1))
g(x + f(function (_) return _ * _ + 1 end))
g(x + function (_) return f(_ * _ + 1) end)
g(function (_) return x + f(_ * _ + 1) end)

?
Plus, you have to make parser backtrack arbitrarily far upon encountering '$'.
Currently it pretty much can emit bytecode as it goes over expression.

There was a nice proposal in another message,
Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
And one possible syntax could be
	function (x,y,z)=x+y*z

function (x,y,z = x+y*z), which doesn't have issues with finding expression end would be even nicer.

What would be nice to have, instead of arbitrary set of syntax extensions,
is some hooks into parser, that would allow to implement things like these,
or like the current shorthand notations as external C library.