aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-11-30 21:02:04 -0800
committerbnewbold <bnewbold@robocracy.org>2016-11-30 21:02:04 -0800
commite56f9354d62a27b3ebbe9e4c0bd285bb71785369 (patch)
tree385f08827171afc50535537687704a872b6d8ad8
parent686edc56adc0d8964af8505e41dc9455c1fe732c (diff)
downloadmodelthing-e56f9354d62a27b3ebbe9e4c0bd285bb71785369.tar.gz
modelthing-e56f9354d62a27b3ebbe9e4c0bd285bb71785369.zip
start working on Expr helpers
-rw-r--r--src/modelica_ast.rs60
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 {