diff options
Diffstat (limited to 'src/modelica_ast.rs')
-rw-r--r-- | src/modelica_ast.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/modelica_ast.rs b/src/modelica_ast.rs index cbb82ea..2828f81 100644 --- a/src/modelica_ast.rs +++ b/src/modelica_ast.rs @@ -79,6 +79,10 @@ impl ModelicaModel { binds } + // This crude function finds "unbound" variables: those which are not constants, parameters, or + // the sole element on the LHS of an equation. + // Bugs: + // if a var is on LHS and RHS of same equation pub fn get_free_vars(&self) -> Vec<String> { // Start with components, and remove constants and parameters let vars = self.components.iter().filter(|v| match v.prefix { @@ -100,6 +104,62 @@ impl ModelicaModel { } } +impl Expr { + + // Order is undefined + pub fn identifiers(&self) -> Vec<String> { + use self::Expr::*; + match *self { + Integer(_) | Float(_) => vec![], + Ident(ref s) => vec![s.clone()], + Der(ref e) | Abs(ref e) => e.identifiers(), + BinExpr(_, ref e1, ref e2) => { + let mut all = e1.identifiers(); + for i in e2.identifiers() { + if !(all.contains(&i)) { + all.push(i.clone()); + } + } + all + }, + } + } +} + +#[test] +fn test_expr_identifiers() { + use self::Expr::*; + + fn set_eq(a: Vec<String>, b: Vec<String>) -> bool { + let set_a: HashSet<String> = HashSet::from_iter(a); + let set_b: HashSet<String> = HashSet::from_iter(b); + return set_a == set_b; + } + + assert!(set_eq( + vec![], + Integer(0).identifiers())); + assert!(set_eq( + vec!["x".to_string()], + Ident("x".to_string()).identifiers())); + assert!(set_eq( + vec!["x".to_string(), "y".to_string(), "z".to_string()], + BinExpr(BinOperator::Add, + Box::new(Abs(Box::new(Ident("z".to_string())))), + Box::new(BinExpr(BinOperator::Add, + Box::new(Abs(Box::new(Ident("x".to_string())))), + Box::new(Abs(Box::new(Ident("y".to_string()))))))).identifiers())); + assert!(set_eq( + vec!["z".to_string()], + BinExpr(BinOperator::Add, + Box::new(Ident("z".to_string())), + Box::new(Ident("z".to_string()))).identifiers())); +} + +impl SimpleEquation { +} + + //// Debug Implementations impl Debug for ModelicaModel { |