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" return self.value_compilers[t] ~= nil or t == "value"
end, end,
name = function(self, node, ...) name = function(self, node, ...)
return self:value(node, ...) if type(node) == "string" then
return node
else
return self:value(node, ...)
end
end, end,
value = function(self, node, ...) value = function(self, node, ...)
node = self.transform.value(node) node = self.transform.value(node)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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