diff options
| author | 2026-10-14 09:48:11 +0000 | |
|---|---|---|
| committer | 2026-10-14 09:48:11 +0000 | |
| commit | 98f9e13829204d32c7dea8a58f8f11efc2a38550 (patch) | |
| tree | b96940376e0f2f306ded70736018afa92f6c0c89 /tests/programs/zc0umd.zc | |
| parent | Merge branch 'worktree-agent-a96a10dc1489688ea' into ccr-0054c02e-dfdvnd (diff) | |
zc0: byte string types, u/m/d as names, enhancements in asm
bstr locals, globals, parameters and returns are byte strings for + , the
comparisons, indexing, slicing, len and put_s; b"..." globals; embed inside
a function. u, m and d are ordinary names: a recast is the letter with the
star touching it (u*x), tags .u .m .d are accepted on types and bare. an
enhancement named inside asm { } emits its opcode. zc0's image of the
genome and language server sources is unchanged.
Diffstat (limited to 'tests/programs/zc0umd.zc')
| -rw-r--r-- | tests/programs/zc0umd.zc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/programs/zc0umd.zc b/tests/programs/zc0umd.zc new file mode 100644 index 0000000..9693111 --- /dev/null +++ b/tests/programs/zc0umd.zc @@ -0,0 +1,58 @@ +namespace; +include rt; + +// u, m and d are ordinary names, and a tag is written .u, .m or .d after a type. +// u*x, m*x and d*x with no space before the star is a recast, and u * x with a +// space is a product. the same program runs on the resident compiler and, built +// into a run-only image, on stage 0 (src/tools/zc0.c), which reads the tags and +// recasts and does nothing with them. + +int u = 2, m = 3; // globals +int[4] d4; +int d(int x) { return x + 1; } // a function + +int mul3(int u, int m, int d) { return u * m * d; } // parameters +int mixed(int.d u, int m, int d) { return u * m - d; } +int tagged(int(int) f, int.m x) { return f(x); } + +// owned objects through locals named for their tags +word*.u box(int v) { word* m = rt.new(2); m[0] = v; m[1] = v * 2; return u*m; } +word*.u pair(int u, int m) { + word* d = rt.new(2); + d[0] = u; d[1] = m; + return u*d; +} +.u single(int v) { let u = rt.new(1); u[0] = v; return u*u; } +int sum(word*.m m) { return m[0] + m[1]; } +int eat(word*.d d) { return d[0] + d[1]; } +int hold(.m u, .d d) { return u[0] * 100 + d[0]; } +int dig(word*.m u) { int m = u[0], d = u[1]; return m * 10 + d; } + +fn main() { + int a = 4, b = 5; + int.m n = 9; + word*.m p = box(7); + .m q = single(6); + word* r = pair(8, 9); + let c = box(10), e = pair(1, 2); + rt.put_n(u * m); rt.put_c(32); // two globals multiplied + rt.put_n(u*=m); rt.put_c(32); // u *= m is not a recast either + rt.put_n(d(u) * d(m)); rt.put_c(32); + rt.put_n(mul3(2, 3, 4)); rt.put_c(32); + rt.put_n(mixed(7, 6, 5)); rt.put_c(32); + rt.put_n(tagged(d, n)); rt.put_c(10); + { + int u = 6, m = 7, d = 8; // locals shadow the globals + rt.put_n(u * m); rt.put_c(32); + rt.put_n(u * d - m); rt.put_c(32); + rt.put_n(m * u * d); rt.put_c(32); + rt.put_n(a * u + m * b); rt.put_c(10); + } + rt.put_n(sum(p)); rt.put_c(32); + rt.put_n(q[0]); rt.put_c(32); + rt.put_n(r[0] * r[1]); rt.put_c(32); + rt.put_n(dig(c)); rt.put_c(32); + rt.put_n(hold(e, single(5))); rt.put_c(32); + rt.put_n(eat(d*p)); rt.put_c(10); + return 0; +} |
