Simple escape sequences

This commit is contained in:
2025-09-01 13:10:46 +10:00
parent 2e1e2e727b
commit 872392c1c5

View File

@@ -2098,6 +2098,37 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
return retLiteral; return retLiteral;
} }
string interpretEscapeSequences(string input) {
string output;
for (size_t i = 0; i < input.length(); ++i) {
if (input[i] == '\\' && i + 1 < input.length()) {
char next = input[i + 1];
switch (next) {
case 'n': output += '\n'; break;
case 't': output += '\t'; break;
case 'r': output += '\r'; break;
case 'b': output += '\b'; break;
case 'f': output += '\f'; break;
case 'a': output += '\a'; break;
case 'v': output += '\v'; break;
case '\\': output += '\\'; break;
case '\'': output += '\''; break;
case '\"': output += '\"'; break;
case '0': output += '\0'; break;
default:
output += '\\';
output += next;
break;
}
++i;
} else {
output += input[i];
}
}
return output;
}
/* /*
lexer function lexer function
This function takes a string (the user's program) and splits it into smaller chunks This function takes a string (the user's program) and splits it into smaller chunks
@@ -2297,7 +2328,7 @@ vector<Instruction> parser(vector<vector<string>> in) {
case Types::String: case Types::String:
{ {
Literal newLiteral; Literal newLiteral;
string str = i.substr(1, i.size() - 2); string str = interpretEscapeSequences(i.substr(1, i.size() - 2));
newLiteral.val = str; newLiteral.val = str;
newInst.args.push_back(newLiteral); newInst.args.push_back(newLiteral);
} }