aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/types.zc
blob: 5eaa964f10de2c9bb76d2ce0cebd51910667f496 (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
50
51
52
53
54
55
56
57
58
namespace;
include rt, run;

struct point { int x, y; };
struct rect { point a, b; int tag; };
struct list<T> { int len; T* items; };
struct node { int v; node* next; };
type num = int;

int counter = 5;
int[4] squares;
point origin;

int area(rect* r) { return (r.b.x - r.a.x) * (r.b.y - r.a.y); }
list<T>*.u mklist<T>(int cap) {
	list<T>* l = rt.new(list<T>);
	l.len = 0;
	l.items = rt.new(T, cap) as T*;
	return u*l;
}
T first<T>(list<T>* l) { return l.items[0]; }
int sum(node* n) { int s = 0; while (n) { s += n.v; n = n.next; } return s; }

fn main() {
	rect r;
	r.a.x = 1; r.a.y = 2; r.b.x = 4; r.b.y = 6;
	rt.put_n(area(&r)); rt.put_c(' ');
	rt.put_n(sizeof(rect)); rt.put_c(' '); rt.put_n(sizeof(point)); rt.put_c(' '); rt.put_n(sizeof(rect[3])); rt.put_c(' ');
	let p = rt.new(point);	// let infers *point
	p.x = 7; p->y = 8;
	rt.put_n(p.x * p.y); rt.put_c(' ');
	rt.free(d*p);
	list<int>* l = mklist<int>(4);
	l.items[0] = 42; l.len = 1;
	rt.put_n(first(l)); rt.put_c(' ');
	rt.free(d*l);
	node* a = rt.new(node), b = rt.new(node);
	a.v = 3; b.v = 4; a.next = b;
	rt.put_n(sum(a)); rt.put_c(' ');
	rt.free(d*a);	// b went with it: a.next = b made a its owner
	point[3] pts;
	pts[2].y = 9;
	rt.put_n(pts[2].y + counter); rt.put_c(' ');
	num k = 3;
	rt.put_n(k); rt.put_c(10);

	// reflection
	run.tprint(typeof(r)); rt.put_c(' '); run.tprint(typeof(&r)); rt.put_c(' '); run.tprint(typeof(l)); rt.put_c(' ');
	run.tprint(run.sigof(mklist)); rt.put_c(' '); run.tprint(run.sigof(first)); rt.put_c(' '); run.tprint(run.sigofs("free")); rt.put_c(10);
	let t = typeof(r), i = 0;
	while (i < run.tnfields(t)) { rt.put_s(run.tfname(t, i)); rt.put_c(':'); run.tprint(run.tftype(t, i)); rt.put_c('@'); rt.put_n(run.tfoff(t, i)); rt.put_c(' '); i++; }
	rt.put_c(10);
	point* q = rt.new(point);
	run.tprint(rt.typeof_obj(q)); rt.put_c(' '); rt.put_n(rt.typeof_obj(q) == typeof(point)); rt.put_c(' '); rt.put_n(run.tkind(rt.typeof_obj(q)) == run.TY_STRUCT);
	rt.free(d*q);
	rt.put_c(10);
	return 0;
}