Skip to content

Commit 7eb269b

Browse files
committed
add distinct class and instance super transformers
1 parent 1bdcf26 commit 7eb269b

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

moonscript/transform/class.lua

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ do
1313
local _obj_0 = require("moonscript.types")
1414
build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP
1515
end
16+
local unpack
17+
unpack = require("moonscript.util").unpack
1618
local transform_super
17-
transform_super = function(cls_name, block, chain)
19+
transform_super = function(cls_name, on_base, block, chain)
20+
if on_base == nil then
21+
on_base = true
22+
end
1823
local relative_parent = {
1924
"chain",
2025
cls_name,
@@ -36,6 +41,12 @@ transform_super = function(cls_name, block, chain)
3641
local new_chain = relative_parent
3742
local _exp_0 = head[1]
3843
if "call" == _exp_0 then
44+
if on_base then
45+
insert(new_chain, {
46+
"dot",
47+
"__base"
48+
})
49+
end
3950
local calling_name = block:get("current_method")
4051
assert(calling_name, "missing calling name")
4152
chain_tail[1] = {
@@ -103,9 +114,13 @@ return function(self, node, ret, parent_assign)
103114
local base_name = NameProxy("base")
104115
local self_name = NameProxy("self")
105116
local cls_name = NameProxy("class")
117+
local cls_instance_super
118+
cls_instance_super = function(...)
119+
return transform_super(cls_name, true, ...)
120+
end
106121
local cls_super
107122
cls_super = function(...)
108-
return transform_super(cls_name, ...)
123+
return transform_super(cls_name, false, ...)
109124
end
110125
local statements = { }
111126
local properties = { }
@@ -150,7 +165,7 @@ return function(self, node, ret, parent_assign)
150165
key, val = tuple[1], tuple[2]
151166
_value_0 = {
152167
key,
153-
super_scope(val, cls_super, key)
168+
super_scope(val, cls_instance_super, key)
154169
}
155170
end
156171
_accum_0[_len_0] = _value_0

moonscript/transform/class.moon

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ CONSTRUCTOR_NAME = "new"
55

66
import insert from table
77
import build, ntype, NOOP from require "moonscript.types"
8+
import unpack from require "moonscript.util"
89

9-
transform_super = (cls_name, block, chain) ->
10+
transform_super = (cls_name, on_base=true, block, chain) ->
1011
relative_parent = {
1112
"chain",
1213
cls_name
@@ -26,6 +27,9 @@ transform_super = (cls_name, block, chain) ->
2627
switch head[1]
2728
-- calling super, inject calling name and self into chain
2829
when "call"
30+
if on_base
31+
insert new_chain, {"dot", "__base"}
32+
2933
calling_name = block\get "current_method"
3034
assert calling_name, "missing calling name"
3135
chain_tail[1] = {"call", {"self", unpack head[2]}}
@@ -80,7 +84,11 @@ super_scope = (value, t, key) ->
8084
self_name = NameProxy "self"
8185
cls_name = NameProxy "class"
8286

83-
cls_super = (...) -> transform_super cls_name, ...
87+
-- super call on instance
88+
cls_instance_super = (...) -> transform_super cls_name, true, ...
89+
90+
-- super call on parent class
91+
cls_super = (...) -> transform_super cls_name, false, ...
8492

8593
-- split apart properties and statements
8694
statements = {}
@@ -107,7 +115,7 @@ super_scope = (value, t, key) ->
107115
continue
108116
else
109117
{key, val} = tuple
110-
{key, super_scope val, cls_super, key}
118+
{key, super_scope val, cls_instance_super, key}
111119

112120

113121
unless constructor

0 commit comments

Comments
 (0)