[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Recipe: setfenv with Lua 5.2.0 work3
- From: David Manura <dm.lua@...>
- Date: Tue, 15 Jun 2010 03:06:39 -0400
On Tue, Jun 15, 2010 at 1:15 AM, Juris Kalnins <juris@mt.lv> wrote:
>>On Tue, 15 Jun 2010 06:31:29 +0300, David Manura wrote:
>> A tangential point on DSLs: environments or not, it often feels to me
>> that fitting DSL's into unmodified Lua syntax in clunky. I think it's
>> because that unlike XML or s-expressions, Lua has a particular
>> evaluation order (not pure data), can only be combined in certain ways
>> (e.g. expressions v.s. statements), and omits order and multiplicity
>> of key-value pair lists (tables), so the DSL must be fit into this "Lua" model.
>
> Actually, XML trees map to Lua table constructors very nicely:
>
> <n1 a1="1" a2="3"><n2/><n2 a3="3">lala</n2></n1>
>
> becomes
>
> n1 { a1=1, a2=3, n2 {}, n2 { a3=3, "lala" } }
>
> Provided that attribute names are not integer numbers. This is
> currently my main use of environment changing.
True, but try adding conditionals, loops, and local variables in the
middle of that, like XSLT:
<table><xsl:variable name="x" select=" 'test' " /><tr>
<xsl:for-each select="1 to 3">
<xsl:variable name="index" select="."/>
<td><xsl:value-of select="$index"/><xsl:value-of select="$x"/></td>
</xsl:for-each>
</tr></table>
which in a Lisp-like / AST form is
(table (let x 'test') (tr (fornum i 1 3 (td i x))))
Sure, you can rewrite the analogous form in Lua:
table { let {'x', 'test'}, tr { fornum { 'i', 1, 3, { td { var 'i',
var 'x' } }} }}
but given that the Lua form is not as simple as the Lisp, why do that
unless you can get something out of the use of Lua like this?
table { local x = 'test'; tr { for i=1,3 do td { i, x } end } }
but statements inside expressions are not allowed unless we do this:
table {
function()
local x = 'test'
tr {
function()
for i=1,3 do td { index } end
end
}
end
}
Maybe that works, but our ability to manipulate that structure is more
limited because it is not pure data (unless we write our own parser
like Metalua that treats it as such). We can define functions
appropriately and eval, which expands the control structures for us
with no way to preserve them.