Getting Started with Razen Lang
Razen Lang is a beginner-friendly programming language designed for simplicity, readability, and ease of use. This guide covers everything you need to start writing Razen code, from installation to core language features.
Installation
Razen Lang supports multiple operating systems with platform-specific installation methods. Visit the official GitHub repository for detailed installation instructions for your system:
GitHub Repository: BasaiCorp/Razen-Lang
The repository contains installation guides, examples, and the latest updates.
File Structure
Every Razen program begins with a module declaration:
mod main;
fun main() {
println("Hello, World!");
}
Variables and Constants
Razen provides two ways to declare variables: mutable and immutable.
Immutable Variables
Use const
for values that won't change:
const name: str = "Razen";
const version: int = 1;
const pi: float = 3.14159;
Mutable Variables
Use var
for values that can be modified:
var age: int = 25;
var is_active: bool = true;
var balance: float = 1000.50;
Data Types
Razen supports several built-in data types:
Primitive Types
const number: int = 42;
const text: str = "Hello, Razen!";
const flag: bool = true;
const letter: char = 'R';
Collections
const numbers: array<int> = [1, 2, 3, 4, 5];
const user_data: map<str, any> = {
"name": "Alice",
"age": 30,
"active": true
};
Dynamic Typing
var data = "flexible content";
data = 42; // Can be reassigned to different types
Functions
Define functions using the fun
keyword:
fun greet(name: str) -> str {
return f"Hello, {name}!"
}
fun add_numbers(a: int, b: int) -> int {
return a + b
}
fun main() {
const message = greet("World")
println(message)
const result = add_numbers(5, 3)
println(f"5 + 3 = {result}")
}
Control Flow
Conditional Statements
const age = 18
if age >= 18 {
println("You are an adult")
} elif age >= 13 {
println("You are a teenager")
} else {
println("You are a child")
}
Loops
// While loop
var counter = 0
while counter < 5 {
println(f"Count: {counter}")
counter++
}
// For loop
const items = ["apple", "banana", "cherry"]
for item in items {
println(f"Fruit: {item}")
}
// Range-based loop
for i in 1..10 {
println(f"Number: {i}")
}
Pattern Matching
Use match
for powerful pattern matching:
const value = 42
match value {
0 => println("Zero"),
1..10 => println("Small number"),
42 => println("The answer!"),
_ => println("Something else")
}
Error Handling
Razen provides structured error handling:
fun divide(a: int, b: int) -> int {
try {
if b == 0 {
throw "Division by zero";
}
return a / b;
} catch error {
println(f"Error: {error}");
return 0;
}
}
Input and Output
Console I/O
println("Enter your name:")
const user_name = input()
println(f"Hello, {user_name}!")
print("Loading") // No newline
println("... Done!")
File I/O
const file_handle = open("data.txt", "r")
const content = read(file_handle)
close(file_handle)
const output_file = open("output.txt", "w")
write(output_file, "Hello, File!")
close(output_file)
Modules and Imports
Creating Modules
// math_utils.rzn
mod math_utils
pub fun square(x: int) -> int {
return x * x
}
pub const PI: float = 3.14159
Using Modules
mod main
use "math_utils"
use {square, PI} from "math_utils"
from "math_utils" use square as sq
fun main() {
const result = square(5)
println(f"Square of 5: {result}")
println(f"PI value: {PI}")
}
Structures and Enums
Defining Structures
struct Person {
name: str,
age: int,
email: str
}
fun main() {
const person = Person {
name: "Alice",
age: 30,
email: "alice@example.com"
};
println(f"Name: {person.name}")
println(f"Age: {person.age}")
}
Defining Enums
enum Status {
Active,
Inactive,
Pending
}
fun check_status(status: Status) {
match status {
Status::Active => println("User is active"),
Status::Inactive => println("User is inactive"),
Status::Pending => println("User status is pending")
}
}
String Interpolation and Formatting
Razen supports modern string interpolation:
const name = "Razen"
const version = 1.0
const message = f"Welcome to {name} version {version}!"
println(message)
// Raw strings
const file_path = r"C:\Users\Documents\file.txt";
const multiline = """
This is a
multiline string
in Razen
""";
Comments and Documentation
// Single-line comment
/*
* Multi-line comment
* for detailed explanations
*/
/// Documentation comment for functions
/// This function calculates the factorial of a number
fun factorial(n: int) -> int {
if n <= 1 {
return 1
}
return n * factorial(n - 1)
}
Complete Example
mod calculator
struct Calculator {
history: array<str>
}
impl Calculator {
pub fun new() -> Calculator {
return Calculator {
history: []
}
}
pub fun add(&mut self, a: int, b: int) -> int {
const result = a + b;
self.history.push(f"{a} + {b} = {result}")
return result
}
pub fun show_history(&self) {
println("Calculation History:")
for entry in self.history {
println(f" {entry}")
}
}
}
fun main() {
var calc = Calculator::new()
const sum1 = calc.add(10, 5)
const sum2 = calc.add(20, 15)
println(f"Results: {sum1}, {sum2}")
calc.show_history()
}
Next Steps
To continue learning Razen Lang:
- Check the GitHub repository for more examples
- Read the language specification for advanced features
- Join the community discussions and contribute to the project
- Build small projects to practice the syntax and concepts
Happy coding with Razen Lang!
Top comments (0)