diff --git a/.gitignore b/.gitignore index d59432d8..5ebf76d4 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,9 @@ *.suo *.user +# IDEA +.idea/ + # Build directories CrazyDebug*/ Debug*/ diff --git a/compiler/lexer.cpp b/compiler/lexer.cpp index 7d8e19c4..a3eedbe5 100644 --- a/compiler/lexer.cpp +++ b/compiler/lexer.cpp @@ -33,6 +33,8 @@ #include #include #include +#include +#include #if defined __linux__ || defined __FreeBSD__ || defined __OpenBSD__ || defined DARWIN # include @@ -2571,8 +2573,13 @@ std::string Lexer::PerformMacroSubstitution(MacroEntry* macro, out.push_back(substitute[pos + 1]); continue; } - if (stringize) - out += '"' + iter->second + '"'; + if (stringize) { + auto arg_macro = FindMacro(cc_.atom(iter->second)); + if (arg_macro != nullptr && !arg_macro->args) + out += QuoteString(arg_macro->substitute->str()); + else + out += QuoteString(iter->second); + } else out += iter->second; } @@ -2654,4 +2661,10 @@ void Lexer::DiscardCachedTokens() { injected_token_stream_.clear(); } +std::string QuoteString(const std::string& string) { + std::stringstream ss; + ss << std::quoted(string); + return ss.str(); +} + } // namespace sp diff --git a/compiler/lexer.h b/compiler/lexer.h index 31269ebd..cb2faa4c 100644 --- a/compiler/lexer.h +++ b/compiler/lexer.h @@ -533,5 +533,6 @@ class Lexer }; std::string StringizePath(const std::filesystem::path& in_path); +std::string QuoteString(const std::string& string); } // namespace sp diff --git a/tests/macros/stringification.out b/tests/macros/stringification.out new file mode 100644 index 00000000..9ae7a7d1 --- /dev/null +++ b/tests/macros/stringification.out @@ -0,0 +1,6 @@ +ab +"ab" +C3 +C3() +C4(15) +UNDEFINED \ No newline at end of file diff --git a/tests/macros/stringification.sp b/tests/macros/stringification.sp new file mode 100644 index 00000000..516bd638 --- /dev/null +++ b/tests/macros/stringification.sp @@ -0,0 +1,27 @@ +#include + +#define TO_STR(%0) #%0 + +#define C1 ab +#define C2 "ab" +#define C3() cd +#define C4(%0) %0 + +public main() { + print(TO_STR(C1)); + print("\n"); + + print(TO_STR(C2)); + print("\n"); + + print(TO_STR(C3)); + print("\n"); + + print(TO_STR(C3())); + print("\n"); + + print(TO_STR(C4(15))); + print("\n"); + + print(TO_STR(UNDEFINED)); +} \ No newline at end of file