diff options
Diffstat (limited to 'src/transpile_js.rs')
-rw-r--r-- | src/transpile_js.rs | 19 |
1 files changed, 15 insertions, 4 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} |