mirror of
https://github.com/bearlanguageorg/bear.git
synced 2026-08-26 14:57:18 +00:00
Tier Three: Added threads/jobs
This commit is contained in:
+2
-2
@@ -2,7 +2,7 @@
|
||||
// Run with: vr run examples/arrays.vr
|
||||
|
||||
fn sum(items) {
|
||||
let total = 0
|
||||
mut total = 0
|
||||
for x in items {
|
||||
total = total + x
|
||||
}
|
||||
@@ -79,7 +79,7 @@ fn main() {
|
||||
println("")
|
||||
|
||||
// find the first even number in an array
|
||||
let found = -1
|
||||
mut found = -1
|
||||
for x in a {
|
||||
if x % 2 == 0 {
|
||||
found = x
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// compound assignment operators: +=, -=, *=, /=
|
||||
|
||||
fn main() {
|
||||
let x = 10
|
||||
mut x = 10
|
||||
println(x)
|
||||
|
||||
x += 5
|
||||
|
||||
@@ -25,7 +25,7 @@ fn main() {
|
||||
}
|
||||
|
||||
// iterate and use count
|
||||
let total = 0
|
||||
mut total = 0
|
||||
for c in Color {
|
||||
total = total + c.count()
|
||||
}
|
||||
@@ -40,7 +40,7 @@ fn main() {
|
||||
}
|
||||
|
||||
// break and continue work
|
||||
let weekdays = 0
|
||||
mut weekdays = 0
|
||||
for d in Day {
|
||||
if d == Day.saturday {
|
||||
break
|
||||
|
||||
@@ -10,7 +10,7 @@ fn main() {
|
||||
|
||||
// use the index for computation
|
||||
let nums = [10, 20, 30, 40, 50]
|
||||
let sum = 0
|
||||
mut sum = 0
|
||||
for i, v in nums {
|
||||
if i % 2 == 0 {
|
||||
sum += v
|
||||
|
||||
+3
-3
@@ -15,8 +15,8 @@ fn fib(n) {
|
||||
|
||||
// returns n! via a loop
|
||||
fn fact(n) {
|
||||
let result = 1
|
||||
let i = 1
|
||||
mut result = 1
|
||||
mut i = 1
|
||||
while i <= n {
|
||||
result = result * i
|
||||
i = i + 1
|
||||
@@ -54,7 +54,7 @@ fn main() {
|
||||
}
|
||||
|
||||
// while loop
|
||||
let i = 0
|
||||
mut i = 0
|
||||
while i < 3 {
|
||||
println("counting " + i)
|
||||
i = i + 1
|
||||
|
||||
+1
-1
@@ -64,7 +64,7 @@ fn main() {
|
||||
}
|
||||
|
||||
// match inside a loop (break/continue still work)
|
||||
let found = 0
|
||||
mut found = 0
|
||||
for i in 0..10 {
|
||||
match i {
|
||||
4 {
|
||||
|
||||
+89
-20
@@ -33,8 +33,8 @@ fn test_strings() {
|
||||
}
|
||||
|
||||
fn test_loops() {
|
||||
let total = 0
|
||||
let i = 1
|
||||
mut total = 0
|
||||
mut i = 1
|
||||
while i <= 10 {
|
||||
total = total + i
|
||||
i = i + 1
|
||||
@@ -64,12 +64,12 @@ fn test_arrays() {
|
||||
}
|
||||
|
||||
fn test_for_range() {
|
||||
let total = 0
|
||||
mut total = 0
|
||||
for i in 0..5 {
|
||||
total = total + i
|
||||
}
|
||||
assert total == 10 // 0+1+2+3+4
|
||||
let inc = 0
|
||||
mut inc = 0
|
||||
for i in 1...3 {
|
||||
inc = inc + i
|
||||
}
|
||||
@@ -78,7 +78,7 @@ fn test_for_range() {
|
||||
|
||||
fn test_for_in() {
|
||||
let a = [5, 6, 7]
|
||||
let total = 0
|
||||
mut total = 0
|
||||
for x in a {
|
||||
total = total + x
|
||||
}
|
||||
@@ -94,7 +94,7 @@ fn test_for_in() {
|
||||
|
||||
fn test_break_continue() {
|
||||
// break exits the loop early
|
||||
let total = 0
|
||||
mut total = 0
|
||||
for i in 0..100 {
|
||||
if i == 5 {
|
||||
break
|
||||
@@ -104,7 +104,7 @@ fn test_break_continue() {
|
||||
assert total == 10 // 0+1+2+3+4
|
||||
|
||||
// continue skips to the next iteration
|
||||
let sum = 0
|
||||
mut sum = 0
|
||||
for i in 0..6 {
|
||||
if i == 3 {
|
||||
continue
|
||||
@@ -114,7 +114,7 @@ fn test_break_continue() {
|
||||
assert sum == 12 // 0+1+2+4+5 (3 skipped)
|
||||
|
||||
// break and continue in while loops
|
||||
let n = 0
|
||||
mut n = 0
|
||||
while true {
|
||||
n = n + 1
|
||||
if n == 3 {
|
||||
@@ -141,7 +141,7 @@ fn test_break_continue() {
|
||||
|
||||
fn test_else_if() {
|
||||
let score = 85
|
||||
let grade = ""
|
||||
mut grade = ""
|
||||
if score >= 90 {
|
||||
grade = "A"
|
||||
} else if score >= 80 {
|
||||
@@ -156,7 +156,7 @@ fn test_else_if() {
|
||||
}
|
||||
|
||||
fn test_match() {
|
||||
let total = 0
|
||||
mut total = 0
|
||||
match 2 {
|
||||
1 {
|
||||
total = total + 1
|
||||
@@ -172,7 +172,7 @@ fn test_match() {
|
||||
|
||||
// match on strings
|
||||
let word = "cat"
|
||||
let sound = ""
|
||||
mut sound = ""
|
||||
match word {
|
||||
"dog" {
|
||||
sound = "woof"
|
||||
@@ -187,7 +187,7 @@ fn test_match() {
|
||||
assert sound == "meow"
|
||||
|
||||
// no else arm: nothing runs when nothing matches
|
||||
let hit = 0
|
||||
mut hit = 0
|
||||
match 99 {
|
||||
1 {
|
||||
hit = 1
|
||||
@@ -376,7 +376,7 @@ fn test_closures() {
|
||||
assert "v=" + str(dbl(4)) == "v=8"
|
||||
// closure containing a loop
|
||||
let sum = fn () {
|
||||
let total = 0
|
||||
mut total = 0
|
||||
for i in 0..5 {
|
||||
total = total + i
|
||||
}
|
||||
@@ -402,7 +402,7 @@ fn test_enums() {
|
||||
let next = Suit.hearts + 1
|
||||
assert next == 1
|
||||
// enum iteration
|
||||
let names = []
|
||||
mut names = []
|
||||
for s in Suit {
|
||||
names = push(names, s.to_string())
|
||||
}
|
||||
@@ -471,7 +471,7 @@ fn test_closure_capture() {
|
||||
assert greet() == "hi alice age=30"
|
||||
|
||||
// capture by value: later changes to the outer var don't affect the closure
|
||||
let counter = 10
|
||||
mut counter = 10
|
||||
let read = fn() {
|
||||
return counter
|
||||
}
|
||||
@@ -491,7 +491,7 @@ fn test_closure_capture() {
|
||||
assert outer() == 106
|
||||
|
||||
// assignment to a captured variable writes the closure's own copy
|
||||
let n = 1
|
||||
mut n = 1
|
||||
let bump = fn() {
|
||||
n = n + 1
|
||||
return n
|
||||
@@ -530,17 +530,17 @@ fn test_dynamic_map_keys() {
|
||||
|
||||
fn test_sort_large() {
|
||||
// merge sort must handle large arrays (O(n log n)) and stay sorted
|
||||
let a = []
|
||||
mut a = []
|
||||
let n = 2000
|
||||
let i = 0
|
||||
mut i = 0
|
||||
while i < n {
|
||||
a = push(a, (n - i) % 97)
|
||||
i = i + 1
|
||||
}
|
||||
sort(a)
|
||||
assert len(a) == n
|
||||
let ok = 1
|
||||
let j = 1
|
||||
mut ok = 1
|
||||
mut j = 1
|
||||
while j < n {
|
||||
if a[j - 1] > a[j] {
|
||||
ok = 0
|
||||
@@ -552,6 +552,75 @@ fn test_sort_large() {
|
||||
assert a[n - 1] == 96
|
||||
}
|
||||
|
||||
fn test_mutable_bindings() {
|
||||
// `let` is immutable: reassignment is a compile-time error (tested in CI)
|
||||
// `mut` opts into reassignment
|
||||
mut counter = 0
|
||||
counter = counter + 1
|
||||
counter = counter + 2
|
||||
assert counter == 3
|
||||
// `let` still reads fine and does not accept reassignment
|
||||
let fixed = 42
|
||||
assert fixed == 42
|
||||
}
|
||||
|
||||
fn test_string_builder() {
|
||||
// string builders avoid O(n^2) reallocation of repeated `+`
|
||||
mut sb = strings.builder()
|
||||
for i in 0..10 {
|
||||
sb = strings.build_add(sb, "x" + str(i) + ",")
|
||||
}
|
||||
let out = strings.build_str(sb)
|
||||
assert out == "x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,"
|
||||
}
|
||||
|
||||
fn test_spawn_join() {
|
||||
// spawn runs a closure on an OS thread; spawn_join waits and returns
|
||||
let j1 = spawn(worker_sum, 100)
|
||||
let j2 = spawn(worker_sum, 200)
|
||||
let r1 = spawn_join(j1)
|
||||
let r2 = spawn_join(j2)
|
||||
assert r1 == 4950
|
||||
assert r2 == 19900
|
||||
}
|
||||
|
||||
fn worker_sum(n) {
|
||||
mut s = 0
|
||||
mut i = 0
|
||||
while i < n {
|
||||
s = s + i
|
||||
i = i + 1
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
fn test_spawn_string_result() {
|
||||
let j = spawn(worker_greet, "world")
|
||||
let res = spawn_join(j)
|
||||
assert res == "hello world"
|
||||
}
|
||||
|
||||
fn worker_greet(name) {
|
||||
return "hello " + name
|
||||
}
|
||||
|
||||
fn test_spawn_error_propagation() {
|
||||
mut caught = false
|
||||
try {
|
||||
let j = spawn(worker_boom)
|
||||
let r = spawn_join(j)
|
||||
assert r == 0
|
||||
} catch e {
|
||||
caught = true
|
||||
}
|
||||
assert caught
|
||||
}
|
||||
|
||||
fn worker_boom() {
|
||||
let x = 1 / 0
|
||||
return x
|
||||
}
|
||||
|
||||
fn test_failing() {
|
||||
// this one is meant to fail — shows up in `vr test` output
|
||||
assert 1 == 2
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ fn main() {
|
||||
|
||||
// milliseconds for timing
|
||||
let start = time.ms()
|
||||
let x = 0
|
||||
mut x = 0
|
||||
for i in 0..100000 {
|
||||
x = x + i
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user