Skip to content

Commit 021d487

Browse files
committed
class name can be any assignable expression #46
1 parent b6987fe commit 021d487

File tree

5 files changed

+191
-22
lines changed

5 files changed

+191
-22
lines changed

moonscript/parse.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ local build_grammar = wrap_env(function()
507507
TableBlockInner = Ct(KeyValueLine * (SpaceBreak^1 * KeyValueLine)^0),
508508
TableBlock = SpaceBreak^1 * Advance * ensure(TableBlockInner, PopIndent) / mark"table",
509509

510-
ClassDecl = key"class" * Name * (key"extends" * PreventIndent * ensure(Exp, PopIndent) + C"")^-1 * ClassBlock / mark"class",
510+
ClassDecl = key"class" * Assignable * (key"extends" * PreventIndent * ensure(Exp, PopIndent) + C"")^-1 * ClassBlock / mark"class",
511511

512512
ClassBlock = SpaceBreak^1 * Advance *
513513
Ct(ClassLine * (SpaceBreak^1 * ClassLine)^0) * PopIndent,

moonscript/transform.lua

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,28 @@ Statement = Transformer({
954954
smart_node(constructor)
955955
constructor.arrow = "fat"
956956
end
957+
local real_name
958+
if ntype(name) == "chain" then
959+
local last = name[#name]
960+
local _exp_0 = ntype(last)
961+
if "dot" == _exp_0 then
962+
real_name = {
963+
"string",
964+
'"',
965+
last[2]
966+
}
967+
elseif "index" == _exp_0 then
968+
real_name = last[2]
969+
else
970+
real_name = "nil"
971+
end
972+
else
973+
real_name = {
974+
"string",
975+
'"',
976+
name
977+
}
978+
end
957979
local cls = build.table({
958980
{
959981
"__init",
@@ -965,11 +987,7 @@ Statement = Transformer({
965987
},
966988
{
967989
"__name",
968-
{
969-
"string",
970-
'"',
971-
name
972-
}
990+
real_name
973991
},
974992
{
975993
"__parent",
@@ -1197,21 +1215,25 @@ Statement = Transformer({
11971215
})
11981216
}
11991217
}),
1200-
_with_0.assign_one(name, cls_name)
1218+
_with_0.assign_one(name, cls_name),
1219+
(function()
1220+
if ret then
1221+
return ret(cls_name)
1222+
end
1223+
end)()
12011224
}
12021225
hoist_declarations(out_body)
12031226
value = _with_0.group({
1204-
_with_0.declare({
1205-
names = {
1206-
name
1207-
}
1208-
}),
1209-
_with_0["do"](out_body),
12101227
(function()
1211-
if ret then
1212-
return ret(name)
1228+
if ntype(name) == "value" then
1229+
return _with_0.declare({
1230+
names = {
1231+
name
1232+
}
1233+
})
12131234
end
1214-
end)()
1235+
end)(),
1236+
_with_0["do"](out_body)
12151237
})
12161238
end
12171239
return value

moonscript/transform.moon

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,22 @@ Statement = Transformer {
488488
smart_node constructor
489489
constructor.arrow = "fat"
490490

491+
real_name = if ntype(name) == "chain"
492+
last = name[#name]
493+
switch ntype last
494+
when "dot"
495+
{"string", '"', last[2]}
496+
when "index"
497+
last[2]
498+
else
499+
"nil"
500+
else
501+
{"string", '"', name}
502+
491503
cls = build.table {
492504
{"__init", constructor}
493505
{"__base", base_name}
494-
{"__name", {"string", '"', name}} -- "quote the string"
506+
{"__name", real_name} -- "quote the string"
495507
{"__parent", parent_cls_name}
496508
}
497509

@@ -610,16 +622,17 @@ Statement = Transformer {
610622
}
611623

612624
.assign_one name, cls_name
625+
if ret
626+
ret cls_name
613627
}
614628

615629
hoist_declarations out_body
616630

617631
value = .group {
618-
.declare names: {name}
619-
.do out_body
632+
if ntype(name) == "value"
633+
.declare names: {name}
620634

621-
if ret
622-
ret name
635+
.do out_body
623636
}
624637

625638
value

tests/inputs/class.moon

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,17 @@ yyy = ->
133133
class Cool
134134
nil
135135

136+
137+
--
138+
139+
class a.b.c.D
140+
nil
141+
142+
143+
class a.b["hello"]
144+
nil
145+
146+
class (-> require "moon")!.Something extends Hello.World
147+
nil
148+
149+

tests/outputs/class.lua

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,126 @@ yyy = function()
631631
_parent_0.__inherited(_parent_0, _class_0)
632632
end
633633
Cool = _class_0
634+
return _class_0
634635
end
635-
return Cool
636+
end
637+
do
638+
local _parent_0 = nil
639+
local _base_0 = { }
640+
_base_0.__index = _base_0
641+
if _parent_0 then
642+
setmetatable(_base_0, _parent_0.__base)
643+
end
644+
local _class_0 = setmetatable({
645+
__init = function(self, ...)
646+
if _parent_0 then
647+
return _parent_0.__init(self, ...)
648+
end
649+
end,
650+
__base = _base_0,
651+
__name = "D",
652+
__parent = _parent_0
653+
}, {
654+
__index = function(cls, name)
655+
local val = rawget(_base_0, name)
656+
if val == nil and _parent_0 then
657+
return _parent_0[name]
658+
else
659+
return val
660+
end
661+
end,
662+
__call = function(cls, ...)
663+
local _self_0 = setmetatable({}, _base_0)
664+
cls.__init(_self_0, ...)
665+
return _self_0
666+
end
667+
})
668+
_base_0.__class = _class_0
669+
local self = _class_0
670+
_ = nil
671+
if _parent_0 and _parent_0.__inherited then
672+
_parent_0.__inherited(_parent_0, _class_0)
673+
end
674+
a.b.c.D = _class_0
675+
end
676+
do
677+
local _parent_0 = nil
678+
local _base_0 = { }
679+
_base_0.__index = _base_0
680+
if _parent_0 then
681+
setmetatable(_base_0, _parent_0.__base)
682+
end
683+
local _class_0 = setmetatable({
684+
__init = function(self, ...)
685+
if _parent_0 then
686+
return _parent_0.__init(self, ...)
687+
end
688+
end,
689+
__base = _base_0,
690+
__name = "hello",
691+
__parent = _parent_0
692+
}, {
693+
__index = function(cls, name)
694+
local val = rawget(_base_0, name)
695+
if val == nil and _parent_0 then
696+
return _parent_0[name]
697+
else
698+
return val
699+
end
700+
end,
701+
__call = function(cls, ...)
702+
local _self_0 = setmetatable({}, _base_0)
703+
cls.__init(_self_0, ...)
704+
return _self_0
705+
end
706+
})
707+
_base_0.__class = _class_0
708+
local self = _class_0
709+
_ = nil
710+
if _parent_0 and _parent_0.__inherited then
711+
_parent_0.__inherited(_parent_0, _class_0)
712+
end
713+
a.b["hello"] = _class_0
714+
end
715+
do
716+
local _parent_0 = Hello.World
717+
local _base_0 = { }
718+
_base_0.__index = _base_0
719+
if _parent_0 then
720+
setmetatable(_base_0, _parent_0.__base)
721+
end
722+
local _class_0 = setmetatable({
723+
__init = function(self, ...)
724+
if _parent_0 then
725+
return _parent_0.__init(self, ...)
726+
end
727+
end,
728+
__base = _base_0,
729+
__name = "Something",
730+
__parent = _parent_0
731+
}, {
732+
__index = function(cls, name)
733+
local val = rawget(_base_0, name)
734+
if val == nil and _parent_0 then
735+
return _parent_0[name]
736+
else
737+
return val
738+
end
739+
end,
740+
__call = function(cls, ...)
741+
local _self_0 = setmetatable({}, _base_0)
742+
cls.__init(_self_0, ...)
743+
return _self_0
744+
end
745+
})
746+
_base_0.__class = _class_0
747+
local self = _class_0
748+
_ = nil
749+
if _parent_0 and _parent_0.__inherited then
750+
_parent_0.__inherited(_parent_0, _class_0)
751+
end
752+
(function()
753+
return require("moon")
754+
end)().Something = _class_0
755+
return _class_0
636756
end

0 commit comments

Comments
 (0)