From bf8de1cf5eb7450b8363d173da9c986e66b43481 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 10:30:43 -0800 Subject: [PATCH] move extract assign name out, add statement compiles as param --- moonscript/compile.lua | 35 ++++++++++++++++++++--------------- moonscript/compile.moon | 39 +++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 52f3981..5eab8e6 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -256,6 +256,7 @@ do export_all = false, export_proper = false, value_compilers = value_compilers, + statement_compilers = statement_compilers, __tostring = function(self) local h if "string" == type(self.header) then @@ -288,6 +289,22 @@ do 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) local undeclared do @@ -297,19 +314,7 @@ do local _continue_0 = false repeat local name = names[_index_0] - local is_local = false - 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 + local real_name, is_local = self:extract_assign_name(name) if not (is_local or real_name and not self:has_name(real_name, true)) then _continue_0 = true break @@ -450,7 +455,7 @@ do end end, is_stm = function(self, node) - return statement_compilers[ntype(node)] ~= nil + return self.statement_compilers[ntype(node)] ~= nil end, is_value = function(self, node) local t = ntype(node) @@ -516,7 +521,7 @@ do node = self.transform.statement(node) local result do - local fn = statement_compilers[ntype(node)] + local fn = self.statement_compilers[ntype(node)] if fn then result = fn(self, node, ...) else diff --git a/moonscript/compile.moon b/moonscript/compile.moon index af700e1..d05a3bb 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -162,6 +162,7 @@ class Block export_proper: false value_compilers: value_compilers + statement_compilers: statement_compilers __tostring: => h = if "string" == type @header @@ -211,22 +212,28 @@ class Block if fn = @_listeners[name] 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) => undeclared = for name in *names - is_local = false - 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 - + real_name, is_local = @extract_assign_name name 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 continue if @name_exported real_name real_name @@ -322,7 +329,7 @@ class Block \append ... is_stm: (node) => - statement_compilers[ntype node] != nil + @statement_compilers[ntype node] != nil is_value: (node) => t = ntype node @@ -369,8 +376,8 @@ class Block return if not node -- skip blank statements node = @transform.statement node - result = if fn = statement_compilers[ntype(node)] - fn self, node, ... + result = if fn = @statement_compilers[ntype(node)] + fn @, node, ... else -- coerce value into statement if has_value node