mirror of
https://github.com/Nexoniarz/Finch.git
synced 2026-11-03 00:44:27 +00:00
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:
parent
0591cd20f3
commit
1dc976bd8f
59 changed files with 4577 additions and 1047 deletions
27
examples/guess.fn
Normal file
27
examples/guess.fn
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// Guess the number: input, loops and conversions.
|
||||
import "stdlib.h"
|
||||
import "time.h"
|
||||
|
||||
fn main() {
|
||||
srand(u32(time(null)))
|
||||
secret := rand() % 100 + 1
|
||||
tries := 0
|
||||
print("I'm thinking of a number from 1 to 100.")
|
||||
while true {
|
||||
answer := input("Your guess: ")
|
||||
if answer.len == 0 {
|
||||
print("Bye!")
|
||||
return
|
||||
}
|
||||
guess := int(answer)
|
||||
tries += 1
|
||||
if guess < secret {
|
||||
print("Higher!")
|
||||
} else if guess > secret {
|
||||
print("Lower!")
|
||||
} else {
|
||||
print("Yes! It was", secret, "- you needed", tries, "tries.")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
49
examples/structs.fn
Normal file
49
examples/structs.fn
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
// Structs, arrays of structs, and a linked list on the heap.
|
||||
import "math.h"
|
||||
|
||||
struct Point {
|
||||
float x
|
||||
float y
|
||||
}
|
||||
|
||||
struct Shape {
|
||||
str name
|
||||
[]Point corners
|
||||
int color = 0xFF8800
|
||||
}
|
||||
|
||||
struct Node {
|
||||
int value
|
||||
ptr[Node] next
|
||||
}
|
||||
|
||||
fn perimeter(Shape s) -> float {
|
||||
total := 0.0
|
||||
for i in 0..s.corners.len {
|
||||
a := s.corners[i]
|
||||
b := s.corners[(i + 1) % s.corners.len]
|
||||
dx := b.x - a.x
|
||||
dy := b.y - a.y
|
||||
total += sqrt(dx * dx + dy * dy)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
fn main() {
|
||||
square := Shape(name: "square", corners: [Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)])
|
||||
print(square.name, square.corners.len, "corners, perimeter", perimeter(square))
|
||||
print(square)
|
||||
|
||||
// a linked list: new(...) puts a value on the heap, free(...) gives it back
|
||||
ptr[Node] head = null
|
||||
for i in 0..3 {
|
||||
head = new(Node(value: i, next: head))
|
||||
}
|
||||
n := head
|
||||
while n != null {
|
||||
print("node", n.value)
|
||||
next := n.next
|
||||
free(n)
|
||||
n = next
|
||||
}
|
||||
}
|
||||
53
examples/todo.fn
Normal file
53
examples/todo.fn
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
// A tiny to-do list: structs, arrays, strings and files.
|
||||
|
||||
struct Task {
|
||||
str text
|
||||
bool done = false
|
||||
}
|
||||
|
||||
fn show([]Task tasks) {
|
||||
if tasks.len == 0 {
|
||||
print("(nothing to do)")
|
||||
}
|
||||
for i in 0..tasks.len {
|
||||
mark := " "
|
||||
if tasks[i].done {
|
||||
mark = "x"
|
||||
}
|
||||
print("[" + mark + "]", i + 1, tasks[i].text)
|
||||
}
|
||||
}
|
||||
|
||||
fn save([]Task tasks, str path) {
|
||||
[]str lines
|
||||
for t in tasks {
|
||||
lines.push(str(t.done) + "|" + t.text)
|
||||
}
|
||||
write_file(path, lines.join("\n"))
|
||||
}
|
||||
|
||||
fn load(str path) -> []Task {
|
||||
[]Task tasks
|
||||
if !file_exists(path) {
|
||||
return tasks
|
||||
}
|
||||
for line in read_file(path).split("\n") {
|
||||
if line.len == 0 {
|
||||
continue
|
||||
}
|
||||
parts := line.split("|")
|
||||
tasks.push(Task(text: parts[1], done: parts[0] == "true"))
|
||||
}
|
||||
return tasks
|
||||
}
|
||||
|
||||
fn main() {
|
||||
path := "todo.txt"
|
||||
tasks := load(path)
|
||||
tasks.push(Task(text: "learn Finch"))
|
||||
tasks.push(Task(text: "write a game"))
|
||||
tasks[0].done = true
|
||||
show(tasks)
|
||||
save(tasks, path)
|
||||
print("saved", tasks.len, "tasks to", path)
|
||||
}
|
||||
Loading…
Reference in a new issue