summaryrefslogtreecommitdiff
path: root/morphology-cli/src/main.rs
blob: 3686842bc9cafdbc9f6542e6e29b9c5113ef6c85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! CLI tool to generate all valid meta-affine sequences of a given length.
//!
//! Usage: generate [options] <length>
//!
//! Example:
//!   generate 5
//!   generate --pretty 5
//!   generate 5 --neg-first-outer --backward
//!
//! This will list all valid meta-affine sequences of length 5 and print the total count.
//! With --pretty, values are displayed in nested bracket notation.

use meta_affine::sequence::{
    generate_all_sequences, SequenceOptions, PrecedenceOrder, SortingMethod,
    SortingDirection, LengthSorting
};
use std::env;
use std::process;

fn main() {
    // Parse command-line arguments
    let args: Vec<String> = env::args().collect();

    // Check for flags
    let pretty = args.contains(&"--pretty".to_string());
    let neg_first_outer = args.contains(&"--neg-first-outer".to_string());
    let neg_first_inner = args.contains(&"--neg-first-inner".to_string());
    let breadth_first = args.contains(&"--breadth-first".to_string());
    let backward = args.contains(&"--backward".to_string());
    let interpolate_length = args.contains(&"--interpolate-length".to_string());

    // Filter out flags to get positional arguments
    let positional: Vec<&String> = args.iter()
        .skip(1)  // Skip program name
        .filter(|arg| !arg.starts_with("--"))
        .collect();

    if positional.len() != 1 {
        eprintln!("Usage: {} [options] <length>", args[0]);
        eprintln!("\nGenerates all valid meta-affine sequences of the specified length.");
        eprintln!("\nOptions:");
        eprintln!("  --pretty              Display values in nested bracket notation");
        eprintln!("\nSorting options:");
        eprintln!("  --neg-first-outer     Use IncreasingNegativeFirst for outer precedence");
        eprintln!("  --neg-first-inner     Use IncreasingNegativeFirst for inner precedence");
        eprintln!("  --breadth-first       Use BreadthFirst sorting method");
        eprintln!("  --backward            Sort sequences backward (last to first)");
        eprintln!("  --interpolate-length  Interpolate sequences of different lengths");
        eprintln!("\nExamples:");
        eprintln!("  {} 5", args[0]);
        eprintln!("  {} --pretty 5", args[0]);
        eprintln!("  {} 5 --neg-first-outer --backward", args[0]);
        eprintln!("  {} 5 --breadth-first --interpolate-length", args[0]);
        process::exit(1);
    }

    let length: usize = match positional[0].parse() {
        Ok(n) => n,
        Err(_) => {
            eprintln!("Error: Length must be a positive integer");
            eprintln!("Usage: {} [--pretty] <length>", args[0]);
            process::exit(1);
        }
    };

    if length == 0 {
        println!("No valid sequences of length 0.");
        println!("\nTotal sequences: 0");
        return;
    }

    println!("Generating all valid meta-affine sequences of length {}...\n", length);

    // Build sorting options from flags
    let mut options = SequenceOptions::new();

    if neg_first_outer {
        options = options.outer_order(PrecedenceOrder::IncreasingNegativeFirst);
    }

    if neg_first_inner {
        options = options.inner_order(PrecedenceOrder::IncreasingNegativeFirst);
    }

    if breadth_first {
        options = options.sorting_method(SortingMethod::BreadthFirst);
    }

    if backward {
        options = options.sorting_direction(SortingDirection::Backward);
    }

    if interpolate_length {
        options = options.length_sorting(LengthSorting::InterpolateLength);
    }

    // Generate all sequences with specified options
    let sequences = generate_all_sequences(length, Some(options));

    // Choose delimiters based on output format
    let (open, close) = if pretty { ("", "") } else { ("[", "]") };

    // Print each sequence
    for (i, sequence) in sequences.iter().enumerate() {
        print!("{}. {}", i + 1, open);
        for (j, value) in sequence.values().iter().enumerate() {
            if pretty {
                print!("{}", value.pretty_print());
            } else {
                print!("{}", value);
            }
            if j < sequence.values().len() - 1 {
                print!(", ");
            }
        }
        println!("{}", close);
    }

    // Print summary
    println!("\n{}", "=".repeat(70));
    println!("Total sequences of length {}: {}", length, sequences.len());
    println!("{}", "=".repeat(70));
}