From 1b92ad61f4fb47dc54c19e785e4660ea920ea0c3 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 29 Jul 2025 20:59:30 +0900 Subject: fix: type string literals --- ast.c | 4 ++++ parse.c | 1 + tests/069.sh | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 tests/069.sh diff --git a/ast.c b/ast.c index 0326855..83f65d9 100644 --- a/ast.c +++ b/ast.c @@ -43,6 +43,10 @@ Type* type_new_array(Type* elem, int size) { return ty; } +Type* type_new_static_string(int len) { + return type_new_array(type_new(TypeKind_char), len + 1); +} + Type* type_array_to_ptr(Type* ty) { return type_new_ptr(ty->to); } diff --git a/parse.c b/parse.c index 0b682e2..f61386b 100644 --- a/parse.c +++ b/parse.c @@ -205,6 +205,7 @@ AstNode* parse_primary_expr(Parser* p) { } else if (t->kind == TokenKind_literal_str) { e = ast_new(AstNodeKind_str_expr); e->node_idx = register_str_literal(p, string_to_cstr(&t->raw)); + e->ty = type_new_static_string(t->raw.len); return e; } else if (t->kind == TokenKind_paren_l) { e = parse_expr(p); diff --git a/tests/069.sh b/tests/069.sh new file mode 100644 index 0000000..2a43022 --- /dev/null +++ b/tests/069.sh @@ -0,0 +1,18 @@ +set -e + +cat <<'EOF' > expected +h +l +, +EOF + +bash ../../test_diff.sh <<'EOF' +int printf(); + +int main() { + char* h = " hello,world" + 1; + printf("%c\n", *h); + printf("%c\n", h[2]); + printf("%c\n", *(h + 5)); +} +EOF -- cgit v1.2.3-70-g09d2