Example programs in the Roost language. Check back later for more. You should write many many more test programs as you build your compiler.

Contents

Core Language

fib.roost

fn main(args : str[]) -> unit {
    Roost.printi64(fib(Roost.parsei64(args[0], 0)));
    Roost.println("");
}

fn fib(n : i64) -> i64 {
    if (n < 2) {
        n
    } else {
        fib(n-1) + fib(n-2)
    }
}

matrix.roost

fn multiply(a: i64[][], b: i64[][], c: i64[][]) -> unit {
    let i = 0 in
    while (i < a.length) {
        let j = 0 in
        while (j < c[i].length) {
            c[i][j] = 0;
            let k = 0 in
            while (k < a[i].length) {
                c[i][j] = c[i][j] + a[i][k] * b[k][j];
                k = k + 1;
            }
            j = j + 1;
        }
        i = i + 1;
    }
}

fn init_matrix(m: i64[][]) -> unit {
    let i = 0 in
    while (i < m.length) {
        let j = 0 in
        while (j < m[i].length) {
            m[i][j] = Roost.random(m.length*2);
            j = j + 1;
        }
        i = i + 1;
    }
}

fn print_matrix(m: i64[][]) -> unit {
    let i = 0 in
    while (i < m.length) {
        let j = 0 in
        while (j< m[i].length){
            Roost.printi64(m[i][j]);
            Roost.print(" ");
            j = j + 1;
        }
        Roost.print("\n");
        i = i + 1;
    }
    Roost.print("\n");
}

fn main(args: str[]) -> unit {
    if (args.length != 3) {
        Roost.println("Invalid number of parameters.");
        Roost.exit(1);
    }

    let m = Roost.parsei64(args[0], 0)
        n = Roost.parsei64(args[1], 0)
        p = Roost.parsei64(args[2], 0)
    in {
        if(m < 1){
            Roost.println("Invalid value for parameter `m'");
            Roost.exit(1);
        }
        if(n < 1){
            Roost.println("Invalid value for parameter `n'");
            Roost.exit(1);
        }
        if(p < 1){
            Roost.println("Invalid value for parameter `p'");
            Roost.exit(1);
        }

        let a = new i64[][m]
            b = new i64[][n]
            c = new i64[][m]
        in {
            let i = 0 in
            while (i < m){
                a[i] = new i64[n];
                i = i + 1;
            }
            let i = 0 in
            while (i < n){
                b[i] = new i64[p];
                i = i + 1;
            }
            let i = 0 in
            while (i < m){
                c[i] = new i64[p];
                i = i + 1;
            }

            init_matrix(a);
            Roost.println("Matrix A:");
            print_matrix(a);
            init_matrix(b);
            Roost.println("Matrix B:");
            print_matrix(b);
            multiply(a, b, c);
            Roost.println("Matrix C = A x B:");
            print_matrix(c);
        }
    }
}

quicksort.roost

fn partition(a: i64[], low: i64, high: i64) -> i64 {
    let pivot = a[low]
        i = low
        j = high in {
        while true { 
            while a[i] < pivot { i = i + 1; }
            while a[j] > pivot { j = j - 1; }
            if i >= j { return j; }
            let tmp = a[i] in {
                a[i] = a[j]; 
                a[j] = tmp;
            }
            i = i + 1;
            j = j - 1;
        }
        j
    }
}

fn quicksort(a: i64[], low: i64, high: i64) -> unit {
    if low < high let mid = partition(a, low, high) in {
        quicksort(a, low, mid);
        quicksort(a, mid+1, high);
    }
}

fn init_array(len: i64) -> i64[] {
    let a = new i64[len] in {
        let i = 0 in
        while i < a.length {
            a[i] = Roost.random(a.length * 2);
            i = i + 1;
        }
        a
    }
}

fn print_array(a: i64[]) -> unit {
    Roost.print("Array elements: ");
    let i = 0 in
    while i < a.length {
        Roost.printi64(a[i]);
        Roost.print(" ");
        i = i + 1;
    }
    Roost.println("");
}

fn main(args: str[]) -> unit {
    if args.length != 1 {
        Roost.println("Unspecified array length");
        Roost.exit(1);
    }

    let n = Roost.parsei64(args[0], 0) in {
        if n <= 0 {
            Roost.println("Invalid array length");
            Roost.exit(1);
        }
        let a = init_array(n) in {
            print_array(a);
            quicksort(a, 0, n - 1);
            print_array(a);
        }
    }
}

sieve.roost

fn init_array(len: i64) -> i64[] {
    let nums = new i64[len] in {
        let i = 0 in
        while (i < nums.length) {
            nums[i] = i;
            i=i+1;
        }
        nums
    }
}

fn sieve_all(nums: i64[]) -> unit {
    let i = 2 in
    while (i < nums.length) {
        sieve(nums, i);
        i=i+1; 
    }
}

fn sieve(nums: i64[], n: i64) -> unit {
    let i = 2*n in
    while (i < nums.length) {
        nums[i] = 0;
        i=i+n;
    }
}

fn print_primes(nums: i64[]) -> unit {
    Roost.print("Primes less than ");
    Roost.printi64(nums.length);
    Roost.print(": ");
    let i = 2 in
    while (i < nums.length) {
        if (nums[i] != 0) {
            Roost.printi64(nums[i]);
            Roost.print(" ");
        }
        i=i+1;
    }
    Roost.println("");
}

fn main(args: str[]) -> unit {
    if (args.length != 1) {
        Roost.println("Unspecified numsber.");
        return ();
    }
    Roost.println("");
    let n = Roost.parsei64(args[0], 0) in {
        if (n <= 0) {
            Roost.println("Invalid array length");
            return ();
        }
        let nums = init_array(n) in {
            sieve_all(nums);
            print_primes(nums);
        }
    }
}

Standard Extensions

Coming soon!