NOTE: These are pulled from the examples/ folder in the repo.

animals

## A friendly animal.
Animal :: trait {
	name: string
	greeting: string = "hello"

	greet :: fn(self) string {
		"{self.name} says {self.greeting}!"
	}
}

## Kitty cat.
Cat :: struct {
	name: string
	greeting: string = "meow"
}

Cat :< Animal

## Little pupper.
Dog :: struct {
	name: string
	greeting := "woof"
}

Dog :< {
	walk :: fn(self) {}
}

Dog : Animal < {
	greet :: {
		"{self.name} the doggo says {self.greeting}!"
	}
}

# A... rock?
Rock :: struct {}

# Rocks are people, too.
Rock : Animal < {
	name :: "foo"
}

cat := Cat.{
	name = "Jerry"
}
cat.greet() |> print
# expect: Jerry says meow!

terry := Dog.{
	name = "Terry"
}
terry.greet() |> print
# expect: Terry the doggo says woof!

larry := Dog.{
	name = "Larry"
	greeting = "bark"
}
larry.greet() |> print
# expect: Larry the doggo says bark!

boxes

Box[T] :: struct { v: T }
Box[T] :< {
	get :: fn(self) T { self.v }
}

b := Box.{ v = 21 }
assert!(b.get() == 21)

Box.{ "hi" }.get()
# expect: hi

dimensions

dimensions :: fn() (width: int, height: int) {
	(1920, 1080)
}

dimensions()
# expect: (width = 1920, height = 1080)

errors

find :: fn(id: int) ?int {
	if id == 7 { return 42 }
	none
}

total := 0
loop id in [ 1 7 3 7 ] {
	total += find(id) or { 0 }
}

total
# expect: 84

files

use fs

Perm : u3 : enum {
	none  = 0b000
	read  = 0b001
	write = 0b010
	exec  = 0b100
}

Handle :: struct {
	path: string,
	perms: []Perm,
}

main :: fn() {
	# TODO: mktemp kind of deal
	file :: "/tmp/oi-data.json"

	rw := Handle.{ file, [ .read .write ]}
	ro := Handle.{ file, [ .read ]}

	p : Perm
	# print(p)
	# print(ro)
}

grades

Grade :: enum { A B C D F }

grade :: fn(score: int) Grade {
	match true {
		score >= 90 => .A,
		score >= 80 => .B,
		score >= 70 => .C,
		score >= 60 => .C,
		else => .F
	}
}

grade(89)
# expect: B

main

## comments

# Single line comments
# (can be stacked)

#{ Block comments
	#{ (can be nested) }#
}#

## Doc comments.
##
## # support markdown
## ```
## # code block language defaults to Oi
## ```

## Main entrypoint.
##
## Called by Oi if present.
main :: fn() {
	## primatives

	bull := true
	str : string
	integer :: 1337
	float :: 69.420
	atm :: :adam

	bull = false
	str = "string"

	print(2.0 + 1.2)
	# expect: 3.2
}

pipelines

("hi" "mom") |> "{$.0}, {$.1}!"
# expect: hi, mom!

points

Point :: struct {
	x: int
	y: int
}

p := Point.{3, 4}
p.x += 10

(p.x p.y) |> print
# expect: (13, 4)

ranges

total := 0

loop i in 1..10 {
	total += i
}

assert! total == 45
total
# expect: 45

shapes

Shape :: enum {
	point
	circle { radius: f64 }
	rect { w: float, h: float }
	triangle(f64, f64, f64)
}

area :: fn(s: Shape) f64 {
	match s {
		.circle.(r) => 3.14159 * r ** 2,
		.rect.(w, h) => w * h,
		else => 0.0,
	}
}

Shape.rect.{ 3.0, 4.0 }
	|> area
	|> assert!($ == 12.0)

shape := Shape.triangle.(3.0, 4.0, 5.0)

match shape {
	.point => {
		print("origin: {()}")
	}
	.circle.(r) => {
		print("circle: {(r,)}")
	}
	.rect.(w, h) => {
		print("rect: {(w h)}")
	}
	t @ .triangle.(a, b, c) => {
		print(t)
	}
}

# expect: triangle.(3.0, 4.0, 5.0)

users

Status :: enum {
	offline
	online
	away
}

User :: struct {
	name: string
	status: Status
}

badge :: fn(u: User) string {
	match u.status {
		.online => "🟢 {u.name}",
		.away => "🟡 {u.name}",
		.offline => "âš« {u.name}",
	}
}

User :< {
	create :: fn(name: string) Self {
		return User.{ name }
	}

	greet :: fn(self) string {
		match self.status {
			.online => "Welcome back, {self.name}!",
			.away => "See you soon, {self.name}.",
			.offline => "Goodbye, {self.name}.",
		}
	}
}

user := User.create(
	"ur_mom_lolol"
)
user.status = .online

badge(user) |> print
# expect: 🟢 ur_mom_lolol

user.greet()
# expect: Welcome back, ur_mom_lolol!