harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 5101c9750507c9700bc7787c66831039064dceee
parent 39759083e51f857773fd7c028ddd17afcb2d4c52
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Mon, 11 Jan 2021 14:30:22 -0500

Implement implicit string concatenation

Diffstat:
Msrc/parse.c | 11+++++++++++
Mtests/04-strings.ha | 11+++++++++++
2 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/src/parse.c b/src/parse.c @@ -715,6 +715,17 @@ parse_constant(struct lexer *lexer) case TYPE_STORAGE_STRING: exp->constant.string.len = tok.string.len; exp->constant.string.value = tok.string.value; + while (lex(lexer, &tok) == T_LITERAL + && tok.storage == TYPE_STORAGE_STRING) { + size_t len = exp->constant.string.len; + exp->constant.string.value = xrealloc( + exp->constant.string.value, + len + tok.string.len); + memcpy(exp->constant.string.value + len, + tok.string.value, tok.string.len); + exp->constant.string.len += tok.string.len; + } + unlex(lexer, &tok); break; default: assert(0); // TODO diff --git a/tests/04-strings.ha b/tests/04-strings.ha @@ -38,8 +38,19 @@ fn storage() void = { }; }; +fn concat() void = { + const s = "Hell" "o, " "wor" "ld!"; + const t = s: *const char: *[*]u8; + const expected = ['H', 'e', 'l', 'l', 'o', ',', ' ', + 'w', 'o', 'r', 'l', 'd', '!']; + for (let i = 0z; i < len(expected); i += 1z) { + assert(t[i] == expected[i]: u32: u8); + }; +}; + export fn main() void = { measurements(); charptr(); storage(); + concat(); };