Brute Language Reference
Comprehensive documentation of Brute's syntax, types, functions, and standard library.
Syntax Overview
Brute's syntax is designed to be familiar to developers coming from Rust, Python, or C/C++. It emphasizes readability while maintaining expressiveness.
Comments
// This is a single-line comment
/*
This is a
multi-line comment
*/
/// Documentation comment for functions, structs, etc.
/// Supports markdown formattingType System
Brute features a static type system with type inference. The following are the primary built-in types:
Primitive Types
int- Signed integer (default is platform-dependent, typically 32 or 64-bit)float- Floating-point number (default is 64-bit)bool- Boolean type with valuestrueandfalsechar- Single Unicode characterstring- UTF-8 encoded string
Type Declarations
// Type inference
let x = 5; // x is inferred as int
let name = "Alice"; // name is inferred as string
// Explicit type annotations
let y: float = 3.14;
let active: bool = true;
// Type aliases
type UserId = int;
let user_id: UserId = 1001;Expressions
An expression in Brute evaluates to a value. Most constructs in Brute are expressions.
Literals
// Integer literals
42 // Decimal
0x2A // Hexadecimal
0b101010 // Binary
0o52 // Octal
// Float literals
3.14
2.71e2 // Scientific notation
// Boolean literals
true
false
// Character literal
'A'
// String literals
"Hello, World!"
"Line 1\nLine 2" // With escape sequences
// Multiline strings
"""
This is a multiline
string in Brute
"""Operators
Arithmetic Operators
+- Addition-- Subtraction*- Multiplication/- Division%- Modulo (remainder)**- Exponentiation
Comparison Operators
==- Equal to!=- Not equal to<- Less than>- Greater than<=- Less than or equal to>=- Greater than or equal to
Logical Operators
&&- Logical AND||- Logical OR!- Logical NOT
Statements
Statements are instructions that perform some action but don't return a value.
Variable Declarations
// Immutable variable declaration
let x = 5;
// Mutable variable declaration
let mut y = 10;
// Constants
const MAX_ITEMS = 100;Control Flow
// If statement
if condition {
// Code executed if condition is true
} else if another_condition {
// Code executed if another_condition is true
} else {
// Code executed if no conditions are true
}
// Match statement
match value {
pattern1 => expression1,
pattern2 => expression2,
_ => default_expression,
}
// While loop
while condition {
// Loop body
}
// For loop
for item in collection {
// Loop body
}
// Range-based for loop
for i in 0..10 {
// Loop body, i takes values 0 through 9
}Functions
Functions in Brute are defined using the fn keyword.
// Basic function
fn add(a: int, b: int) -> int {
return a + b;
}
// Function with no return value (void)
fn print_hello() -> void {
println("Hello!");
}
// Function with default parameters
fn greet(name: string, greeting: string = "Hello") -> string {
return greeting + ", " + name + "!";
}
// Function with variadic parameters
fn sum(...numbers: int) -> int {
let total = 0;
for n in numbers {
total = total + n;
}
return total;
}
// Function with early return
fn is_even(num: int) -> bool {
if num % 2 == 0 {
return true;
}
return false;
}Structs
Structs are used to create custom data types that group related values together.
// Struct definition
struct Point {
x: float,
y: float,
// Method
fn distance_from_origin() -> float {
return (self.x * self.x + self.y * self.y).sqrt();
}
// Static method (constructor)
fn new(x: float, y: float) -> Point {
return Point { x: x, y: y };
}
}
// Creating an instance
let p1 = Point { x: 3.0, y: 4.0 };
let p2 = Point::new(5.0, 12.0);
// Accessing fields
let x_coord = p1.x;
// Calling methods
let distance = p1.distance_from_origin();Modules
Modules are used to organize code into logical units and control visibility (public vs. private).
// Defining a module
module math {
// Public function (can be accessed outside the module)
pub fn add(a: int, b: int) -> int {
return a + b;
}
// Private function (only accessible within the module)
fn subtract(a: int, b: int) -> int {
return a - b;
}
// Nested module
module advanced {
pub fn power(base: int, exponent: int) -> int {
return base ** exponent;
}
}
}
// Importing modules
import math;
import math.advanced;
// Using imported modules
let sum = math.add(5, 3);
let pow = math.advanced.power(2, 8);Error Handling
Brute provides multiple mechanisms for error handling: try/catch blocks and Result types.
Try/Catch
try {
// Code that might throw an error
let result = risky_operation();
} catch err {
// Code that handles the error
println("Error: " + err.to_string());
} finally {
// Code that always runs, whether there was an error or not
cleanup_resources();
}Result Type
// Function returning a Result type
fn divide(a: int, b: int) -> Result<int, string> {
if b == 0 {
return Err("Division by zero");
}
return Ok(a / b);
}
// Using the Result
let result = divide(10, 2);
if result.is_ok() {
println("Result: " + result.unwrap().to_string());
} else {
println("Error: " + result.unwrap_err());
}
// Using the ? operator for error propagation
fn calculate() -> Result<int, string> {
let a = divide(10, 2)?; // Returns early if error
let b = divide(20, a)?; // Returns early if error
return Ok(b);
}