Skip to content

Commit 255a92f

Browse files
committed
expressions inside of class body is parsed
1 parent 7ff1158 commit 255a92f

File tree

3 files changed

+62
-26
lines changed

3 files changed

+62
-26
lines changed

moonscript/parse.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,15 @@ local build_grammar = wrap_env(function()
422422
TableBlockInner = Ct(KeyValueLine * (SpaceBreak^1 * KeyValueLine)^0),
423423
TableBlock = SpaceBreak^1 * Advance * TableBlockInner * PopIndent / mark"table",
424424

425-
ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * TableBlock / mark"class",
425+
ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * ClassBlock / mark"class",
426+
427+
ClassBlock = SpaceBreak^1 * Advance *
428+
Ct(ClassLine * (SpaceBreak^1 * ClassLine)^0) * PopIndent,
429+
ClassLine = CheckIndent * ((
430+
KeyValueList / mark"props" +
431+
Exp / mark"stm"
432+
) * sym","^-1),
433+
426434
Export = key"export" * (
427435
Cc"class" * ClassDecl +
428436
op"*" + op"^" +

moonscript/transform.lua

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -536,20 +536,36 @@ Statement = Transformer({
536536
})
537537
end,
538538
class = function(self, node)
539-
local _, name, parent_val, tbl = unpack(node)
539+
local _, name, parent_val, body = unpack(node)
540+
local statements = { }
541+
local properties = { }
542+
local _list_0 = body
543+
for _index_0 = 1, #_list_0 do
544+
local item = _list_0[_index_0]
545+
local _exp_0 = item[1]
546+
if "stm" == _exp_0 then
547+
insert(statements, item[2])
548+
elseif "props" == _exp_0 then
549+
local _list_1 = item
550+
for _index_0 = 2, #_list_1 do
551+
local tuple = _list_1[_index_0]
552+
insert(properties, tuple)
553+
end
554+
end
555+
end
540556
local constructor = nil
541-
local properties = (function()
557+
properties = (function()
542558
local _accum_0 = { }
543559
local _len_0 = 0
544-
local _list_0 = tbl[2]
545-
for _index_0 = 1, #_list_0 do
546-
local entry = _list_0[_index_0]
560+
local _list_1 = properties
561+
for _index_0 = 1, #_list_1 do
562+
local tuple = _list_1[_index_0]
547563
local _value_0
548-
if entry[1] == constructor_name then
549-
constructor = entry[2]
564+
if tuple[1] == constructor_name then
565+
constructor = tuple[2]
550566
_value_0 = nil
551567
else
552-
_value_0 = entry
568+
_value_0 = tuple
553569
end
554570
if _value_0 ~= nil then
555571
_len_0 = _len_0 + 1
@@ -558,7 +574,6 @@ Statement = Transformer({
558574
end
559575
return _accum_0
560576
end)()
561-
tbl[2] = properties
562577
local parent_cls_name = NameProxy("parent")
563578
local base_name = NameProxy("base")
564579
local self_name = NameProxy("self")
@@ -660,9 +675,9 @@ Statement = Transformer({
660675
local slice = (function()
661676
local _accum_0 = { }
662677
local _len_0 = 0
663-
local _list_0 = chain
664-
for _index_0 = 3, #_list_0 do
665-
local item = _list_0[_index_0]
678+
local _list_1 = chain
679+
for _index_0 = 3, #_list_1 do
680+
local item = _list_1[_index_0]
666681
_len_0 = _len_0 + 1
667682
_accum_0[_len_0] = item
668683
end
@@ -710,9 +725,9 @@ Statement = Transformer({
710725
}
711726
}
712727
end
713-
local _list_0 = slice
714-
for _index_0 = 1, #_list_0 do
715-
local item = _list_0[_index_0]
728+
local _list_1 = slice
729+
for _index_0 = 1, #_list_1 do
730+
local item = _list_1[_index_0]
716731
insert(new_chain, item)
717732
end
718733
return new_chain
@@ -722,7 +737,10 @@ Statement = Transformer({
722737
end)
723738
end),
724739
_with_0.assign_one(parent_cls_name, parent_val == "" and "nil" or parent_val),
725-
_with_0.assign_one(base_name, tbl),
740+
_with_0.assign_one(base_name, {
741+
"table",
742+
properties
743+
}),
726744
_with_0.assign_one(base_name:chain("__index"), base_name),
727745
build["if"]({
728746
cond = parent_cls_name,

moonscript/transform.moon

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,27 @@ Statement = Transformer {
302302
}
303303

304304
class: (node) =>
305-
_, name, parent_val, tbl = unpack node
306-
305+
_, name, parent_val, body = unpack node
306+
307+
-- split apart properties and statements
308+
statements = {}
309+
properties = {}
310+
for item in *body
311+
switch item[1]
312+
when "stm"
313+
insert statements, item[2]
314+
when "props"
315+
for tuple in *item[2,]
316+
insert properties, tuple
317+
318+
-- find constructor
307319
constructor = nil
308-
properties = for entry in *tbl[2]
309-
if entry[1] == constructor_name
310-
constructor = entry[2]
320+
properties = for tuple in *properties
321+
if tuple[1] == constructor_name
322+
constructor = tuple[2]
311323
nil
312324
else
313-
entry
314-
315-
tbl[2] = properties
325+
tuple
316326

317327
parent_cls_name = NameProxy "parent"
318328
base_name = NameProxy "base"
@@ -398,7 +408,7 @@ Statement = Transformer {
398408
parent_cls_name
399409

400410
.assign_one parent_cls_name, parent_val == "" and "nil" or parent_val
401-
.assign_one base_name, tbl
411+
.assign_one base_name, {"table", properties}
402412
.assign_one base_name\chain"__index", base_name
403413

404414
build["if"] {

0 commit comments

Comments
 (0)