From fc72baf4234ca3be5fe2f583b791a7e59e246b87 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 2 Dec 2011 20:37:47 -0800 Subject: [PATCH] calling function of super with \ uses self instead of class as first arg --- moonscript/compile/value.lua | 2 +- moonscript/compile/value.moon | 2 +- moonscript/transform.lua | 19 +++++++++++++++++-- moonscript/transform.moon | 20 ++++++++++++++------ tests/inputs/class.moon | 5 +++++ tests/outputs/class.lua | 29 +++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 10 deletions(-) diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 5843bb4..6c4d0f1 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -109,7 +109,7 @@ value_compile = { elseif t == "index" then return "[", self:value(arg), "]" elseif t == "dot" then - return ".", arg + return ".", self:value(arg) elseif t == "colon" then return ":", arg, chain_item(node[3]) elseif t == "colon_stub" then diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 7133b7e..5b8ac98 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -66,7 +66,7 @@ value_compile = elseif t == "index" "[", @value(arg), "]" elseif t == "dot" - ".", arg + ".", @value arg elseif t == "colon" ":", arg, chain_item(node[3]) elseif t == "colon_stub" diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 45184dc..60583e9 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -672,13 +672,15 @@ Statement = Transformer({ "chain", parent_cls_name } - if slice[1][1] == "call" then + local head = slice[1] + local _exp_0 = head[1] + if "call" == _exp_0 then local calling_name = block:get("current_block") slice[1] = { "call", { "self", - unpack(slice[1][2]) + unpack(head[2]) } } local act @@ -691,6 +693,19 @@ Statement = Transformer({ act, calling_name }) + elseif "colon" == _exp_0 then + local call = head[3] + insert(new_chain, { + "dot", + head[2] + }) + slice[1] = { + "call", + { + "self", + unpack(call[2]) + } + } end local _list_0 = slice for _index_0 = 1, #_list_0 do diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 29c49ed..9681305 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -372,12 +372,20 @@ Statement = Transformer { slice = [item for item in *chain[3,]] new_chain = {"chain", parent_cls_name} - -- 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} + head = slice[1] + switch head[1] + -- calling super, inject calling name and self into chain + when "call" + calling_name = block\get"current_block" + slice[1] = {"call", {"self", unpack head[2]}} + act = if ntype(calling_name) != "value" then "index" else "dot" + insert new_chain, {act, calling_name} + + -- colon call on super, replace class with self as first arg + when "colon" + call = head[3] + insert new_chain, {"dot", head[2]} + slice[1] = { "call", { "self", unpack call[2] } } insert new_chain, item for item in *slice diff --git a/tests/inputs/class.moon b/tests/inputs/class.moon index 6de280f..fcebe59 100644 --- a/tests/inputs/class.moon +++ b/tests/inputs/class.moon @@ -51,3 +51,8 @@ class Biggie extends Okay super.something another_self, 1,2,3,4 assert super == Okay + +class Yeah + okay: => + super\something 1,2,3,4 + diff --git a/tests/outputs/class.lua b/tests/outputs/class.lua index e2965d4..258e909 100644 --- a/tests/outputs/class.lua +++ b/tests/outputs/class.lua @@ -199,4 +199,33 @@ Biggie = (function() }) _base_0.__class = _class_0 return _class_0 +end)() +local Yeah +Yeah = (function() + local _parent_0 = nil + local _base_0 = { + okay = function(self) + return _parent_0.something(self, 1, 2, 3, 4) + 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