From e56f9354d62a27b3ebbe9e4c0bd285bb71785369 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Wed, 30 Nov 2016 21:02:04 -0800 Subject: start working on Expr helpers --- src/modelica_ast.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'src') 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 { // 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 { + 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, b: Vec) -> bool { + let set_a: HashSet = HashSet::from_iter(a); + let set_b: HashSet = 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 { -- cgit v1.2.3