mirror of
https://github.com/Nexoniarz/Finch.git
synced 2026-11-03 00:44:27 +00:00
.fn is already used by other tools (e.g. Fennel). Modules are now looked up as name.fch; a leftover name.fn gets a clear message telling you to rename it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
49 lines
1 KiB
Text
49 lines
1 KiB
Text
// 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
|
|
}
|
|
}
|