Programming Language

Nocter

A self-contained systems language built around simplicity, encapsulation, and foolproof design.

index.nct

#package: {
    name: "text-report",
    version: "0.1.0",
}

#executable: {
    name: "text-report",
}

use ./report.Report
use ./report as report_module
use std/cli.Application
use std/io.{BlockingFile, BlockingReader}
use std/io
use std/path.Utf8Path
use std/string.String

blocking func main(): i32! {
    var application = Application.new("text-report", "Report matching lines in one text file.")?
    application.add_positional("PATH", true, "UTF-8 file to read.")?
    application.add_positional("NEEDLE", true, "Substring to search for.")?
    let arguments = application.parse_process() catch failure {
        if !failure.has_code("std.cli.missing_positional")
        && !failure.has_code("std.cli.unexpected_positional") {
            return move failure
        }
        let usage = application.usage()
        io.print(&usage)?
        io.print("\n")?
        return 2
    }
    let requested = arguments.positional("PATH") otherwise { return 2 }
    let needle = arguments.positional("NEEDLE") otherwise { return 2 }
    let path = Utf8Path.new(requested)?
    var file = BlockingFile.open(&path)?
    let text = file.read_to_string_blocking()?
    let report = report_module.analyze(&text, needle)
    print_report(&report)?
    return 0
}

blocking func print_report(report: &Report): void! {
    let lines = report.lines().to_string()
    let matching = report.matching().to_string()
    let output = String.concat(
        "lines: ",
        &lines,
        "\nmatching: ",
        &matching,
        "\n",
    )
    io.print(&output)?
    return
}