diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-17 15:56:41 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-17 15:56:41 +0000 |
| commit | 567d1c932284bdd53c60937ff6f4ce4a63d104bf (patch) | |
| tree | 7d303ca345ba9de3398275830b22a25b37f81337 | |
| parent | d953e2acec913027c49520f2db5aa6d8b55e9fba (diff) | |
test(extern): call functions from a user C object
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
| -rw-r--r-- | tests/i/extern-c-object/case.json | 5 | ||||
| -rw-r--r-- | tests/i/extern-c-object/expected.txt | 6 | ||||
| -rw-r--r-- | tests/i/extern-c-object/lib.c | 10 | ||||
| -rw-r--r-- | tests/i/extern-c-object/notes.md | 7 | ||||
| -rw-r--r-- | tests/i/extern-c-object/program.cps | 13 |
5 files changed, 41 insertions, 0 deletions
diff --git a/tests/i/extern-c-object/case.json b/tests/i/extern-c-object/case.json new file mode 100644 index 0000000..1ce5970 --- /dev/null +++ b/tests/i/extern-c-object/case.json @@ -0,0 +1,5 @@ +{ + "objects": [ + {"source": "lib.c", "output": "lib.o"} + ] +} diff --git a/tests/i/extern-c-object/expected.txt b/tests/i/extern-c-object/expected.txt new file mode 100644 index 0000000..58ebb6d --- /dev/null +++ b/tests/i/extern-c-object/expected.txt @@ -0,0 +1,6 @@ +6 +10.000000 +9 +1 +2 +from lib.c diff --git a/tests/i/extern-c-object/lib.c b/tests/i/extern-c-object/lib.c new file mode 100644 index 0000000..568133d --- /dev/null +++ b/tests/i/extern-c-object/lib.c @@ -0,0 +1,10 @@ +#include <stdio.h> +#include <string.h> + +static int calls; + +int add3(int a, int b, int c) { return a + b + c; } +double scale(double x, int k) { return x * k; } +int name_length(const char *s) { return (int)strlen(s); } +int next_call(void) { return ++calls; } +void say(const char *s) { puts(s); } diff --git a/tests/i/extern-c-object/notes.md b/tests/i/extern-c-object/notes.md new file mode 100644 index 0000000..51af8cb --- /dev/null +++ b/tests/i/extern-c-object/notes.md @@ -0,0 +1,7 @@ +# extern-c-object + +Human review: pending. Expected output was independently traced by AI, not captured from cpsc. + +Basis: compiler extension (EXTERN "C", INCLUDE_OBJ of a user object); C semantics. + +Trace: the harness compiles `lib.c` with the host C compiler (`objects` in `case.json`). add3(1,2,3)=6. scale(2.5,4)=10.0, printed like other C doubles as `10.000000`. name_length("cambridge")=9. next_call increments a static counter in the object, giving 1 then 2. say calls puts, which prints the string and a newline. diff --git a/tests/i/extern-c-object/program.cps b/tests/i/extern-c-object/program.cps new file mode 100644 index 0000000..5ac34ec --- /dev/null +++ b/tests/i/extern-c-object/program.cps @@ -0,0 +1,13 @@ +// Functions from a user C object built with the host C compiler. +INCLUDE_OBJ "lib.o" +EXTERN "C" EXTERN_FUNCTION add3(C_INT, C_INT, C_INT) RETURNS C_INT +EXTERN "C" EXTERN_FUNCTION scale(C_DOUBLE, C_INT) RETURNS C_DOUBLE +EXTERN "C" EXTERN_FUNCTION name_length(C_STRING) RETURNS C_INT +EXTERN "C" EXTERN_FUNCTION next_call() RETURNS C_INT +EXTERN "C" EXTERN_PROCEDURE say(C_STRING) +OUTPUT add3((C_INT)1, (C_INT)2, (C_INT)3) +OUTPUT scale((C_DOUBLE)2.5, (C_INT)4) +OUTPUT name_length((C_STRING)"cambridge") +OUTPUT next_call() +OUTPUT next_call() +CALL say((C_STRING)"from lib.c") |
