Skip to content

Commit d1059b8

Browse files
committed
class level inheritance, changed class representation slightly
Looking up a class property will now search the parent class properties if parent exists Classes have a few more built in properties: * __name holds the name of the class as it was defined as a string * __base holds the instance metatable as it was defined (not dynamic) * __parent holds the class's parent class if it exists (not dynamic) The way class inheritance is handled was CHANGED (uses __base instead of looking at __index of parents metatable). Make sure you recompile all your code if using class inheritance because old classes won't work with new ones.
1 parent e55fa25 commit d1059b8

File tree

4 files changed

+241
-51
lines changed

4 files changed

+241
-51
lines changed

moonscript/transform.lua

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -638,12 +638,69 @@ Statement = Transformer({
638638
{
639639
"__init",
640640
constructor
641+
},
642+
{
643+
"__base",
644+
base_name
645+
},
646+
{
647+
"__name",
648+
{
649+
"string",
650+
'"',
651+
name
652+
}
653+
},
654+
{
655+
"__parent",
656+
parent_cls_name
657+
}
658+
})
659+
local class_lookup = build["if"]({
660+
cond = {
661+
"exp",
662+
"val",
663+
"==",
664+
"nil",
665+
"and",
666+
parent_cls_name
667+
},
668+
["then"] = {
669+
parent_cls_name:index("name")
670+
}
671+
})
672+
insert(class_lookup, {
673+
"else",
674+
{
675+
"val"
641676
}
642677
})
643678
local cls_mt = build.table({
644679
{
645680
"__index",
646-
base_name
681+
build.fndef({
682+
args = {
683+
{
684+
"cls"
685+
},
686+
{
687+
"name"
688+
}
689+
},
690+
body = {
691+
build.assign_one(LocalName("val"), build.chain({
692+
base = "rawget",
693+
{
694+
"call",
695+
{
696+
base_name,
697+
"name"
698+
}
699+
}
700+
})),
701+
class_lookup
702+
}
703+
})
647704
},
648705
{
649706
"__call",
@@ -779,16 +836,10 @@ Statement = Transformer({
779836
{
780837
base_name,
781838
_with_0.chain({
782-
base = "getmetatable",
783-
{
784-
"call",
785-
{
786-
parent_cls_name
787-
}
788-
},
839+
base = parent_cls_name,
789840
{
790841
"dot",
791-
"__index"
842+
"__base"
792843
}
793844
})
794845
}

moonscript/transform.moon

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,30 @@ Statement = Transformer {
353353

354354
cls = build.table {
355355
{"__init", constructor}
356+
{"__base", base_name}
357+
{"__name", {"string", '"', name}} -- "quote the string"
358+
{"__parent", parent_cls_name}
356359
}
357360

361+
-- look up a name in the class object
362+
class_lookup = build["if"] {
363+
cond: {"exp", "val", "==", "nil", "and", parent_cls_name}
364+
then: {
365+
parent_cls_name\index"name"
366+
}
367+
}
368+
insert class_lookup, {"else", {"val"}}
369+
358370
cls_mt = build.table {
359-
{"__index", base_name}
371+
{"__index", build.fndef {
372+
args: {{"cls"}, {"name"}}
373+
body: {
374+
build.assign_one LocalName"val", build.chain {
375+
base: "rawget", {"call", {base_name, "name"}}
376+
}
377+
class_lookup
378+
}
379+
}}
360380
{"__call", build.fndef {
361381
args: {{"cls"}, {"..."}}
362382
body: {
@@ -421,11 +441,10 @@ Statement = Transformer {
421441
then: {
422442
.chain {
423443
base: "setmetatable"
424-
{"call", {base_name, .chain {
425-
base: "getmetatable"
426-
{"call", {parent_cls_name}}
427-
{"dot", "__index"}
428-
}}}
444+
{"call", {
445+
base_name,
446+
.chain { base: parent_cls_name, {"dot", "__base"}}
447+
}}
429448
}
430449
}
431450
}

0 commit comments

Comments
 (0)