Lua域特定配置帮助

时间:2010-10-20 08:52:15

标签: configuration lua dsl

我有5000个数据项定义配置,我们期望将其设计为

-- Name of the device
VARIABLE Name { 
   type=Float,
   length=20,
   <<few more definition here>>
}

-- The device running elapsed time since its last boot
VARIABLE BootTime {
   type=Integer,       
   <<few more definition here>>
}

我将使用diffent通信协议从设备读取“Name”,“BootTime”的值,我们使用上面定义的属性。

我希望VARIABLE也具有pre_processor和post_processor函数的属性。

  1. 如何在Lua中定义这样的结构?如果这种结构不可能,那么Lua中的壁橱结构是什么

  2. 我想为这个变量定义重载运算符,以便我能做到,

    我可以配置BootTime = 23456789或 像BootTime + 100(毫秒)这样的算术 或者比如BootTime&gt; 23456789然后做点什么

1 个答案:

答案 0 :(得分:2)

如果您可以放弃关键字VARIABLE,那么代码就是Lua,您只需要一些支持代码(一些__index元方法魔法)。

Integer="Integer"
setmetatable(_G,
        { __index = function(t,n)
                        return function (x) _G[n]=x.value end
                end })
BootTime {
        type=Integer,
        value=10000
}
print(BootTime+2345)

如果您想保留关键字VARIABLE,那么您提供的语法不再是普通的Lua,但如果您可以使用VARIABLE.BootTimeVARIABLE"BootTime"VARIABLE[BootTime],那么是简单的Lua,可以使用适当的元方法。