mirror of
https://github.com/bearlanguageorg/bear.git
synced 2026-08-26 14:57:18 +00:00
Production pass: floats, GC, type checking, and tooling
Adds floats, bitwise ops, UTF-8 strings with methods, try/catch, closures, generics validation, a compile-time type checker, a mark-and-sweep GC, source-level debug info, constant folding, and the repl/fmt/package-manager commands. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// for i, v in arr — iterate with both index and value
|
||||
|
||||
fn main() {
|
||||
let fruits = ["apple", "banana", "cherry"]
|
||||
|
||||
// basic indexed iteration
|
||||
for i, v in fruits {
|
||||
println("${i}: ${v}")
|
||||
}
|
||||
|
||||
// use the index for computation
|
||||
let nums = [10, 20, 30, 40, 50]
|
||||
let sum = 0
|
||||
for i, v in nums {
|
||||
if i % 2 == 0 {
|
||||
sum += v
|
||||
}
|
||||
}
|
||||
println("sum of even-indexed elements: ${sum}")
|
||||
|
||||
// build a new array using index
|
||||
let letters = ["a", "b", "c", "d"]
|
||||
let tagged = []
|
||||
for i, v in letters {
|
||||
push(tagged, "${v}${i}")
|
||||
}
|
||||
println(tagged)
|
||||
|
||||
// plain for-in still works (no index)
|
||||
for v in fruits {
|
||||
print(v)
|
||||
print(" ")
|
||||
}
|
||||
println("")
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// generic function syntax (V-style type parameters)
|
||||
|
||||
// generic functions: type params are parsed but the VM handles types dynamically
|
||||
fn first[T](arr) {
|
||||
return arr[0]
|
||||
}
|
||||
|
||||
fn last[T](arr) {
|
||||
let n = len(arr)
|
||||
return arr[n - 1]
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// works with integers
|
||||
let nums = [10, 20, 30]
|
||||
println("first num: ${first[int](nums)}")
|
||||
println("last num: ${last[int](nums)}")
|
||||
|
||||
// works with strings
|
||||
let words = ["hello", "world", "foo"]
|
||||
println("first word: ${first[string](words)}")
|
||||
println("last word: ${last[string](words)}")
|
||||
|
||||
// type args are optional (VM is dynamically typed)
|
||||
println("first (no type arg): ${first(nums)}")
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// string interpolation: embed expressions inside "..." with ${}
|
||||
|
||||
fn main() {
|
||||
let name = "alice"
|
||||
let age = 30
|
||||
|
||||
// simple variable interpolation
|
||||
println("hello ${name}!")
|
||||
|
||||
// multiple interpolations
|
||||
println("${name} is ${age} years old")
|
||||
|
||||
// expression inside interpolation
|
||||
println("${name} will be ${age + 1} next year")
|
||||
|
||||
// interpolation with function calls
|
||||
let nums = [10, 20, 30]
|
||||
println("first element: ${nums[0]}")
|
||||
|
||||
// string concatenation with interpolation
|
||||
let greeting = "hi"
|
||||
println("${greeting}, ${name}! age=${age}")
|
||||
|
||||
// no interpolation (regular string)
|
||||
println("no interpolation here")
|
||||
|
||||
// empty interpolation adjacent to text
|
||||
let x = 42
|
||||
println("value is ${x}")
|
||||
|
||||
// arithmetic in interpolation
|
||||
println("double age: ${age * 2}")
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// array and string slicing: arr[1..3], arr[1:], str[0..5]
|
||||
|
||||
fn main() {
|
||||
// --- array slicing ---
|
||||
let nums = [10, 20, 30, 40, 50]
|
||||
|
||||
// basic slice: arr[start..end]
|
||||
let mid = nums[1..3]
|
||||
println(mid) // [20, 30]
|
||||
|
||||
// slice from start
|
||||
let first_two = nums[0..2]
|
||||
println(first_two) // [10, 20]
|
||||
|
||||
// slice to end (open-ended)
|
||||
let tail = nums[2..]
|
||||
println(tail) // [30, 40, 50]
|
||||
|
||||
// single element slice
|
||||
let single = nums[3..4]
|
||||
println(single) // [40]
|
||||
|
||||
// --- string slicing ---
|
||||
let greeting = "hello, world!"
|
||||
|
||||
// basic substring
|
||||
let hello = greeting[0..5]
|
||||
println(hello) // "hello"
|
||||
|
||||
// slice from index
|
||||
let world = greeting[7..]
|
||||
println(world) // "world!"
|
||||
|
||||
// slice to index
|
||||
let hello_comma = greeting[0..6]
|
||||
println(hello_comma) // "hello,"
|
||||
|
||||
// --- slicing in expressions ---
|
||||
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
let first = data[0..3]
|
||||
let last = data[7..]
|
||||
println(first)
|
||||
println(last)
|
||||
}
|
||||
Reference in New Issue
Block a user