aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/mt-tool.rs
blob: 317ac20d9e038df68608f814d248da56d72f9184 (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

extern crate modelthing;
extern crate rustc_serialize;
extern crate clap;
extern crate modelica_parser;

use modelthing::transpile_scheme::TranspileScheme;
use modelthing::transpile_js::TranspileJS;
use modelthing::modelica_model::ModelicaModelExt;
use clap::{App, SubCommand};
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 main() {

    let matches = App::new("mt-tool")
        .version(env!("CARGO_PKG_VERSION"))
        .subcommand(SubCommand::with_name("parse")
            .about("checks that file contains a valid modelica model")
            .arg_from_usage("<FILE>... 'file to parse from'"))
        .subcommand(SubCommand::with_name("list")
            .about("dumps all the known models in database"))
        .subcommand(SubCommand::with_name("load")
            .about("checks that directory contains valid model and metadata")
            .arg_from_usage("<DIR> 'model to load'"))
        .subcommand(SubCommand::with_name("transpile_js")
            .about("converts modelica to javascript function")
            .arg_from_usage("<DIR> 'model to load'"))
        .subcommand(SubCommand::with_name("transpile_scheme")
            .about("converts modelica to scheme function")
            .arg_from_usage("<DIR> 'model to load'"))
        .subcommand(SubCommand::with_name("solve_for")
            .about("")
            .arg_from_usage("<DIR> 'model to load'
                             --dep <VAR>... 'dependent variable'
                             --indep <VAR>... 'independent variable'"))
        .get_matches();

    
    match matches.subcommand() {
        ("parse", Some(subm)) => {
            parse_modelica_files(subm.values_of_lossy("FILE").unwrap());
        },
        ("solve_for", Some(subm)) => {
            let dir = Path::new(subm.value_of("DIR").unwrap());
            let indep = subm.values_of_lossy("indep").unwrap();
            let dep = subm.values_of_lossy("dep").unwrap();
            let me = modelthing::load_model_entry(dir).unwrap();
            println!("{:?}", me.ast.solve_for(indep, dep));
        },
        ("transpile_scheme", Some(subm)) => {
            let dir = Path::new(subm.value_of("DIR").unwrap());
            let me = modelthing::load_model_entry(dir).unwrap();
            println!("{}", me.ast.repr_scheme().unwrap());
        },
        ("transpile_js", Some(subm)) => {
            let dir = Path::new(subm.value_of("DIR").unwrap());
            let me = modelthing::load_model_entry(dir).unwrap();
            println!("{}", me.ast.repr_js().unwrap());
        },
        ("list", Some(subm)) => {
            let dir = Path::new(subm.value_of("DIR").unwrap_or("examples"));
            for m in modelthing::search_models(dir) {
                println!("{}", m)
            }
        },
        ("load", Some(subm)) => {
            let dir = Path::new(subm.value_of("DIR").unwrap());
            let me = modelthing::load_model_entry(dir).unwrap();
            println!("{:?}", me);
        },
        _ => {
            println!("Missing or unimplemented command!");
            println!("{}", matches.usage());
            exit(-1);
        },
    }
}