From 872392c1c58999c97cd6a34e2867961c04f08e65 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Mon, 1 Sep 2025 13:10:46 +1000 Subject: [PATCH] Simple escape sequences --- src/main.cpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index a4479db..d9bd7c8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2098,6 +2098,37 @@ Literal exec(vector in, bool executingFunction) { 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 This function takes a string (the user's program) and splits it into smaller chunks @@ -2297,7 +2328,7 @@ vector parser(vector> in) { case Types::String: { Literal newLiteral; - string str = i.substr(1, i.size() - 2); + string str = interpretEscapeSequences(i.substr(1, i.size() - 2)); newLiteral.val = str; newInst.args.push_back(newLiteral); }