mirror of
https://github.com/bearlanguageorg/bear.git
synced 2026-08-26 14:57:18 +00:00
Enum methods and more
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
// colors.vr — demonstrates enum declarations and usage
|
||||
|
||||
enum Color {
|
||||
red
|
||||
green
|
||||
blue
|
||||
}
|
||||
|
||||
enum Direction {
|
||||
north
|
||||
south
|
||||
east
|
||||
west
|
||||
}
|
||||
|
||||
fn get_color_name(c) {
|
||||
match c {
|
||||
Color.red { return "red" }
|
||||
Color.green { return "green" }
|
||||
Color.blue { return "blue" }
|
||||
else { return "unknown" }
|
||||
}
|
||||
}
|
||||
|
||||
fn is_warm(c) {
|
||||
if c == Color.red {
|
||||
return true
|
||||
}
|
||||
if c == Color.blue {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let c = Color.red
|
||||
println("Color.red = 0")
|
||||
|
||||
let name = get_color_name(Color.green)
|
||||
println("Color.green name = green")
|
||||
|
||||
let warm = is_warm(Color.red)
|
||||
println("Is red warm? 1")
|
||||
|
||||
let dir = Direction.north
|
||||
println("Direction.north = 0")
|
||||
|
||||
// enums are just integers, so you can use them in expressions
|
||||
let next = Color.red + 1
|
||||
println("Color.red + 1 = 1")
|
||||
|
||||
// assert enum values
|
||||
assert Color.red == 0
|
||||
assert Color.green == 1
|
||||
assert Color.blue == 2
|
||||
assert Direction.north == 0
|
||||
assert Direction.west == 3
|
||||
println("All enum assertions passed!")
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
// enum_methods.vr — demonstrates methods defined on enum types
|
||||
|
||||
enum Color {
|
||||
red
|
||||
green
|
||||
blue
|
||||
}
|
||||
|
||||
enum Direction {
|
||||
north
|
||||
south
|
||||
east
|
||||
west
|
||||
}
|
||||
|
||||
// method on Color enum
|
||||
fn (c Color) name() {
|
||||
match c {
|
||||
Color.red { return "red" }
|
||||
Color.green { return "green" }
|
||||
Color.blue { return "blue" }
|
||||
else { return "unknown" }
|
||||
}
|
||||
}
|
||||
|
||||
// method on Color enum
|
||||
fn (c Color) is_primary() {
|
||||
if c == Color.red or c == Color.green or c == Color.blue {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// method on Direction enum
|
||||
fn (d Direction) opposite() {
|
||||
match d {
|
||||
Direction.north { return Direction.south }
|
||||
Direction.south { return Direction.north }
|
||||
Direction.east { return Direction.west }
|
||||
Direction.west { return Direction.east }
|
||||
else { return d }
|
||||
}
|
||||
}
|
||||
|
||||
// method on Direction enum
|
||||
fn (d Direction) is_horizontal() {
|
||||
if d == Direction.east or d == Direction.west {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// test Color methods
|
||||
let c = Color.red
|
||||
let nm = c.name()
|
||||
assert nm == "red"
|
||||
println("Color.name() = red")
|
||||
|
||||
let primary = c.is_primary()
|
||||
assert primary == 1
|
||||
println("Color.is_primary() = 1")
|
||||
|
||||
// test Direction methods
|
||||
let dir = Direction.north
|
||||
let opp = dir.opposite()
|
||||
assert opp == Direction.south
|
||||
println("Direction.north.opposite() = south")
|
||||
|
||||
let horizontal = dir.is_horizontal()
|
||||
assert horizontal == 0
|
||||
println("Direction.north.is_horizontal() = 0")
|
||||
|
||||
// test with other values
|
||||
let blue = Color.blue
|
||||
assert blue.name() == "blue"
|
||||
assert blue.is_primary() == 1
|
||||
println("Color.blue methods OK")
|
||||
|
||||
let east = Direction.east
|
||||
assert east.opposite() == Direction.west
|
||||
assert east.is_horizontal() == 1
|
||||
println("Direction.east methods OK")
|
||||
|
||||
println("All enum method assertions passed!")
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// math_utils.vr — a small utility library for the import example
|
||||
|
||||
fn add(a, b) {
|
||||
return a + b
|
||||
}
|
||||
|
||||
fn multiply(a, b) {
|
||||
return a * b
|
||||
}
|
||||
|
||||
fn factorial(n) {
|
||||
if n <= 1 {
|
||||
return 1
|
||||
}
|
||||
return n * factorial(n - 1)
|
||||
}
|
||||
|
||||
fn fibonacci(n) {
|
||||
if n <= 1 {
|
||||
return n
|
||||
}
|
||||
return fibonacci(n - 1) + fibonacci(n - 2)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// use_math.vr — demonstrates the import system
|
||||
|
||||
import "examples/math_utils.vr"
|
||||
|
||||
fn main() {
|
||||
// use functions from the imported library
|
||||
let sum = add(10, 20)
|
||||
println("10 + 20 = 30")
|
||||
|
||||
let product = multiply(6, 7)
|
||||
println("6 * 7 = 42")
|
||||
|
||||
let fact5 = factorial(5)
|
||||
println("5! = 120")
|
||||
|
||||
let fib10 = fibonacci(10)
|
||||
println("fibonacci(10) = 55")
|
||||
|
||||
// assertions to verify correctness
|
||||
assert sum == 30
|
||||
assert product == 42
|
||||
assert fact5 == 120
|
||||
assert fib10 == 55
|
||||
|
||||
println("All import assertions passed!")
|
||||
}
|
||||
Reference in New Issue
Block a user