From 6a07d50277974f5a470761e5f2e8457a605c3443 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 2 Dec 2011 20:07:42 -0800 Subject: [PATCH] fixed super dot access, made super available as a value --- moonscript/compile/value.lua | 3 ++ moonscript/compile/value.moon | 4 ++ moonscript/transform.lua | 73 ++++++++++++++++++++--------------- moonscript/transform.moon | 21 +++++++--- tests/inputs/class.moon | 7 ++++ tests/outputs/class.lua | 31 +++++++++++++++ 6 files changed, 102 insertions(+), 37 deletions(-) diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 8c0e102..5843bb4 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -309,6 +309,9 @@ value_compile = { return "self:" .. self:value(node[2]) end, raw_value = function(self, value) + if value == "super" then + return self:value(self:get("super")(self)) + end if value == "..." then self.has_varargs = true end diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 3c37551..7133b7e 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -181,6 +181,10 @@ value_compile = -- catch all pure string values raw_value: (value) => + if value == "super" + return @value @get"super" self + if value == "..." @has_varargs = true + tostring value diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 268d7ae..45184dc 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -656,40 +656,51 @@ Statement = Transformer({ value = _with_0.block_exp({ Run(function(self) return self:set("super", function(block, chain) - local calling_name = block:get("current_block") - local slice = (function() - local _accum_0 = { } - local _len_0 = 0 - local _list_0 = chain - for _index_0 = 3, #_list_0 do - local item = _list_0[_index_0] - _len_0 = _len_0 + 1 - _accum_0[_len_0] = item - end - return _accum_0 - end)() - slice[1] = { - "call", - { - "self", - unpack(slice[1][2]) + if chain then + local slice = (function() + local _accum_0 = { } + local _len_0 = 0 + local _list_0 = chain + for _index_0 = 3, #_list_0 do + local item = _list_0[_index_0] + _len_0 = _len_0 + 1 + _accum_0[_len_0] = item + end + return _accum_0 + end)() + local new_chain = { + "chain", + parent_cls_name } - } - local act - if ntype(calling_name) ~= "value" then - act = "index" + if slice[1][1] == "call" then + local calling_name = block:get("current_block") + slice[1] = { + "call", + { + "self", + unpack(slice[1][2]) + } + } + local act + if ntype(calling_name) ~= "value" then + act = "index" + else + act = "dot" + end + insert(new_chain, { + act, + calling_name + }) + end + local _list_0 = slice + for _index_0 = 1, #_list_0 do + local item = _list_0[_index_0] + insert(new_chain, item) + end + return new_chain else - act = "dot" + return parent_cls_name end - return { - "chain", - parent_cls_name, - { - act, - calling_name - }, - unpack(slice) - } end) end), _with_0.assign_one(parent_cls_name, parent_val == "" and "nil" or parent_val), diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 60f1385..29c49ed 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -368,13 +368,22 @@ Statement = Transformer { value = .block_exp { Run => @set "super", (block, chain) -> - calling_name = block\get"current_block" - slice = [item for item in *chain[3,]] - -- inject self - slice[1] = {"call", {"self", unpack slice[1][2]}} + if chain + slice = [item for item in *chain[3,]] + new_chain = {"chain", parent_cls_name} - act = if ntype(calling_name) != "value" then "index" else "dot" - {"chain", parent_cls_name, {act, calling_name}, unpack slice} + -- calling super, inject calling name and self into chain + if slice[1][1] == "call" + calling_name = block\get"current_block" + slice[1] = {"call", {"self", unpack slice[1][2]}} + act = if ntype(calling_name) != "value" then "index" else "dot" + insert new_chain, {act, calling_name} + + insert new_chain, item for item in *slice + + new_chain + else + parent_cls_name .assign_one parent_cls_name, parent_val == "" and "nil" or parent_val .assign_one base_name, tbl diff --git a/tests/inputs/class.moon b/tests/inputs/class.moon index b604137..6de280f 100644 --- a/tests/inputs/class.moon +++ b/tests/inputs/class.moon @@ -44,3 +44,10 @@ class Okay something: 20323 -- yeaha + +class Biggie extends Okay + something: => + super 1,2,3,4 + super.something another_self, 1,2,3,4 + assert super == Okay + diff --git a/tests/outputs/class.lua b/tests/outputs/class.lua index eb536f7..e2965d4 100644 --- a/tests/outputs/class.lua +++ b/tests/outputs/class.lua @@ -168,4 +168,35 @@ Okay = (function() }) _base_0.__class = _class_0 return _class_0 +end)() +local Biggie +Biggie = (function() + local _parent_0 = Okay + local _base_0 = { + something = function(self) + _parent_0.something(self, 1, 2, 3, 4) + _parent_0.something(another_self, 1, 2, 3, 4) + return assert(_parent_0 == Okay) + end + } + _base_0.__index = _base_0 + if _parent_0 then + setmetatable(_base_0, getmetatable(_parent_0).__index) + end + local _class_0 = setmetatable({ + __init = function(self, ...) + if _parent_0 then + return _parent_0.__init(self, ...) + end + end + }, { + __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 + return _class_0 end)() \ No newline at end of file