This commit is contained in:
brevalferrari 2025-05-29 00:33:19 +02:00
parent 9d02a2faaf
commit 11788900b9

View file

@ -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<Self, CompilerError> {
*acc.get_mut(*v)? = instruction.eval(
slab,
&mut acc
.variables
.iter()
.map(|(c, f)| (c.to_string(), *f))
.collect::<BTreeMap<String, f64>>(),
)?;
|mut acc, (v, e)| -> Result<Self, CompilerError> {
*acc.get_mut(*v)? = acc.eval(e)?;
Ok(acc)
},
)?;
Ok(())
}
fn namespace_generator(&self) -> BTreeMap<String, f64> {
self.variables
.iter()
.map(|(c, f)| (c.to_string(), *f))
.collect()
fn namespace_generator(&self) -> impl Iterator<Item = (String, f64)> {
self.variables.iter().map(|(c, f)| (c.to_string(), *f))
}
pub fn eval(&self, expr: &Expression) -> Result<f64, fasteval::Error> {
self.eval_with(expr, &mut self.namespace_generator())
self.eval_with(
expr,
&mut self.namespace_generator().collect::<BTreeMap<_, _>>(),
)
}
#[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<f64, fasteval::Error> {
let result = instruction.eval(slab, ns)?;
println!("expression {from}\twith values {ns:?}\t equals {result}");
Ok(result)
}
pub fn render(&mut self, n: Option<u8>) -> Result<Vec<f64>, 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::<BTreeMap<_, _>>();
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 {