Skip to content

Commit 4b26091

Browse files
committed
change how super method name is set
1 parent 76454f6 commit 4b26091

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

moonscript/compile/statement.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import concat, insert from table
2727
_, names, values = unpack node
2828

2929
undeclared = @declare names
30-
declare = "local "..concat(undeclared, ", ")
30+
declare = "local " .. concat(undeclared, ", ")
3131

3232
has_fndef = false
3333
i = 1

moonscript/compile/value.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ local string_chars = {
1717
["\n"] = "\\n"
1818
}
1919
return {
20+
scoped = function(self, node)
21+
local _, before, value, after
22+
_, before, value, after = node[1], node[2], node[3], node[4]
23+
_ = before and before:call(self)
24+
do
25+
local _with_0 = self:value(value)
26+
_ = after and after:call(self)
27+
return _with_0
28+
end
29+
end,
2030
exp = function(self, node)
2131
local _comp
2232
_comp = function(i, value)
@@ -248,9 +258,7 @@ return {
248258
else
249259
assign = self:line("[", _with_0:value(key), "]")
250260
end
251-
_with_0:set("current_block", key)
252261
local out = self:line(assign, " = ", _with_0:value(value))
253-
_with_0:set("current_block", nil)
254262
return out
255263
else
256264
return self:line(_with_0:value(tuple[1]))

moonscript/compile/value.moon

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ string_chars = {
1515
}
1616

1717
{
18+
scoped: (node) =>
19+
{_, before, value, after} = node
20+
before and before\call @
21+
with @value value
22+
after and after\call @
23+
1824
-- list of values separated by binary operators
1925
exp: (node) =>
2026
_comp = (i, value) ->
@@ -146,9 +152,7 @@ string_chars = {
146152
else
147153
@line "[", \value(key), "]"
148154

149-
\set "current_block", key
150155
out = @line assign, " = ", \value(value)
151-
\set "current_block", nil
152156
out
153157
else
154158
@line \value tuple[1]

moonscript/transform/class.lua

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ 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 super_scope
17+
super_scope = function(value, key)
18+
local prev_method
19+
return {
20+
"scoped",
21+
Run(function(self)
22+
prev_method = self:get("current_method")
23+
return self:set("current_method", key)
24+
end),
25+
value,
26+
Run(function(self)
27+
return self:set("current_method", prev_method)
28+
end)
29+
}
30+
end
1631
return function(self, node, ret, parent_assign)
1732
local _, name, parent_val, body = unpack(node)
1833
if parent_val == "" then
@@ -51,7 +66,12 @@ return function(self, node, ret, parent_assign)
5166
_continue_0 = true
5267
break
5368
else
54-
_value_0 = tuple
69+
local val
70+
key, val = tuple[1], tuple[2]
71+
_value_0 = {
72+
key,
73+
super_scope(val, key)
74+
}
5575
end
5676
_accum_0[_len_0] = _value_0
5777
_len_0 = _len_0 + 1
@@ -129,7 +149,10 @@ return function(self, node, ret, parent_assign)
129149
local cls = build.table({
130150
{
131151
"__init",
132-
constructor
152+
super_scope(constructor, {
153+
"key_literal",
154+
"__init"
155+
})
133156
},
134157
{
135158
"__base",
@@ -304,7 +327,7 @@ return function(self, node, ret, parent_assign)
304327
local new_chain = relative_parent
305328
local _exp_1 = head[1]
306329
if "call" == _exp_1 then
307-
local calling_name = block:get("current_block")
330+
local calling_name = block:get("current_method")
308331
assert(calling_name, "missing calling name")
309332
chain_tail[1] = {
310333
"call",

moonscript/transform/class.moon

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ CONSTRUCTOR_NAME = "new"
66
import insert from table
77
import build, ntype, NOOP from require "moonscript.types"
88

9+
super_scope = (value, key) ->
10+
local prev_method
11+
12+
{
13+
"scoped",
14+
Run =>
15+
prev_method = @get "current_method"
16+
@set "current_method", key
17+
value
18+
Run =>
19+
@set "current_method", prev_method
20+
}
21+
922
(node, ret, parent_assign) =>
1023
_, name, parent_val, body = unpack node
1124
parent_val = nil if parent_val == ""
@@ -32,7 +45,8 @@ import build, ntype, NOOP from require "moonscript.types"
3245
constructor = tuple[2]
3346
continue
3447
else
35-
tuple
48+
{key, val} = tuple
49+
{key, super_scope val, key}
3650

3751
parent_cls_name = NameProxy "parent"
3852
base_name = NameProxy "base"
@@ -77,7 +91,7 @@ import build, ntype, NOOP from require "moonscript.types"
7791
{"string", '"', flattened_name}
7892

7993
cls = build.table {
80-
{"__init", constructor}
94+
{"__init", super_scope constructor, {"key_literal", "__init"}}
8195
{"__base", base_name}
8296
{"__name", real_name} -- "quote the string"
8397
parent_val and {"__parent", parent_cls_name} or nil
@@ -173,7 +187,7 @@ import build, ntype, NOOP from require "moonscript.types"
173187
switch head[1]
174188
-- calling super, inject calling name and self into chain
175189
when "call"
176-
calling_name = block\get"current_block"
190+
calling_name = block\get "current_method"
177191
assert calling_name, "missing calling name"
178192
chain_tail[1] = {"call", {"self", unpack head[2]}}
179193

0 commit comments

Comments
 (0)