From 463b8d795969833c63b2a901ce7d40844aacf037 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Mon, 16 Jan 2017 18:20:16 -0800 Subject: sort the output of HashMaps (eg, for argument order) --- src/transpile_js.rs | 19 +++++++++++++++---- src/transpile_scheme.rs | 10 +++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/transpile_js.rs b/src/transpile_js.rs index 225aa99..97eff68 100644 --- a/src/transpile_js.rs +++ b/src/transpile_js.rs @@ -15,12 +15,18 @@ pub trait TranspileJSODE { impl TranspileJS for ModelicaModel { fn transpile_js(&self) -> Result { + let mut params = vec![]; let mut constants = vec![]; for (c, e) in self.get_constant_vars() { if let Some(v) = e { constants.push(format!("var {} = {};", c, try!(v.transpile_js()))); + } else { + params.push(c); } } + // HashMaps are unsorted, so we need to re-sort here + constants.sort(); + params.sort(); let mut binds = vec![]; let mut outputs = vec![]; for eq in self.equations.iter() { @@ -31,7 +37,9 @@ impl TranspileJS for ModelicaModel { bail!("Expected an identifier on LHS (in this partial implementation)") } } - let args: Vec = self.get_free_vars().iter().map(|s| s.clone()).collect(); + let mut args: Vec = self.get_free_vars().iter().map(|s| s.clone()).collect(); + args.sort(); + args.extend(params); Ok(format!(r#"function ({args}) {{ {constants} {binds} @@ -48,8 +56,7 @@ impl TranspileJS for ModelicaModel { impl TranspileJSODE for ModelicaModel { fn transpile_js_ode(&self) -> Result { - // TODO: distinguish true constants from parameters? - let mut params: Vec = vec![]; + let mut params = vec![]; let mut constants = vec![]; for (c, e) in self.get_constant_vars() { if let Some(v) = e { @@ -58,6 +65,9 @@ impl TranspileJSODE for ModelicaModel { params.push(c); } } + // HashMaps are unsorted, so we need to re-sort here + constants.sort(); + params.sort(); let mut exprs = vec![]; for eq in self.equations.iter() { if let Expr::Der(ref der_of) = eq.lhs { @@ -71,7 +81,8 @@ impl TranspileJSODE for ModelicaModel { bail!("Not a simple set of ODEs (aka, all derivatives on LHS of equations)"); } } - let args: Vec = self.get_free_vars().iter().map(|s| s.clone()).collect(); + let mut args: Vec = self.get_free_vars().iter().map(|s| s.clone()).collect(); + args.sort(); Ok(format!(r#"function ({params}) {{ return function({args}) {{ {constants} diff --git a/src/transpile_scheme.rs b/src/transpile_scheme.rs index e29a422..6f993b3 100644 --- a/src/transpile_scheme.rs +++ b/src/transpile_scheme.rs @@ -11,12 +11,18 @@ pub trait TranspileScheme { impl TranspileScheme for ModelicaModel { fn transpile_scheme(&self) -> Result { + let mut params = vec![]; let mut constants = vec![]; for (c, e) in self.get_constant_vars() { if let Some(v) = e { constants.push(format!("({} {})", c, try!(v.transpile_scheme()))); + } else { + params.push(c); } } + // HashMaps are unsorted, so we need to re-sort here + constants.sort(); + params.sort(); let mut binds = vec![]; let mut outputs = vec![]; for eq in self.equations.iter() { @@ -27,7 +33,9 @@ impl TranspileScheme for ModelicaModel { bail!("Expected an identifier on LHS (in this partial implementation)") } } - let args: Vec = self.get_free_vars().iter().map(|s| s.clone()).collect(); + let mut args: Vec = self.get_free_vars().iter().map(|s| s.clone()).collect(); + args.sort(); + args.extend(params); Ok(format!(r#"(lambda ({args}) (let ({constants}) (letrec ({binds}) -- cgit v1.2.3