Fix multiline strings with single quotes in Cephelapod and implement both types in Kalypso, one new test should pass for both
This commit is contained in:
@@ -1066,13 +1066,16 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
||||
case value:
|
||||
{
|
||||
// ok, we now check for it being a multiline string and escape all returns if it is (so that multiline strings work)
|
||||
if (data.symbol.getName()[0] == '"' && strSlice(data.symbol.getName(), 0, 3) == "\"\"\"") {
|
||||
std::string innerString = strSlice(data.symbol.getName(), 3, -4);
|
||||
if (data.symbol.getName()[0] == '"') {
|
||||
std::string innerString = strSlice(data.symbol.getName(), 1, -2);
|
||||
bool triple = strSlice(data.symbol.getName(), 0, 3) == "\"\"\"";
|
||||
if (triple)
|
||||
innerString = strSlice(data.symbol.getName(), 3, -4);
|
||||
std::string newStr;
|
||||
for (auto character: innerString)
|
||||
if (character == '\n')
|
||||
newStr += "\\n";
|
||||
else if (character == '"')
|
||||
else if (triple && character == '"')
|
||||
newStr += "\\\"";
|
||||
else
|
||||
newStr += character;
|
||||
|
||||
@@ -306,7 +306,25 @@ obj c_generator (Object) {
|
||||
defer_stack->top().second.push(node->defer_statement.statement)
|
||||
return code_triple("/*defer wanna know what*/")
|
||||
}
|
||||
fun generate_value(node: *ast_node): code_triple return code_triple(node->value.string_value);
|
||||
fun generate_value(node: *ast_node): code_triple {
|
||||
var value = node->value.string_value
|
||||
if (value[0] != '"')
|
||||
return code_triple(value);
|
||||
var to_ret = string("\"")
|
||||
if (value.slice(0,3) == "\"\"\"")
|
||||
value = value.slice(3,-4)
|
||||
else
|
||||
value = value.slice(1,-2)
|
||||
value.for_each(fun(c: char) {
|
||||
if (c == '\n')
|
||||
to_ret += "\\n"
|
||||
else if (c == '"')
|
||||
to_ret += "\\\""
|
||||
else
|
||||
to_ret += c
|
||||
})
|
||||
return code_triple(to_ret + "\"")
|
||||
}
|
||||
fun generate_code_block(node: *ast_node, enclosing_object: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
|
||||
var to_ret = code_triple("{\n")
|
||||
// stick another stack on
|
||||
|
||||
@@ -181,5 +181,8 @@ obj string (Object, Serializable) {
|
||||
out.add(current)
|
||||
return out
|
||||
}
|
||||
fun for_each(func: fun(char):void) {
|
||||
data.for_each(func)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user