diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-05-04 20:48:41 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-05-04 20:48:41 +0900 |
| commit | 6c12928c82d694b7b7b96a7b08d61d211f966867 (patch) | |
| tree | d00076a18a30b2b4f61341d7637f8c1f52905af6 | |
| parent | 4126ae8633a1bb495df28591ebf67f8f7d2c28cc (diff) | |
| download | P4Dcc-6c12928c82d694b7b7b96a7b08d61d211f966867.tar.gz P4Dcc-6c12928c82d694b7b7b96a7b08d61d211f966867.tar.zst P4Dcc-6c12928c82d694b7b7b96a7b08d61d211f966867.zip | |
array subscript
| -rw-r--r-- | main.c | 12 | ||||
| -rw-r--r-- | tests/033.sh | 46 |
2 files changed, 58 insertions, 0 deletions
@@ -770,6 +770,18 @@ struct AstNode* parse_postfix_expr(struct Parser* p) { struct AstNode* args = parse_arg_list(p); expect(p, TK_PAREN_R); ret->expr1 = args; + } else if (tk == TK_BRACKET_L) { + next_token(p); + struct AstNode* idx = parse_expr(p); + expect(p, TK_BRACKET_R); + + struct AstNode* e = ast_new(AST_DEREF_EXPR); + struct AstNode* ptr_expr = ast_new_binary_expr(TK_PLUS, ret, idx); + ptr_expr->ty = ret->ty; + e->expr1 = ptr_expr; + e->ty = ret->ty->to; + + ret = e; } else if (tk == TK_DOT) { next_token(p); char* name = parse_ident(p); diff --git a/tests/033.sh b/tests/033.sh new file mode 100644 index 0000000..1fb92ac --- /dev/null +++ b/tests/033.sh @@ -0,0 +1,46 @@ +set -e + +cat <<'EOF' > expected +42 +EOF +bash ../../test_diff.sh <<'EOF' +int printf(); +void* calloc(); + +int main() { + int b; + int* a = &b; + a[0] = 42; + printf("%d\n", *a); + return 0; +} +EOF + +cat <<'EOF' > expected +0 0 +1 1 +2 2 +3 3 +4 4 +5 5 +6 6 +7 7 +8 8 +9 9 +EOF +bash ../../test_diff.sh <<'EOF' +int printf(); +void* calloc(); + +int main() { + long* a = calloc(10, sizeof(long)); + long i = 0; + for (i = 0; i < 10; i = i + 1) { + a[i] = i; + } + for (i = 0; i < 10; i = i + 1) { + printf("%d %d\n", *(a + i), a[i]); + } + return 0; +} +EOF |
