Files
rexlang/examples/basics/structs.dino
T
Your NameandClaude Sonnet 5.5 eaf9567a36 Dino compiler: language, formatter, language server, docs, editor support
Compiler in Rust (lexer, parser, checker, C codegen), maps, chars,
dino fmt, dino lsp (diagnostics, formatting, outline, definition,
hover, completion, signature help), docs, examples, VS Code/Vim files.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 23:46:12 +01:00

81 lines
1.5 KiB
Plaintext

struct Vec2 {
x, y: float
func add(b: Vec2): Vec2 {
return Vec2{x + b.x, y + b.y}
}
func scale(k: float): Vec2 {
return Vec2{x * k, y * k}
}
func length_squared(): float {
return x * x + y * y
}
}
struct Player {
name: string
pos: Vec2
vel: Vec2
hp: int
// changes pos and vel, so the compiler treats it as a mutating method
func update(dt: float) {
pos = pos.add(vel.scale(dt))
if pos.x < 0 || pos.x > 100 {
vel.x = -vel.x
}
if pos.y < 0 || pos.y > 100 {
vel.y = -vel.y
}
}
// calls a mutating method, so it mutates too
func step_twice(dt: float) {
update(dt)
update(dt)
}
func hurt(amount: int) {
hp -= amount
if hp < 0 {
hp = 0
}
}
func is_alive(): bool {
return hp > 0
}
func describe(): string {
return "{name} at ({pos.x}, {pos.y}) hp={hp}"
}
}
func main() {
var p = Player{name: "Ana", pos: Vec2{90, 50}, vel: Vec2{10, 5}, hp: 20}
print(p.describe())
p.update(1)
print(p.describe())
p.update(1)
print(p.describe())
p.step_twice(0.5)
print(p.pos)
// omitted fields start at zero
var q = Player{name: "Zed"}
print(q)
print(q.is_alive())
p.hurt(15)
p.hurt(15)
print(p.hp, p.is_alive())
var origin: Vec2
print(origin, origin.length_squared())
print(Vec2{3, 4}.length_squared())
}