From 255a92f8ee1a23f820012fc37ad0b4f6341c918a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 3 Dec 2011 14:07:05 -0800 Subject: [PATCH] expressions inside of class body is parsed --- moonscript/parse.lua | 10 +++++++- moonscript/transform.lua | 50 ++++++++++++++++++++++++++------------- moonscript/transform.moon | 26 +++++++++++++------- 3 files changed, 61 insertions(+), 25 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 27fb5cb..7fea314 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -422,7 +422,15 @@ local build_grammar = wrap_env(function() TableBlockInner = Ct(KeyValueLine * (SpaceBreak^1 * KeyValueLine)^0), TableBlock = SpaceBreak^1 * Advance * TableBlockInner * PopIndent / mark"table", - ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * TableBlock / mark"class", + ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * ClassBlock / mark"class", + + ClassBlock = SpaceBreak^1 * Advance * + Ct(ClassLine * (SpaceBreak^1 * ClassLine)^0) * PopIndent, + ClassLine = CheckIndent * (( + KeyValueList / mark"props" + + Exp / mark"stm" + ) * sym","^-1), + Export = key"export" * ( Cc"class" * ClassDecl + op"*" + op"^" + diff --git a/moonscript/transform.lua b/moonscript/transform.lua index ac7dd59..5aa3a79 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -536,20 +536,36 @@ Statement = Transformer({ }) end, class = function(self, node) - local _, name, parent_val, tbl = unpack(node) + local _, name, parent_val, body = unpack(node) + local statements = { } + local properties = { } + local _list_0 = body + for _index_0 = 1, #_list_0 do + local item = _list_0[_index_0] + local _exp_0 = item[1] + if "stm" == _exp_0 then + insert(statements, item[2]) + elseif "props" == _exp_0 then + local _list_1 = item + for _index_0 = 2, #_list_1 do + local tuple = _list_1[_index_0] + insert(properties, tuple) + end + end + end local constructor = nil - local properties = (function() + properties = (function() local _accum_0 = { } local _len_0 = 0 - local _list_0 = tbl[2] - for _index_0 = 1, #_list_0 do - local entry = _list_0[_index_0] + local _list_1 = properties + for _index_0 = 1, #_list_1 do + local tuple = _list_1[_index_0] local _value_0 - if entry[1] == constructor_name then - constructor = entry[2] + if tuple[1] == constructor_name then + constructor = tuple[2] _value_0 = nil else - _value_0 = entry + _value_0 = tuple end if _value_0 ~= nil then _len_0 = _len_0 + 1 @@ -558,7 +574,6 @@ Statement = Transformer({ end return _accum_0 end)() - tbl[2] = properties local parent_cls_name = NameProxy("parent") local base_name = NameProxy("base") local self_name = NameProxy("self") @@ -660,9 +675,9 @@ Statement = Transformer({ local slice = (function() local _accum_0 = { } local _len_0 = 0 - local _list_0 = chain - for _index_0 = 3, #_list_0 do - local item = _list_0[_index_0] + local _list_1 = chain + for _index_0 = 3, #_list_1 do + local item = _list_1[_index_0] _len_0 = _len_0 + 1 _accum_0[_len_0] = item end @@ -710,9 +725,9 @@ Statement = Transformer({ } } end - local _list_0 = slice - for _index_0 = 1, #_list_0 do - local item = _list_0[_index_0] + local _list_1 = slice + for _index_0 = 1, #_list_1 do + local item = _list_1[_index_0] insert(new_chain, item) end return new_chain @@ -722,7 +737,10 @@ Statement = Transformer({ end) end), _with_0.assign_one(parent_cls_name, parent_val == "" and "nil" or parent_val), - _with_0.assign_one(base_name, tbl), + _with_0.assign_one(base_name, { + "table", + properties + }), _with_0.assign_one(base_name:chain("__index"), base_name), build["if"]({ cond = parent_cls_name, diff --git a/moonscript/transform.moon b/moonscript/transform.moon index a662736..dcc06c7 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -302,17 +302,27 @@ Statement = Transformer { } class: (node) => - _, name, parent_val, tbl = unpack node + _, name, parent_val, body = unpack node + -- split apart properties and statements + statements = {} + properties = {} + for item in *body + switch item[1] + when "stm" + insert statements, item[2] + when "props" + for tuple in *item[2,] + insert properties, tuple + + -- find constructor constructor = nil - properties = for entry in *tbl[2] - if entry[1] == constructor_name - constructor = entry[2] + properties = for tuple in *properties + if tuple[1] == constructor_name + constructor = tuple[2] nil else - entry - - tbl[2] = properties + tuple parent_cls_name = NameProxy "parent" base_name = NameProxy "base" @@ -398,7 +408,7 @@ Statement = Transformer { parent_cls_name .assign_one parent_cls_name, parent_val == "" and "nil" or parent_val - .assign_one base_name, tbl + .assign_one base_name, {"table", properties} .assign_one base_name\chain"__index", base_name build["if"] {