diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-02-07 15:42:00 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-02-07 17:33:55 +0900 |
| commit | cdddf2422553f1f21c8d2c57cd382b8362dc80fb (patch) | |
| tree | 3053026498552aecda2e4889c8455298ad70c8cb /tests | |
| parent | e1042a6373773830297dfd5718938c12f21ae624 (diff) | |
| download | ducc-cdddf2422553f1f21c8d2c57cd382b8362dc80fb.tar.gz ducc-cdddf2422553f1f21c8d2c57cd382b8362dc80fb.tar.zst ducc-cdddf2422553f1f21c8d2c57cd382b8362dc80fb.zip | |
feat: support function calls via function pointers
The two-pass parsing of function pointer declaration is referenced from chibicc:
https://github.com/rui314/chibicc
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/cli.sh | 3 | ||||
| -rw-r--r-- | tests/functions.c | 18 |
2 files changed, 20 insertions, 1 deletions
diff --git a/tests/cli.sh b/tests/cli.sh index fc8a481..2c6ac72 100644 --- a/tests/cli.sh +++ b/tests/cli.sh @@ -45,8 +45,9 @@ int main(int argc, char** argv) { EOF # compile errors +# TODO: report as "undefined function" cat <<'EOF' > expected -main.c:2: undefined function: f +main.c:2: undefined variable: f EOF test_compile_error <<'EOF' int main() { diff --git a/tests/functions.c b/tests/functions.c index b5ffecb..eef9496 100644 --- a/tests/functions.c +++ b/tests/functions.c @@ -95,6 +95,10 @@ int f9(int select, int a, int b, int c, int d, S e, int f, int g) { } } +int f10() { + return 12345; +} + // recursive functions int fib(int n) { if (n <= 1) { @@ -136,6 +140,20 @@ int main() { ASSERT_EQ(7, f9(6, 1, 2, 3, 4, s, 7, 8)); ASSERT_EQ(8, f9(7, 1, 2, 3, 4, s, 7, 8)); + // function pointers + ASSERT_EQ(12345, (f10)()); + ASSERT_EQ(12345, (*f10)()); + ASSERT_EQ(12345, (**f10)()); + + int (*fp1)() = f10; + ASSERT_EQ(12345, fp1()); + int (*fp2)(int, int, int, int, int, int) = f; + ASSERT_EQ(1, fp2(1, 2, 3, 4, 5, 6)); + int (*fp3)(int, int, int, int, int, int) = f6; + ASSERT_EQ(6, fp3(1, 2, 3, 4, 5, 6)); + int (*fp4)(int, int, int, int, int, int, int, int, int, int, int) = f7; + ASSERT_EQ(7, fp4(6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + // recursive functions ASSERT_EQ(89, fib(10)); } |
