// Frut Language Demonstration
// This file showcases all the features of the Frut programming language

// 0. Importing modules
import std.io.*;

// 1. Variable declarations with different types
var greeting: string = "Hello, World!";
var age: int = 25;
var test_pi: double = 3.14159;
var is_student: bool = true;
var temperature: double = 23.5;

// 2. Function definitions with parameters and return types
func calculate_area(width: double, height: double): double {
    return width * height;
}

func factorial(n: int): int {
    if n <= 1 {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

func is_even(num: int): bool {
    return num % 2 == 0;
}

// 3. Main program logic
var area: double = calculate_area(10.5, 8.2);
var fact_5: int = factorial(5);
var even_check: bool = is_even(42);

// 4. Conditional statements with if-elif-else
if age < 18 {
    println("You are a minor");
} elif age >= 18 && age < 65 {
    println("You are an adult");

    if is_student {
        println("You are a student");
    }
} else {
    println("You are a senior citizen");
}

// 5. More complex conditional logic
if temperature > 30.0 {
    println("It's hot outside!");
} elif temperature < 10.0 {
    println("It's cold outside!");
} else {
    println("The weather is pleasant");
}

// 6. Function calls and variable usage
println("Greeting: ${greeting}");
println("Age: ${age}");
println("Pi value: ${test_pi}");
println("Is student: ${is_student}");
println("Temperature: ${temperature}");
println("Rectangle area: ${area}");
println("Factorial of 5: ${fact_5}");
println("Is 42 even? ${even_check}");

// 7. Boolean expressions and comparisons
var is_adult: bool = age >= 18;
var is_teenager: bool = age >= 13 && age <= 19;
var can_vote: bool = age >= 18 && is_student == false;

println("Is adult: ${is_adult}");
println("Is teenager: ${is_teenager}");
println("Can vote: ${can_vote}");

// 8. Arithmetic operations
var doubled_age: int = age * 2;
var half_pi: double = test_pi / 2.0;
var temp_minus_pi: double = temperature - test_pi;
var greeting_age: string = greeting + age;

// Error: Type mismatch
// var age_plus_temp: double = age + temperature;

println("Doubled age: ${doubled_age}");
println("Half pi: ${half_pi}");
println("Temperature - Pi: ${temp_minus_pi}");
println("Greeting + age: ${greeting_age}");

// 9. While loop with counter
var counter: int = 0;
while counter < 5 {
    println("Iteration: ${counter}");
    counter = counter + 1;
}

// 10. Type cast
var int_pi: int = test_pi as int;
println("Pi as int: ${int_pi}");

// 11. Input
var name: string = input("Enter your name: ");
println("Hello, ${name}!");

// 12. Types
type Example {
    x: int;
    y: int;
}

// 13. Extension for types
ext Example {
    func display(self: Example) {
        println("Example { x: ${self.x}, y: ${self.y} }");
    }

    func create(x: int, y: int): Example {
        return Example { x, y };
    }
}

// 14. Creating and using variables with a custom type
var example: Example = Example.create(1, 2);
example.display();

var example2: Example = Example { x: 3, y: 4 };
example2.display();

println("example.x = ${example.x}");
println("example2.y = ${example2.y}");

// 15. Std modules
import std.math.*;

println("min_i(3, 5) = ${min_i(3, 5)}");
println("max_i(3, 5) = ${max_i(3, 5)}");

// 16. Methods for primitive types
println("Length of your name is ${name.len()}");

if "Frut".contains("ru") {
    println("Frut contains ru");
}

println("true is ${true.to_int()}")

// 17. Interpolation
var xa: int = 42;
var ya: double = 3.5;
var sa: string = "hello";
var ba: bool = true;

println("xa = ${xa}");
println("ya = ${ya}");
println("sa.len = ${sa.len()}");
println("abs = ${(-5).abs()} ${(-3.14).abs()}");
println("mix = ${sa} ${xa} ${ya} ${ba}");

// Escaping
println("escaped: \${not} and plain ${1+2}");

// Expressions
println("expr = ${xa + 1}");
