aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/mt-tool.rs
blob: 95a89386864f09eddc3301ecbfe26041d29c5adc (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
124
125
126
127
128
129
130
131
132
133

extern crate modelthing;
extern crate rustc_serialize;
extern crate getopts;
extern crate modelica_parser;

use modelthing::transpile_scheme::TranspileScheme;
use modelthing::transpile_js::TranspileJS;
use modelthing::modelica_model::ModelicaModelExt;
use getopts::Options;
use std::env;
use std::io::Read;
use std::fs::File;
use std::path::Path;
use std::time::Instant;
use std::process::exit;


fn parse_modelica_files(paths: Vec<String>) {
    for input in &paths {
        let mut s = String::new();
        if let Err(err) = File::open(input).and_then(|mut f| f.read_to_string(&mut s)) {
            println!("Input `{}`: I/O Error {}",
                     input, err);
            continue;
        }

        let time_stamp = Instant::now();
        let result = modelica_parser::parser::parse_model(&s);
        let elapsed = time_stamp.elapsed();
        let elapsed = elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1000_000_000.0;

        match result {
            Ok(_) => println!("Input `{}` ({}s): OK", input, elapsed),
            Err(err) => println!("Input `{}` ({}sec): ERROR\n{}", input, elapsed, modelica_parser::pp_parseerror(&s, err)),
        }
    }
}

fn print_usage(opts: Options) {
    let brief = "usage:\tmt-tool <CMD> [options]";
    println!("");
    print!("{}", opts.usage(&brief));
    println!("");
    println!(r#"Commands:
    parse <file>            checks that file contains a valid modelica model
    list                    dumps all the known models in database
    load <dir>              checks that directory contains valid model and metadata
    transpile_js <dir>      converts modelica to javascript function
    transpile_scheme <dir>  converts modelica to scheme function
"#);
}

fn main() {

    let args: Vec<String> = env::args().collect();

    let mut opts = Options::new();
    opts.optflag("h", "help", "print this help menu");
    opts.optflag("", "version", "print the version");
    opts.optmulti("", "dep", "dependent variable", "VAR");
    opts.optmulti("", "indep", "dependent variable", "VAR");

    let matches = match opts.parse(&args[1..]) {
        Ok(m) => m,
        Err(f) => {
            println!("{}\n", f.to_string());
            print_usage(opts);
            exit(-1);
        }
    };

    if matches.opt_present("help") {
        print_usage(opts);
        return;
    }

    if matches.opt_present("version") {
        println!("modelthing {}", env!("CARGO_PKG_VERSION"));
        return;
    }

    if matches.free.is_empty() {
        println!("At least a CMD is required; see --help");
        exit(-1);
    }

    let cmd = matches.free[0].clone();
    match cmd.as_str() {
        "parse" => { parse_modelica_files(matches.free[1..].iter().map(|x| x.to_string()).collect()); },
        "solve_for" => {
            if matches.free.len() != 2 {
                println!("Expected a path to load");
                exit(-1);
            }
            let me = modelthing::load_model_entry(Path::new(&matches.free[1])).unwrap();
            println!("{:?}", me.ast.solve_for(matches.opt_strs("indep"), matches.opt_strs("dep")));
        },
        "transpile_scheme" => {
            if matches.free.len() != 2 {
                println!("Expected a single path to load");
                exit(-1);
            }
            let me = modelthing::load_model_entry(Path::new(&matches.free[1])).unwrap();
            println!("{}", me.ast.repr_scheme().unwrap());
        },
        "transpile_js" => {
            if matches.free.len() != 2 {
                println!("Expected a single path to load");
                exit(-1);
            }
            let me = modelthing::load_model_entry(Path::new(&matches.free[1])).unwrap();
            println!("{}", me.ast.repr_js().unwrap());
        },
        "list" => {
            let search_path =
                if matches.free.len() >= 2 { matches.free[1].clone() }
                else { "examples".to_string() };
            for m in modelthing::search_models(Path::new(&search_path)) {
                println!("{}", m)
            }
        },
        "load" => {
            if matches.free.len() != 2 {
                println!("Expected a single path to load");
                exit(-1);
            }
            let me = modelthing::load_model_entry(Path::new(&matches.free[1])).unwrap();
            println!("{:?}", me);
        },
        unknown => { println!("Unknown command: {}", unknown); exit(-1); },
    }
}