Getting Started

Welcome to Rui! This guide will help you write your first programs and get familiar with the language. We'll start with simple examples and gradually build up to more complex programs.

Your First Program

Let's start with the classic "Hello, World!" program. Create a file called hello.rui:

write("Hello, World!")

Run it with:

rui hello.rui

You should see:

Hello, World!

Basic Calculator

Let's build a simple calculator that demonstrates variables, functions, and basic operations:

define add(a, b) {
    return a + b
}

define subtract(a, b) {
    return a - b
}

define multiply(a, b) {
    return a * b
}

define divide(a, b) {
    if (b != 0) {
        return a / b
    } else {
        write("Error: Cannot divide by zero!")
        return 0
    }
}

write("Calculator Demo:")
write("5 + 3 = " + add(5, 3))
write("10 - 4 = " + subtract(10, 4))
write("6 * 7 = " + multiply(6, 7))
write("15 / 3 = " + divide(15, 3))

Array Operations

Here's an example that demonstrates array creation, manipulation, and iteration:

suppose numbers = [1, 2, 3, 4, 5]

write("Original: " + numbers)

// Add elements
numbers.push(6, 7)
write("After push: " + numbers)

// Remove elements
write("Popped: " + numbers.pop())
write("Final: " + numbers)

// Find sum
suppose sum = 0
suppose i = 0
until (i < length(numbers)) {
    sum = sum + numbers[i]
    i = i + 1
}
write("Sum: " + sum)

// Find average
suppose average = sum / length(numbers)
write("Average: " + average)

String Manipulation

This example shows various string operations and methods:

suppose text = "Hello World"

write("Original: " + text)
write("Upper: " + text.upper())
write("Lower: " + text.lower())
write("Split: " + text.split(" "))

// Process a sentence
suppose sentence = "The quick brown fox jumps over the lazy dog"
write("Original: " + sentence)

// Split into words
suppose words = sentence.split(" ")
write("Words: " + words)
write("Word count: " + length(words))

// Find longest word
suppose longest = words[0]
suppose i = 1
until (i < length(words)) {
    if (length(words[i]) > length(longest)) {
        longest = words[i]
    }
    i = i + 1
}
write("Longest word: " + longest)

Control Flow Example

This example demonstrates conditionals and loops with a grade calculator:

suppose scores = [85, 92, 78, 96, 88]

write("Student Grade Report:")
write("====================")

suppose i = 0
until (i < length(scores)) {
    suppose score = scores[i]
    suppose grade = ""
    
    if (score >= 90) {
        grade = "A"
    } else if (score >= 80) {
        grade = "B"
    } else if (score >= 70) {
        grade = "C"
    } else if (score >= 60) {
        grade = "D"
    } else {
        grade = "F"
    }
    
    write("Score " + score + ": " + grade)
    i = i + 1
}

// Calculate class average
suppose total = 0
suppose j = 0
until (j < length(scores)) {
    total = total + scores[j]
    j = j + 1
}
suppose average = total / length(scores)
write("Class Average: " + average)

Object-Oriented Example

This example shows how to work with objects and their properties:

suppose person = {
    name: "Alice",
    age: 25,
    city: "New York",
    hobbies: ["reading", "coding", "hiking"]
}

write("Person Information:")
write("Name: " + person.name)
write("Age: " + person.age)
write("City: " + person.city)
write("Hobbies: " + person.hobbies)

// Update information
person.age = 26
person.city = "Boston"
person.hobbies.push("photography")

write("Updated Information:")
write("Age: " + person.age)
write("City: " + person.city)
write("Hobbies: " + person.hobbies)

// Display hobbies
write("Hobby List:")
suppose k = 0
until (k < length(person.hobbies)) {
    write("- " + person.hobbies[k])
    k = k + 1
}

Recursive Function Example

This example demonstrates recursion with a factorial function:

define factorial(n) {
    if (n <= 1) {
        return 1
    } else {
        return n * factorial(n - 1)
    }
}

write("Factorial Calculator:")
suppose num = 5
write("Factorial of " + num + ": " + factorial(num))

// Calculate factorials for multiple numbers
suppose numbers = [1, 2, 3, 4, 5, 6]
write("Factorial Table:")
suppose i = 0
until (i < length(numbers)) {
    suppose n = numbers[i]
    write(n + "! = " + factorial(n))
    i = i + 1
}

Complete Program Example

Here's a complete program that combines many Rui features - a simple student management system:

// Student Management System
write("=== Student Management System ===")

// Define functions
define createStudent(name, age, major) {
    return {
        name: name,
        age: age,
        major: major,
        grades: []
    }
}

define addGrade(student, grade) {
    student.grades.push(grade)
}

define calculateAverage(student) {
    if (length(student.grades) == 0) {
        return 0
    }
    
    suppose sum = 0
    suppose i = 0
    until (i < length(student.grades)) {
        sum = sum + student.grades[i]
        i = i + 1
    }
    
    return sum / length(student.grades)
}

define getLetterGrade(average) {
    if (average >= 90) {
        return "A"
    } else if (average >= 80) {
        return "B"
    } else if (average >= 70) {
        return "C"
    } else if (average >= 60) {
        return "D"
    } else {
        return "F"
    }
}

// Create students
suppose students = [
    createStudent("Alice", 20, "Computer Science"),
    createStudent("Bob", 19, "Mathematics"),
    createStudent("Charlie", 21, "Physics")
]

// Add grades
addGrade(students[0], 85)
addGrade(students[0], 92)
addGrade(students[0], 78)

addGrade(students[1], 90)
addGrade(students[1], 88)
addGrade(students[1], 95)

addGrade(students[2], 75)
addGrade(students[2], 82)
addGrade(students[2], 79)

// Display student information
suppose i = 0
until (i < length(students)) {
    suppose student = students[i]
    suppose average = calculateAverage(student)
    suppose letterGrade = getLetterGrade(average)
    
    write("Student: " + student.name)
    write("Major: " + student.major)
    write("Grades: " + student.grades)
    write("Average: " + average)
    write("Letter Grade: " + letterGrade)
    write("---")
    
    i = i + 1
}

write("Program completed!")

Next Steps

Congratulations! You've learned the basics of Rui programming. Here's what to explore next:

  • Language Basics: Learn about syntax and statement termination
  • Data Types: Understand numbers, strings, booleans, and null
  • Variables: Master variable declaration and naming rules
  • Operators: Explore arithmetic, comparison, and logical operators
  • Functions: Learn to create reusable code blocks
  • Data Structures: Work with arrays and objects
  • Built-in Functions: Discover Rui's built-in capabilities

Start with the Language Basics section to build a solid foundation, then explore the other topics based on your interests and needs.