diff options
Diffstat (limited to 'src/modelica_ast.rs')
-rw-r--r-- | src/modelica_ast.rs | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/modelica_ast.rs b/src/modelica_ast.rs new file mode 100644 index 0000000..425993f --- /dev/null +++ b/src/modelica_ast.rs @@ -0,0 +1,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, "-"), + } + } +} |