diff --git a/src/compiler.rs b/src/compiler.rs index b615785..39e3c27 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -339,41 +339,26 @@ impl Context { *self.get_mut('t')? += 1f64 / (SAMPLE_RATE as f64); *self = self.slopes.iter().try_fold( self.clone(), - |mut acc, - ( - v, - Expression { - from: _, - instruction, - slab, - }, - )| - -> Result { - *acc.get_mut(*v)? = instruction.eval( - slab, - &mut acc - .variables - .iter() - .map(|(c, f)| (c.to_string(), *f)) - .collect::>(), - )?; + |mut acc, (v, e)| -> Result { + *acc.get_mut(*v)? = acc.eval(e)?; Ok(acc) }, )?; Ok(()) } - fn namespace_generator(&self) -> BTreeMap { - self.variables - .iter() - .map(|(c, f)| (c.to_string(), *f)) - .collect() + fn namespace_generator(&self) -> impl Iterator { + self.variables.iter().map(|(c, f)| (c.to_string(), *f)) } pub fn eval(&self, expr: &Expression) -> Result { - self.eval_with(expr, &mut self.namespace_generator()) + self.eval_with( + expr, + &mut self.namespace_generator().collect::>(), + ) } + #[cfg(not(test))] pub fn eval_with( &self, Expression { @@ -386,15 +371,34 @@ impl Context { instruction.eval(slab, ns) } + #[cfg(test)] + pub fn eval_with( + &self, + Expression { + from, + instruction, + slab, + }: &Expression, + ns: &mut (impl EvalNamespace + Debug), + ) -> Result { + let result = instruction.eval(slab, ns)?; + println!("expression {from}\twith values {ns:?}\t equals {result}"); + Ok(result) + } + pub fn render(&mut self, n: Option) -> Result, CompilerError> { let curr_t = *self.get('t')?; if let Some(note) = n { let mut result = Vec::new(); - let mut map = self.namespace_generator(); + let mut map = self.namespace_generator().collect::>(); map.insert('n'.to_string(), note as f64); while self.current_length()? > *self.get('t')? - curr_t + (1f64 / SAMPLE_RATE as f64) { result.push(self.eval_with(&self.instrument, &mut map)?); self.tick()?; + map = self.namespace_generator().fold(map, |mut acc, (k, v)| { + acc.insert(k, v); + acc + }) } Ok(result) } else {