|
| 1 | +mod compiler; |
| 2 | +mod cythanenc; |
| 3 | +mod decompiler; |
| 4 | +mod execute; |
| 5 | + |
| 6 | +use clap::*; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + let matches = App::new("My Super Program") |
| 10 | + .version("1.0") |
| 11 | + .author("Kevin K. <kbknapp@gmail.com>") |
| 12 | + .about("Does awesome things") |
| 13 | + .subcommand(SubCommand::with_name("compile") |
| 14 | + .about("Compile a Cythan file | use .cct extension for Compressed Executable or .rct for raw format") |
| 15 | + .version("1.0") |
| 16 | + .author("Laurent Gaucheron <gaucheron.laurent@gmail.com>") |
| 17 | + .arg(Arg::with_name("INPUT") |
| 18 | + .required(true) |
| 19 | + .help("Set input file")) |
| 20 | + .arg(Arg::with_name("OUTPUT") |
| 21 | + .help("Set output file"))) |
| 22 | + .subcommand(SubCommand::with_name("decompile") |
| 23 | + .about("Decompile a Cythan file | This require a compressed cythan file in .cct to work") |
| 24 | + .version("1.0") |
| 25 | + .author("Laurent Gaucheron <gaucheron.laurent@gmail.com>") |
| 26 | + .arg(Arg::with_name("INPUT") |
| 27 | + .required(true) |
| 28 | + .help("Set input file")) |
| 29 | + .arg(Arg::with_name("OUTPUT") |
| 30 | + .help("Set output file"))) |
| 31 | + .subcommand(SubCommand::with_name("execute") |
| 32 | + .about("Execute compiled Cythan file | Support raw and compressed format (.rct and .cct)") |
| 33 | + .version("1.0") |
| 34 | + .author("Laurent Gaucheron <gaucheron.laurent@gmail.com>") |
| 35 | + .arg(Arg::with_name("INPUT") |
| 36 | + .required(true) |
| 37 | + .help("Set input file")) |
| 38 | + .arg(Arg::with_name("ITERATIONS") |
| 39 | + .required(true) |
| 40 | + .help("Number of iterations to compute")) |
| 41 | + .arg(Arg::with_name("OUTPUT") |
| 42 | + .help("Set output file if none will print to the console the result"))) |
| 43 | + .get_matches(); |
| 44 | + if let Some(matches) = matches.subcommand_matches("compile") { |
| 45 | + let output = matches.value_of("OUTPUT").unwrap_or("compiled.cct"); |
| 46 | + let input = matches.value_of("INPUT").unwrap(); |
| 47 | + let instant = std::time::Instant::now(); |
| 48 | + compiler::compile(input, output).unwrap(); |
| 49 | + println!( |
| 50 | + "Compiled `{}` to `{}` in {}", |
| 51 | + input, |
| 52 | + output, |
| 53 | + ms_to_str(instant.elapsed().as_millis()) |
| 54 | + ); |
| 55 | + } else if let Some(matches) = matches.subcommand_matches("execute") { |
| 56 | + let output = matches.value_of("OUTPUT"); |
| 57 | + let input = matches.value_of("INPUT").unwrap(); |
| 58 | + let iterations: usize = matches |
| 59 | + .value_of("ITERATIONS") |
| 60 | + .map(|x| x.parse().ok()) |
| 61 | + .flatten() |
| 62 | + .expect("<ITERATIONS> must be a positive integer"); |
| 63 | + |
| 64 | + let instant = std::time::Instant::now(); |
| 65 | + execute::execute(input, iterations, output.map(|x| x.to_owned())).unwrap(); |
| 66 | + if let Some(e) = output { |
| 67 | + println!( |
| 68 | + "Executed `{}` and written the result to `{}` in {}", |
| 69 | + input, |
| 70 | + e, |
| 71 | + ms_to_str(instant.elapsed().as_millis()) |
| 72 | + ); |
| 73 | + } else { |
| 74 | + println!( |
| 75 | + "Executed `{}` in {}", |
| 76 | + input, |
| 77 | + ms_to_str(instant.elapsed().as_millis()) |
| 78 | + ); |
| 79 | + } |
| 80 | + } else if let Some(matches) = matches.subcommand_matches("decompile") { |
| 81 | + let output = matches.value_of("OUTPUT").unwrap_or("decompiled.rct"); |
| 82 | + let input = matches.value_of("INPUT").unwrap(); |
| 83 | + |
| 84 | + let instant = std::time::Instant::now(); |
| 85 | + decompiler::decompile(input, output); |
| 86 | + println!( |
| 87 | + "Decompiled `{}` to `{}` in {}", |
| 88 | + input, |
| 89 | + output, |
| 90 | + ms_to_str(instant.elapsed().as_millis()) |
| 91 | + ); |
| 92 | + } else { |
| 93 | + println!("Invalid use: cythan-cli compile <FILE> [OUTPUT]") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn ms_to_str(ms: u128) -> String { |
| 98 | + if ms < 100 { |
| 99 | + format!("{}ms", ms) |
| 100 | + } else if ms < 60_000 { |
| 101 | + format!("{}s", (ms / 100) as f32 / 10.0) |
| 102 | + } else { |
| 103 | + format!( |
| 104 | + "{}m {}s", |
| 105 | + (ms / 60_000) as f32, |
| 106 | + ((ms % 60_000) / 100) as f32 / 10.0 |
| 107 | + ) |
| 108 | + } |
| 109 | +} |
0 commit comments