Allow references to other properties in substances

This commit is contained in:
Tiffany Bennett 2016-09-28 23:01:48 -04:00
parent e29787c0fb
commit a205f26e6c
2 changed files with 23 additions and 0 deletions

View file

@ -21,6 +21,7 @@ pub struct Context {
pub docs: BTreeMap<String, String>,
pub datepatterns: Vec<Vec<DatePattern>>,
pub substances: BTreeMap<String, Substance>,
pub temporaries: BTreeMap<String, Number>,
pub short_output: bool,
}
@ -38,6 +39,7 @@ impl Context {
docs: BTreeMap::new(),
datepatterns: Vec::new(),
substances: BTreeMap::new(),
temporaries: BTreeMap::new(),
short_output: false,
}
}
@ -50,6 +52,9 @@ impl Context {
/// prefixes, plurals, bare dimensions like length, and quantities.
pub fn lookup(&self, name: &str) -> Option<Number> {
fn inner(ctx: &Context, name: &str) -> Option<Number> {
if let Some(v) = ctx.temporaries.get(name).cloned() {
return Some(v)
}
if let Some(k) = ctx.dimensions.get(name) {
return Some(Number::one_unit(k.to_owned()))
}

View file

@ -318,6 +318,23 @@ impl Context {
);
}
existing.append(&mut unique);
self.temporaries.insert(
prop.name.clone(),
(&input / &output)
.expect("Non-zero property")
);
if output == Number::one() {
self.temporaries.insert(
prop.input_name.clone(),
input.clone()
);
}
if input == Number::one() {
self.temporaries.insert(
prop.output_name.clone(),
output.clone()
);
}
Ok((prop.name.clone(), Property {
input: input,
input_name: prop.input_name.clone(),
@ -326,6 +343,7 @@ impl Context {
doc: prop.doc.clone(),
}))
}).collect::<Result<BTreeMap<_,_>, _>>();
self.temporaries.clear();
match res {
Ok(res) => {
self.substances.insert(name.clone(), Substance {