aboutsummaryrefslogtreecommitdiffstats
path: root/src/modelica_ast.rs
blob: 425993f3b84a85148c49ecd19433931f3b134705 (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
134
135
136
137
138
139
140
141

use std::fmt::{Debug, Formatter, Error};

pub struct Model {
    pub name: String,
    pub variables: Vec<Component>,
    pub equations: Vec<SimpleEquation>,
    pub connections: Vec<Connection>,
    pub extends: Vec<String>,
}

#[derive(Copy, Clone)]
pub enum ComponentPrefix {
    // incomplete: eg, can be parameter and input
    Flow,
    Stream,
    Input,
    Output,
    Discrete,
    Parameter,
    Constant,
}

pub struct Component {
    pub prefix: Option<ComponentPrefix>,
    pub specifier: String,
    pub name: String,
}

#[derive(Clone)]
pub struct Connection {
    pub a: String,
    pub b: String,
}

pub struct SimpleEquation {
    pub lhs: Expr,
    pub rhs: Expr,
}

pub enum Expr {
    Integer(i64),
    Float(f64),
    Ident(String),
    Der(Box<Expr>),
    Abs(Box<Expr>),
    BinExpr(BinOperator, Box<Expr>, Box<Expr>),
}

#[derive(Copy, Clone)]
pub enum BinOperator {
    Multiply,
    Divide,
    Add,
    Subtract,
}

//// Debug Implementations

impl Debug for Model {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        try!(write!(fmt, "model {}\n", self.name));
        for e in self.extends.iter() {
            try!(write!(fmt, "  extends {};\n", e));
        }
        for v in self.variables.iter() {
            try!(write!(fmt, "  {:?};\n", v));
        }
        try!(write!(fmt, "equation\n"));
        for e in self.equations.iter() {
            try!(write!(fmt, "  {:?};\n", e));
        }
        for c in self.connections.iter() {
            try!(write!(fmt, "  {:?};\n", c));
        }
        write!(fmt, "end {};\n", self.name)
    }
}

impl Debug for ComponentPrefix {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        use self::ComponentPrefix::*;
        write!(fmt, "{:?}", 
            match *self {
                Flow => "flow",
                Stream => "stream",
                Input => "input",
                Output => "output",
                Discrete => "discrete",
                Parameter => "parameter",
                Constant => "constant",
            })
    }
}

impl Debug for Component {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        write!(fmt, "{:?}{}",
            self.prefix.unwrap(),
            self.name,
        )
    }
}

impl Debug for Connection {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        write!(fmt, "connect({:?}, {:?});", self.a, self.b)
    }
}

impl Debug for SimpleEquation {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        write!(fmt, "{:?} = {:?}", self.lhs, self.rhs)
    }
}

impl Debug for Expr {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        use self::Expr::*;
        match *self {
            Integer(e) => write!(fmt, "{}", e),
            Float(e) => write!(fmt, "{}", e),
            Ident(ref e) => write!(fmt, "{}", e),
            Der(ref e) => write!(fmt, "der({:?})", e),
            Abs(ref e) => write!(fmt, "abs({:?})", e),
            BinExpr(op, ref l, ref r) => write!(fmt, "({:?} {:?} {:?})", l, op, r),
        }
    }
}

impl Debug for BinOperator {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        use self::BinOperator::*;
        match *self {
            Multiply => write!(fmt, "*"),
            Divide => write!(fmt, "/"),
            Add => write!(fmt, "+"),
            Subtract => write!(fmt, "-"),
        }
    }
}