diff options
Diffstat (limited to 'examples/typed.zc')
| -rw-r--r-- | examples/typed.zc | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/examples/typed.zc b/examples/typed.zc index 44aff88..340b6d2 100644 --- a/examples/typed.zc +++ b/examples/typed.zc @@ -1,51 +1,54 @@ +namespace; +include rt, run; + // types: structs, a generic container, and looking at it all through reflection. -// a pointer type put_s its star after the type, like c: int*, point*, vec<T>*. +// a pointer type has its star after the type, like c: int*, point*, vec<T>*. typecheck on; struct point { int x, y; }; struct vec<T> { int len, cap; T* items; }; vec<T>*.u mkvec<T>(int cap) { - vec<T>* v = new(vec<T>); + vec<T>* v = rt.new(vec<T>); v.cap = cap; - v.items = new(T, cap); - return u*v; + v.items = rt.new(T, cap); + return v; } // x is given away (.d): the vector's memory holds it from now on, and the // collector looks after it bool push<T>(vec<T>* v, T.d x) { - if (v.len == v.cap) { free(d*x); return 0; } + if (v.len == v.cap) { rt.free(x); return 0; } v.items[v.len] = d*x; v.len++; return 1; } -fn show(word t) { +void show(word t) { let i = 0; - tprint(t); put_s(" is "); put_n(tsize(t)); put_s(" words: "); - while (i < tnfields(t)) { - if (i) put_s(", "); - put_s(tfname(t, i)); put_s(" "); tprint(tftype(t, i)); + run.tprint(t); rt.put_s(" is "); rt.put_n(run.tsize(t)); rt.put_s(" words: "); + while (i < run.tnfields(t)) { + if (i) rt.put_s(", "); + rt.put_s(run.tfname(t, i)); rt.put_s(" "); run.tprint(run.tftype(t, i)); i++; } - put_c(10); - return 0; + rt.put_c(10); + return; } int main() { vec<point*>* v = mkvec<point*>(4); int i = 0; while (i < 3) { - point* p = new(point); + point* p = rt.new(point); p.x = i; p.y = i * i; - push(v, d*p); + push(v, p); i++; } - put_s("last: "); put_n(v.items[2].y); put_c(10); + rt.put_s("last: "); rt.put_n(v.items[2].y); rt.put_c(10); show(typeof(point)); show(typeof(*v)); - put_s("push is "); tprint(sigof(push)); put_c(10); + rt.put_s("push is "); run.tprint(run.sigof(push)); rt.put_c(10); // each set of type arguments gets its own copy of mkvec, so what it stamped is vec<point*> - put_s("the heap says v is a "); tprint(typeof_obj(v)); put_s(", and v.items[0] is a "); tprint(typeof_obj(v.items[0])); put_c(10); + rt.put_s("the heap says v is a "); run.tprint(rt.typeof_obj(v)); rt.put_s(", and v.items[0] is a "); run.tprint(rt.typeof_obj(v.items[0])); rt.put_c(10); return 0; } |
