Multiline strings work now, both single and triple quotes. Triple quotes also correctly escapes other quotes.

This commit is contained in:
Nathan Braswell
2015-06-19 12:06:27 -04:00
parent 30802fbcf8
commit 3a87970eb3
3 changed files with 48 additions and 1 deletions

View File

@@ -516,7 +516,9 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
}
// The actual passthrough string is the last child now, as we might
// have passthrough_params be the first child
return pre_passthrough + strSlice(generate(children.back(), enclosingObject, justFuncName).oneString(), 3, -4) + post_passthrough;
// we don't generate, as that will escape the returns and we don't want that. We'll just grab the string
//return pre_passthrough + strSlice(generate(children.back(), enclosingObject, justFuncName).oneString(), 3, -4) + post_passthrough;
return pre_passthrough + strSlice(children.back()->getDataRef()->symbol.getName(), 3, -4) + post_passthrough;
}
case function_call:
{
@@ -628,7 +630,24 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
return output;
}
case value:
{
// ok, we now check for it being a string and escape all returns if it is (so that multiline strings work)
if (data.symbol.getName()[0] == '"') {
std::string innerString = strSlice(data.symbol.getName(), 0, 3) == "\"\"\""
? strSlice(data.symbol.getName(), 3, -4)
: strSlice(data.symbol.getName(), 1, -2);
std::string newStr;
for (auto character: innerString)
if (character == '\n')
newStr += "\\n";
else if (character == '"')
newStr += "\\\"";
else
newStr += character;
return "\"" + newStr + "\"";
}
return data.symbol.getName();
}
default:
std::cout << "Nothing!" << std::endl;

View File

@@ -0,0 +1,10 @@
multi
line
first
second
third
How about this?
multi line?
inner quote "

View File

@@ -0,0 +1,18 @@
import io:*
fun main():int {
println("multi
line")
var str = "first
second
third"
println(str)
var tripQute = """
How about this?
multi line?
inner quote "
"""
println(tripQute)
return 0
}