assigning class variables with @ property names

This commit is contained in:
leaf corcoran 2012-01-08 23:10:25 -08:00
parent cd240444de
commit 5933b733ba
5 changed files with 65 additions and 6 deletions

View File

@ -260,8 +260,6 @@ local build_grammar = wrap_env(function()
return patt return patt
end end
local SimpleName = Name -- for table key
-- make sure name is not a keyword -- make sure name is not a keyword
local Name = Cmt(Name, function(str, pos, name) local Name = Cmt(Name, function(str, pos, name)
if keywords[name] then return false end if keywords[name] then return false end
@ -269,6 +267,7 @@ local build_grammar = wrap_env(function()
end) / trim end) / trim
local SelfName = Space * "@" * ("@" * _Name / mark"self_class" + _Name / mark"self") local SelfName = Space * "@" * ("@" * _Name / mark"self_class" + _Name / mark"self")
local KeyName = SelfName + Space * _Name
local Name = SelfName + Name + Space * "..." / trim local Name = SelfName + Name + Space * "..." / trim
@ -444,7 +443,7 @@ local build_grammar = wrap_env(function()
op"*" + op"^" + op"*" + op"^" +
Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export", 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, KeyValueList = KeyValue * (sym"," * KeyValue)^0,
KeyValueLine = CheckIndent * KeyValueList * sym","^-1, KeyValueLine = CheckIndent * KeyValueList * sym","^-1,

View File

@ -616,7 +616,11 @@ Statement = Transformer({
local _list_1 = item local _list_1 = item
for _index_0 = 2, #_list_1 do for _index_0 = 2, #_list_1 do
local tuple = _list_1[_index_0] local tuple = _list_1[_index_0]
insert(properties, tuple) if ntype(tuple[1]) == "self" then
insert(statements, build.assign_one(unpack(tuple)))
else
insert(properties, tuple)
end
end end
end end
end end

View File

@ -318,7 +318,10 @@ Statement = Transformer {
insert statements, item[2] insert statements, item[2]
when "props" when "props"
for tuple in *item[2,] for tuple in *item[2,]
insert properties, tuple if ntype(tuple[1]) == "self"
insert statements, build.assign_one unpack tuple
else
insert properties, tuple
-- find constructor -- find constructor
constructor = nil constructor = nil

View File

@ -91,3 +91,13 @@ x = @@hello
xx = (@hello, @@world, cool) -> xx = (@hello, @@world, cool) ->
-- class properties
class ClassMan
@yeah: 343
blue: =>
@hello: 3434, @world: 23423
green: =>
@red: =>

View File

@ -452,4 +452,47 @@ self.__class:one(self.__class:two(4, 5)(self.three, self.four))
local xx local xx
xx = function(hello, world, cool) xx = function(hello, world, cool)
self.hello, self.__class.world = hello, world self.hello, self.__class.world = hello, world
end 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)()