move extract assign name out, add statement compiles as param

This commit is contained in:
leaf corcoran 2015-02-28 10:30:43 -08:00
parent 163748b8c9
commit bf8de1cf5e
2 changed files with 43 additions and 31 deletions

View File

@ -256,6 +256,7 @@ do
export_all = false, export_all = false,
export_proper = false, export_proper = false,
value_compilers = value_compilers, value_compilers = value_compilers,
statement_compilers = statement_compilers,
__tostring = function(self) __tostring = function(self)
local h local h
if "string" == type(self.header) then if "string" == type(self.header) then
@ -288,6 +289,22 @@ do
end end
end end
end, end,
extract_assign_name = function(self, node)
local is_local = false
local real_name
local _exp_0 = mtype(node)
if LocalName == _exp_0 then
is_local = true
real_name = node:get_name(self)
elseif NameProxy == _exp_0 then
real_name = node:get_name(self)
elseif "table" == _exp_0 then
real_name = node[1] == "ref" and node[2]
elseif "string" == _exp_0 then
real_name = node
end
return real_name, is_local
end,
declare = function(self, names) declare = function(self, names)
local undeclared local undeclared
do do
@ -297,19 +314,7 @@ do
local _continue_0 = false local _continue_0 = false
repeat repeat
local name = names[_index_0] local name = names[_index_0]
local is_local = false local real_name, is_local = self:extract_assign_name(name)
local real_name
local _exp_0 = mtype(name)
if LocalName == _exp_0 then
is_local = true
real_name = name:get_name(self)
elseif NameProxy == _exp_0 then
real_name = name:get_name(self)
elseif "table" == _exp_0 then
real_name = name[1] == "ref" and name[2]
elseif "string" == _exp_0 then
real_name = name
end
if not (is_local or real_name and not self:has_name(real_name, true)) then if not (is_local or real_name and not self:has_name(real_name, true)) then
_continue_0 = true _continue_0 = true
break break
@ -450,7 +455,7 @@ do
end end
end, end,
is_stm = function(self, node) is_stm = function(self, node)
return statement_compilers[ntype(node)] ~= nil return self.statement_compilers[ntype(node)] ~= nil
end, end,
is_value = function(self, node) is_value = function(self, node)
local t = ntype(node) local t = ntype(node)
@ -516,7 +521,7 @@ do
node = self.transform.statement(node) node = self.transform.statement(node)
local result local result
do do
local fn = statement_compilers[ntype(node)] local fn = self.statement_compilers[ntype(node)]
if fn then if fn then
result = fn(self, node, ...) result = fn(self, node, ...)
else else

View File

@ -162,6 +162,7 @@ class Block
export_proper: false export_proper: false
value_compilers: value_compilers value_compilers: value_compilers
statement_compilers: statement_compilers
__tostring: => __tostring: =>
h = if "string" == type @header h = if "string" == type @header
@ -211,22 +212,28 @@ class Block
if fn = @_listeners[name] if fn = @_listeners[name]
fn self, ... fn self, ...
extract_assign_name: (node) =>
is_local = false
real_name = switch mtype node
when LocalName
is_local = true
node\get_name self
when NameProxy
node\get_name self
when "table"
node[1] == "ref" and node[2]
when "string"
-- TOOD: some legacy transfomers might use string for ref
node
real_name, is_local
declare: (names) => declare: (names) =>
undeclared = for name in *names undeclared = for name in *names
is_local = false real_name, is_local = @extract_assign_name name
real_name = switch mtype name
when LocalName
is_local = true
name\get_name self
when NameProxy then name\get_name self
when "table"
name[1] == "ref" and name[2]
when "string"
-- TODO: don't use string literal as ref
name
continue unless is_local or real_name and not @has_name real_name, true continue unless is_local or real_name and not @has_name real_name, true
-- put exported names so they can be assigned to in deeper scope -- this also puts exported names so they can be assigned a new value in
-- deeper scope
@put_name real_name @put_name real_name
continue if @name_exported real_name continue if @name_exported real_name
real_name real_name
@ -322,7 +329,7 @@ class Block
\append ... \append ...
is_stm: (node) => is_stm: (node) =>
statement_compilers[ntype node] != nil @statement_compilers[ntype node] != nil
is_value: (node) => is_value: (node) =>
t = ntype node t = ntype node
@ -369,8 +376,8 @@ class Block
return if not node -- skip blank statements return if not node -- skip blank statements
node = @transform.statement node node = @transform.statement node
result = if fn = statement_compilers[ntype(node)] result = if fn = @statement_compilers[ntype(node)]
fn self, node, ... fn @, node, ...
else else
-- coerce value into statement -- coerce value into statement
if has_value node if has_value node