lua-users home
lua-l archive

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

On Fri, 27 Sep 2002, Pyrogon Public wrote:

> I'd like to start using Lua for configuration file uses (which I
> understand was its original purpose).  The straightforward approach that
> I would normally do with a simpler language is just to load the file and
> then parse it directly -- items that need to be created are determined
> by the parser, not by the language.
> For example, in a general scripting language you would have something
> like:
> window a = { x = 100, y = 10 };
> It would hit "window", and then have context for the parsing afterwards.
> With Lua this is a bit tougher, since it has no types.  Everything is a
> table.

Pedantic note: Lua definitely has types, and not everything is a
table.  Numbers, strings, bool, nil, userdata are all different types.
Enough pedantry, since it doesn't solve your problem...

>  One approach is to have the type embedded in the table, e.g.
> a =
> {
>    type = "window",
>    x = 100,
>    y = 10
> }
> Then the calling program iterates over all tables in global space,
> checks their type strings, etc.  This seems cumbersome.  The examples
> generally assume you know what you're looking for, e.g. "width",
> "height", "color", whereas a general configuration language that's
> specifying arbitrary data for your application (like a level description
> for a game) doesn't have that facility.
> The "script" approach would be to actually have the Lua script call back
> into the C program, e.g.
> function createWindows()
>    appCreateWindow( "a", 100, 10 ) -- app function previously registered
> end
> Comments/opinions/suggestions?

I think one common idiom is constructor syntax, using a table
argument, like:

a = window{
	x = 100,
	y = 10

Lua turns that into a call of the "window" function, passing the {...}
as a table argument to the function.  So you could just return the new
object from that function.  It can be defined in Lua or in the host
language; in Lua it might look like:

function window(attributes)
-- return a new window with the given attributes
	return appCreateWindow(
		attributes.x or 640,
		attributes.y or 480)