mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
assigning class variables with @ property names
This commit is contained in:
parent
cd240444de
commit
5933b733ba
@ -260,8 +260,6 @@ local build_grammar = wrap_env(function()
|
||||
return patt
|
||||
end
|
||||
|
||||
local SimpleName = Name -- for table key
|
||||
|
||||
-- make sure name is not a keyword
|
||||
local Name = Cmt(Name, function(str, pos, name)
|
||||
if keywords[name] then return false end
|
||||
@ -269,6 +267,7 @@ local build_grammar = wrap_env(function()
|
||||
end) / trim
|
||||
|
||||
local SelfName = Space * "@" * ("@" * _Name / mark"self_class" + _Name / mark"self")
|
||||
local KeyName = SelfName + Space * _Name
|
||||
|
||||
local Name = SelfName + Name + Space * "..." / trim
|
||||
|
||||
@ -444,7 +443,7 @@ local build_grammar = wrap_env(function()
|
||||
op"*" + op"^" +
|
||||
Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export",
|
||||
|
||||
KeyValue = (sym":" * Name) / self_assign + Ct((SimpleName + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)),
|
||||
KeyValue = (sym":" * Name) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)),
|
||||
KeyValueList = KeyValue * (sym"," * KeyValue)^0,
|
||||
KeyValueLine = CheckIndent * KeyValueList * sym","^-1,
|
||||
|
||||
|
@ -616,10 +616,14 @@ Statement = Transformer({
|
||||
local _list_1 = item
|
||||
for _index_0 = 2, #_list_1 do
|
||||
local tuple = _list_1[_index_0]
|
||||
if ntype(tuple[1]) == "self" then
|
||||
insert(statements, build.assign_one(unpack(tuple)))
|
||||
else
|
||||
insert(properties, tuple)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local constructor = nil
|
||||
properties = (function()
|
||||
local _accum_0 = { }
|
||||
|
@ -318,6 +318,9 @@ Statement = Transformer {
|
||||
insert statements, item[2]
|
||||
when "props"
|
||||
for tuple in *item[2,]
|
||||
if ntype(tuple[1]) == "self"
|
||||
insert statements, build.assign_one unpack tuple
|
||||
else
|
||||
insert properties, tuple
|
||||
|
||||
-- find constructor
|
||||
|
@ -91,3 +91,13 @@ x = @@hello
|
||||
|
||||
xx = (@hello, @@world, cool) ->
|
||||
|
||||
|
||||
-- class properties
|
||||
class ClassMan
|
||||
@yeah: 343
|
||||
blue: =>
|
||||
@hello: 3434, @world: 23423
|
||||
green: =>
|
||||
@red: =>
|
||||
|
||||
|
||||
|
@ -453,3 +453,46 @@ local xx
|
||||
xx = function(hello, world, cool)
|
||||
self.hello, self.__class.world = hello, world
|
||||
end
|
||||
local ClassMan
|
||||
ClassMan = (function()
|
||||
local _parent_0 = nil
|
||||
local _base_0 = {
|
||||
blue = function(self) end,
|
||||
green = function(self) end
|
||||
}
|
||||
_base_0.__index = _base_0
|
||||
if _parent_0 then
|
||||
setmetatable(_base_0, _parent_0.__base)
|
||||
end
|
||||
local _class_0 = setmetatable({
|
||||
__init = function(self, ...)
|
||||
if _parent_0 then
|
||||
return _parent_0.__init(self, ...)
|
||||
end
|
||||
end,
|
||||
__base = _base_0,
|
||||
__name = "ClassMan",
|
||||
__parent = _parent_0
|
||||
}, {
|
||||
__index = function(cls, name)
|
||||
local val = rawget(_base_0, name)
|
||||
if val == nil and _parent_0 then
|
||||
return _parent_0[name]
|
||||
else
|
||||
return val
|
||||
end
|
||||
end,
|
||||
__call = function(cls, ...)
|
||||
local _self_0 = setmetatable({}, _base_0)
|
||||
cls.__init(_self_0, ...)
|
||||
return _self_0
|
||||
end
|
||||
})
|
||||
_base_0.__class = _class_0
|
||||
local self = _class_0
|
||||
self.yeah = 343
|
||||
self.hello = 3434
|
||||
self.world = 23423
|
||||
self.red = function(self) end
|
||||
return _class_0
|
||||
end)()
|
Loading…
Reference in New Issue
Block a user