Pyro

A scripting language for people who enjoy the simpler things in life.

Version 0.9.35

Examples


Hello world

You can echo any value to the standard output stream, e.g.

echo "hello world";

The value doesn't have to be a string — echo stringifies the value before printing it, e.g.

echo 123;

This is equivalent to calling $str() on the value before printing it. Note that echo automatically adds a newline character to the output.

Pyro also has $print()/$println() functions which print to the standard output stream, e.g.

$print("hello world\n");
$println("hello world");

The only difference between $print() and $println() is that $println() automatically adds a newline character to the output.

Pyro also has $eprint()/$eprintln() functions which print to the standard error stream.

String interpolation

You can interpolate the value of an expression into a double-quoted string using ${}, e.g.

var value = "xyz";
assert "abc ${value} def" == `abc xyz def`;
assert "abc ${value:to_upper()} def" == `abc XYZ def`;

The value doesn't have to be a string — if it isn't a string, it will be automatically stringified, e.g.

var value = 123;
assert "abc ${value} def" == `abc 123 def`;
assert "abc ${value + 1} def" == `abc 124 def`;

See the string formatting documentation for details.

Run a shell command

The $() function runs a shell command and returns its output as a string, e.g.

var current_directory = $("pwd");

The $shell() function provides more control over input and output. It returns a three-item tuple containing the command's exit code as an integer, its stdout output as a string, and its stderr output as a string:

var (exit_code, std_output, err_output) = $shell("pwd");

You can provide an input string to $shell() which will be written to the command's stdin:

assert $shell("cat", "foo bar") == $tup(0, "foo bar", "");

Fibonacci numbers

Calculate the n-th Fibonacci number:

def fib(n) {
    if n < 2 {
        return n;
    }
    return fib(n - 1) + fib(n - 2);
}

Reading from standard input

Read a single line from the standard input stream:

var line = $input();

Read a sequence of lines in a loop from the standard input stream:

loop {
    var line = $input(">>> ");

    if line == null || line == "exit" {
        break;
    }

    echo line;
}

The $input() function is provided for simple use-cases. You can also access the standard input stream as a file using the $stdin() function, e.g.

var input = $stdin():read_string();

Reading from a file

Read the content of a file into a string:

var string = $read_file("input.txt");

This is a convenience function — a file object provides more fine-grained control:

with file = $file("input.txt", "r") {
    echo file:read_string();
}

Writing to a file

Write a string to a file:

$write_file("output.txt", "Content for file...");

This is a convenience function — a file object provides more fine-grained control:

with file = $file("output.txt", "w") {
    file:write("Content for file...");
}

Guessing game

The classic guess-a-random-number game:

import std::prng;

var target = prng::rand_int(10) + 1;

loop {
    var guess = $input("Enter a number between 1 and 10: ");
    if guess == null || guess == "exit" {
        break;
    }

    if guess == $str(target) {
        echo "Correct!";
        break;
    }

    echo "Wrong! Try again...";
}

FizzBuzz

The classic interview question:

def fizzbuzz(n) {
    for i in $range(1, n + 1) {
        if i % 15 == 0 {
            echo "fizzbuzz";
        } else if i % 3 == 0 {
            echo "fizz";
        } else if i % 5 == 0 {
            echo "buzz";
        } else {
            echo i;
        }
    }
}

Iterating over a range

The $range() function returns an iterator over a range of integers:

>>> for i in $range(5) {
...     echo i;
... }
0
1
2
3
4

You can specify start, stop, and step arguments:

>>> for i in $range(5, 15, 2) {
...     echo i;
... }
5
7
9
11
13