lua-users home
lua-l archive

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


Hi,

I would like to have a factory function node(t) which returns an object (a table is fine). I would like these to be able to nest. If node just returns t, this works:

A = node { }
B = node { node {} }

In this case, A is a node with no children, and B is a node with one child.

Now, I want to be able to make these in modules. Supposing I expect to find a tree root in mod.tree, then this is fine:

----- BEGIN MODULE -----
return {
    tree = node { node {}, node { node {} } }
}
----- END MODULE -----

And if I configure a table and environment (like the module function), I can omit the table creation:

----- BEGIN MODULE -----
tree = node { node {}, node { node {} } }
----- END MODULE -----

So far so good, that is semantically what I want. But I'd like to make the syntax simpler, something like:

----- BEGIN MODULE -----
node {}
node { node {} }
----- END MODULE -----

where the root level mod.tree node is implicitly made, and the top level nodes in the module implicitly add themselves as children to the root level node.

In other words, previous two modules produce the same result, just differ in syntax.

This means, in the above syntax, that the two top level nodes have to add themselves to the implicit tree root node, but the child node must not do that.

So, any suggestion as to techniques to accomplish something like this?

Thanks,
Marc