diff options
| author | 2026-10-12 08:46:23 +0330 | |
|---|---|---|
| committer | 2026-10-12 08:46:23 +0330 | |
| commit | ecc350ca5c2eb50be8309c5f14f8bf0d87db9a02 (patch) | |
| tree | 70e0af76a1b13affff7c6cc8aac7b3e6ef960478 /tests/programs/cabi.zc | |
a
Diffstat (limited to 'tests/programs/cabi.zc')
| -rw-r--r-- | tests/programs/cabi.zc | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/programs/cabi.zc b/tests/programs/cabi.zc new file mode 100644 index 0000000..6a608ab --- /dev/null +++ b/tests/programs/cabi.zc @@ -0,0 +1,46 @@ +// native calls: extern declares a c function, cfunc hands a zc function to c +extern double sqrt(double); +extern double pow(double x, double y); +extern float sqrtf(float); +extern long c_strlen(str s) = "strlen"; +extern int c_abs(int x) = "abs"; +extern int c_atoi(str s) = "atoi"; +extern void qsort(word *base, long n, long size, void *cmp); +extern int snprintf2(word *buf, long cap, str fmt, long a, double b) = "snprintf"; +extern double hypot(double, double); + +fn put_f(x) { asm { x @ #1220 } return 0; } +fn sp() { put_c(32); return 0; } + +int cmp(int* a, int* b) { return *a - *b; } +// a callback that makes a native call of its own, and an o3 one +int cmpdesc(int* a, int* b) { return c_abs(*b - *a) * (*b - *a) / c_abs(*b - *a + (*b == *a)); } +o3 int cmpo3(int* a, int* b) { int x = *a; int y = *b; if (x < y) return 1; if (x > y) return -1; return 0; } + +fn main() { + let a[6]; + let i = 0; + put_f(sqrt(2.0)); sp(); + put_f(pow(2.0, 10.0)); sp(); + put_f(sqrtf(16.0)); sp(); + put_n(c_strlen(b"hello, world")); sp(); + put_n(c_abs(0 - 42)); sp(); + put_n(c_atoi(b"1234xyz")); sp(); + a[0] = 5; a[1] = 3; a[2] = 9; a[3] = 1; a[4] = 7; a[5] = 2; + qsort(a, 6, 8, cfunc(cmp)); + while (i < 6) { put_n(a[i]); sp(); i++; } + put_c(10); + qsort(a, 6, 8, cfunc(cmpdesc)); + i = 0; while (i < 6) { put_n(a[i]); sp(); i++; } + put_c(10); + qsort(a, 6, 8, cfunc(cmp)); + i = 0; while (i < 6) { put_n(a[i]); sp(); i++; } + qsort(a, 6, 8, cfunc(cmpo3)); + i = 0; while (i < 6) { put_n(a[i]); sp(); i++; } + put_c(10); + put_n(snprintf2(a, 48, b"%ld:%.2f", 7, 2.5)); sp(); + i = 0; while (i < 6) { put_c(ld8(addr_b(a) + i)); i++; } + put_c(10); + put_f(hypot(3.0, 4.0)); put_c(10); + return 0; +} |
