oofoe
oofoe

Reputation: 158

How to handle unknown initializer functions in lua?

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!

Upvotes: 3

Views: 208

Answers (2)

lhf
lhf

Reputation: 72342

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})

Upvotes: 2

lhf
lhf

Reputation: 72342

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})

Upvotes: 4

Related Questions