tags:

views:

60

answers:

2

I want to load data written in a variant of lua (eyeonScript). However, the data is peppered with references to initialization functions that are not in plain lua:

Redden = BrightnessContrast {
    Inputs = {
        Red = Input {
            Value = 0,
        },
    },
}

Standard lua gives "attempt to call a nil value" or "unexpected symbol" errors. Is there any way to catch these and pass it to some sort of generic initializer?

I want to wind up with a nested table data structure.

Thanks!

+4  A: 

Set an __index metamethod for the table of globals. For instance, so that undefined functions behave like the identity:

setmetatable(_G,{__index=function (n)
                           return function (x) return x end
                         end})
lhf
Thanks! That's pretty much just what I needed. A small side effect is that any typos now turn into functions, but since I'm running Lua just for parsing this one structure, I'm not too worried about that.
oofoe
nice trick lhf. @oofoe: What you can do anyway is to create a custom environment to execute the configuration file in (with setfenv), so that it can't clobber or pollute your normal namespace. You can then define the metamethod only on that environment.
kaizer.se
+2  A: 

Here is another trick: set __call for nil, but you need to do it in C or using the debug library. The advantage of this trick is that it only handles calls to undefined functions:

debug.setmetatable(nil,{__call=function (x,v) return v end})
lhf