Rework .print() and one_line() slightly

This commit is contained in:
Tiffany Bennett 2016-08-02 21:21:48 -04:00
parent fadab5c53b
commit c136c8a216
3 changed files with 14 additions and 7 deletions

View file

@ -15,7 +15,7 @@ fn main() {
Ok(_) => (),
Err(_) => return
};
match one_line(&mut ctx, line.trim()) {
match one_line(&mut ctx, &*line) {
Ok(v) => ctx.print(&v),
Err(e) => println!("{}", e)
};

View file

@ -82,15 +82,22 @@ impl Value {
}
impl Context {
pub fn print(&self, value: &Value) {
print!("{}", value.0);
pub fn show(&self, value: &Value) -> String {
use std::io::Write;
let mut out = vec![];
write!(out, "{}", value.0).unwrap();
for (&dim, &exp) in &value.1 {
print!(" {}^{}", self.dimensions[dim], exp);
write!(out, " {}^{}", self.dimensions[dim], exp).unwrap();
}
if let Some(name) = self.aliases.get(&value.1) {
print!(" ({})", name);
write!(out, " ({})", name).unwrap();
}
println!("");
String::from_utf8(out).unwrap()
}
pub fn print(&self, value: &Value) {
println!("{}", self.show(value));
}
pub fn lookup(&self, name: &str) -> Option<Value> {

View file

@ -18,7 +18,7 @@ pub fn load() -> Context {
}
pub fn one_line(ctx: &mut Context, line: &str) -> Result<Value, String> {
let mut iter = unit_defs::TokenIterator::new(&*line).peekable();
let mut iter = unit_defs::TokenIterator::new(line.trim()).peekable();
let expr = unit_defs::parse_expr(&mut iter);
ctx.eval(&expr)
}