Skip to content

Commit 6a07d50

Browse files
committed
fixed super dot access, made super available as a value
1 parent 68e209c commit 6a07d50

File tree

6 files changed

+102
-37
lines changed

6 files changed

+102
-37
lines changed

moonscript/compile/value.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ value_compile = {
309309
return "self:" .. self:value(node[2])
310310
end,
311311
raw_value = function(self, value)
312+
if value == "super" then
313+
return self:value(self:get("super")(self))
314+
end
312315
if value == "..." then
313316
self.has_varargs = true
314317
end

moonscript/compile/value.moon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ value_compile =
181181

182182
-- catch all pure string values
183183
raw_value: (value) =>
184+
if value == "super"
185+
return @value @get"super" self
186+
184187
if value == "..."
185188
@has_varargs = true
189+
186190
tostring value

moonscript/transform.lua

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -656,40 +656,51 @@ Statement = Transformer({
656656
value = _with_0.block_exp({
657657
Run(function(self)
658658
return self:set("super", function(block, chain)
659-
local calling_name = block:get("current_block")
660-
local slice = (function()
661-
local _accum_0 = { }
662-
local _len_0 = 0
663-
local _list_0 = chain
664-
for _index_0 = 3, #_list_0 do
659+
if chain then
660+
local slice = (function()
661+
local _accum_0 = { }
662+
local _len_0 = 0
663+
local _list_0 = chain
664+
for _index_0 = 3, #_list_0 do
665+
local item = _list_0[_index_0]
666+
_len_0 = _len_0 + 1
667+
_accum_0[_len_0] = item
668+
end
669+
return _accum_0
670+
end)()
671+
local new_chain = {
672+
"chain",
673+
parent_cls_name
674+
}
675+
if slice[1][1] == "call" then
676+
local calling_name = block:get("current_block")
677+
slice[1] = {
678+
"call",
679+
{
680+
"self",
681+
unpack(slice[1][2])
682+
}
683+
}
684+
local act
685+
if ntype(calling_name) ~= "value" then
686+
act = "index"
687+
else
688+
act = "dot"
689+
end
690+
insert(new_chain, {
691+
act,
692+
calling_name
693+
})
694+
end
695+
local _list_0 = slice
696+
for _index_0 = 1, #_list_0 do
665697
local item = _list_0[_index_0]
666-
_len_0 = _len_0 + 1
667-
_accum_0[_len_0] = item
698+
insert(new_chain, item)
668699
end
669-
return _accum_0
670-
end)()
671-
slice[1] = {
672-
"call",
673-
{
674-
"self",
675-
unpack(slice[1][2])
676-
}
677-
}
678-
local act
679-
if ntype(calling_name) ~= "value" then
680-
act = "index"
700+
return new_chain
681701
else
682-
act = "dot"
702+
return parent_cls_name
683703
end
684-
return {
685-
"chain",
686-
parent_cls_name,
687-
{
688-
act,
689-
calling_name
690-
},
691-
unpack(slice)
692-
}
693704
end)
694705
end),
695706
_with_0.assign_one(parent_cls_name, parent_val == "" and "nil" or parent_val),

moonscript/transform.moon

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,22 @@ Statement = Transformer {
368368
value = .block_exp {
369369
Run =>
370370
@set "super", (block, chain) ->
371-
calling_name = block\get"current_block"
372-
slice = [item for item in *chain[3,]]
373-
-- inject self
374-
slice[1] = {"call", {"self", unpack slice[1][2]}}
375-
376-
act = if ntype(calling_name) != "value" then "index" else "dot"
377-
{"chain", parent_cls_name, {act, calling_name}, unpack slice}
371+
if chain
372+
slice = [item for item in *chain[3,]]
373+
new_chain = {"chain", parent_cls_name}
374+
375+
-- calling super, inject calling name and self into chain
376+
if slice[1][1] == "call"
377+
calling_name = block\get"current_block"
378+
slice[1] = {"call", {"self", unpack slice[1][2]}}
379+
act = if ntype(calling_name) != "value" then "index" else "dot"
380+
insert new_chain, {act, calling_name}
381+
382+
insert new_chain, item for item in *slice
383+
384+
new_chain
385+
else
386+
parent_cls_name
378387

379388
.assign_one parent_cls_name, parent_val == "" and "nil" or parent_val
380389
.assign_one base_name, tbl

tests/inputs/class.moon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ class Okay
4444
something: 20323
4545
-- yeaha
4646

47+
48+
class Biggie extends Okay
49+
something: =>
50+
super 1,2,3,4
51+
super.something another_self, 1,2,3,4
52+
assert super == Okay
53+

tests/outputs/class.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,35 @@ Okay = (function()
168168
})
169169
_base_0.__class = _class_0
170170
return _class_0
171+
end)()
172+
local Biggie
173+
Biggie = (function()
174+
local _parent_0 = Okay
175+
local _base_0 = {
176+
something = function(self)
177+
_parent_0.something(self, 1, 2, 3, 4)
178+
_parent_0.something(another_self, 1, 2, 3, 4)
179+
return assert(_parent_0 == Okay)
180+
end
181+
}
182+
_base_0.__index = _base_0
183+
if _parent_0 then
184+
setmetatable(_base_0, getmetatable(_parent_0).__index)
185+
end
186+
local _class_0 = setmetatable({
187+
__init = function(self, ...)
188+
if _parent_0 then
189+
return _parent_0.__init(self, ...)
190+
end
191+
end
192+
}, {
193+
__index = _base_0,
194+
__call = function(cls, ...)
195+
local _self_0 = setmetatable({}, _base_0)
196+
cls.__init(_self_0, ...)
197+
return _self_0
198+
end
199+
})
200+
_base_0.__class = _class_0
201+
return _class_0
171202
end)()

0 commit comments

Comments
 (0)