mirror of
https://github.com/tiffany352/rink-rs
synced 2024-11-10 13:44:15 +00:00
Allow inline dimension definitions
This commit is contained in:
parent
56b990e968
commit
a7e847781b
2 changed files with 15 additions and 4 deletions
16
src/eval.rs
16
src/eval.rs
|
@ -202,7 +202,11 @@ impl Context {
|
|||
} else {
|
||||
let mut map = Unit::new();
|
||||
map.insert(dim.clone(), 1);
|
||||
write!(buf, " {}", self.aliases[&map]).unwrap();
|
||||
if let Some(name) = self.aliases.get(&map) {
|
||||
write!(buf, " {}", name).unwrap();
|
||||
} else {
|
||||
write!(buf, " '{}'", dim).unwrap();
|
||||
}
|
||||
if pow != 1 {
|
||||
write!(buf, "^{}", pow).unwrap();
|
||||
}
|
||||
|
@ -223,13 +227,18 @@ impl Context {
|
|||
} else {
|
||||
let mut map = Unit::new();
|
||||
map.insert(dim.clone(), 1);
|
||||
write!(buf, " {}", self.aliases[&map]).unwrap();
|
||||
if let Some(name) = self.aliases.get(&map) {
|
||||
write!(buf, " {}", name).unwrap();
|
||||
} else {
|
||||
write!(buf, " '{}'", dim).unwrap();
|
||||
}
|
||||
if pow != 1 {
|
||||
write!(buf, "^{}", pow).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
buf.remove(0);
|
||||
}
|
||||
|
||||
(recip, String::from_utf8(buf).unwrap())
|
||||
|
@ -253,6 +262,7 @@ impl Context {
|
|||
|
||||
match *expr {
|
||||
Expr::Unit(ref name) => self.lookup(name).ok_or(format!("Unknown unit {}", name)).map(Value::Number),
|
||||
Expr::Quote(ref name) => Ok(Value::Number(Number::one_unit(Rc::new(name.clone())))),
|
||||
Expr::Const(ref num, ref frac, ref exp) =>
|
||||
Number::from_parts(
|
||||
num,
|
||||
|
@ -293,7 +303,7 @@ impl Context {
|
|||
},
|
||||
ref x => Err(format!("Expected identifier, got {:?}", x))
|
||||
},
|
||||
Expr::Unit(ref name) => {
|
||||
Expr::Unit(ref name) | Expr::Quote(ref name) => {
|
||||
let mut map = BTreeMap::new();
|
||||
map.insert(name.clone(), 1);
|
||||
Ok(map)
|
||||
|
|
|
@ -258,6 +258,7 @@ pub type Iter<'a> = Peekable<TokenIterator<'a>>;
|
|||
#[derive(Debug, Clone)]
|
||||
pub enum Expr {
|
||||
Unit(String),
|
||||
Quote(String),
|
||||
Const(String, Option<String>, Option<String>),
|
||||
Date(Vec<Token>),
|
||||
Frac(Box<Expr>, Box<Expr>),
|
||||
|
@ -301,7 +302,7 @@ pub struct Defs {
|
|||
fn parse_term(mut iter: &mut Iter) -> Expr {
|
||||
match iter.next().unwrap() {
|
||||
Token::Ident(name) => Expr::Unit(name),
|
||||
Token::Quote(name) => Expr::Unit(name),
|
||||
Token::Quote(name) => Expr::Quote(name),
|
||||
Token::Number(num, frac, exp) => Expr::Const(num, frac, exp),
|
||||
Token::Plus => Expr::Plus(Box::new(parse_term(iter))),
|
||||
Token::Minus => Expr::Neg(Box::new(parse_term(iter))),
|
||||
|
|
Loading…
Reference in a new issue