fix
This commit is contained in:
parent
9d02a2faaf
commit
11788900b9
1 changed files with 29 additions and 25 deletions
|
@ -339,41 +339,26 @@ impl Context {
|
||||||
*self.get_mut('t')? += 1f64 / (SAMPLE_RATE as f64);
|
*self.get_mut('t')? += 1f64 / (SAMPLE_RATE as f64);
|
||||||
*self = self.slopes.iter().try_fold(
|
*self = self.slopes.iter().try_fold(
|
||||||
self.clone(),
|
self.clone(),
|
||||||
|mut acc,
|
|mut acc, (v, e)| -> Result<Self, CompilerError> {
|
||||||
(
|
*acc.get_mut(*v)? = acc.eval(e)?;
|
||||||
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>>(),
|
|
||||||
)?;
|
|
||||||
Ok(acc)
|
Ok(acc)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn namespace_generator(&self) -> BTreeMap<String, f64> {
|
fn namespace_generator(&self) -> impl Iterator<Item = (String, f64)> {
|
||||||
self.variables
|
self.variables.iter().map(|(c, f)| (c.to_string(), *f))
|
||||||
.iter()
|
|
||||||
.map(|(c, f)| (c.to_string(), *f))
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eval(&self, expr: &Expression) -> Result<f64, fasteval::Error> {
|
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(
|
pub fn eval_with(
|
||||||
&self,
|
&self,
|
||||||
Expression {
|
Expression {
|
||||||
|
@ -386,15 +371,34 @@ impl Context {
|
||||||
instruction.eval(slab, ns)
|
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> {
|
pub fn render(&mut self, n: Option<u8>) -> Result<Vec<f64>, CompilerError> {
|
||||||
let curr_t = *self.get('t')?;
|
let curr_t = *self.get('t')?;
|
||||||
if let Some(note) = n {
|
if let Some(note) = n {
|
||||||
let mut result = Vec::new();
|
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);
|
map.insert('n'.to_string(), note as f64);
|
||||||
while self.current_length()? > *self.get('t')? - curr_t + (1f64 / SAMPLE_RATE 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)?);
|
result.push(self.eval_with(&self.instrument, &mut map)?);
|
||||||
self.tick()?;
|
self.tick()?;
|
||||||
|
map = self.namespace_generator().fold(map, |mut acc, (k, v)| {
|
||||||
|
acc.insert(k, v);
|
||||||
|
acc
|
||||||
|
})
|
||||||
}
|
}
|
||||||
Ok(result)
|
Ok(result)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue