我希望能够使用MoonScript为ComputerCraft制作一个程序,但由于CC的沙盒方式可以防止Minecraft服务器上的安全问题,我不能直接要求moonscript并从那里运行moonscript代码.我必须将我的moonscript代码转换为lua.
然而,这是有问题的,因为moonscript的类实现非常大,并且不是非常文件化保守的.当我输入"class Bacon"时,它会为lua输出:
local Bacon
do
local _parent_0 = nil
local _base_0 = { }
_base_0.__index = _base_0
if _parent_0 then
setmetatable(_base_0, _parent_0.__base)
end
local _class_0 = setmetatable({
__init = function(self, ...)
if _parent_0 then
return _parent_0.__init(self, ...)
end
end,
__base = _base_0,
__name = "Bacon",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil and _parent_0 then
return _parent_0[name]
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
Bacon = _class_0
return _class_0
end
Run Code Online (Sandbox Code Playgroud)
这适用于每个类的实现,这有点荒谬.有什么方法可以在我的moonscript代码中缩短这个吗?
在最新版本中,生成的代码量已经被清理了很多,0.2.4:http://leafo.net/posts/moonscript_v024.html#code_generation_changes
一个类现在最低限度看起来像这样
local Hello
do
local _base_0 = { }
_base_0.__index = _base_0
local _class_0 = setmetatable({
__init = function() end,
__base = _base_0,
__name = "Hello"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
Hello = _class_0
end
Run Code Online (Sandbox Code Playgroud)