From 050c77c52e3a112170ee0c1ef9043b749aaa3663 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 30 Sep 2012 08:57:11 -0700 Subject: [PATCH] fix bug in string interpolation and add more tests --- moonscript/compile/value.lua | 6 +----- moonscript/compile/value.moon | 5 +---- moonscript/transform.lua | 17 ++++++++++------- moonscript/transform.moon | 13 +++++++++---- tests/inputs/string.moon | 14 ++++---------- tests/outputs/string.lua | 13 +++++-------- 6 files changed, 30 insertions(+), 38 deletions(-) diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 0de729a..fa6839f 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -88,11 +88,7 @@ value_compile = { string = function(self, node) local _, delim, inner = unpack(node) local end_delim = delim:gsub("%[", "]") - if inner then - return delim .. inner .. end_delim - else - return delim .. end_delim - end + return delim .. inner .. end_delim end, chain = function(self, node) local callee = node[2] diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index be2d353..3900907 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -46,10 +46,7 @@ value_compile = string: (node) => _, delim, inner = unpack node end_delim = delim\gsub "%[", "]" - if inner - delim..inner..end_delim - else - delim..end_delim + delim..inner..end_delim chain: (node) => callee = node[2] diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 003aee0..dc508ae 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -1136,16 +1136,10 @@ Value = Transformer({ ["while"] = default_accumulator, foreach = default_accumulator, string = function(self, node) - if #node <= 3 then - return node - end local delim = node[2] local convert_part convert_part = function(part) - if part == nil then - return - end - if type(part) == "string" then + if type(part) == "string" or part == nil then return { "string", delim, @@ -1163,6 +1157,15 @@ Value = Transformer({ }) end end + if #node <= 3 then + return (function() + if type(node[3]) == "string" then + return node + else + return convert_part(node[3]) + end + end)() + end local e = { "exp", convert_part(node[3]) diff --git a/moonscript/transform.moon b/moonscript/transform.moon index ddd28fb..af3fef4 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -594,19 +594,24 @@ Value = Transformer { foreach: default_accumulator string: (node) => - return node if #node <= 3 delim = node[2] convert_part = (part) -> - return if part == nil - if type(part) == "string" + if type(part) == "string" or part == nil {"string", delim, part} else build.chain { base: "tostring", {"call", {part[2]}} } + -- reduced to single item + if #node <= 3 + return if type(node[3]) == "string" + node + else + convert_part node[3] + e = {"exp", convert_part node[3]} - for i=4,#node + for i=4, #node insert e, ".." insert e, convert_part node[i] e diff --git a/tests/inputs/string.moon b/tests/inputs/string.moon index 174ab21..8c12455 100644 --- a/tests/inputs/string.moon +++ b/tests/inputs/string.moon @@ -36,22 +36,16 @@ x = "\"" a = "hello #{hello} hello" b = "#{hello} hello" -c = "hello #{hello}" -d = "" +c = "hello #{5+1}" +d = "#{hello world}" +e = "#{1} #{2} #{3}" -y = "hello" .. "world" - -y = [[hello world]] +f = [[hello #{world} world]] -- a = 'hello #{hello} hello' b = '#{hello} hello' c = 'hello #{hello}' -d = '' - -y = 'hello' .. 'world' - -y = [[hello world]] diff --git a/tests/outputs/string.lua b/tests/outputs/string.lua index fc2e6f3..179ea16 100644 --- a/tests/outputs/string.lua +++ b/tests/outputs/string.lua @@ -18,13 +18,10 @@ x = "\\\n" x = "\"" local a = "hello " .. tostring(hello) .. " hello" local b = tostring(hello) .. " hello" -local c = "hello " .. tostring(hello) -local d = "" -local y = "hello" .. "world" -y = [[hello world]] +local c = "hello " .. tostring(5 + 1) +local d = tostring(hello(world)) +local e = tostring(1) .. " " .. tostring(2) .. " " .. tostring(3) +local f = [[hello #{world} world]] a = 'hello #{hello} hello' b = '#{hello} hello' -c = 'hello #{hello}' -d = '' -y = 'hello' .. 'world' -y = [[hello world]] \ No newline at end of file +c = 'hello #{hello}' \ No newline at end of file