@@ syntax for class variables within instances

This commit is contained in:
leaf corcoran 2012-01-08 22:02:43 -08:00
parent ad999af9ba
commit 3fc11c813e
6 changed files with 59 additions and 7 deletions

View File

@ -7,3 +7,6 @@ global:
compile: compile:
bin/moonc moon/ moonscript/ bin/moonc moon/ moonscript/
watch:
moonc -w moon/ moonscript/

View File

@ -774,6 +774,29 @@ object to retrieve class methods and properties.
print BackPack.size -- prints 10 print BackPack.size -- prints 10
``` ```
Where as `@` can be put in front of a name to access it within self, `@@` can
be used to access a value that is stored in the `__class` of `self`. Thus,
`@@hello` is shorthand for `self.__class.hello`.
```moon
class Counter
count: 0
new: =>
@@count += 1
Counter!
Counter!
print Counter.count -- prints 2
```
The calling semantics of `@@` are similar to `@`. Calling a `@@` name will pass
the class in as the first argument using Lua's colon syntax.
```moon
@@hello 1,2,3,4
```
## Export Statement ## Export Statement
Because, by default, all assignments to variables that are not lexically visible will Because, by default, all assignments to variables that are not lexically visible will

View File

@ -118,8 +118,9 @@ value_compile = {
return error("Unknown chain action: " .. t) return error("Unknown chain action: " .. t)
end end
end end
if ntype(callee) == "self" and node[3] and ntype(node[3]) == "call" then local t = ntype(callee)
callee[1] = "self_colon" if (t == "self" or t == "self_class") and node[3] and ntype(node[3]) == "call" then
callee[1] = t .. "_colon"
end end
local callee_value = self:value(callee) local callee_value = self:value(callee)
if ntype(callee) == "exp" then if ntype(callee) == "exp" then
@ -151,7 +152,7 @@ value_compile = {
if type(name) == "string" then if type(name) == "string" then
name = name name = name
else else
if name[1] == "self" then if name[1] == "self" or name[1] == "self_class" then
insert(self_args, name) insert(self_args, name)
end end
name = name[2] name = name[2]
@ -305,9 +306,15 @@ value_compile = {
self = function(self, node) self = function(self, node)
return "self." .. self:value(node[2]) return "self." .. self:value(node[2])
end, end,
self_class = function(self, node)
return "self.__class." .. self:value(node[2])
end,
self_colon = function(self, node) self_colon = function(self, node)
return "self:" .. self:value(node[2]) return "self:" .. self:value(node[2])
end, end,
self_class_colon = function(self, node)
return "self.__class:" .. self:value(node[2])
end,
raw_value = function(self, value) raw_value = function(self, value)
local sup = self:get("super") local sup = self:get("super")
if value == "super" and sup then if value == "super" and sup then

View File

@ -74,8 +74,9 @@ value_compile =
else else
error "Unknown chain action: "..t error "Unknown chain action: "..t
if ntype(callee) == "self" and node[3] and ntype(node[3]) == "call" t = ntype(callee)
callee[1] = "self_colon" if (t == "self" or t == "self_class") and node[3] and ntype(node[3]) == "call"
callee[1] = t.."_colon"
callee_value = @value callee callee_value = @value callee
callee_value = @line "(", callee_value, ")" if ntype(callee) == "exp" callee_value = @line "(", callee_value, ")" if ntype(callee) == "exp"
@ -95,7 +96,7 @@ value_compile =
name = if type(name) == "string" name = if type(name) == "string"
name name
else else
if name[1] == "self" if name[1] == "self" or name[1] == "self_class"
insert self_args, name insert self_args, name
name[2] name[2]
insert default_args, arg if default_value insert default_args, arg if default_value
@ -176,9 +177,15 @@ value_compile =
self: (node) => self: (node) =>
"self."..@value node[2] "self."..@value node[2]
self_class: (node) =>
"self.__class."..@value node[2]
self_colon: (node) => self_colon: (node) =>
"self:"..@value node[2] "self:"..@value node[2]
self_class_colon: (node) =>
"self.__class:"..@value node[2]
-- catch all pure string values -- catch all pure string values
raw_value: (value) => raw_value: (value) =>
sup = @get"super" sup = @get"super"

View File

@ -268,7 +268,9 @@ local build_grammar = wrap_env(function()
return true return true
end) / trim end) / trim
local Name = sym"@" * Name / mark"self" + Name + Space * "..." / trim local SelfName = Space * "@" * ("@" * _Name / mark"self_class" + _Name / mark"self")
local Name = SelfName + Name + Space * "..." / trim
local g = lpeg.P{ local g = lpeg.P{
File, File,

View File

@ -80,4 +80,14 @@ class CoolSuper
nil nil
-- selfing
x = @hello
x = @@hello
@hello "world"
@@hello "world"
@@one @@two(4,5) @three, @four
xx = (@hello, @@world, cool) ->