diff options
| author | 2026-10-12 08:46:23 +0330 | |
|---|---|---|
| committer | 2026-10-12 08:46:23 +0330 | |
| commit | ecc350ca5c2eb50be8309c5f14f8bf0d87db9a02 (patch) | |
| tree | 70e0af76a1b13affff7c6cc8aac7b3e6ef960478 /examples/learn | |
a
Diffstat (limited to 'examples/learn')
52 files changed, 245 insertions, 0 deletions
diff --git a/examples/learn/01-hello.out b/examples/learn/01-hello.out new file mode 100644 index 0000000..270c611 --- /dev/null +++ b/examples/learn/01-hello.out @@ -0,0 +1 @@ +hello, world! diff --git a/examples/learn/01-hello.zc b/examples/learn/01-hello.zc new file mode 100644 index 0000000..10cf4bf --- /dev/null +++ b/examples/learn/01-hello.zc @@ -0,0 +1,4 @@ +fn main() { + put_s("hello, world!\n"); + return 0; +} diff --git a/examples/learn/02-loops.out b/examples/learn/02-loops.out new file mode 100644 index 0000000..1e8b314 --- /dev/null +++ b/examples/learn/02-loops.out @@ -0,0 +1 @@ +6 diff --git a/examples/learn/02-loops.zc b/examples/learn/02-loops.zc new file mode 100644 index 0000000..832e32f --- /dev/null +++ b/examples/learn/02-loops.zc @@ -0,0 +1,6 @@ +fn main() { + let i = 0, n = 0; + while (i < 4) { n = n + i; i++; } + put_n(n); put_c(10); + return 0; +} diff --git a/examples/learn/03-do-continue.out b/examples/learn/03-do-continue.out new file mode 100644 index 0000000..b1bd38b --- /dev/null +++ b/examples/learn/03-do-continue.out @@ -0,0 +1 @@ +13 diff --git a/examples/learn/03-do-continue.zc b/examples/learn/03-do-continue.zc new file mode 100644 index 0000000..70959f4 --- /dev/null +++ b/examples/learn/03-do-continue.zc @@ -0,0 +1,6 @@ +fn main() { + int n = 0, i = 0; + do { i++; if (i == 2) continue; n = n + i; } while (i < 5); + put_n(n); put_c(10); + return 0; +} diff --git a/examples/learn/04-enums.out b/examples/learn/04-enums.out new file mode 100644 index 0000000..ead6e4a --- /dev/null +++ b/examples/learn/04-enums.out @@ -0,0 +1 @@ +0 5 6 diff --git a/examples/learn/04-enums.zc b/examples/learn/04-enums.zc new file mode 100644 index 0000000..8f80e3c --- /dev/null +++ b/examples/learn/04-enums.zc @@ -0,0 +1,5 @@ +enum color { RED, GREEN = 5, BLUE }; +fn main() { + put_n(RED); put_c(32); put_n(GREEN); put_c(32); put_n(BLUE); put_c(10); + return 0; +} diff --git a/examples/learn/05-small-ints.out b/examples/learn/05-small-ints.out new file mode 100644 index 0000000..2a9bc9e --- /dev/null +++ b/examples/learn/05-small-ints.out @@ -0,0 +1,3 @@ +4 +13 +44 diff --git a/examples/learn/05-small-ints.zc b/examples/learn/05-small-ints.zc new file mode 100644 index 0000000..b288181 --- /dev/null +++ b/examples/learn/05-small-ints.zc @@ -0,0 +1,9 @@ +fn main() { + u8 a = 250; a = a + 10; + put_n(a); put_c(10); + put_n(sizeof([100]u8)); put_c(10); + u8* px = new(u8, 4096); + px[10] = 300; + put_n(px[10]); put_c(10); + return 0; +} diff --git a/examples/learn/06-floats.out b/examples/learn/06-floats.out new file mode 100644 index 0000000..9406425 --- /dev/null +++ b/examples/learn/06-floats.out @@ -0,0 +1 @@ +4.000000 diff --git a/examples/learn/06-floats.zc b/examples/learn/06-floats.zc new file mode 100644 index 0000000..06d23fa --- /dev/null +++ b/examples/learn/06-floats.zc @@ -0,0 +1,6 @@ +fn put_f(x) { asm { x @ #1220 } return 0; } +fn main() { + let a = 1.5; + put_f(a + 2.5); put_c(10); + return 0; +} diff --git a/examples/learn/07-bytes.out b/examples/learn/07-bytes.out new file mode 100644 index 0000000..209c2ea --- /dev/null +++ b/examples/learn/07-bytes.out @@ -0,0 +1 @@ +17 8721 diff --git a/examples/learn/07-bytes.zc b/examples/learn/07-bytes.zc new file mode 100644 index 0000000..7d06282 --- /dev/null +++ b/examples/learn/07-bytes.zc @@ -0,0 +1,7 @@ +fn main() { + let w = 0; + st8(addr_b(&w), 0x11); + st8(addr_b(&w) + 1, 0x22); + put_n(ld8(addr_b(&w))); put_c(32); put_n(w); put_c(10); + return 0; +} diff --git a/examples/learn/08-byte-strings.out b/examples/learn/08-byte-strings.out new file mode 100644 index 0000000..1e10779 --- /dev/null +++ b/examples/learn/08-byte-strings.out @@ -0,0 +1,2 @@ +hello, zc +9 42 diff --git a/examples/learn/08-byte-strings.zc b/examples/learn/08-byte-strings.zc new file mode 100644 index 0000000..0959ceb --- /dev/null +++ b/examples/learn/08-byte-strings.zc @@ -0,0 +1,8 @@ +fn main() { + let name = b"zc"; + let hi = b"hello, " + name; + put_s(hi); put_c(10); + put_n(len(hi)); put_c(32); + put_n(s_to_n(b"40") + 2); put_c(10); + return 0; +} diff --git a/examples/learn/09-shared.out b/examples/learn/09-shared.out new file mode 100644 index 0000000..2fb73a0 --- /dev/null +++ b/examples/learn/09-shared.out @@ -0,0 +1 @@ +1 1 diff --git a/examples/learn/09-shared.zc b/examples/learn/09-shared.zc new file mode 100644 index 0000000..ab3ca06 --- /dev/null +++ b/examples/learn/09-shared.zc @@ -0,0 +1,8 @@ +fn main() { + let tail = cons(1, 2); + let left = cons(tail, 3); + let right = cons(tail, 4); + put_n(car(car(left))); put_c(32); + put_n(car(left) == car(right)); put_c(10); + return 0; +} diff --git a/examples/learn/10-owned.out b/examples/learn/10-owned.out new file mode 100644 index 0000000..7f8f011 --- /dev/null +++ b/examples/learn/10-owned.out @@ -0,0 +1 @@ +7 diff --git a/examples/learn/10-owned.zc b/examples/learn/10-owned.zc new file mode 100644 index 0000000..3aff9a4 --- /dev/null +++ b/examples/learn/10-owned.zc @@ -0,0 +1,6 @@ +fn main() { + int* p = new(int); + *p = 7; + put_n(*p); put_c(10); + return 0; +} diff --git a/examples/learn/11-tags.out b/examples/learn/11-tags.out new file mode 100644 index 0000000..b8626c4 --- /dev/null +++ b/examples/learn/11-tags.out @@ -0,0 +1 @@ +4 diff --git a/examples/learn/11-tags.zc b/examples/learn/11-tags.zc new file mode 100644 index 0000000..08edb58 --- /dev/null +++ b/examples/learn/11-tags.zc @@ -0,0 +1,6 @@ +int*.u box(int v) { int* b = new(int); *b = v; return u*b; } +fn main() { + int* p = box(4); + put_n(*p); put_c(10); + return 0; +} diff --git a/examples/learn/12-aliases.out b/examples/learn/12-aliases.out new file mode 100644 index 0000000..b8626c4 --- /dev/null +++ b/examples/learn/12-aliases.out @@ -0,0 +1 @@ +4 diff --git a/examples/learn/12-aliases.zc b/examples/learn/12-aliases.zc new file mode 100644 index 0000000..abb39d5 --- /dev/null +++ b/examples/learn/12-aliases.zc @@ -0,0 +1,8 @@ +struct node { int v; node* next; }; +fn main() { + node* a = new(node), b = new(node); + a.v = 3; b.v = 4; a.next = b; + node* c = a.next; + put_n(c.v); put_c(10); + return 0; +} diff --git a/examples/learn/13-memdebug.out b/examples/learn/13-memdebug.out new file mode 100644 index 0000000..c168a7b --- /dev/null +++ b/examples/learn/13-memdebug.out @@ -0,0 +1,2 @@ +leak: line 4 3 words +1 diff --git a/examples/learn/13-memdebug.zc b/examples/learn/13-memdebug.zc new file mode 100644 index 0000000..80ee0da --- /dev/null +++ b/examples/learn/13-memdebug.zc @@ -0,0 +1,10 @@ +memdebug on; +int* g; +fn main() { + int* p = new(int); + *p = 1; + g = d*p; + put_n(memreport()); + put_c(10); + return 0; +} diff --git a/examples/learn/14-include.out b/examples/learn/14-include.out new file mode 100644 index 0000000..1e8b314 --- /dev/null +++ b/examples/learn/14-include.out @@ -0,0 +1 @@ +6 diff --git a/examples/learn/14-include.zc b/examples/learn/14-include.zc new file mode 100644 index 0000000..c126389 --- /dev/null +++ b/examples/learn/14-include.zc @@ -0,0 +1,7 @@ +include "src/lib/std.zc"; +fn main() { + let s = readfile("tests/data/hi.txt"); + put_n(len_w(s)); put_c(10); + free(d*s); + return 0; +} diff --git a/examples/learn/15-generics.out b/examples/learn/15-generics.out new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/examples/learn/15-generics.out @@ -0,0 +1 @@ +9 diff --git a/examples/learn/15-generics.zc b/examples/learn/15-generics.zc new file mode 100644 index 0000000..dfc4833 --- /dev/null +++ b/examples/learn/15-generics.zc @@ -0,0 +1,9 @@ +struct record<K, V> { K key; V val; }; +struct point { int x; int y; }; +K keyof<K, V>(record<K, V>* r) { return r.key; } +fn main() { + record<int, point>* r = new(record<int, point>); + r.key = 9; + put_n(keyof(r)); put_c(10); + return 0; +} diff --git a/examples/learn/16-tagged-enums.out b/examples/learn/16-tagged-enums.out new file mode 100644 index 0000000..0b2b75d --- /dev/null +++ b/examples/learn/16-tagged-enums.out @@ -0,0 +1,2 @@ +27 +10 diff --git a/examples/learn/16-tagged-enums.zc b/examples/learn/16-tagged-enums.zc new file mode 100644 index 0000000..ba430f4 --- /dev/null +++ b/examples/learn/16-tagged-enums.zc @@ -0,0 +1,18 @@ +enum shape { circle { int r; }, rect { int w; int h; }, dot }; +int area(shape* s) { + switch (s) { + case circle(r): return r * r * 3; + case rect(w, h): return w * h; + case dot: return 0; + } + return -1; +} +fn main() { + shape* s = shape.circle(3); + put_n(area(s)); put_c(10); + free(d*s); + shape* t = shape.rect(2, 5); + put_n(area(t)); put_c(10); + free(d*t); + return 0; +} diff --git a/examples/learn/17-switch.out b/examples/learn/17-switch.out new file mode 100644 index 0000000..f719efd --- /dev/null +++ b/examples/learn/17-switch.out @@ -0,0 +1 @@ +two diff --git a/examples/learn/17-switch.zc b/examples/learn/17-switch.zc new file mode 100644 index 0000000..f03aba9 --- /dev/null +++ b/examples/learn/17-switch.zc @@ -0,0 +1,6 @@ +fn main() { + int k = 2; + switch (k) { case 1: put_s("one"); break; case 2: put_s("two"); break; default: put_s("other"); } + put_c(10); + return 0; +} diff --git a/examples/learn/18-o3.out b/examples/learn/18-o3.out new file mode 100644 index 0000000..11c5ab3 --- /dev/null +++ b/examples/learn/18-o3.out @@ -0,0 +1,2 @@ +30 +143 diff --git a/examples/learn/18-o3.zc b/examples/learn/18-o3.zc new file mode 100644 index 0000000..cb2237a --- /dev/null +++ b/examples/learn/18-o3.zc @@ -0,0 +1,10 @@ +o3 int sum(fixed []int a) { int s = 0; for (int i = 0; i < len(a); i++) s += a[i]; return s; } +fn main() { + fixed []int a = new(fixed []int, 5); + for (int i = 0; i < 5; i++) a[i] = i * i; + put_n(sum(a)); put_c(10); + o3 int hits = 0; + o3 { for (int i = 0; i < 1000; i++) if (i % 7 == 3) hits++; } + put_n(hits); put_c(10); + return 0; +} diff --git a/examples/learn/19-growable.out b/examples/learn/19-growable.out new file mode 100644 index 0000000..4a9aeea --- /dev/null +++ b/examples/learn/19-growable.out @@ -0,0 +1,3 @@ +2 +5 +3 diff --git a/examples/learn/19-growable.zc b/examples/learn/19-growable.zc new file mode 100644 index 0000000..e5ed8de --- /dev/null +++ b/examples/learn/19-growable.zc @@ -0,0 +1,13 @@ +struct cnt { + int n; + static int make() { return 3; } + int get() { return this.n; } +}; +fn main() { + []int xs = new([]int); + push(xs, 4); push(xs, 5); + put_n(len(xs)); put_c(10); + put_n(pop(xs)); put_c(10); + put_n(cnt.make()); put_c(10); + return 0; +} diff --git a/examples/learn/20-text-num.out b/examples/learn/20-text-num.out new file mode 100644 index 0000000..7b3cce8 --- /dev/null +++ b/examples/learn/20-text-num.out @@ -0,0 +1,2 @@ +6 1024 7 +124 diff --git a/examples/learn/20-text-num.zc b/examples/learn/20-text-num.zc new file mode 100644 index 0000000..027b721 --- /dev/null +++ b/examples/learn/20-text-num.zc @@ -0,0 +1,6 @@ +include "src/lib/std.zc"; +fn main() { + put_n(Num.gcd(12, 18)); put_c(32); put_n(Num.pow(2, 10)); put_c(32); put_n(Num.isqrt(50)); put_c(10); + put_n(Text.atoi("123") + 1); put_c(10); + return 0; +} diff --git a/examples/learn/21-host.out b/examples/learn/21-host.out new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/examples/learn/21-host.out @@ -0,0 +1 @@ +1 diff --git a/examples/learn/21-host.zc b/examples/learn/21-host.zc new file mode 100644 index 0000000..550e747 --- /dev/null +++ b/examples/learn/21-host.zc @@ -0,0 +1,5 @@ +include "src/lib/std.zc"; +fn main() { + put_n(Host.now() > 0); put_c(10); + return 0; +} diff --git a/examples/learn/22-raw-x86.out b/examples/learn/22-raw-x86.out new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/examples/learn/22-raw-x86.out @@ -0,0 +1 @@ +1 diff --git a/examples/learn/22-raw-x86.zc b/examples/learn/22-raw-x86.zc new file mode 100644 index 0000000..e274aee --- /dev/null +++ b/examples/learn/22-raw-x86.zc @@ -0,0 +1,5 @@ +include "src/lib/std.zc"; +fn main() { + put_n(Host.raw(39, 0, 0, 0, 0, 0, 0) > 0); put_c(10); + return 0; +} diff --git a/examples/learn/23-c-abi.out b/examples/learn/23-c-abi.out new file mode 100644 index 0000000..0701ad5 --- /dev/null +++ b/examples/learn/23-c-abi.out @@ -0,0 +1,2 @@ +1.414214 +1 3 5 9 diff --git a/examples/learn/23-c-abi.zc b/examples/learn/23-c-abi.zc new file mode 100644 index 0000000..3df4b75 --- /dev/null +++ b/examples/learn/23-c-abi.zc @@ -0,0 +1,12 @@ +fn put_f(x) { asm { x @ #1220 } return 0; } +extern double sqrt(double); +extern void qsort(word *base, long n, long size, void *cmp); +int cmp(int* a, int* b) { return *a - *b; } +fn main() { + let a[4]; + a[0] = 5; a[1] = 3; a[2] = 9; a[3] = 1; + put_f(sqrt(2.0)); put_c(10); + qsort(a, 4, 8, cfunc(cmp)); + put_n(a[0]); put_c(32); put_n(a[1]); put_c(32); put_n(a[2]); put_c(32); put_n(a[3]); put_c(10); + return 0; +} diff --git a/examples/learn/24-backtick.out b/examples/learn/24-backtick.out new file mode 100644 index 0000000..81bf396 --- /dev/null +++ b/examples/learn/24-backtick.out @@ -0,0 +1 @@ +ab diff --git a/examples/learn/24-backtick.zc b/examples/learn/24-backtick.zc new file mode 100644 index 0000000..0317631 --- /dev/null +++ b/examples/learn/24-backtick.zc @@ -0,0 +1,5 @@ +fn main() { + put_c(get_c()); put_c(get_c()); put_c(10); + return 0; +} +`abc diff --git a/examples/learn/25-closures.out b/examples/learn/25-closures.out new file mode 100644 index 0000000..b04b592 --- /dev/null +++ b/examples/learn/25-closures.out @@ -0,0 +1,2 @@ +11 +15 diff --git a/examples/learn/25-closures.zc b/examples/learn/25-closures.zc new file mode 100644 index 0000000..72c236d --- /dev/null +++ b/examples/learn/25-closures.zc @@ -0,0 +1,8 @@ +fn apply(f, x) { return f(x); } +fn main() { + let k = 10; + let add = fn(x) { return x + k; }; + put_n(add(1)); put_c(10); + put_n(apply(add, 5)); put_c(10); + return 0; +} diff --git a/examples/learn/26-closure-state.out b/examples/learn/26-closure-state.out new file mode 100644 index 0000000..33aea5d --- /dev/null +++ b/examples/learn/26-closure-state.out @@ -0,0 +1,3 @@ +109 +1 5 +0 diff --git a/examples/learn/26-closure-state.zc b/examples/learn/26-closure-state.zc new file mode 100644 index 0000000..f3ddf50 --- /dev/null +++ b/examples/learn/26-closure-state.zc @@ -0,0 +1,13 @@ +fn main() { + let base = 2; + let outer = fn(x) { + let inner = fn(y) { return y + base + x; }; + return inner(100); + }; + put_n(outer(7)); put_c(10); + let count = 0; + let tick = fn(d) { count = count + d; return count; }; + put_n(tick(1)); put_c(32); put_n(tick(4)); put_c(10); + put_n(count); put_c(10); + return 0; +} |
