2013-04-30 46 views
0

我希望能夠使用MoonScript來製作ComputerCraft的程序,但由於CC被沙箱化以防止Minecraft服務器等的安全問題,我不能要求moonscript直接並從那裏運行moonscript代碼。我必須將我的moonscript代碼轉換成lua。moonscript類的Lua輸出是MASSIVE

然而,這是有問題的,因爲moonscript的類實現是非常大的,而不是很文件化 - 保守。當我鍵入「類培根」,它輸出本作盧阿:

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 

這是類的實現,這是一種荒謬的。有什麼辦法可以在我的moonscript代碼中縮短這個嗎?

+2

你爲什麼要擔心Lua代碼的大小?爲什麼不簡單地複製粘貼長代碼? ComputerCraft對程序大小有限制嗎? – 2013-04-30 14:07:22

+1

這根本不是「大規模」。如果你真的擔心它,也許你可以嘗試刪除空格。和Egor說的一樣,文件大小在幾乎所有的事情上都是不相關的。 – ECrownofFire 2013-04-30 18:06:50

回答

1

只要看看我的代碼可以消除由於_parent_0是零一些死的路徑...

local Bacon 
do 
    local _base_0 = { } 
    _base_0.__index = _base_0 
    local _class_0 = setmetatable({ 
    __init = function(self, ...) 
    end, 
    __base = _base_0, 
    __name = "Bacon", 
    }, { 
    __index = function(cls, name) 
     return rawget(_base_0, name) 
    end, 
    __call = function(cls, ...) 
     local _self_0 = setmetatable({}, _base_0) 
     cls.__init(_self_0, ...) 
     return _self_0 
    end 
    }) 
    _base_0.__class = _class_0 
    Bacon = _class_0 
    return _class_0 
end 

你可以找到一個靜態的分析來爲你做這個。

否則,如果它是純粹的代碼尺寸(以字節爲單位)涉及您,然後用壓縮機(如Squish

7

的生成代碼量已經清理了在最新版本不少,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 
0

沒有辦法,你可以在你的moonscript代碼縮短這個。但是,您可能能夠手動後處理輸出,而不是輕微地重構公共部分。值得這樣做嗎?試想一下:

  • 如果你想使用「類」的概念,因爲它是在moonscript可用,那麼你必須反正代碼的所有這些步驟(否則你會得到一個「類」不同的「類」行爲)。
  • 不同的是,在moonscript中,N個類會導致您發佈代碼的N個副本,每個副本只有兩行不同,而如果您手動實現了moonscript的類概念,則可能會使用函數來定義類在一些Lua OO庫中完成:Bacon = newClass(「Bacon」)會調用上面的代碼,適當地替換「__name =」和「Bacon =」行。
  • 因此,通過手動複製moonscript代碼而不是代碼,每個類需要20行代碼(最新的moonscript)。但是除非你的課程是微不足道的,否則它們可能會包含比20行更多的代碼,大概200個代碼。
  • 因此,儘管每個班級重複18行,但這可能還不到10%的代碼,你將不得不寫。
  • 但這是一個假設。如果假設是錯誤的,那麼問題是,爲什麼你需要在第一個地方上課?只需使用基於帶有':'糖的表的基本Lua對象。

如果你真的想使用moonscript輸出,我認爲你唯一的選擇是手動重構它。您發佈的代碼可以放入函數中,只需要將兩行更改爲類名的參數化。

請注意,在您的Lua文件中留下由moonscript生成的Lua代碼重複,至少對於經常使用的操作(類定義可能不是其中之一,但moonscript比這更多):

  • 它削減函數調用;這可能會提高速度,因爲不需要將變量壓入堆棧,調用函數,將返回值放入堆棧,讀取堆棧,彈出堆棧值)。速度增加是否顯着取決於代碼被調用的頻率。
  • 它更易於維護:如果操作正確,當moonscript更容易更新時,您可能會更新代碼。