mirror of
https://github.com/bearlanguageorg/bear.git
synced 2026-08-26 14:57:18 +00:00
Bunch of updates
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
// constants.vr — demonstrates const declarations
|
||||
|
||||
const MAX_SIZE = 100
|
||||
const MIN_VALUE = 0
|
||||
const IS_DEBUG = 1
|
||||
const VERSION = 42
|
||||
|
||||
fn calculate(x) {
|
||||
// constants work inside functions
|
||||
if x > MAX_SIZE {
|
||||
return MAX_SIZE
|
||||
}
|
||||
if x < MIN_VALUE {
|
||||
return MIN_VALUE
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// constants at top level
|
||||
assert MAX_SIZE == 100
|
||||
assert MIN_VALUE == 0
|
||||
assert IS_DEBUG == 1
|
||||
assert VERSION == 42
|
||||
println("Constants: 100, 0, 1, 42")
|
||||
|
||||
// constants in expressions
|
||||
let limit = MAX_SIZE * 2
|
||||
assert limit == 200
|
||||
println("MAX_SIZE * 2 = 200")
|
||||
|
||||
// constants in conditions
|
||||
if IS_DEBUG == 1 {
|
||||
println("Debug mode is ON")
|
||||
}
|
||||
|
||||
// constants in functions
|
||||
let val = calculate(150)
|
||||
assert val == 100
|
||||
println("calculate(150) = 100")
|
||||
|
||||
let val2 = calculate(-5)
|
||||
assert val2 == 0
|
||||
println("calculate(-5) = 0")
|
||||
|
||||
// constants with boolean operations
|
||||
assert MAX_SIZE > MIN_VALUE
|
||||
assert VERSION == 42
|
||||
println("All constant assertions passed!")
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// enum_count.vr — demonstrates built-in enum.count()
|
||||
|
||||
enum Color {
|
||||
red
|
||||
green
|
||||
blue
|
||||
}
|
||||
|
||||
enum Day {
|
||||
monday
|
||||
tuesday
|
||||
wednesday
|
||||
thursday
|
||||
friday
|
||||
saturday
|
||||
sunday
|
||||
}
|
||||
|
||||
enum Coin {
|
||||
penny
|
||||
nickel
|
||||
dime
|
||||
quarter
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Color.count() = 3
|
||||
let c = Color.red
|
||||
assert c.count() == 3
|
||||
println("Color has 3 variants")
|
||||
|
||||
// Day.count() = 7
|
||||
assert Day.monday.count() == 7
|
||||
println("Day has 7 variants")
|
||||
|
||||
// Coin.count() = 4
|
||||
let coin = Coin.dime
|
||||
assert coin.count() == 4
|
||||
println("Coin has 4 variants")
|
||||
|
||||
// on a variant literal directly
|
||||
assert Color.blue.count() == 3
|
||||
println("Color.blue.count() = 3")
|
||||
|
||||
println("All count assertions passed!")
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// enum_iter.vr — demonstrates for-in over enum types
|
||||
|
||||
enum Color {
|
||||
red
|
||||
green
|
||||
blue
|
||||
}
|
||||
|
||||
enum Day {
|
||||
monday
|
||||
tuesday
|
||||
wednesday
|
||||
thursday
|
||||
friday
|
||||
saturday
|
||||
sunday
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// iterate over Color variants
|
||||
println("Colors:")
|
||||
for c in Color {
|
||||
print(" ")
|
||||
println(c.to_string())
|
||||
}
|
||||
|
||||
// iterate and use count
|
||||
let total = 0
|
||||
for c in Color {
|
||||
total = total + c.count()
|
||||
}
|
||||
assert total == 9 // 3 + 3 + 3
|
||||
println("Sum of counts: 9")
|
||||
|
||||
// iterate and use to_string in a match
|
||||
for d in Day {
|
||||
if d == Day.saturday or d == Day.sunday {
|
||||
println(d.to_string() + " is weekend")
|
||||
}
|
||||
}
|
||||
|
||||
// break and continue work
|
||||
let weekdays = 0
|
||||
for d in Day {
|
||||
if d == Day.saturday {
|
||||
break
|
||||
}
|
||||
weekdays = weekdays + 1
|
||||
}
|
||||
assert weekdays == 5
|
||||
println("Weekday count: 5")
|
||||
|
||||
// collect variant indices
|
||||
let indices = []
|
||||
for c in Color {
|
||||
push(indices, c)
|
||||
}
|
||||
assert indices[0] == 0
|
||||
assert indices[1] == 1
|
||||
assert indices[2] == 2
|
||||
println("Indices: 0, 1, 2")
|
||||
|
||||
println("All enum iteration assertions passed!")
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// enum_tostring.vr — demonstrates built-in enum.to_string()
|
||||
|
||||
enum Color {
|
||||
red
|
||||
green
|
||||
blue
|
||||
}
|
||||
|
||||
enum Day {
|
||||
monday
|
||||
tuesday
|
||||
wednesday
|
||||
thursday
|
||||
friday
|
||||
saturday
|
||||
sunday
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Color.to_string()
|
||||
let c = Color.red
|
||||
println(c.to_string())
|
||||
assert c.to_string() == "red"
|
||||
|
||||
let g = Color.green
|
||||
println(g.to_string())
|
||||
assert g.to_string() == "green"
|
||||
|
||||
let b = Color.blue
|
||||
println(b.to_string())
|
||||
assert b.to_string() == "blue"
|
||||
|
||||
// Day.to_string()
|
||||
let d = Day.friday
|
||||
println(d.to_string())
|
||||
assert d.to_string() == "friday"
|
||||
|
||||
let sun = Day.sunday
|
||||
println(sun.to_string())
|
||||
assert sun.to_string() == "sunday"
|
||||
|
||||
// enum variant literal .to_string()
|
||||
println(Color.green.to_string())
|
||||
assert Color.green.to_string() == "green"
|
||||
|
||||
println(Day.monday.to_string())
|
||||
assert Day.monday.to_string() == "monday"
|
||||
|
||||
println("All to_string assertions passed!")
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// maps.vr — demonstrates map/dictionary features
|
||||
|
||||
fn main() {
|
||||
// create a map using struct literal syntax
|
||||
let person = { "name": "alice", "age": 30 }
|
||||
|
||||
// access with string index
|
||||
assert person["name"] == "alice"
|
||||
assert person["age"] == 30
|
||||
println("person[\"name\"] = alice")
|
||||
println("person[\"age\"] = 30")
|
||||
|
||||
// update with string index
|
||||
person["age"] = 31
|
||||
assert person["age"] == 31
|
||||
println("person[\"age\"] after update = 31")
|
||||
|
||||
// add new keys dynamically
|
||||
person["city"] = "nyc"
|
||||
assert person["city"] == "nyc"
|
||||
println("person[\"city\"] = nyc")
|
||||
|
||||
// check key existence with has()
|
||||
assert has(person, "name") == 1
|
||||
assert has(person, "missing") == 0
|
||||
println("has(person, \"name\") = 1")
|
||||
println("has(person, \"missing\") = 0")
|
||||
|
||||
// get all keys with keys()
|
||||
let k = keys(person)
|
||||
assert len(k) == 3
|
||||
println("keys count = 3")
|
||||
|
||||
// delete a key
|
||||
delete(person, "city")
|
||||
assert has(person, "city") == 0
|
||||
assert len(person) == 2
|
||||
println("after delete: len = 2")
|
||||
|
||||
// len() works on maps
|
||||
let scores = { "math": 95, "english": 88 }
|
||||
assert len(scores) == 2
|
||||
println("scores len = 2")
|
||||
|
||||
// nested maps
|
||||
let data = { "user": { "name": "bob" } }
|
||||
assert data["user"]["name"] == "bob"
|
||||
println("nested access = bob")
|
||||
|
||||
// maps in arrays
|
||||
let users = [{ "name": "a" }, { "name": "b" }]
|
||||
assert users[0]["name"] == "a"
|
||||
assert users[1]["name"] == "b"
|
||||
println("maps in arrays OK")
|
||||
|
||||
// empty map
|
||||
let empty = {}
|
||||
assert len(empty) == 0
|
||||
println("empty map len = 0")
|
||||
|
||||
println("All map assertions passed!")
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// maps_debug.vr — debug map keys function
|
||||
|
||||
fn main() {
|
||||
let person = { "name": "alice", "age": 30 }
|
||||
|
||||
let k = keys(person)
|
||||
let n = len(k)
|
||||
println("len(k) = 2")
|
||||
|
||||
if n > 0 {
|
||||
println("key0")
|
||||
}
|
||||
if n > 1 {
|
||||
println("key1")
|
||||
}
|
||||
|
||||
assert n == 2
|
||||
println("keys OK")
|
||||
}
|
||||
Reference in New Issue
Block a user