diff options
author | bnewbold <bnewbold@robocracy.org> | 2017-01-16 18:20:16 -0800 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2017-01-16 18:20:16 -0800 |
commit | 463b8d795969833c63b2a901ce7d40844aacf037 (patch) | |
tree | 4fcec27c5b6d2a3ea1b35efae9d57513978db9f0 | |
parent | 7486ed83a310cc82a1c3149f9258d853f43cc11b (diff) | |
download | modelthing-463b8d795969833c63b2a901ce7d40844aacf037.tar.gz modelthing-463b8d795969833c63b2a901ce7d40844aacf037.zip |
sort the output of HashMaps (eg, for argument order)
-rw-r--r-- | src/transpile_js.rs | 19 | ||||
-rw-r--r-- | 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<String> { + 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<String> = self.get_free_vars().iter().map(|s| s.clone()).collect(); + let mut args: Vec<String> = 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<String> { - // TODO: distinguish true constants from parameters? - let mut params: Vec<String> = 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<String> = self.get_free_vars().iter().map(|s| s.clone()).collect(); + let mut args: Vec<String> = 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<String> { + 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<String> = self.get_free_vars().iter().map(|s| s.clone()).collect(); + let mut args: Vec<String> = self.get_free_vars().iter().map(|s| s.clone()).collect(); + args.sort(); + args.extend(params); Ok(format!(r#"(lambda ({args}) (let ({constants}) (letrec ({binds}) |