DEV Community

Cover image for Razen Lang Guide: Simple, Fast, and Developer-Friendly
Prathmesh barot
Prathmesh barot

Posted on • Edited on

Razen Lang Guide: Simple, Fast, and Developer-Friendly

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!");
}
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Mutable Variables

Use var for values that can be modified:

var age: int = 25;
var is_active: bool = true;
var balance: float = 1000.50;
Enter fullscreen mode Exit fullscreen mode

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';
Enter fullscreen mode Exit fullscreen mode

Collections

const numbers: array<int> = [1, 2, 3, 4, 5];
const user_data: map<str, any> = {
    "name": "Alice",
    "age": 30,
    "active": true
};
Enter fullscreen mode Exit fullscreen mode

Dynamic Typing

var data = "flexible content";
data = 42;  // Can be reassigned to different types
Enter fullscreen mode Exit fullscreen mode

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}")
}
Enter fullscreen mode Exit fullscreen mode

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")
}
Enter fullscreen mode Exit fullscreen mode

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}")
}
Enter fullscreen mode Exit fullscreen mode

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")
}
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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!")
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}")
}
Enter fullscreen mode Exit fullscreen mode

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}")
}
Enter fullscreen mode Exit fullscreen mode

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")
    }
}
Enter fullscreen mode Exit fullscreen mode

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
""";
Enter fullscreen mode Exit fullscreen mode

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)
}
Enter fullscreen mode Exit fullscreen mode

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()
}
Enter fullscreen mode Exit fullscreen mode

Next Steps

To continue learning Razen Lang:

  1. Check the GitHub repository for more examples
  2. Read the language specification for advanced features
  3. Join the community discussions and contribute to the project
  4. Build small projects to practice the syntax and concepts

Happy coding with Razen Lang!

Top comments (0)