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>
73 lines
1.3 KiB
Plaintext
73 lines
1.3 KiB
Plaintext
interface Shape {
|
|
area(): float
|
|
name(): string
|
|
}
|
|
|
|
interface Resizable {
|
|
scale(k: float)
|
|
}
|
|
|
|
struct Circle {
|
|
r: float
|
|
|
|
func area(): float { return 3.0 * r * r }
|
|
func name(): string { return "circle" }
|
|
func scale(k: float) { r = r * k }
|
|
}
|
|
|
|
struct Rect {
|
|
w, h: float
|
|
|
|
func area(): float { return w * h }
|
|
func name(): string { return "rect" }
|
|
}
|
|
|
|
enum Dot {
|
|
Small
|
|
Big
|
|
|
|
func area(): float { return 1.0 }
|
|
func name(): string { return "dot" }
|
|
}
|
|
|
|
func describe[T: Shape](s: T): string {
|
|
return "{s.name()} with area {s.area()}"
|
|
}
|
|
|
|
func total_area[T: Shape](shapes: []T): float {
|
|
var sum = 0.0
|
|
for s in shapes { sum += s.area() }
|
|
return sum
|
|
}
|
|
|
|
func grow[T: Resizable](s: T): T {
|
|
var copy = s
|
|
copy.scale(2.0)
|
|
return copy
|
|
}
|
|
|
|
struct Gallery[T: Shape] {
|
|
items: []T
|
|
|
|
func add(x: T) { items.push(x) }
|
|
func biggest(): float {
|
|
var best = 0.0
|
|
for it in items {
|
|
if it.area() > best { best = it.area() }
|
|
}
|
|
return best
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
print(describe(Circle{2.0}))
|
|
print(describe(Rect{2.0, 5.0}))
|
|
print(describe(Dot.Big))
|
|
print(total_area([Circle{1.0}, Circle{2.0}]))
|
|
print(grow(Circle{1.5}))
|
|
var g = Gallery[Rect]{}
|
|
g.add(Rect{1.0, 2.0})
|
|
g.add(Rect{3.0, 4.0})
|
|
print(g.biggest())
|
|
}
|