use ref correctly in generated code, don't pass basic names to value

This commit is contained in:
leaf corcoran 2013-12-27 03:51:59 -08:00
parent d6eaf8ddfb
commit 887745491d
10 changed files with 54 additions and 18 deletions

View File

@ -467,7 +467,11 @@ do
return self.value_compilers[t] ~= nil or t == "value"
end,
name = function(self, node, ...)
if type(node) == "string" then
return node
else
return self:value(node, ...)
end
end,
value = function(self, node, ...)
node = self.transform.value(node)

View File

@ -326,8 +326,12 @@ class Block
t = ntype node
@value_compilers[t] != nil or t == "value"
-- line wise compile functions
name: (node, ...) => @value node, ...
-- compile name for assign
name: (node, ...) =>
if type(node) == "string"
node
else
@value node, ...
value: (node, ...) =>
node = @transform.value node

View File

@ -176,7 +176,10 @@ local value_compilers = {
'if',
{
'exp',
name,
{
"ref",
name
},
'==',
'nil'
},
@ -286,16 +289,16 @@ local value_compilers = {
return self:line("not ", self:value(node[2]))
end,
self = function(self, node)
return "self." .. self:value(node[2])
return "self." .. self:name(node[2])
end,
self_class = function(self, node)
return "self.__class." .. self:value(node[2])
return "self.__class." .. self:name(node[2])
end,
self_colon = function(self, node)
return "self:" .. self:value(node[2])
return "self:" .. self:name(node[2])
end,
self_class_colon = function(self, node)
return "self.__class:" .. self:value(node[2])
return "self.__class:" .. self:name(node[2])
end,
ref = function(self, value)
do

View File

@ -110,7 +110,7 @@ value_compilers =
name, value = unpack default
name = name[2] if type(name) == "table"
\stm {
'if', {'exp', name, '==', 'nil'}, {
'if', {'exp', {"ref", name}, '==', 'nil'}, {
{'assign', {name}, {value}}
}
}
@ -173,16 +173,16 @@ value_compilers =
@line "not ", @value node[2]
self: (node) =>
"self."..@value node[2]
"self."..@name node[2]
self_class: (node) =>
"self.__class."..@value node[2]
"self.__class."..@name node[2]
self_colon: (node) =>
"self:"..@value node[2]
"self:"..@name node[2]
self_class_colon: (node) =>
"self.__class:"..@value node[2]
"self.__class:"..@name node[2]
-- a variable reference
ref: (value) =>
@ -198,5 +198,4 @@ value_compilers =
tostring value
{ :value_compilers }

View File

@ -1006,7 +1006,10 @@ Statement = Transformer({
local class_lookup = build["if"]({
cond = {
"exp",
"val",
{
"ref",
"val"
},
"==",
"nil"
},
@ -1036,9 +1039,12 @@ Statement = Transformer({
"call",
{
base_name,
{
"ref",
"name"
}
}
}
})),
class_lookup
}

View File

@ -603,7 +603,7 @@ Statement = Transformer {
-- looking up a name in the class object
class_index = if parent_val
class_lookup = build["if"] {
cond: { "exp", "val", "==", "nil" }
cond: { "exp", {"ref", "val"}, "==", "nil" }
then: {
parent_cls_name\index"name"
}
@ -614,7 +614,7 @@ Statement = Transformer {
args: {{"cls"}, {"name"}}
body: {
build.assign_one LocalName"val", build.chain {
base: "rawget", {"call", {base_name, "name"}}
base: "rawget", {"call", {base_name, {"ref", "name"}}}
}
class_lookup
}

View File

@ -74,6 +74,12 @@ do
})
end,
index = function(self, key)
if type(key) == "string" then
key = {
"ref",
key
}
end
return build.chain({
base = self,
{

View File

@ -30,6 +30,9 @@ class NameProxy
}
index: (key) =>
if type(key) == "string"
key = {"ref", key}
build.chain {
base: self, {"index", key}
}

View File

@ -263,6 +263,12 @@ build = setmetatable({
end,
chain = function(parts)
local base = parts.base or error("expecting base property for chain")
if type(base) == "string" then
base = {
"ref",
base
}
end
local node = {
"chain",
base

View File

@ -141,8 +141,13 @@ build = setmetatable {
{"table", tbl}
block_exp: (body) ->
{"block_exp", body}
chain: (parts) ->
base = parts.base or error"expecting base property for chain"
if type(base) == "string"
base = {"ref", base}
node = {"chain", base}
for part in *parts
insert node, part