authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-29 00:31:50-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-03-29 00:31:50-04:00
log86795c03f918dbcc0fc06811cc7c496d275deae1
tree3479a672c77f3f8c02d519287c728a3fe4053679
parent2b0d66736acdd156ec15a983c1f7d51d69c51559
parenta55897106e34c55b02aeb5a5256e7da34f253de5
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4844 from layneson/translatec_string_concat

Support string concatenation in Translate-C

2 files changed, 51 insertions(+), 0 deletions(-)

src-self-hosted/translate_c.zig+12
...@@ -5782,6 +5782,18 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5782,6 +5782,18 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5782 op_id = .Mod;5782 op_id = .Mod;
5783 op_token = try appendToken(c, .Percent, "%");5783 op_token = try appendToken(c, .Percent, "%");
5784 },5784 },
5785 .StringLiteral => {
5786 op_id = .ArrayCat;
5787 op_token = try appendToken(c, .PlusPlus, "++");
5788
5789 _ = it.prev();
5790 },
5791 .Identifier => {
5792 op_id = .ArrayCat;
5793 op_token = try appendToken(c, .PlusPlus, "++");
5794
5795 _ = it.prev();
5796 },
5785 else => {5797 else => {
5786 _ = it.prev();5798 _ = it.prev();
5787 return node;5799 return node;
test/translate_c.zig+39
...@@ -2808,4 +2808,43 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2808,4 +2808,43 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2808 \\ return if (x > y) x else y;2808 \\ return if (x > y) x else y;
2809 \\}2809 \\}
2810 });2810 });
2811
2812 cases.add("string concatenation in macros",
2813 \\#define FOO "hello"
2814 \\#define BAR FOO " world"
2815 \\#define BAZ "oh, " FOO
2816 , &[_][]const u8{
2817 \\pub const FOO = "hello";
2818 ,
2819 \\pub const BAR = FOO ++ " world";
2820 ,
2821 \\pub const BAZ = "oh, " ++ FOO;
2822 });
2823
2824 cases.add("string concatenation in macros: two defines",
2825 \\#define FOO "hello"
2826 \\#define BAZ " world"
2827 \\#define BAR FOO BAZ
2828 , &[_][]const u8{
2829 \\pub const FOO = "hello";
2830 ,
2831 \\pub const BAZ = " world";
2832 ,
2833 \\pub const BAR = FOO ++ BAZ;
2834 });
2835
2836 cases.add("string concatenation in macros: two strings",
2837 \\#define FOO "a" "b"
2838 \\#define BAR FOO "c"
2839 , &[_][]const u8{
2840 \\pub const FOO = "a" ++ "b";
2841 ,
2842 \\pub const BAR = FOO ++ "c";
2843 });
2844
2845 cases.add("string concatenation in macros: three strings",
2846 \\#define FOO "a" "b" "c"
2847 , &[_][]const u8{
2848 \\pub const FOO = "a" ++ ("b" ++ "c");
2849 });
2811}2850}