diff options
Diffstat (limited to 'tests/programs/types.zc')
| -rw-r--r-- | tests/programs/types.zc | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/tests/programs/types.zc b/tests/programs/types.zc index 86488db..5eaa964 100644 --- a/tests/programs/types.zc +++ b/tests/programs/types.zc @@ -1,3 +1,6 @@ +namespace; +include rt, run; + struct point { int x, y; }; struct rect { point a, b; int tag; }; struct list<T> { int len; T* items; }; @@ -5,14 +8,14 @@ struct node { int v; node* next; }; type num = int; int counter = 5; -[4]int squares; +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 = new(list<T>); + list<T>* l = rt.new(list<T>); l.len = 0; - l.items = new(T, cap) as T*; + l.items = rt.new(T, cap) as T*; return u*l; } T first<T>(list<T>* l) { return l.items[0]; } @@ -21,35 +24,35 @@ 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; - put_n(area(&r)); put_c(' '); - put_n(sizeof(rect)); put_c(' '); put_n(sizeof(point)); put_c(' '); put_n(sizeof([3]rect)); put_c(' '); - let p = new(point); // let infers *point + 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; - put_n(p.x * p.y); put_c(' '); - free(d*p); + 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; - put_n(first(l)); put_c(' '); - free(d*l); - node* a = new(node), b = new(node); + 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; - put_n(sum(a)); put_c(' '); - free(d*a); // b went with it: a.next = b made a its owner - [3]point pts; + 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; - put_n(pts[2].y + counter); put_c(' '); + rt.put_n(pts[2].y + counter); rt.put_c(' '); num k = 3; - put_n(k); put_c(10); + rt.put_n(k); rt.put_c(10); // reflection - tprint(typeof(r)); put_c(' '); tprint(typeof(&r)); put_c(' '); tprint(typeof(l)); put_c(' '); - tprint(sigof(mklist)); put_c(' '); tprint(sigof(first)); put_c(' '); tprint(sigofs("free")); put_c(10); + 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 < tnfields(t)) { put_s(tfname(t, i)); put_c(':'); tprint(tftype(t, i)); put_c('@'); put_n(tfoff(t, i)); put_c(' '); i++; } - put_c(10); - point* q = new(point); - tprint(typeof_obj(q)); put_c(' '); put_n(typeof_obj(q) == typeof(point)); put_c(' '); put_n(tkind(typeof_obj(q)) == TY_STRUCT); - free(d*q); - put_c(10); + 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; } |
