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>
This commit is contained in:
Nexoniarz 2026-10-08 18:57:37 +02:00
commit 1dc976bd8f
59 changed files with 4577 additions and 1047 deletions

View file

@ -0,0 +1,4 @@
// expect: compare the parts one by one
fn main() {
print([1] == [1])
}

4
tests/fail/bad_int.fn Normal file
View file

@ -0,0 +1,4 @@
// expect: can't turn "12x" into int
fn main() {
print(int("12x"))
}

View file

@ -0,0 +1,4 @@
// expect: return can't be used inside defer
fn main() {
defer return
}

View file

@ -0,0 +1,6 @@
// expect: the loop variable 'x' can't be changed
fn main() {
for x in [1, 2] {
x = 3
}
}

View file

@ -0,0 +1,4 @@
// expect: an empty [] needs a type
fn main() {
a := []
}

5
tests/fail/free_value.fn Normal file
View file

@ -0,0 +1,5 @@
// expect: arrays, str and structs are freed by Finch
fn main() {
x := 5
free(x)
}

View file

@ -0,0 +1,5 @@
// expect: runtime error: index 5 is out of range (the length is 3)
fn main() {
a := [1, 2, 3]
print(a[5])
}

View file

@ -0,0 +1,3 @@
// expect: can't find the module 'nothere'
import nothere
fn main() {}

View file

@ -0,0 +1,4 @@
// expect: to use a module, add import geometry
fn main() {
v := geometry.Vec(1, 2)
}

5
tests/fail/pop_empty.fn Normal file
View file

@ -0,0 +1,5 @@
// expect: runtime error: pop() on an empty array
fn main() {
[]int a
a.pop()
}

5
tests/fail/push_type.fn Normal file
View file

@ -0,0 +1,5 @@
// expect: the element must be int, but this is str
fn main() {
a := [1, 2]
a.push("x")
}

6
tests/fail/str_null.fn Normal file
View file

@ -0,0 +1,6 @@
// expect: a str is never null
fn main() {
s := "a"
if s == null {
}
}

View file

@ -0,0 +1,4 @@
// expect: turn the other side into text first: str(...)
fn main() {
s := "a" + 5
}

View file

@ -0,0 +1,8 @@
// expect: P has 2 fields, but got 1 values
struct P {
int x
int y
}
fn main() {
p := P(1)
}

View file

@ -0,0 +1,8 @@
// expect: the struct P has no field 'y' (it has: x)
struct P {
int x
}
fn main() {
p := P(1)
print(p.y)
}

View file

@ -0,0 +1,5 @@
// expect: contains itself; use ptr[A] or []A
struct A {
A inner
}
fn main() {}