Finch/tests/run/c_structs.fn
Nexoniarz 1dc976bd8f Structs, arrays, owned strings, memory model, modules, debug info
- struct with default values; constructors Point(1, 2) / Point(x: 1); field access, auto-deref through pointers
- []T arrays with bounds checks, literals, for-each, push/pop/insert/remove/clear/resize/find/contains/join/slice/reverse/sort
- str carries its length: .len, s[i], +, comparison, sub/find/split/trim/upper/lower/replace/repeat/bytes, str(x), int(s), float(s)
- Memory: values own their memory and are freed at the end of their block; assignment copies, temporaries move;
  parameters are lent and only copied when the function changes them; defer; new()/free() for manual heap memory
- input(), read_file/write_file/file_exists, exit(), fn main([]str args)
- Finch modules: import name, module.fn(), module.Struct
- C structs from headers (fields, fixed arrays, nested); passed and returned by value with the System V x86-64 ABI;
  addr(fn) gives C a callback
- link "file.c" compiles and links your own C code
- -g emits DWARF debug info (gdb/lldb)
- Small C runtime (runtime/finch_rt.c) embedded in the compiler, cached in ~/.cache/finch
- Tests: 58, all clean under valgrind (MEMCHECK=1 tests/run.sh)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 18:57:37 +02:00

34 lines
1 KiB
Text

import "abi.h"
link "abi.c"
fn pairTimes(Pair p, int k) -> Pair {
return Pair(a: p.a * k, b: p.b + k)
}
fn flip(Vec2 v) -> Vec2 {
return Vec2(x: v.y, y: v.x * 10)
}
fn main() {
print(vec2_add(Vec2(1.5, 2), Vec2(x: 0.5, y: 3)))
print(vec3_scale(Vec3(1, 2, 3), 2))
print(color_invert(Color(r: 10, g: 20, b: 30, a: 255)))
print(mixed_bump(Mixed(d: 1.25, i: 41)))
print(pair_swap(Pair(1, 2)))
big := big_sum(Big(1, 2, 3), Big(10, 20, 30))
print(big, big_total(big))
Named n
n.name[0] = 'h'
n.name[1] = 'i'
n.count = 5
print(named_len(n), n.name.len)
s := sprite_move(Sprite(pos: Vec2(1, 1), color: Color(1, 2, 3, 4)), 2.5)
print(s)
v := Vec2(1, 0)
print(many(v, v, v, v, v, v, v, v, Vec2(2, 3), Pair(1, 0), Pair(0, 2), Pair(3, 0), Pair(0, 4)))
print(apply_pair(addr(pairTimes), Pair(2, 3), 5))
print(apply_vec(addr(flip), Vec2(1, 2)))
second := Item(value: 2, next: null)
first := Item(value: 1, next: addr(second))
print(item_sum(addr(first)))
}