diff options
Diffstat (limited to 'docs/history/10-zc/code/examples')
| -rw-r--r-- | docs/history/10-zc/code/examples/echo.zc | 2 | ||||
| -rw-r--r-- | docs/history/10-zc/code/examples/fact.zc | 4 | ||||
| -rw-r--r-- | docs/history/10-zc/code/examples/hello.zc | 1 | ||||
| -rw-r--r-- | docs/history/10-zc/code/examples/lists.zc | 35 | ||||
| -rw-r--r-- | docs/history/10-zc/code/examples/repl.zc | 27 | ||||
| -rw-r--r-- | docs/history/10-zc/code/examples/selfpatch.zc | 17 |
6 files changed, 0 insertions, 86 deletions
diff --git a/docs/history/10-zc/code/examples/echo.zc b/docs/history/10-zc/code/examples/echo.zc deleted file mode 100644 index 01f28b0..0000000 --- a/docs/history/10-zc/code/examples/echo.zc +++ /dev/null @@ -1,2 +0,0 @@ -// 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 deleted file mode 100644 index 2a218b2..0000000 --- a/docs/history/10-zc/code/examples/fact.zc +++ /dev/null @@ -1,4 +0,0 @@ -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 deleted file mode 100644 index f36a810..0000000 --- a/docs/history/10-zc/code/examples/hello.zc +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index 7392752..0000000 --- a/docs/history/10-zc/code/examples/lists.zc +++ /dev/null @@ -1,35 +0,0 @@ -// 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 deleted file mode 100644 index 49a8c7b..0000000 --- a/docs/history/10-zc/code/examples/repl.zc +++ /dev/null @@ -1,27 +0,0 @@ -// 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 deleted file mode 100644 index 35a4843..0000000 --- a/docs/history/10-zc/code/examples/selfpatch.zc +++ /dev/null @@ -1,17 +0,0 @@ -// 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; -} |
