aboutsummaryrefslogtreecommitdiff
path: root/docs/history/13-gap-fixes/code/examples
diff options
context:
space:
mode:
authorGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
committerGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
commitecc350ca5c2eb50be8309c5f14f8bf0d87db9a02 (patch)
tree70e0af76a1b13affff7c6cc8aac7b3e6ef960478 /docs/history/13-gap-fixes/code/examples
a
Diffstat (limited to 'docs/history/13-gap-fixes/code/examples')
-rw-r--r--docs/history/13-gap-fixes/code/examples/echo.zc2
-rw-r--r--docs/history/13-gap-fixes/code/examples/fact.zc4
-rw-r--r--docs/history/13-gap-fixes/code/examples/hello.zc1
-rw-r--r--docs/history/13-gap-fixes/code/examples/lists.zc35
-rw-r--r--docs/history/13-gap-fixes/code/examples/repl.zc27
-rw-r--r--docs/history/13-gap-fixes/code/examples/selfpatch.zc17
-rw-r--r--docs/history/13-gap-fixes/code/examples/typed.zc47
-rw-r--r--docs/history/13-gap-fixes/code/examples/wiring.zc19
8 files changed, 152 insertions, 0 deletions
diff --git a/docs/history/13-gap-fixes/code/examples/echo.zc b/docs/history/13-gap-fixes/code/examples/echo.zc
new file mode 100644
index 0000000..01f28b0
--- /dev/null
+++ b/docs/history/13-gap-fixes/code/examples/echo.zc
@@ -0,0 +1,2 @@
+// everything after the backtick is left unread, for the program itself
+fn main() { while (!eof()) putc(getc()); return 0; }
diff --git a/docs/history/13-gap-fixes/code/examples/fact.zc b/docs/history/13-gap-fixes/code/examples/fact.zc
new file mode 100644
index 0000000..2a218b2
--- /dev/null
+++ b/docs/history/13-gap-fixes/code/examples/fact.zc
@@ -0,0 +1,4 @@
+fn fact(n) { if (n < 2) return 1; return n * fact(n - 1); }
+// prints in binary, like the old asm example
+fn bin(n) { if (n > 1) bin(n >> 1); putc('0' + (n & 1)); return 0; }
+fn main() { bin(fact(10)); putc(10); putn(fact(10)); putc(10); return 0; }
diff --git a/docs/history/13-gap-fixes/code/examples/hello.zc b/docs/history/13-gap-fixes/code/examples/hello.zc
new file mode 100644
index 0000000..f36a810
--- /dev/null
+++ b/docs/history/13-gap-fixes/code/examples/hello.zc
@@ -0,0 +1 @@
+fn main() { puts("hello, world!\n"); return 0; }
diff --git a/docs/history/13-gap-fixes/code/examples/lists.zc b/docs/history/13-gap-fixes/code/examples/lists.zc
new file mode 100644
index 0000000..6ac276e
--- /dev/null
+++ b/docs/history/13-gap-fixes/code/examples/lists.zc
@@ -0,0 +1,35 @@
+// ownership in practice: a list owns its boxes. nothing here calls free;
+// everything is released at the end of the block that owns it.
+fn box(v) -> .u { let b = new(1); b[0] = v; return u*b; }
+fn mklist(cap) -> .u { let l = new(cap + 1); l[0] = 0; return u*l; }
+// item m*arrives (it was given down), and goes d*on into the borrowed list
+fn push(.m l, .d item) { l[l[0] + 1] = d*item; l[0] = l[0] + 1; return 0; }
+fn show(.m l)
+{
+ let i = 1;
+ putc('[');
+ while (i <= l[0]) { if (i > 1) putc(' '); putn(l[i][0]); i++; }
+ putc(']');
+ return 0;
+}
+fn squares(n) -> .u
+{
+ let l = mklist(n), i = 0;
+ while (i < n) {
+ let b = box(i * i);
+ push(l, d*b);
+ i++;
+ }
+ return u*l;
+}
+fn main()
+{
+ let before = live_objects();
+ {
+ let l = squares(6);
+ show(l); putc(10);
+ puts("live: "); putn(live_objects() - before); putc(10);
+ }
+ puts("after the block: "); putn(live_objects() - before); putc(10);
+ return 0;
+}
diff --git a/docs/history/13-gap-fixes/code/examples/repl.zc b/docs/history/13-gap-fixes/code/examples/repl.zc
new file mode 100644
index 0000000..b7b81b8
--- /dev/null
+++ b/docs/history/13-gap-fixes/code/examples/repl.zc
@@ -0,0 +1,27 @@
+// a repl on the resident compiler: each backtick-separated chunk of input is
+// compiled as a function body and run. definitions (fn ...) can be loaded too:
+// a chunk that starts with "fn" goes through load instead of eval.
+let buf[4000];
+fn readchunk()
+{
+ let n = 0, c;
+ while (!eof()) {
+ c = getc();
+ if (c == 96) break;
+ if (n < 3999) { buf[n] = c; n++; }
+ }
+ buf[n] = 0;
+ return n;
+}
+fn main()
+{
+ let q, i;
+ while (!eof()) {
+ readchunk();
+ q = tokens(buf);
+ if (q[0] && q[1] == T_ID && q[2] == intern("fn")) load(q);
+ else eval(q);
+ free(d*q);
+ }
+ return 0;
+}
diff --git a/docs/history/13-gap-fixes/code/examples/selfpatch.zc b/docs/history/13-gap-fixes/code/examples/selfpatch.zc
new file mode 100644
index 0000000..074ccec
--- /dev/null
+++ b/docs/history/13-gap-fixes/code/examples/selfpatch.zc
@@ -0,0 +1,17 @@
+// code is data: find the literal 'A' inside f's compiled words and rewrite it.
+// on the jit the store lands on translated code, which gets dropped and
+// retranslated on the next call.
+fn f() { putc('A'); return 0; }
+fn litw(v) { return v + v + 1; } // how the vm encodes a literal push
+fn main()
+{
+ let p = f;
+ f();
+ while (*p != litw('A')) p++;
+ *p = litw('B');
+ f();
+ *p = litw('C');
+ f();
+ putc(10);
+ return 0;
+}
diff --git a/docs/history/13-gap-fixes/code/examples/typed.zc b/docs/history/13-gap-fixes/code/examples/typed.zc
new file mode 100644
index 0000000..d91fe6a
--- /dev/null
+++ b/docs/history/13-gap-fixes/code/examples/typed.zc
@@ -0,0 +1,47 @@
+// types: structs, a generic container, and looking at it all through reflection
+typecheck on;
+struct point { int x, y; };
+struct vec<T> { int len, cap; *T items; };
+
+fn mkvec<T>(int cap) -> *vec<T>.u
+{
+ *vec<T> v = new(vec<T>);
+ v.cap = cap;
+ v.items = new(T, cap);
+ return u*v;
+}
+// x is given away (.d): the vector's memory holds it from now on, and the
+// collector looks after it
+fn push<T>(*vec<T> v, T.d x) -> bool
+{
+ if (v.len == v.cap) { free(d*x); return 0; }
+ v.items[v.len] = d*x;
+ v.len++;
+ return 1;
+}
+fn show(word t)
+{
+ let i = 0;
+ tprint(t); puts(" is "); putn(tsize(t)); puts(" words: ");
+ while (i < tnfields(t)) { if (i) puts(", "); puts(tfname(t, i)); puts(" "); tprint(tftype(t, i)); i++; }
+ putc(10);
+ return 0;
+}
+fn main() -> int
+{
+ *vec<*point> v = mkvec<*point>(4);
+ int i = 0;
+ while (i < 3) {
+ *point p = new(point);
+ p.x = i; p.y = i * i;
+ push(v, d*p);
+ i++;
+ }
+ puts("last: "); putn(v.items[2].y); putc(10);
+ show(typeof(point));
+ show(typeof(*v));
+ puts("push is "); tprint(sigof(push)); putc(10);
+ // generic code is compiled once, so what mkvec stamped is vec<T>
+ puts("the heap says v is a "); tprint(typeof_obj(v)); puts(", and v.items[0] is a "); tprint(typeof_obj(v.items[0])); putc(10);
+ return 0;
+}
diff --git a/docs/history/13-gap-fixes/code/examples/wiring.zc b/docs/history/13-gap-fixes/code/examples/wiring.zc
new file mode 100644
index 0000000..ed30935
--- /dev/null
+++ b/docs/history/13-gap-fixes/code/examples/wiring.zc
@@ -0,0 +1,19 @@
+// virtual enhancements: custom opcodes wired into the cpu while compiling.
+// the recipes run through the core's optimizer; the jit inlines the result.
+enhance popcount(x) micro
+ "r1 = shr x 1; r1 = and r1 0x55555555; x = sub x r1"
+ "; r1 = shr x 2; r1 = and r1 0x33333333; x = and x 0x33333333; x = add x r1"
+ "; r1 = shr x 4; x = add x r1; x = and x 0x0f0f0f0f; x = mul x 0x01010101; x = shr x 24";
+enhance div10(x) micro "x = udiv x 10"; // becomes a multiply-high, no divide
+enhance hypot2(a, b) vm { 0 $ * ~ 0 $ * + }; // lifted from vm code
+enhance minmax(a, b) -> 2 micro "r2 = min a b; r1 = max a b; r0 = r2";
+let lo, hi;
+fn main()
+{
+ putn(popcount(0xff00ff)); putc(' ');
+ putn(div10(12345)); putc(' ');
+ putn(hypot2(3, 4)); putc(' ');
+ asm { 9 4 minmax hi ! lo ! }
+ putn(lo); putc('/'); putn(hi); putc(10);
+ return 0;
+}