8. Documentation
Expressions
Expressions are the building blocks of Braid programs. They produce values and can be composed using operators, function calls, and access patterns.
Literals
The simplest expressions are literal values:
42 // integer literal
3.14 // float literal
"hello" // string literal
true // boolean literal
false // boolean literal
nil // null literalIdentifiers
Variable and parameter names evaluate to their bound value:
let x = 10
let y = x // y = 10
print(x) // function call with identifierBinary Operators
Arithmetic:
let sum = 10 + 5 // 15
let diff = 10 - 5 // 5
let prod = 10 * 5 // 50
let quot = 10 / 5 // 2
let rem = 10 % 3 // 1
// String concatenation
let s = "Hello, " + "World" // "Hello, World"Comparison:
let eq = 5 == 5 // true
let ne = 5 != 5 // false
let lt = 3 < 5 // true
let le = 3 <= 5 // true
let gt = 5 > 3 // true
let ge = 5 >= 5 // trueLogical:
let and = true && false // false
let or = true || false // true
let not = !true // falseUnary Operators
let neg = -42 // unary negation
let not_flag = !true // logical NOTFunction Calls
print("Hello")
let result = add(10, 20)
let r = math.sqrt(9.0)
// Chained calls
let x = foo(bar(baz(1)))Method Calls
diameter.evolve()
tensor.to("cuda")
temperature.observe("tension")Member Access (.)
let p = Point { x: 10, y: 20 }
let x_val = p.x
let y_val = p.y
let circle = Circle { center: Point { x: 0, y: 0 }, radius: 5.0 }
let cx = circle.center.xSubscript / Index ([])
let arr = [1, 2, 3, 4, 5]
let first = arr[0] // 1
let third = arr[2] // 3
let matrix = [[1, 2], [3, 4]]
let val = matrix[0][1] // 2Tensor Slicing
let matrix = [[1, 2], [3, 4], [5, 6]]
let slice = matrix[0:2] // [[1, 2], [3, 4]]
let all = matrix[0:3] // full range
let row = matrix[1:2] // [[3, 4]]
// Multi-dimensional slicing
let tensor3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
let sub = tensor3d[..., 0:2] // ellipsis for all remaining dimsAssignment
Assignment is an expression that produces the assigned value:
let x = 0
x = 42 // assignment expression
let counter = 0
counter = counter + 1
counter = 100Grouping with Parentheses
let result = (10 + 5) * 2 // 30
let combined = (x > 0) && (y < 10)Operator Precedence
From highest to lowest precedence:
1. () parentheses
2. - ! unary negation / NOT
3. * / % multiplicative
4. + - additive
5. < <= > >= == != comparison
6. && logical AND
7. || logical ORExpression Examples
// Compound expressions
let a = 10 + 20 * 3 / 2 - 5 // 10 + 30 - 5 = 35
let b = (10 + 20) * 3 / (2 - 5) // 30 * 3 / -3 = -30
let c = a > b && b != 0
let d = !(a == b) || c
// Function calls in expressions
let result = add(mul(2, 3), div(10, 2)) // 6 + 5 = 11