Getting Started with Brute
Welcome to Brute! This guide will help you get started with the Brute programming language.
Installation
Currently, Brute is in development, but you can build it from source:
bash
# Clone the repository
git clone https://github.com/w0wzahh/brute.git
cd brute
# Build the compiler
cargo build --release
# Add Brute to your PATH
# For Linux/Mac
export PATH="$PATH:$(pwd)/target/release"
# For Windows
# Add the path to your PATH environment variableHello World
Let's start with a simple "Hello, World!" program. Create a file named hello.brt with the following content:
rust
fn main() -> void {
println("Hello, World!");
}To run this program:
bash
brute run hello.brtCongratulations! You've just written and run your first Brute program.
Variables and Types
Brute supports variable declarations with type inference:
rust
// Variable declaration with type inference
let name = "World";
// Explicit type annotation
let age: int = 30;
// Mutable variables
let mut counter = 0;
counter += 1; // This is allowed because counter is mutableBrute has several built-in types:
int: Integer numbersfloat: Floating point numbersbool: Boolean valuesstring: Text stringschar: Single characters
Control Flow
If Statements
rust
let x = 42;
if x > 100 {
println("x is greater than 100");
} else if x > 50 {
println("x is greater than 50 but not greater than 100");
} else {
println("x is 50 or less");
}Loops
rust
// While loop
let mut i = 0;
while i < 5 {
println("i is " + i.to_string());
i += 1;
}
// For loop
for j in range(0, 5) {
println("j is " + j.to_string());
}
// Iterating over collections
let fruits = ["apple", "banana", "cherry"];
for fruit in fruits {
println("I like " + fruit);
}Functions
rust
// Simple function
fn greet(name: string) -> void {
println("Hello, " + name + "!");
}
// Function with return value
fn add(a: int, b: int) -> int {
return a + b;
}
// Function with default parameter
fn greet_with_default(name: string = "World") -> void {
println("Hello, " + name + "!");
}
// Using the functions
greet("Alice");
let sum = add(5, 7);
println("Sum: " + sum.to_string());
greet_with_default(); // Uses default parameterCollections
Lists
rust
// Creating a list
let numbers = [1, 2, 3, 4, 5];
// Accessing elements
let first = numbers[0];
// Adding elements
numbers.push(6);
// List methods
let length = numbers.len();
let contains_three = numbers.contains(3);
// List comprehension
let squares = [x * x for x in numbers];Dictionaries
rust
// Creating a dictionary
let user = {
"name": "Alice",
"age": 30,
"is_admin": true
};
// Accessing elements
let name = user["name"];
// Adding elements
user["email"] = "alice@example.com";
// Dictionary methods
let keys = user.keys();
let has_email = user.contains_key("email");Structs and Methods
rust
// Defining a struct
struct Rectangle {
width: float,
height: float,
// Method
fn area() -> float {
return self.width * self.height;
}
// Static method (constructor)
fn new(w: float, h: float) -> Rectangle {
return Rectangle { width: w, height: h };
}
}
// Creating an instance
let rect1 = Rectangle { width: 10.0, height: 5.0 };
let rect2 = Rectangle::new(20.0, 15.0);
// Calling methods
let area1 = rect1.area();
let area2 = rect2.area();
println("Area 1: " + area1.to_string());
println("Area 2: " + area2.to_string());