aboutsummaryrefslogtreecommitdiff
path: root/docs/history/10-zc/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/10-zc/code/examples
a
Diffstat (limited to 'docs/history/10-zc/code/examples')
-rw-r--r--docs/history/10-zc/code/examples/echo.zc2
-rw-r--r--docs/history/10-zc/code/examples/fact.zc4
-rw-r--r--docs/history/10-zc/code/examples/hello.zc1
-rw-r--r--docs/history/10-zc/code/examples/lists.zc35
-rw-r--r--docs/history/10-zc/code/examples/repl.zc27
-rw-r--r--docs/history/10-zc/code/examples/selfpatch.zc17
6 files changed, 86 insertions, 0 deletions
diff --git a/docs/history/10-zc/code/examples/echo.zc b/docs/history/10-zc/code/examples/echo.zc
new file mode 100644
index 0000000..01f28b0
--- /dev/null
+++ b/docs/history/10-zc/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/10-zc/code/examples/fact.zc b/docs/history/10-zc/code/examples/fact.zc
new file mode 100644
index 0000000..2a218b2
--- /dev/null
+++ b/docs/history/10-zc/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/10-zc/code/examples/hello.zc b/docs/history/10-zc/code/examples/hello.zc
new file mode 100644
index 0000000..f36a810
--- /dev/null
+++ b/docs/history/10-zc/code/examples/hello.zc
@@ -0,0 +1 @@
+fn main() { puts("hello, world!\n"); return 0; }
diff --git a/docs/history/10-zc/code/examples/lists.zc b/docs/history/10-zc/code/examples/lists.zc
new file mode 100644
index 0000000..7392752
--- /dev/null
+++ b/docs/history/10-zc/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 { var b = new(1); b[0] = v; return b as u; }
+fn mklist(cap) -> u { var l = new(cap + 1); l[0] = 0; return l as u; }
+// item arrives as m (it was given down), and goes on as d into the borrowed list
+fn push(m l, d item) { l[l[0] + 1] = item as d; l[0] = l[0] + 1; return 0; }
+fn show(m l)
+{
+ var i = 1;
+ putc('[');
+ while (i <= l[0]) { if (i > 1) putc(' '); putn(l[i][0]); i++; }
+ putc(']');
+ return 0;
+}
+fn squares(n) -> u
+{
+ var l = mklist(n), i = 0;
+ while (i < n) {
+ var b = box(i * i);
+ push(l, b as d);
+ i++;
+ }
+ return l as u;
+}
+fn main()
+{
+ var before = live_objects();
+ {
+ var 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/10-zc/code/examples/repl.zc b/docs/history/10-zc/code/examples/repl.zc
new file mode 100644
index 0000000..49a8c7b
--- /dev/null
+++ b/docs/history/10-zc/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.
+var buf[4000];
+fn readchunk()
+{
+ var 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()
+{
+ var 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(q as d);
+ }
+ return 0;
+}
diff --git a/docs/history/10-zc/code/examples/selfpatch.zc b/docs/history/10-zc/code/examples/selfpatch.zc
new file mode 100644
index 0000000..35a4843
--- /dev/null
+++ b/docs/history/10-zc/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()
+{
+ var p = f;
+ f();
+ while (*p != litw('A')) p++;
+ *p = litw('B');
+ f();
+ *p = litw('C');
+ f();
+ putc(10);
+ return 0;
+}