Examples

Real programs, real output.

Every entry compiles and runs verbatim against Reef v0.9.2. Pick one to see its full source and the actual terminal session it produced.

Checksum CLI

Practical

A complete sha256sum-style tool in one file.

Source

// reefsum.reef - File checksum calculator
// Demonstrates: sys.optparse (CLI parsing), crypto modules, io.file, exit codes
//
// Usage:
//   reefsum sha256 file.txt
//   reefsum md5 file.txt
//   reefsum sha1 file.txt
//   reefsum crc32 file.txt
//   reefsum --help
//
// Exit codes:
//   0  - Success
//   1  - Error (file not found, unknown algorithm)
//   64 - Usage error (missing arguments)

import sys.optparse as opt
import io.file as file
import core.str as str
import core.result as result
import crypto.sha256 as sha256
import crypto.md5 as md5
import crypto.sha1 as sha1
import crypto.crc32 as crc32

// Calculate and print checksum
// Returns: true on success, false on error
fn calculate_checksum(algorithm: string, filename: string, uppercase: bool): bool
    // Check if file exists
    if not file.fileExists(filename)
        print("reefsum: ")
        print(filename)
        println(": No such file or directory")
        return false
    end if

    // Read file contents as binary (handles null bytes)
    let contents_r = file.readBinaryFile(filename)
    if result.is_err(contents_r)
        print("reefsum: ")
        print(filename)
        println(": error reading file")
        return false
    end if
    let contents = result.unwrap_ok(contents_r)

    // Calculate hash based on algorithm
    // All algorithms now support binary data with *_bytes functions
    mut hash = ""

    if str.equals(algorithm, "sha256")
        hash = sha256.sha256_bytes(contents)
    elif str.equals(algorithm, "sha1")
        hash = sha1.sha1_bytes(contents)
    elif str.equals(algorithm, "md5")
        hash = md5.md5_bytes(contents)
    elif str.equals(algorithm, "crc32")
        if uppercase
            hash = crc32.crc32_binary_hex(contents)
        else
            hash = crc32.crc32_binary_hex_lower(contents)
        end if
    else
        print("reefsum: unknown algorithm '")
        print(algorithm)
        println("'")
        println("Use --help to see supported algorithms.")
        return false
    end if

    // Print in standard checksum format: <hash>  <filename>
    print(hash)
    print("  ")
    println(filename)
    return true
end calculate_checksum

fn main(): int
    let p = opt.command("reefsum")
    p.about("Calculate checksums of files using various algorithms.")
    p.version("1.0.0")
    let upper = p.bool_flag("upper", 'u', false, "Output hash in uppercase (default: lowercase)")
    let algorithm = p.arg("algorithm")
    let files = p.rest()

    match p.parse()
        Status_OkHelp() =>
            return 0
        end
        Status_OkVersion() =>
            return 0
        end
        Status_Err(e) =>
            if opt.argc() < 2
                p.usage()
                return 0
            end if
            println("reefsum: " + opt.format(e))
            println("Try 'reefsum --help' for more information.")
            return 64
        end
        Status_OkParsed() =>
            if files.len() == 0
                println("reefsum: missing file operand")
                println("Try 'reefsum --help' for more information.")
                return 64
            end if
            let algo = algorithm.value()
            let uppercase = upper.value()
            mut had_error = false
            let names = files.values()
            mut i = 0
            while i < names.length()
                if not calculate_checksum(algo, names[i], uppercase)
                    had_error = true
                end if
                i = i + 1
            end while
            if had_error
                return 1
            end if
            return 0
        end
    end match
    return 0
end main

Build, run & output

$ reefc reefsum.reef -o reefsum
$ ./reefsum sha256 LICENSE
6eb72651a796d9d2dd147bf50d887646e330d3c779acda79d509ad4634b86ff1  LICENSE

$ ./reefsum crc32 LICENSE
0f95be68  LICENSE