aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/cabi.zc
blob: da9518b88a7105b8097a461f23ef0d2e1de472da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace;
include rt;

// 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() { rt.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();
	rt.put_n(c_strlen(b"hello, world")); sp();
	rt.put_n(c_abs(0 - 42)); sp();
	rt.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) { rt.put_n(a[i]); sp(); i++; }
	rt.put_c(10);
	qsort(a, 6, 8, cfunc(cmpdesc));
	i = 0; while (i < 6) { rt.put_n(a[i]); sp(); i++; }
	rt.put_c(10);
	qsort(a, 6, 8, cfunc(cmp));
	i = 0; while (i < 6) { rt.put_n(a[i]); sp(); i++; }
	qsort(a, 6, 8, cfunc(cmpo3));
	i = 0; while (i < 6) { rt.put_n(a[i]); sp(); i++; }
	rt.put_c(10);
	rt.put_n(snprintf2(a, 48, b"%ld:%.2f", 7, 2.5)); sp();
	i = 0; while (i < 6) { rt.put_c(rt.ld8(rt.addr_b(a) + i)); i++; }
	rt.put_c(10);
	put_f(hypot(3.0, 4.0)); rt.put_c(10);
	return 0;
}