fix bug in string interpolation and add more tests

This commit is contained in:
leaf corcoran 2012-09-30 08:57:11 -07:00
parent 8b2fa75e4c
commit 050c77c52e
6 changed files with 30 additions and 38 deletions

View File

@ -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]

View File

@ -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]

View File

@ -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])

View File

@ -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

View File

@ -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]]

View File

@ -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]]
c = 'hello #{hello}'